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",
|
"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",
|
"description": "I2c Bus Classes for Communication to I2C bus via socket or direct call",
|
||||||
"main": "src/bus",
|
"main": "src/bus",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
34
src/bus.js
34
src/bus.js
|
@ -9,24 +9,16 @@ let log = {}
|
||||||
|
|
||||||
class I2CBus extends Base {
|
class I2CBus extends Base {
|
||||||
constructor(opts) {
|
constructor(opts) {
|
||||||
if (opts.path) opts.ns = { path: opts.path }
|
opts.path =opts.path || 'i2c-bus' // always a pipe by default
|
||||||
if (!opts.ns) opts.ns = { path: 'i2c-bus' }
|
if (typeof opts.port === 'boolean') opts.port=1776
|
||||||
opts.sockets = (opts.sockets ? opts.sockets + ',' : '') + 'ns#s>n'
|
opts.name = opts.name || 'i2c-bus'
|
||||||
// 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'
|
|
||||||
}
|
|
||||||
super(opts)
|
super(opts)
|
||||||
this.id = opts.id || 'i2c-bus'
|
|
||||||
log = logger({
|
log = logger({
|
||||||
name: 'i2c-bus',
|
name: 'i2c-bus',
|
||||||
id: this.id,
|
id: this.id,
|
||||||
file: 'src/bus.js',
|
file: 'src/bus.js',
|
||||||
class: 'Bus'
|
class: 'Bus'
|
||||||
})
|
})
|
||||||
log.debug({ opts: opts }, '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._funcs = bus_funcs
|
this._funcs = bus_funcs
|
||||||
|
@ -41,16 +33,19 @@ class I2CBus extends Base {
|
||||||
this.bus.ack = async function (){
|
this.bus.ack = async function (){
|
||||||
//
|
//
|
||||||
let bus = await this.bus.scan()
|
let bus = await this.bus.scan()
|
||||||
if (bus.error) return bus
|
if (bus.error || !bus.response.length) bus.ack =false
|
||||||
let res = { cmd:'reply', ack: true, scan:bus.response}
|
else bus.ack = true
|
||||||
return res
|
return bus
|
||||||
},
|
},
|
||||||
|
this.ackInterval = opts.ackInterval || 5
|
||||||
this._queue = new PQueue({concurrency: opts.concurrency || 1})
|
this._queue = new PQueue({concurrency: opts.concurrency || 1})
|
||||||
|
this.ready.addObserver('bus:ack')
|
||||||
}
|
}
|
||||||
|
|
||||||
async init() {
|
async init() {
|
||||||
await super.init()
|
await super.init()
|
||||||
let count = 0
|
let count = 0
|
||||||
|
this.autoAck(this.ackInterval)
|
||||||
this._queue.on('active', () => {
|
this._queue.on('active', () => {
|
||||||
log.debug(`Queue: working on item #${++count}. Size: ${this._queue.size} Pending: ${this._queue.pending}`)
|
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}
|
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() {
|
addBusFuncs() {
|
||||||
for (let alias in this.busFuncs) {
|
for (let alias in this.busFuncs) {
|
||||||
let func = this.busFuncs[alias]
|
let func = this.busFuncs[alias]
|
||||||
|
@ -71,7 +74,6 @@ class I2CBus extends Base {
|
||||||
args=name
|
args=name
|
||||||
name=alias
|
name=alias
|
||||||
}
|
}
|
||||||
console.log(alias,name,args)
|
|
||||||
this.bus[alias] = busFunction.bind(this, alias, pify(this.i2cbus[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
|
return valid
|
||||||
}
|
}
|
||||||
|
|
||||||
async function busFunction (name, func, args=[],packet) {
|
async function busFunction (name, func, args=[],packet={}) {
|
||||||
let argsV = []
|
let argsV = []
|
||||||
args.some(arg => {
|
args.some(arg => {
|
||||||
if (packet.args) {
|
if (packet.args) {
|
||||||
|
|
Loading…
Reference in New Issue