refactor bus and device class, testing of i2c r/w

master
David Kebler 2017-01-15 17:59:50 -08:00
parent 25c4cbfba3
commit 25a3ca9095
4 changed files with 63 additions and 16 deletions

View File

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

View File

@ -5,7 +5,7 @@
class Device { class Device {
// bus is i2c-bus bus object // bus is i2c-bus bus object
constructor(bus, address, opts) { constructor(bus, address, opts) {
this.bus = bus.methods // artifact of adapting ic2-bus to class format this.bus = bus
this.address = address this.address = address
if (opts) { if (opts) {
this.id = opts.id // must be unique within a bus this.id = opts.id // must be unique within a bus
@ -15,33 +15,29 @@ class Device {
} }
readRaw(length, buffer) { readRaw(length, buffer) {
return this.bus.i2cRead_p(this.address, length, buffer) return this.bus.readRaw(this.address, length, buffer)
} }
writeRaw(length, buffer) { writeRaw(length, buffer) {
return this.bus.i2cWrite_p(this.address, length, buffer) return this.bus.writeRaw(this.address, length, buffer)
} }
read(cmd) { read(cmd) {
return this.bus.readByte_p(this.address, cmd) return this.bus.read(this.address, cmd)
} }
write(cmd, byte) { write(cmd, byte) {
return this.bus.writeByte_p(this.address, cmd, byte) return this.bus.write(this.address, cmd, byte)
} }
read2(cmd) { read2(cmd) {
return this.bus.readWord_p(this.address, cmd) return this.bus.read2(this.address, cmd)
} }
write2(cmd, bytes) { write2(cmd, bytes) {
return this.bus.writeWord_p(this.address, cmd, bytes) return this.bus.write2(this.address, cmd, bytes)
} }
// command with more than two bytes following
readBytes() {}
writeBytes() {}
} }
module.exports = { module.exports = {

View File

@ -23,6 +23,7 @@
"dependencies": { "dependencies": {
"bluebird": "^3.4.7", "bluebird": "^3.4.7",
"i2c-bus": "^1.2.0", "i2c-bus": "^1.2.0",
"pify": "^2.3.0",
"promisqueue": "^1.0.3", "promisqueue": "^1.0.3",
"require-all": "git+https://github.com/dkebler/node-require-all.git#merge" "require-all": "git+https://github.com/dkebler/node-require-all.git#merge"
}, },

18
test/bus.test.js Normal file
View File

@ -0,0 +1,18 @@
'use strict'
const expect = require('chai').expect,
Bus = require('../lib/bus').Bus
// _ = require('uci-utils')
let bus = new Bus()
// console.log(bus.write.toString())
bus.write(0x21, 0x09, 0xFF)
describe('Bus Class - ', function () {
it('Promise Methods are exposed ', function () {
// expect(pin.cfg, "config getter failed").to.deep.equal(gpio.configs('output'))
})
});