2300xxi
  interrupt:reset observer refactor combined ready observer
  use new method to create incoming consumer observer from gpio interrupt
master
David Kebler 2020-01-23 22:48:28 -08:00
parent 6a02b0510e
commit 98a1d14f0e
3 changed files with 20 additions and 34 deletions

View File

@ -1,7 +1,7 @@
{
"name": "@uci/mcp",
"main": "src",
"version": "0.1.39",
"version": "0.1.40",
"description": "Classes and Helper Functions for using the MCP chip on I2C Bus",
"scripts": {
"outputs": "./node_modules/.bin/nodemon -r esm --preserve-symlinks examples/outputs",

View File

@ -1,17 +1,10 @@
import { I2CDevice as Device, map, changed, isPlainObject, to, merge} from '@uci/i2c-device'
import commands from './commands'
import logger from '@uci-utils/logger'
let log = {}
class MCP230XX extends Device {
constructor(opts) {
super(opts)
log = logger({
file: 'src/mcp230xx.js',
class: 'MCP230XX',
name: 'mcp',
id: this.id
})
this.chip17 = opts.chip17
this.chipCfg = opts.chipCfg || 'default'
this.pinsCfg = opts.pinsCfg || this.chip17 ? [{port:'A', pins:'all'},{port:'B', pins:'all'}] : [{pins:'all'}]

View File

@ -5,20 +5,11 @@ import { byteFormat } from '@uci-utils/byte'
import logger from '@uci-utils/logger'
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 {
constructor(pins, options={}) {
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
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
if (!opts.lport) opts.lpath = opts.lpath || (opts.interrupt || {}).ipath || opts.ipath || 'interrupt' // must has a socket litener for interrupt process
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._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 => {
if ((ev||{}).state ==='connected'){
let data = (ev.data ||{})
if (data.type === 'interrupt' || [ev.name, data.name, data.id].some(name => (name||'').includes('interrupt')) ) {
return true
}
}
return false
}
let interruptobs = this.ready.combineObservers(`${opts.interrupt.name}`,['mcp:configure',`${opts.interrupt.name}:consumer`,`${opts.interrupt.name}:process`])
this.ready.addObserver('interrupt:reset',interruptobs
.pipe(
map(async ready => {
if (ready) {
let imcpready = await this.resetInterrupt()
if (imcpready){
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
async interruptState(pin) {