uci-i2c-device/lib/bus.js

57 lines
1.2 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_p)() }
close() { return pify(this.bus.close_p)() }
readRaw(address, length, buffer) {
2017-01-15 18:47:43 -08:00
return pify(this.bus.i2cRead)(address, length, buffer)
}
writeRaw(address, length, buffer) {
2017-01-15 18:47:43 -08:00
return pify(this.bus.i2cWrite)(address, length, buffer)
}
read(address, cmd) {
2017-01-15 18:47:43 -08:00
return pify(this.bus.readByte)(address, cmd)
2017-01-13 19:51:55 -08:00
}
2017-01-12 19:50:06 -08:00
write(address, cmd, byte) {
2017-01-15 18:47:43 -08:00
return pify(this.bus.writeByte)(address, cmd, byte)
}
2017-01-12 19:50:06 -08:00
read2(address, cmd) {
2017-01-15 18:47:43 -08:00
return pify(this.bus.readWord)(address, cmd)
}
write2(address, cmd, bytes) {
2017-01-15 18:47:43 -08:00
return pify(this.bus.writeWord)(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
};