refactored pin config and pin state commands now handle various pins: property formats

master
David Kebler 2018-02-11 11:41:45 -08:00
parent 8a9950459a
commit 851b73c7f5
2 changed files with 92 additions and 44 deletions

View File

@ -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)

View File

@ -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
}