uci-i2c-bus/lib/bus.js

57 lines
1.3 KiB
JavaScript

'use strict'
const i2c = require('i2c-bus'),
pA = require('bluebird').promisifyAll,
pQ = require('promisqueue')
const pify = require('pify')
// TODO Create a Bus master class then BusRPi classes etc for actual hardware.
class Bus {
constructor(busnum = 1) {
this.busnum = busnum
this.q = new pQ({ limit: 1 })
this.qAdd = (pjob) => { this.q.add(() => pjob) }
this.bus = i2c.open(this.busnum, () => {})
}
// init() {
// const bus = pA(i2c.open(this.busnum, () => {}), { suffix: "_p" }) //,multiArgs: true})
//
// }
scan() { return pify(this.bus.scan).bind(this.bus)() }
close() { return pify(this.bus.close).bind(this.bus)() }
readRaw(address, length, buffer) {
return pify(this.bus.i2cRead).bind(this.bus)(address, length, buffer)
}
writeRaw(address, length, buffer) {
return pify(this.bus.i2cWrite).bind(this.bus)(address, length, buffer)
}
read(address, cmd) {
return pify(this.bus.readByte).bind(this.bus)(address, cmd)
}
write(address, cmd, byte) {
return pify(this.bus.writeByte.bind(this.bus))(address, cmd, byte)
}
read2(address, cmd) {
return pify(this.bus.readWord.bind(this.bus))(address, cmd)
}
write2(address, cmd, bytes) {
return pify(this.bus.writeWord.bind(this.bus))(address, cmd, bytes)
}
} // end of Bus Class
module.exports = {
Bus
};