2018-02-10 15:28:51 -08:00
|
|
|
// import _ from '@uci/utils/src/byte'
|
2018-02-17 18:25:27 -08:00
|
|
|
// import _ from '../../archive/uci-utils/src/byte'
|
|
|
|
import _ from '@uci/utils/src/byte'
|
2018-02-10 15:28:51 -08:00
|
|
|
import { CHIP, PIN } from './config'
|
|
|
|
|
|
|
|
export const chip = {
|
2018-02-11 19:56:44 -08:00
|
|
|
// for custom chip configuration set packet.cfg='custom' then packet.setting should be a
|
|
|
|
// configuration byte with given format ('STR' by defaul).
|
2018-02-10 15:28:51 -08:00
|
|
|
cfg: async function(packet){
|
2018-02-11 19:56:44 -08:00
|
|
|
// first make sure chip is in set to BANK=0 if not already
|
2018-02-14 19:01:08 -08:00
|
|
|
let bus = await this.bus.write(0x05,0)
|
2018-02-11 19:56:44 -08:00
|
|
|
if (bus.error) return bus
|
2018-02-10 15:28:51 -08:00
|
|
|
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' })
|
2018-02-11 19:56:44 -08:00
|
|
|
if (byte < 128) byte += 128 // make sure BANK=1 remains on
|
2018-02-13 18:18:03 -08:00
|
|
|
let reg = this.chip17 ? 0x0A : 0x05
|
2018-02-14 19:01:08 -08:00
|
|
|
bus = await this.bus.write(reg,byte)
|
2018-02-11 19:56:44 -08:00
|
|
|
if (bus.error) return bus
|
2018-02-14 19:01:08 -08:00
|
|
|
bus = await this.bus.read(0x05)
|
2018-02-11 19:56:44 -08:00
|
|
|
if (bus.error) return bus
|
2018-02-13 18:18:03 -08:00
|
|
|
return { cmd:'reply', response:_.byteFormat(bus.response,{in:'DEC',out:'STR'}) }
|
2018-02-10 15:28:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 = {}
|
2018-02-13 18:18:03 -08:00
|
|
|
let reply = { cmd:'reply', status:{} }
|
2018-02-10 15:28:51 -08:00
|
|
|
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 19:56:44 -08:00
|
|
|
// console.log(name, op)
|
2018-02-17 18:25:27 -08:00
|
|
|
let busreply = await this.pin._state(packet,op,PIN.setting[name])
|
2018-02-11 11:41:45 -08:00
|
|
|
if (busreply.error) return busreply
|
2018-02-13 18:18:03 -08:00
|
|
|
reply.status[name] = busreply.status
|
2018-02-10 15:28:51 -08:00
|
|
|
}
|
|
|
|
return reply
|
|
|
|
},
|
|
|
|
|
|
|
|
// state is equivalent to read
|
2018-02-11 19:56:44 -08:00
|
|
|
status: async function (packet) {
|
|
|
|
let reg = packet.reg ? PIN.cmd[packet.reg] : PIN.cmd.gpio
|
|
|
|
if (!reg) return {error:`unknown register ${packet.reg}`, packet:packet }
|
2018-02-17 18:25:27 -08:00
|
|
|
let reply = { cmd:'reply'}
|
2018-02-11 19:56:44 -08:00
|
|
|
let pins = parsePins(packet.pins)
|
|
|
|
let state = new _.Byte()
|
2018-02-14 19:01:08 -08:00
|
|
|
let bus = await this.bus.read(sreg(reg, packet.port))
|
2018-02-11 19:56:44 -08:00
|
|
|
if (bus.error) return bus
|
|
|
|
state.value = bus.response
|
2018-02-13 18:18:03 -08:00
|
|
|
reply.status =
|
|
|
|
{ port:state.toFmt('ARY'),
|
|
|
|
pins: pins.value.map(pin => {
|
|
|
|
if (state.toFmt('PLC').indexOf(pin) !==-1) return [pin, 'on']
|
|
|
|
else return [pin,'off']
|
|
|
|
})
|
|
|
|
}
|
2018-02-11 19:56:44 -08:00
|
|
|
return reply
|
2018-02-10 15:28:51 -08:00
|
|
|
},
|
2018-02-17 18:25:27 -08:00
|
|
|
_state: async function(packet,op,reg){
|
|
|
|
reg = (reg!==undefined)? reg : PIN.cmd.gpio
|
|
|
|
// console.log(op, reg, packet)
|
|
|
|
let reply = { cmd:'reply'}
|
|
|
|
let pins = parsePins(packet.pins)
|
|
|
|
let state = new _.Byte()
|
|
|
|
let bus = await this.bus.read(sreg(reg,packet.port))
|
|
|
|
if (bus.error) return bus
|
|
|
|
state.value = bus.response
|
|
|
|
bus = await this.bus.write(sreg(reg,packet.port),state.bwOp(pins.value,op,{in:'PLC', out:'DEC'}))
|
|
|
|
if (bus.error) return bus
|
|
|
|
bus = await this.bus.read(sreg(reg,packet.port))
|
|
|
|
if (bus.error) return bus
|
|
|
|
state.value = bus.response
|
|
|
|
reply.status = state.bwOp(pins.value,'check',{in:'PLC', out:'PLC'})
|
|
|
|
return reply
|
|
|
|
},
|
2018-02-10 15:28:51 -08:00
|
|
|
// threse three only for output pins
|
|
|
|
state : {
|
|
|
|
on: async function (packet) {
|
2018-02-17 18:25:27 -08:00
|
|
|
return this.pin._state(packet,'on')
|
2018-02-10 15:28:51 -08:00
|
|
|
},
|
|
|
|
off: async function (packet) {
|
2018-02-17 18:25:27 -08:00
|
|
|
return this.pin._state(packet,'off')
|
2018-02-10 15:28:51 -08:00
|
|
|
},
|
|
|
|
toggle: async function (packet) {
|
2018-02-17 18:25:27 -08:00
|
|
|
return this.pin._state(packet,'toggle')
|
2018-02-10 15:28:51 -08:00
|
|
|
}
|
|
|
|
},
|
2018-02-17 18:25:27 -08:00
|
|
|
// will create packet to determine pin caused interrupt, packet will come from interrupt module
|
|
|
|
interrupt: {
|
|
|
|
find: async function (packet) {
|
|
|
|
console.log('interrupt fired',packet)
|
|
|
|
return Promise.resolve({cmd:null})
|
|
|
|
},
|
|
|
|
report: ()=>{}, // come here after determining which pin to report to requester
|
|
|
|
async: async ()=>{}
|
|
|
|
}
|
2018-02-10 15:28:51 -08:00
|
|
|
}
|
|
|
|
|
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'})
|
2018-02-17 18:25:27 -08:00
|
|
|
else pins = pins.split(/[,:\s]+/).map(Number).filter( (x) => !Number.isNaN(x))
|
2018-02-11 11:41:45 -08:00
|
|
|
}
|
|
|
|
return new _.Byte(pins,'PLC')
|
|
|
|
}
|
|
|
|
|
2018-02-14 19:01:08 -08:00
|
|
|
const sreg = (reg,port) => {
|
|
|
|
return reg + ((port==='B') ? 0x10 : 0)
|
2018-02-11 11:41:45 -08:00
|
|
|
}
|