refactored pin config and pin state commands now handle various pins: property formats
parent
8a9950459a
commit
851b73c7f5
|
@ -24,31 +24,39 @@ const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
||||||
// mcpclient.send(packet)
|
// 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'}
|
let packet = {cmd:'chip.cfg'}
|
||||||
console.dir(packet)
|
console.dir(packet)
|
||||||
mcpclient.send(packet)
|
mcpclient.send(packet)
|
||||||
await delay(1000)
|
await delay(1000)
|
||||||
// packet = {cmd:'pin.cfg', pin:2, cfg:'toggle_switch'}
|
packet = {cmd:'pin.state.off', pins:'all' }
|
||||||
packet = {cmd:'pin.cfg', pin:2}
|
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)
|
console.dir(packet)
|
||||||
mcpclient.send(packet)
|
mcpclient.send(packet)
|
||||||
await delay(1000)
|
await delay(1000)
|
||||||
packet = {cmd:'pin.state.off', pin:2, }
|
packet = {cmd:'pin.state.on', pins:pins, }
|
||||||
console.dir(packet)
|
console.dir(packet)
|
||||||
mcpclient.send(packet)
|
mcpclient.send(packet)
|
||||||
await delay(1000)
|
await delay(1000)
|
||||||
packet = {cmd:'pin.state.on', pin:2, }
|
packet = {cmd:'pin.state.toggle', pins:pins, }
|
||||||
console.dir(packet)
|
console.dir(packet)
|
||||||
mcpclient.send(packet)
|
mcpclient.send(packet)
|
||||||
await delay(1000)
|
// await delay(1000)
|
||||||
packet = {cmd:'pin.state.toggle', pin:2, }
|
// packet = {cmd:'pin.state.toggle', pins:pins, }
|
||||||
console.dir(packet)
|
// console.dir(packet)
|
||||||
mcpclient.send(packet)
|
// mcpclient.send(packet)
|
||||||
await delay(1000)
|
// packet = {cmd:'pin.cfg', pins:3}
|
||||||
packet = {cmd:'pin.state.toggle', pin:2, }
|
|
||||||
console.dir(packet)
|
|
||||||
mcpclient.send(packet)
|
|
||||||
// packet = {cmd:'pin.cfg', pin:3}
|
|
||||||
// console.dir(packet)
|
// console.dir(packet)
|
||||||
// mcpclient.send(packet)
|
// mcpclient.send(packet)
|
||||||
|
|
||||||
|
|
102
src/commands.mjs
102
src/commands.mjs
|
@ -57,33 +57,56 @@ export const chip = {
|
||||||
// first get the current byte (pin) state for that setting
|
// first get the current byte (pin) state for that setting
|
||||||
export const pin = {
|
export const pin = {
|
||||||
cfg: async function(packet){
|
cfg: async function(packet){
|
||||||
let shift = 0
|
|
||||||
let cfg = {}
|
let cfg = {}
|
||||||
let reply = { cmd:'reply', _req:packet, response:{} }
|
let reply = { cmd:'reply', _req:packet, response:{} }
|
||||||
if (packet.port === 'B') shift= 0x10 // TODO check on shift amount
|
|
||||||
packet.cfg = packet.cfg || 'output'
|
packet.cfg = packet.cfg || 'output'
|
||||||
if (packet.cfg==='custom') cfg = packet.set
|
if (packet.cfg==='custom') cfg = packet.set
|
||||||
else cfg = PIN.cfgset[packet.cfg]
|
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)) {
|
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'
|
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'}) } }
|
console.log(name, op)
|
||||||
bus = await this.busSend(bus_packet)
|
let busreply = await state.bind(this)(packet,op,PIN.setting[name])
|
||||||
if (bus.error) return bus
|
if (busreply.error) return busreply
|
||||||
bus_packet = { cmd:'read', args: {address:this.address, cmd:PIN.setting[name]+shift } }
|
reply.response[name] = reply.status
|
||||||
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('==============')
|
||||||
return reply
|
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
|
// state is equivalent to read
|
||||||
status: packet => {
|
status: packet => {
|
||||||
},
|
},
|
||||||
|
@ -99,30 +122,47 @@ export const pin = {
|
||||||
return state.bind(this)(packet,'toggle')
|
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: {
|
// interrupt: {
|
||||||
// find: packet =>{},
|
// find: packet =>{},
|
||||||
// report: packet=>{} // come here after determining which pin to report to requester
|
// report: packet=>{} // come here after determining which pin to report to requester
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
const state = async function(packet,op){
|
const parsePins = function(pins) {
|
||||||
console.log(op)
|
if (typeof pins==='number') pins = [pins]
|
||||||
let shift = 0
|
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}
|
let reply = { cmd:'reply', _req:packet}
|
||||||
if (packet.port === 'B') shift= 0x10 // TODO check on shift amount
|
let pins = parsePins(packet.pins)
|
||||||
let pin = new _.Byte([packet.pin],'PLC')
|
|
||||||
let state = new _.Byte()
|
let state = new _.Byte()
|
||||||
let bus_packet = { cmd:'read', args: {address:this.address, cmd:PIN.cmd.gpio+shift} }
|
let bus = await this.busSend(busPacket('read',reg, packet.port))
|
||||||
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)
|
|
||||||
if (bus.error) return bus
|
if (bus.error) return bus
|
||||||
state.value = bus.response
|
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
|
return reply
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue