parent
f2db2ca87c
commit
5c225fc455
|
@ -32,8 +32,8 @@
|
||||||
"i2c-bus": "^3.x"
|
"i2c-bus": "^3.x"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@uci/base": "^0.1.6",
|
"@uci/base": "^0.1.8",
|
||||||
"@uci/logger": "0.0.3",
|
"@uci/logger": "0.0.6",
|
||||||
"better-try-catch": "^0.6.2",
|
"better-try-catch": "^0.6.2",
|
||||||
"pify": "^3.0.0"
|
"pify": "^3.0.0"
|
||||||
},
|
},
|
||||||
|
|
124
src/bus.js
124
src/bus.js
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
import i2c from 'i2c-bus'
|
import i2c from 'i2c-bus'
|
||||||
import pify from 'pify'
|
import pify from 'pify'
|
||||||
import btc from 'better-try-catch'
|
import btc from 'better-try-catch'
|
||||||
|
@ -7,56 +6,63 @@ import Base from '@uci/base'
|
||||||
import logger from '@uci/logger'
|
import logger from '@uci/logger'
|
||||||
let log = {}
|
let log = {}
|
||||||
|
|
||||||
export default class Bus extends Base {
|
class I2CBus extends Base {
|
||||||
constructor(opts) {
|
constructor(opts) {
|
||||||
log = logger({name:'i2c-bus',id:opts.id,file:'src/bus.js',class:'Bus'})
|
log = logger({
|
||||||
if (opts.path) opts.ns ={path:opts.path}
|
name: 'i2c-bus',
|
||||||
if(!opts.ns) opts.ns = {path:'i2c-bus'}
|
id: opts.id,
|
||||||
opts.sockets = (opts.sockets ? (opts.sockets+',') : '') + 'ns#s>n'
|
file: 'src/bus.js',
|
||||||
if(opts.tcp) {
|
class: 'Bus'
|
||||||
if (typeof opts.tcp==='number') opts.ts ={port:opts.tcp}
|
})
|
||||||
else opts.ts = {port:1776}
|
if (opts.path) opts.ns = { path: opts.path }
|
||||||
opts.sockets = (opts.sockets ? (opts.sockets+',') : '') + 'ts#s>t'
|
if (!opts.ns) opts.ns = { path: 'i2c-bus' }
|
||||||
|
opts.sockets = (opts.sockets ? opts.sockets + ',' : '') + 'ns#s>n'
|
||||||
|
if (opts.tcp) {
|
||||||
|
if (typeof opts.tcp === 'number') opts.ts = { port: opts.tcp }
|
||||||
|
else opts.ts = { port: 1776 }
|
||||||
|
opts.sockets = (opts.sockets ? opts.sockets + ',' : '') + 'ts#s>t'
|
||||||
}
|
}
|
||||||
super(opts)
|
super(opts)
|
||||||
log.info({opts:opts},'created bus with these opts')
|
log.info({ opts: opts }, 'created bus with these opts')
|
||||||
console.log({opts:opts},'\n created bus with these opts')
|
|
||||||
this.busnum = opts.busnum || 1
|
this.busnum = opts.busnum || 1
|
||||||
this.i2cbus = i2c.open(this.busnum, () => {})
|
this.i2cbus = i2c.open(this.busnum, () => {})
|
||||||
this.bus = bus_funcs
|
this.bus = bus_funcs
|
||||||
// this.init = this.init.bind(this)
|
// this.init = this.init.bind(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
async init(){
|
async init() {
|
||||||
await super.init()
|
await super.init()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO see if default processing of base can handle this now
|
// TODO see if default processing of base can handle this now
|
||||||
async _packetProcess (sname,packet){
|
async _packetProcess(sname, packet) {
|
||||||
// console.log('incoming packet', sname, packet)
|
if (!packet.cmd) return { error: 'no cmd: key in packet', packet: packet }
|
||||||
if (!packet.cmd) return {error: 'no cmd: key in packet', packet: packet }
|
|
||||||
if (this.bus[packet.cmd]) {
|
if (this.bus[packet.cmd]) {
|
||||||
let checked = validateArgs(packet) // handle with before hook
|
let checked = validateArgs(packet) // handle with before hook
|
||||||
if (checked.error) return checked.error
|
if (checked.error) return checked.error
|
||||||
let [err,res] = await btc(this.bus[packet.cmd].bind(this))(packet.args)
|
let [err, res] = await btc(this.bus[packet.cmd].bind(this))(packet.args)
|
||||||
if (err) return {error: err.msg, packet:packet}
|
if (err) return { error: err.msg, packet: packet }
|
||||||
packet.response = res
|
packet.response = res || ''
|
||||||
packet.cmd = 'reply'
|
packet.cmd = 'reply'
|
||||||
return packet
|
return packet
|
||||||
} else return {error: 'no i2c bus function available for packet command', packet: packet }
|
} else
|
||||||
|
return {
|
||||||
|
error: 'no i2c bus function available for packet command',
|
||||||
|
packet: packet
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // end of Bus Packet Class
|
} // end of Bus Packet Class
|
||||||
|
|
||||||
|
export default I2CBus
|
||||||
|
|
||||||
const validateArgs = function (packet) {
|
const validateArgs = function(packet) {
|
||||||
let missing = []
|
let missing = []
|
||||||
const ne = arg => { if (packet.args[arg]===undefined) missing.push(arg) }
|
const ne = arg => {
|
||||||
// console.log('validate packet',packet)
|
if (packet.args[arg] === undefined) missing.push(arg)
|
||||||
if (packet.cmd==='scan' || packet.cmd ==='close') return {}
|
}
|
||||||
|
if (packet.cmd === 'scan' || packet.cmd === 'close') return {}
|
||||||
ne('address')
|
ne('address')
|
||||||
switch (packet.cmd)
|
switch (packet.cmd) {
|
||||||
{
|
|
||||||
case 'readRaw':
|
case 'readRaw':
|
||||||
case 'writeRaw':
|
case 'writeRaw':
|
||||||
ne('length')
|
ne('length')
|
||||||
|
@ -68,48 +74,70 @@ const validateArgs = function (packet) {
|
||||||
case 'write2':
|
case 'write2':
|
||||||
ne('cmd')
|
ne('cmd')
|
||||||
}
|
}
|
||||||
switch (packet.cmd)
|
switch (packet.cmd) {
|
||||||
{
|
|
||||||
case 'write':
|
case 'write':
|
||||||
case 'write2':
|
case 'write2':
|
||||||
case 'send':
|
case 'send':
|
||||||
ne('byte')
|
ne('byte')
|
||||||
}
|
}
|
||||||
if (missing.length > 0) {
|
if (missing.length > 0) {
|
||||||
return {error: `following bus arguments are missing ${missing}`, packet: packet }
|
return {
|
||||||
|
error: `following bus arguments are missing ${missing}`,
|
||||||
|
packet: packet
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
|
|
||||||
const bus_funcs = {
|
const bus_funcs = {
|
||||||
scan: function () { return pify(this.i2cbus.scan).bind(this.i2cbus)() },
|
scan: function() {
|
||||||
close: function () { return pify(this.i2cbus.close).bind(this.i2cbus)() },
|
return pify(this.i2cbus.scan).bind(this.i2cbus)()
|
||||||
readRaw: function (args) {
|
|
||||||
return pify(this.i2cbus.i2cRead).bind(this.i2cbus)(args.address, args.length, args.buffer)
|
|
||||||
},
|
},
|
||||||
writeRaw: function (args) {
|
close: function() {
|
||||||
return pify(this.i2cbus.i2cWrite).bind(this.i2cbus)(args.address, args.length, args.buffer)
|
return pify(this.i2cbus.close).bind(this.i2cbus)()
|
||||||
},
|
},
|
||||||
read: function (args) {
|
readRaw: function(args) {
|
||||||
// console.log('read: address, cmd', address, cmd)
|
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)
|
return pify(this.i2cbus.readByte).bind(this.i2cbus)(args.address, args.cmd)
|
||||||
},
|
},
|
||||||
write: function (args) {
|
write: function(args) {
|
||||||
// console.log('write: address, cmd, byte', args.address, args.cmd, args.byte)
|
// 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)
|
return pify(this.i2cbus.writeByte.bind(this.i2cbus))(
|
||||||
|
args.address,
|
||||||
|
args.cmd,
|
||||||
|
args.byte
|
||||||
|
)
|
||||||
},
|
},
|
||||||
read2: function (args) {
|
read2: function(args) {
|
||||||
return pify(this.i2cbus.readWord.bind(this.i2cbus))(args.address, args.cmd)
|
return pify(this.i2cbus.readWord.bind(this.i2cbus))(args.address, args.cmd)
|
||||||
},
|
},
|
||||||
write2: function (args) {
|
write2: function(args) {
|
||||||
return pify(this.i2cbus.writeWord.bind(this.i2cbus))(args.address, args.cmd, args.byte)
|
return pify(this.i2cbus.writeWord.bind(this.i2cbus))(
|
||||||
|
args.address,
|
||||||
|
args.cmd,
|
||||||
|
args.byte
|
||||||
|
)
|
||||||
},
|
},
|
||||||
receive: function (args) {
|
receive: function(args) {
|
||||||
// console.log('receivebyte', address)
|
// console.log('receivebyte', address)
|
||||||
return pify(this.i2cbus.receiveByte.bind(this.i2cbus))(args.address)
|
return pify(this.i2cbus.receiveByte.bind(this.i2cbus))(args.address)
|
||||||
},
|
},
|
||||||
send: function (args) {
|
send: function(args) {
|
||||||
// console.log('sendbyte', address,byte)
|
// console.log('sendbyte', address,byte)
|
||||||
return pify(this.i2cbus.sendByte.bind(this.i2cbus))(args.address, args.byte)
|
return pify(this.i2cbus.sendByte.bind(this.i2cbus))(args.address, args.byte)
|
||||||
}
|
}
|
||||||
} //end i2c functions
|
} //end i2c functions
|
||||||
|
|
Loading…
Reference in New Issue