0.1.40
2300xxi interrupt:reset observer refactor combined ready observer use new method to create incoming consumer observer from gpio interruptmaster
parent
6a02b0510e
commit
98a1d14f0e
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "@uci/mcp",
|
"name": "@uci/mcp",
|
||||||
"main": "src",
|
"main": "src",
|
||||||
"version": "0.1.39",
|
"version": "0.1.40",
|
||||||
"description": "Classes and Helper Functions for using the MCP chip on I2C Bus",
|
"description": "Classes and Helper Functions for using the MCP chip on I2C Bus",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"outputs": "./node_modules/.bin/nodemon -r esm --preserve-symlinks examples/outputs",
|
"outputs": "./node_modules/.bin/nodemon -r esm --preserve-symlinks examples/outputs",
|
||||||
|
|
|
@ -1,17 +1,10 @@
|
||||||
import { I2CDevice as Device, map, changed, isPlainObject, to, merge} from '@uci/i2c-device'
|
import { I2CDevice as Device, map, changed, isPlainObject, to, merge} from '@uci/i2c-device'
|
||||||
import commands from './commands'
|
import commands from './commands'
|
||||||
import logger from '@uci-utils/logger'
|
|
||||||
let log = {}
|
|
||||||
|
|
||||||
class MCP230XX extends Device {
|
class MCP230XX extends Device {
|
||||||
constructor(opts) {
|
constructor(opts) {
|
||||||
super(opts)
|
super(opts)
|
||||||
log = logger({
|
|
||||||
file: 'src/mcp230xx.js',
|
|
||||||
class: 'MCP230XX',
|
|
||||||
name: 'mcp',
|
|
||||||
id: this.id
|
|
||||||
})
|
|
||||||
this.chip17 = opts.chip17
|
this.chip17 = opts.chip17
|
||||||
this.chipCfg = opts.chipCfg || 'default'
|
this.chipCfg = opts.chipCfg || 'default'
|
||||||
this.pinsCfg = opts.pinsCfg || this.chip17 ? [{port:'A', pins:'all'},{port:'B', pins:'all'}] : [{pins:'all'}]
|
this.pinsCfg = opts.pinsCfg || this.chip17 ? [{port:'A', pins:'all'},{port:'B', pins:'all'}] : [{pins:'all'}]
|
||||||
|
|
|
@ -5,20 +5,11 @@ import { byteFormat } from '@uci-utils/byte'
|
||||||
import logger from '@uci-utils/logger'
|
import logger from '@uci-utils/logger'
|
||||||
let log = {}
|
let log = {}
|
||||||
|
|
||||||
log = logger({
|
|
||||||
file: 'src/mcp230xxi.js',
|
|
||||||
class: 'MCP230XXi',
|
|
||||||
name: 'mcp-interrupt',
|
|
||||||
})
|
|
||||||
|
|
||||||
// if opts.iport not set then will be generated based on pin number
|
|
||||||
|
|
||||||
class MCP230XXi extends MCP230XX {
|
class MCP230XXi extends MCP230XX {
|
||||||
constructor(pins, options={}) {
|
constructor(pins, options={}) {
|
||||||
if (!Array.isArray(pins)) { options = pins; pins = options.interrupt.pins} // if pins sent via .interrupt.pins
|
if (!Array.isArray(pins)) { options = pins; pins = options.interrupt.pins} // if pins sent via .interrupt.pins
|
||||||
let opts = Object.assign({},options) // don't allow passed options to mutate
|
let opts = Object.assign({},options) // don't allow passed options to mutate
|
||||||
if (opts.interrupt) delete opts.interrupt.pins // if .interrupt was passed then .pins must be removed
|
if (opts.interrupt) delete opts.interrupt.pins // if .interrupt was passed then .pins must be removed
|
||||||
log.debug({method:'constructor', line:21, msg:'passed options before setting',options:opts})
|
|
||||||
opts.lport = opts.lport || (opts.interrupt || {}).port || opts.iport
|
opts.lport = opts.lport || (opts.interrupt || {}).port || opts.iport
|
||||||
if (!opts.lport) opts.lpath = opts.lpath || (opts.interrupt || {}).ipath || opts.ipath || 'interrupt' // must has a socket litener for interrupt process
|
if (!opts.lport) opts.lpath = opts.lpath || (opts.interrupt || {}).ipath || opts.ipath || 'interrupt' // must has a socket litener for interrupt process
|
||||||
super(opts)
|
super(opts)
|
||||||
|
@ -38,25 +29,27 @@ class MCP230XXi extends MCP230XX {
|
||||||
this.commands.interrupt = this.bindFuncs(icommands) // add interrupt pin commands to base set in "command.js"
|
this.commands.interrupt = this.bindFuncs(icommands) // add interrupt pin commands to base set in "command.js"
|
||||||
this._interruptProcess = process.bind(this) // default processor
|
this._interruptProcess = process.bind(this) // default processor
|
||||||
|
|
||||||
|
this.ready.addObserver(`${opts.interrupt.name}:process`) // will emit on received interrupt process ready packet
|
||||||
|
|
||||||
const conditionHandler = async ev => {
|
let interruptobs = this.ready.combineObservers(`${opts.interrupt.name}`,['mcp:configure',`${opts.interrupt.name}:consumer`,`${opts.interrupt.name}:process`])
|
||||||
if ((ev||{}).state ==='connected'){
|
this.ready.addObserver('interrupt:reset',interruptobs
|
||||||
let data = (ev.data ||{})
|
.pipe(
|
||||||
if (data.type === 'interrupt' || [ev.name, data.name, data.id].some(name => (name||'').includes('interrupt')) ) {
|
map(async ready => {
|
||||||
return true
|
if (ready) {
|
||||||
}
|
let imcpready = await this.resetInterrupt()
|
||||||
}
|
if (imcpready){
|
||||||
return false
|
let res = await this.send(opts.interrupt.name,{cmd:'status'})
|
||||||
}
|
ready = res.ready || res.pins.reduce((acc,pin)=>acc&&pin.ready,true)
|
||||||
|
}
|
||||||
|
|
||||||
this.ready.addObserver('interrupt:connected',this.getSocket(`mcp-${opts.lport? 't':'n'}`),{event:'connection:consumer',condition:conditionHandler})
|
}
|
||||||
|
return ready
|
||||||
|
}) //map
|
||||||
|
))
|
||||||
|
|
||||||
|
let socket = this.getSocket(`${options.name}:${options.interrupt.remote ? 't':'n'}`) // mcp t or n socket was created
|
||||||
|
this.consumerConnected(socket,{add:true,consumer:options.interrupt.name})
|
||||||
|
|
||||||
this.ready.addObserver('interrupt:reset',this.ready.combineObservers(['mcp','interrupt:connected']).pipe(
|
|
||||||
map(async ready=>{
|
|
||||||
if (ready) return await this.resetInterrupt()
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
))
|
|
||||||
} // end constructor
|
} // end constructor
|
||||||
|
|
||||||
async interruptState(pin) {
|
async interruptState(pin) {
|
||||||
|
|
Loading…
Reference in New Issue