From 851b73c7f53086814b35387b06142d0744cccf6e Mon Sep 17 00:00:00 2001 From: David Kebler Date: Sun, 11 Feb 2018 11:41:45 -0800 Subject: [PATCH] refactored pin config and pin state commands now handle various pins: property formats --- examples/ipc-test.mjs | 34 ++++++++------ src/commands.mjs | 102 +++++++++++++++++++++++++++++------------- 2 files changed, 92 insertions(+), 44 deletions(-) diff --git a/examples/ipc-test.mjs b/examples/ipc-test.mjs index 1bea959..807d549 100644 --- a/examples/ipc-test.mjs +++ b/examples/ipc-test.mjs @@ -24,31 +24,39 @@ const delay = time => new Promise(res=>setTimeout(()=>res(),time)) // mcpclient.send(packet) // } + // const pins='2,3,4' + // const pins=3 + // const pins='all' + const pins=[1,3,5,7] + let packet = {cmd:'chip.cfg'} console.dir(packet) mcpclient.send(packet) await delay(1000) - // packet = {cmd:'pin.cfg', pin:2, cfg:'toggle_switch'} - packet = {cmd:'pin.cfg', pin:2} + packet = {cmd:'pin.state.off', pins:'all' } + console.dir(packet) + mcpclient.send(packet) + // await delay(1000) + // packet = {cmd:'pin.cfg', pins:pins, cfg:'toggle_switch'} + // console.dir(packet) + // mcpclient.send(packet) + await delay(1000) + packet = {cmd:'pin.cfg', pins:pins} console.dir(packet) mcpclient.send(packet) await delay(1000) - packet = {cmd:'pin.state.off', pin:2, } + packet = {cmd:'pin.state.on', pins:pins, } console.dir(packet) mcpclient.send(packet) await delay(1000) - packet = {cmd:'pin.state.on', pin:2, } + packet = {cmd:'pin.state.toggle', pins:pins, } console.dir(packet) mcpclient.send(packet) - await delay(1000) - packet = {cmd:'pin.state.toggle', pin:2, } - console.dir(packet) - mcpclient.send(packet) - await delay(1000) - packet = {cmd:'pin.state.toggle', pin:2, } - console.dir(packet) - mcpclient.send(packet) - // packet = {cmd:'pin.cfg', pin:3} + // await delay(1000) + // packet = {cmd:'pin.state.toggle', pins:pins, } + // console.dir(packet) + // mcpclient.send(packet) + // packet = {cmd:'pin.cfg', pins:3} // console.dir(packet) // mcpclient.send(packet) diff --git a/src/commands.mjs b/src/commands.mjs index d7af162..085e333 100644 --- a/src/commands.mjs +++ b/src/commands.mjs @@ -57,33 +57,56 @@ export const chip = { // first get the current byte (pin) state for that setting export const pin = { cfg: async function(packet){ - let shift = 0 let cfg = {} let reply = { cmd:'reply', _req:packet, response:{} } - if (packet.port === 'B') shift= 0x10 // TODO check on shift amount packet.cfg = packet.cfg || 'output' if (packet.cfg==='custom') cfg = packet.set else cfg = PIN.cfgset[packet.cfg] - let pin = new _.Byte([packet.pin],'PLC') - let state = new _.Byte() for(let name of Object.keys(PIN.setting)) { - let bus_packet = { cmd:'read', args: {address:this.address, cmd:PIN.setting[name]+shift } } - let bus = await this.busSend(bus_packet) - if (bus.error) return bus - state.value = bus.response let op = cfg[name] ? 'on' : 'off' - bus_packet = { cmd:'write', args: {address:this.address, cmd:PIN.setting[name]+shift, byte:state.bwOp(pin.value,op,{in:'PLC', out:'DEC'}) } } - bus = await this.busSend(bus_packet) - if (bus.error) return bus - bus_packet = { cmd:'read', args: {address:this.address, cmd:PIN.setting[name]+shift } } - bus = await this.busSend(bus_packet) - if (bus.error) return bus - state.value = bus.response - reply.response[name] = state.bwOp(pin.value,'check',{in:'PLC', out:'PLC'})[0]===packet.pin ? 'on':'off' + console.log(name, op) + let busreply = await state.bind(this)(packet,op,PIN.setting[name]) + if (busreply.error) return busreply + reply.response[name] = reply.status } + console.log('==============') return reply }, + + // cfg: async function(packet){ + // let shift = 0 + // let cfg = {} + // let reply = { cmd:'reply', _req:packet, response:{} } + // if (packet.port === 'B') shift= 0x10 // TODO check on shift amount + // packet.cfg = packet.cfg || 'output' + // if (packet.cfg==='custom') cfg = packet.set + // else cfg = PIN.cfgset[packet.cfg] + // let pins = parsePins(packet.pins) + // let state = new _.Byte() + // for(let name of Object.keys(PIN.setting)) { + // let bus_packet = { cmd:'read', args: {address:this.address, cmd:PIN.setting[name]+shift } } + // let bus = await this.busSend(bus_packet) + // if (bus.error) return bus + // state.value = bus.response + // let op = cfg[name] ? 'on' : 'off' + // console.log(name, op) + // console.log(state.toFmt('ARY')) + // console.log(pins.toFmt('ARY')) + // bus_packet = { cmd:'write', args: {address:this.address, cmd:PIN.setting[name]+shift, byte:state.bwOp(pins.value,op,{in:'PLC', out:'DEC'}) } } + // bus = await this.busSend(bus_packet) + // if (bus.error) return bus + // bus_packet = { cmd:'read', args: {address:this.address, cmd:PIN.setting[name]+shift } } + // bus = await this.busSend(bus_packet) + // if (bus.error) return bus + // state.value = bus.response + // console.log(state.toFmt('ARY')) + // reply.response[name] = state.bwOp(pins.value,'check',{in:'PLC', out:'PLC'}) + // } + // console.log('==============') + // return reply + // }, + // state is equivalent to read status: packet => { }, @@ -99,30 +122,47 @@ export const pin = { return state.bind(this)(packet,'toggle') } }, - // // will create packet to determin pin caused interrupt, packet will come from interrupt module + // // will create packet to determine pin caused interrupt, packet will come from interrupt module // interrupt: { // find: packet =>{}, // report: packet=>{} // come here after determining which pin to report to requester // } } -const state = async function(packet,op){ - console.log(op) - let shift = 0 +const parsePins = function(pins) { + if (typeof pins==='number') pins = [pins] + if (typeof pins==='string') { + if (pins==='all') pins = _.byteFormat(255,{in:'DEC', out:'PLC'}) + else pins = pins.split(',') + } + return new _.Byte(pins,'PLC') +} + +let busPacket = function (cmd,reg,byte,port) { + if (typeof byte==='string') port = byte + let shift = (port==='B') ? 0x10 : 0 + return { cmd:cmd, args: {address:this.address, cmd:reg+shift, byte:byte } } +} + +const state = async function(packet,op,reg){ + busPacket = busPacket.bind(this) + reg = (reg!==undefined)? reg : PIN.cmd.gpio + console.log(op, reg) let reply = { cmd:'reply', _req:packet} - if (packet.port === 'B') shift= 0x10 // TODO check on shift amount - let pin = new _.Byte([packet.pin],'PLC') + let pins = parsePins(packet.pins) let state = new _.Byte() - let bus_packet = { cmd:'read', args: {address:this.address, cmd:PIN.cmd.gpio+shift} } - let bus = await this.busSend(bus_packet) - state.value = bus.response - bus_packet = { cmd:'write', args: {address:this.address, cmd:PIN.cmd.gpio+shift, byte:state.bwOp(pin.value,op,{in:'PLC', out:'DEC'}) } } - bus = await this.busSend(bus_packet) - if (bus.error) return bus - bus_packet = { cmd:'read', args: {address:this.address, cmd:PIN.cmd.gpio+shift} } - bus = await this.busSend(bus_packet) + let bus = await this.busSend(busPacket('read',reg, packet.port)) if (bus.error) return bus state.value = bus.response - reply.status = state.bwOp(pin.value,'check',{in:'PLC', out:'PLC'})[0]===packet.pin ? 'on':'off' + console.log(state.toFmt('ARY')) + console.log(pins.toFmt('ARY')) + // bus_packet = { cmd:'write', args: {address:this.address, cmd:reg+shift, byte:state.bwOp(pins.value,op,{in:'PLC', out:'DEC'}) } } + bus = await this.busSend(busPacket('write',reg,state.bwOp(pins.value,op,{in:'PLC', out:'DEC'}),packet.port)) + if (bus.error) return bus + bus = await this.busSend(busPacket('read',reg, packet.port)) + if (bus.error) return bus + state.value = bus.response + console.log(state.toFmt('ARY')) + reply.status = state.bwOp(pins.value,'check',{in:'PLC', out:'PLC'}) return reply }