2018-02-10 15:28:51 -08:00
|
|
|
// import _ from '@uci/utils/src/byte'
|
|
|
|
import _ from '../../archive/uci-utils/src/byte'
|
|
|
|
import { CHIP, PIN } from './config'
|
|
|
|
|
|
|
|
export const chip = {
|
|
|
|
ack: async function(packet){
|
|
|
|
// process packet based on command and any other props into a bus packet
|
|
|
|
let bus_packet = { cmd:'scan'}
|
|
|
|
// console.log(packet.num)
|
|
|
|
let bus_reply = await this.busSend(bus_packet)
|
|
|
|
if (bus_reply.error) return bus_reply
|
|
|
|
let res = { cmd:'reply', _req:packet, _reqBus:bus_packet, ack: false, address:this.address}
|
|
|
|
if (bus_reply.response.indexOf(parseInt(this.address,16))!=-1) res.ack = true
|
|
|
|
return res
|
|
|
|
},
|
|
|
|
cfg: async function(packet){
|
|
|
|
let setting = {}
|
|
|
|
let cfg = packet.cfg || 'default'
|
|
|
|
if (cfg === 'custom') setting = {val:packet.setting, fmt:packet.fmt || 'STR'}
|
|
|
|
else {
|
|
|
|
if (CHIP[cfg]) setting = CHIP[cfg]
|
|
|
|
else return {error:`no chip settings for ${cfg}`}
|
|
|
|
}
|
|
|
|
let byte = _.byteFormat(setting.val, { in: setting.fmt, out: 'DEC' })
|
|
|
|
let bus_packet = { cmd:'write', args: {address:this.address, cmd:CHIP.cmd, byte:byte}}
|
|
|
|
// console.log ('writing mcp chip config', bus_packet)
|
|
|
|
let bus_reply = await this.busSend(bus_packet)
|
|
|
|
if (bus_reply.error) return bus_reply
|
|
|
|
let res = { cmd:'reply', _req:packet, _reqBus:bus_packet , response:bus_reply.response}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// async writePinsCfg() {
|
|
|
|
// debug.L1('writing mcp pins config')
|
|
|
|
// for (let port in this.ports) {
|
|
|
|
// for (let setting in registers.pin_config) {
|
|
|
|
// let reg = registers.pin_config[setting]
|
|
|
|
// // TODO 0x10 should be based on chip config
|
|
|
|
// let byte = 0
|
|
|
|
// for (let pin of this.ports[port].pins) {
|
|
|
|
// byte += pin.address * pin.cfg[setting]
|
|
|
|
// debug.L3(`port: ${ port } pin: ${pin.id} setting: ${ setting } reg: ${ reg } byte: ${ byte }`)
|
|
|
|
// }
|
|
|
|
// await this.write(portReg(reg, port), byte)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// debug.L1('done writing mcp pins config')
|
|
|
|
// } // end writePinsCfg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Individual Pin Configurations
|
|
|
|
// set is for cfg: 'custom' assume a setting is zero unless given
|
|
|
|
// packet = { pin:, port:, cfg: , set:{dir: 0, ivrt: 0, pullup: 0, intr: 0, usedef: 0,defval: 0} }
|
|
|
|
// first get the current byte (pin) state for that setting
|
|
|
|
export const pin = {
|
|
|
|
cfg: async function(packet){
|
|
|
|
let cfg = {}
|
|
|
|
let reply = { cmd:'reply', _req:packet, response:{} }
|
|
|
|
packet.cfg = packet.cfg || 'output'
|
|
|
|
if (packet.cfg==='custom') cfg = packet.set
|
|
|
|
else cfg = PIN.cfgset[packet.cfg]
|
|
|
|
for(let name of Object.keys(PIN.setting)) {
|
|
|
|
let op = cfg[name] ? 'on' : 'off'
|
2018-02-11 11:41:45 -08:00
|
|
|
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
|
2018-02-10 15:28:51 -08:00
|
|
|
}
|
2018-02-11 11:41:45 -08:00
|
|
|
console.log('==============')
|
2018-02-10 15:28:51 -08:00
|
|
|
return reply
|
|
|
|
},
|
|
|
|
|
2018-02-11 11:41:45 -08:00
|
|
|
|
|
|
|
// 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
|
|
|
|
// },
|
|
|
|
|
2018-02-10 15:28:51 -08:00
|
|
|
// state is equivalent to read
|
|
|
|
status: packet => {
|
|
|
|
},
|
|
|
|
// threse three only for output pins
|
|
|
|
state : {
|
|
|
|
on: async function (packet) {
|
|
|
|
return state.bind(this)(packet,'on')
|
|
|
|
},
|
|
|
|
off: async function (packet) {
|
|
|
|
return state.bind(this)(packet,'off')
|
|
|
|
},
|
|
|
|
toggle: async function (packet) {
|
|
|
|
return state.bind(this)(packet,'toggle')
|
|
|
|
}
|
|
|
|
},
|
2018-02-11 11:41:45 -08:00
|
|
|
// // will create packet to determine pin caused interrupt, packet will come from interrupt module
|
2018-02-10 15:28:51 -08:00
|
|
|
// interrupt: {
|
|
|
|
// find: packet =>{},
|
|
|
|
// report: packet=>{} // come here after determining which pin to report to requester
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
2018-02-11 11:41:45 -08:00
|
|
|
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)
|
2018-02-10 15:28:51 -08:00
|
|
|
let reply = { cmd:'reply', _req:packet}
|
2018-02-11 11:41:45 -08:00
|
|
|
let pins = parsePins(packet.pins)
|
2018-02-10 15:28:51 -08:00
|
|
|
let state = new _.Byte()
|
2018-02-11 11:41:45 -08:00
|
|
|
let bus = await this.busSend(busPacket('read',reg, packet.port))
|
|
|
|
if (bus.error) return bus
|
2018-02-10 15:28:51 -08:00
|
|
|
state.value = bus.response
|
2018-02-11 11:41:45 -08:00
|
|
|
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))
|
2018-02-10 15:28:51 -08:00
|
|
|
if (bus.error) return bus
|
2018-02-11 11:41:45 -08:00
|
|
|
bus = await this.busSend(busPacket('read',reg, packet.port))
|
2018-02-10 15:28:51 -08:00
|
|
|
if (bus.error) return bus
|
|
|
|
state.value = bus.response
|
2018-02-11 11:41:45 -08:00
|
|
|
console.log(state.toFmt('ARY'))
|
|
|
|
reply.status = state.bwOp(pins.value,'check',{in:'PLC', out:'PLC'})
|
2018-02-10 15:28:51 -08:00
|
|
|
return reply
|
|
|
|
}
|