// 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 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' } return reply }, // 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') } }, // // will create packet to determin 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 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 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) 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' return reply }