111 lines
3.1 KiB
JavaScript
111 lines
3.1 KiB
JavaScript
|
|
import i2c from 'i2c-bus'
|
|
import pify from 'pify'
|
|
// import Base from '@uci/base'
|
|
import Base from '../../uci-base/src/base'
|
|
|
|
export default class Bus extends Base {
|
|
constructor(opts) {
|
|
opts.sockets = opts.sockets || 'us'
|
|
super(opts)
|
|
this.busnum = opts.busnum || 1
|
|
this.i2cbus = i2c.open(this.busnum, () => {})
|
|
this.funcs = bus_funcs
|
|
this.registerPacketProcessor.bind(this)(process)
|
|
// this.init = this.init.bind(this)
|
|
}
|
|
|
|
async init(){
|
|
await super.init()
|
|
}
|
|
|
|
} // end of Bus Packet Class
|
|
|
|
// replace base processor with one for i2c bus functions
|
|
const process = async function (packet){
|
|
if (!packet.cmd) return {error: 'no cmd: key in packet', packet: packet }
|
|
if (this.context.funcs[packet.cmd]) {
|
|
// let res = validateArgs(packet)
|
|
// if (res.error) return res.error
|
|
packet.cmd_sent = packet.cmd
|
|
packet.response = await this.context.funcs[packet.cmd].bind(this.context)(packet.args)
|
|
packet.cmd = 'reply'
|
|
return packet
|
|
} else return {error: 'no i2c bus function available for packet command', packet: packet }
|
|
}
|
|
|
|
const validateArgs = function (packet) {
|
|
let missing = []
|
|
console.log(packet)
|
|
if (packet.cmd==='scan' || packet.cmd ==='close') return {}
|
|
if (!packet.args.address) missing.push('address')
|
|
switch (packet.cmd)
|
|
{
|
|
case 'readRaw':
|
|
case 'writeRaw':
|
|
if (!packet.args.length) missing.push('length')
|
|
if (!packet.args.buffer) missing.push('buffer')
|
|
break
|
|
case 'read':
|
|
case 'read2':
|
|
case 'write':
|
|
case 'write2':
|
|
if (!packet.args.cmd) missing.push('cmd')
|
|
}
|
|
|
|
switch (packet.cmd)
|
|
{
|
|
case 'write':
|
|
case 'write2':
|
|
case 'send':
|
|
if (!packet.args.byte) missing.push('byte')
|
|
}
|
|
|
|
if (missing.length > 0) {
|
|
return {error: `following arguments are missing ${missing}`, packet: packet }
|
|
}
|
|
return {}
|
|
}
|
|
|
|
const bus_funcs = {
|
|
|
|
scan: function () { return pify(this.i2cbus.scan).bind(this.i2cbus)() },
|
|
close: function () { return pify(this.i2cbus.close).bind(this.i2cbus)() },
|
|
|
|
readRaw: function (args) {
|
|
return pify(this.i2cbus.i2cRead).bind(this.i2cbus)(args.address, args.length, args.buffer)
|
|
},
|
|
|
|
writeRaw: function (args) {
|
|
return pify(this.i2cbus.i2cWrite).bind(this.i2cbus)(args.address, args.length, args.buffer)
|
|
},
|
|
|
|
read: function (args) {
|
|
// console.log('read: address, cmd', address, cmd)
|
|
return pify(this.i2cbus.readByte).bind(this.i2cbus)(args.address, args.cmd)
|
|
},
|
|
|
|
write: function (args) {
|
|
// console.log('write: address, cmd, byte', args.address, args.cmd, args.byte)
|
|
return pify(this.i2cbus.writeByte.bind(this.i2cbus))(args.address, args.cmd, args.byte)
|
|
},
|
|
|
|
read2: function (args) {
|
|
return pify(this.i2cbus.readWord.bind(this.i2cbus))(args.address, args.cmd)
|
|
},
|
|
|
|
write2: function (args) {
|
|
return pify(this.i2cbus.writeWord.bind(this.i2cbus))(args.address, args.cmd, args.byte)
|
|
},
|
|
|
|
receive: function (args) {
|
|
// console.log('receivebyte', address)
|
|
return pify(this.i2cbus.receiveByte.bind(this.i2cbus))(args.address)
|
|
},
|
|
|
|
send: function (args) {
|
|
// console.log('sendbyte', address,byte)
|
|
return pify(this.i2cbus.sendByte.bind(this.i2cbus))(args.address, args.byte)
|
|
}
|
|
} //end i2c functions
|