0.1.20 make sure options don't mutuate, also that if pin is string it gets cast to number

master
David Kebler 2019-03-07 07:48:16 -08:00
parent 078b24e9d7
commit f6b40be73b
3 changed files with 11 additions and 5 deletions

View File

@ -1,7 +1,7 @@
{
"name": "@uci/mcp",
"main": "src",
"version": "0.1.19",
"version": "0.1.20",
"description": "Classes and Helper Functions for using the MCP chip on I2C Bus",
"scripts": {
"relays": "node -r esm examples/relays",

View File

@ -43,7 +43,6 @@ export default {
else cfg = PIN.cfgset[packet.cfg]
for(let name of Object.keys(PIN.setting)) {
let op = cfg[name] ? 'on' : 'off'
// console.log(name, op)
let busreply = await this.pin._state(packet,op,PIN.setting[name])
if (busreply.error) return busreply
reply.status[name] = busreply.status

View File

@ -7,7 +7,13 @@ let log = {}
// if opts.iport not set then will be generated based on pin number
class MCP230XXi extends MCP230XX {
constructor(pins, opts) {
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 options to mutate
if (opts.interrupt) delete opts.interrupt.pins // if .interrupt is passed then .pins must be removed
delete opts.sockets // .sockets is used by uci base so clear it was sent by mistake
// console.log('options as ready for mcp',pins, opts)
// if iport or ipath is set then all interrupts have a single consolidating socket
if (typeof opts.iport === 'number' || opts.ipath) {
if (typeof opts.iport === 'number') {
@ -27,7 +33,7 @@ class MCP230XXi extends MCP230XX {
if (index === 1) opts[ipin].mport = opts[ipin].mport || 'B'
delete opts[pin]
if (opts[ipin].host) {
opts[ipin].port = opts[ipin].port ? opts[ipin].port + pin : 9000 + pin
opts[ipin].port = opts[ipin].port ? opts[ipin].port + +pin : 9000 + +pin
opts[ipin].host = opts[ipin].host
opts.sockets = (opts.sockets ? opts.sockets + ',' : '') + ipin + '#c>t'
}
@ -88,7 +94,7 @@ class MCP230XXi extends MCP230XX {
async _reset(port) {
// local non-packet hidden command
log.info(`resetting interrupt for port ${port || 'A'},${this.id}`)
console.log(`resetting interrupt for port ${port || 'A'},${this.id} arg ${port !== 'B' ? 0x08 : 0x1}`)
return await this.bus.read(port !== 'B' ? 0x08 : 0x18) // 0x08 is intcap interrupt capture register
}
@ -115,6 +121,7 @@ const ipincommands = {
// given a gpio interrupt then push a packet with cmd: 'pin.interrupt.find' and pin: the gpio pin number
find: async function(inter) {
// inter is a UCI packet
console.log('packet from interrupt to interpret',inter)
if (this.ready) {
// protects tripped interrupt before it's fully initialized, or interrupt requests arriving before porcessing is complete
this.ready = false