'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_p)() } close() { return pify(this.bus.close_p)() } readRaw(address, length, buffer) { return pify(this.bus.i2cRead)(address, length, buffer) } writeRaw(address, length, buffer) { return pify(this.bus.i2cWrite)(address, length, buffer) } read(address, cmd) { return pify(this.bus.readByte)(address, cmd) } write(address, cmd, byte) { return pify(this.bus.writeByte)(address, cmd, byte) } read2(address, cmd) { return pify(this.bus.readWord)(address, cmd) } write2(address, cmd, bytes) { return pify(this.bus.writeWord)(address, cmd, bytes) } } // end of Bus Class module.exports = { Bus };