0.1.25
add auto ack and interval for bus and an observer bus:ack fix bug in bus.ackmaster
parent
7bc824863f
commit
d4f19a9836
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@uci/i2c-bus",
|
||||
"version": "0.1.24",
|
||||
"version": "0.1.25",
|
||||
"description": "I2c Bus Classes for Communication to I2C bus via socket or direct call",
|
||||
"main": "src/bus",
|
||||
"scripts": {
|
||||
|
|
34
src/bus.js
34
src/bus.js
|
@ -9,24 +9,16 @@ let log = {}
|
|||
|
||||
class I2CBus extends Base {
|
||||
constructor(opts) {
|
||||
if (opts.path) opts.ns = { path: opts.path }
|
||||
if (!opts.ns) opts.ns = { path: 'i2c-bus' }
|
||||
opts.sockets = (opts.sockets ? opts.sockets + ',' : '') + 'ns#s>n'
|
||||
// TODO if port is passed as option then set up tcp with that port
|
||||
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'
|
||||
}
|
||||
opts.path =opts.path || 'i2c-bus' // always a pipe by default
|
||||
if (typeof opts.port === 'boolean') opts.port=1776
|
||||
opts.name = opts.name || 'i2c-bus'
|
||||
super(opts)
|
||||
this.id = opts.id || 'i2c-bus'
|
||||
log = logger({
|
||||
name: 'i2c-bus',
|
||||
id: this.id,
|
||||
file: 'src/bus.js',
|
||||
class: 'Bus'
|
||||
})
|
||||
log.debug({ opts: opts }, 'created bus with these opts')
|
||||
this.busnum = opts.busnum || 1
|
||||
this.i2cbus = i2c.open(this.busnum, () => {})
|
||||
this._funcs = bus_funcs
|
||||
|
@ -41,16 +33,19 @@ class I2CBus extends Base {
|
|||
this.bus.ack = async function (){
|
||||
//
|
||||
let bus = await this.bus.scan()
|
||||
if (bus.error) return bus
|
||||
let res = { cmd:'reply', ack: true, scan:bus.response}
|
||||
return res
|
||||
if (bus.error || !bus.response.length) bus.ack =false
|
||||
else bus.ack = true
|
||||
return bus
|
||||
},
|
||||
this.ackInterval = opts.ackInterval || 5
|
||||
this._queue = new PQueue({concurrency: opts.concurrency || 1})
|
||||
this.ready.addObserver('bus:ack')
|
||||
}
|
||||
|
||||
async init() {
|
||||
await super.init()
|
||||
let count = 0
|
||||
this.autoAck(this.ackInterval)
|
||||
this._queue.on('active', () => {
|
||||
log.debug(`Queue: working on item #${++count}. Size: ${this._queue.size} Pending: ${this._queue.pending}`)
|
||||
})
|
||||
|
@ -58,6 +53,14 @@ class I2CBus extends Base {
|
|||
|
||||
get busFuncs() { return this._funcs}
|
||||
|
||||
autoAck(interval) {
|
||||
if (interval>.9) this._autoAck = setInterval(async ()=> {
|
||||
let ack = (await this.bus.ack.call(this)).ack
|
||||
this.emit('bus:ack',ack)
|
||||
},interval*1000)
|
||||
else clearInterval(this._autoAck)
|
||||
}
|
||||
|
||||
addBusFuncs() {
|
||||
for (let alias in this.busFuncs) {
|
||||
let func = this.busFuncs[alias]
|
||||
|
@ -71,7 +74,6 @@ class I2CBus extends Base {
|
|||
args=name
|
||||
name=alias
|
||||
}
|
||||
console.log(alias,name,args)
|
||||
this.bus[alias] = busFunction.bind(this, alias, pify(this.i2cbus[name]), args ||[])
|
||||
}
|
||||
|
||||
|
@ -104,7 +106,7 @@ const validateArg = function (arg,value) {
|
|||
return valid
|
||||
}
|
||||
|
||||
async function busFunction (name, func, args=[],packet) {
|
||||
async function busFunction (name, func, args=[],packet={}) {
|
||||
let argsV = []
|
||||
args.some(arg => {
|
||||
if (packet.args) {
|
||||
|
|
Loading…
Reference in New Issue