uci-i2c-device/lib/bus.js

57 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-01-12 19:50:06 -08:00
'use strict'
const i2c = require('i2c-bus'),
2017-01-13 19:51:55 -08:00
pA = require('bluebird').promisifyAll,
pQ = require('promisqueue')
2017-01-12 19:50:06 -08:00
const pify = require('pify')
2017-01-13 19:51:55 -08:00
// TODO Create a Bus master class then BusRPi classes etc for actual hardware.
2017-01-12 19:50:06 -08:00
class Bus {
constructor(busnum = 1) {
this.busnum = busnum
2017-01-13 19:51:55 -08:00
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)
2017-01-13 19:51:55 -08:00
}
2017-01-12 19:50:06 -08:00
write(address, cmd, byte) {
return pify(this.bus.writeByte.bind(this.bus))(address, cmd, byte)
}
2017-01-12 19:50:06 -08:00
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)
}
2017-01-12 19:50:06 -08:00
} // end of Bus Class
2017-01-13 21:13:39 -08:00
module.exports = {
Bus
};