split device class into a separate repo from bus (i2c)
parent
cc4270fd83
commit
7f9b54e2c7
15
lib/bus.js
15
lib/bus.js
|
@ -1,10 +1,8 @@
|
|||
'use strict'
|
||||
|
||||
const i2c = require('i2c-bus'),
|
||||
pA = require('bluebird').promisifyAll,
|
||||
pQ = require('promisqueue')
|
||||
|
||||
const pify = require('pify')
|
||||
pify = require('pify'),
|
||||
PQueue = require('p-queue')
|
||||
|
||||
// TODO Create a Bus master class then BusRPi classes etc for actual hardware.
|
||||
|
||||
|
@ -12,16 +10,11 @@ class Bus {
|
|||
|
||||
constructor(busnum = 1) {
|
||||
this.busnum = busnum
|
||||
this.q = new pQ({ limit: 1 })
|
||||
this.qAdd = (pjob) => { this.q.add(() => pjob) }
|
||||
this.queue = new PQueue({ concurrency: 1 });
|
||||
this.qAdd = (p) => { this.queue.add(() => p) } // p is a promise
|
||||
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)() }
|
||||
|
||||
|
|
|
@ -1,45 +0,0 @@
|
|||
'use strict'
|
||||
|
||||
// **********************************
|
||||
|
||||
class Device {
|
||||
// bus is i2c-bus bus object
|
||||
constructor(bus, address, opts) {
|
||||
this.bus = bus
|
||||
this.address = address
|
||||
if (opts) {
|
||||
this.id = opts.id // must be unique within a bus
|
||||
this.desc = opts.desc
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
readRaw(length, buffer) {
|
||||
return this.bus.readRaw(this.address, length, buffer)
|
||||
}
|
||||
|
||||
writeRaw(length, buffer) {
|
||||
return this.bus.writeRaw(this.address, length, buffer)
|
||||
}
|
||||
|
||||
read(cmd) {
|
||||
return this.bus.read(this.address, cmd)
|
||||
}
|
||||
|
||||
write(cmd, byte) {
|
||||
return this.bus.write(this.address, cmd, byte)
|
||||
}
|
||||
|
||||
read2(cmd) {
|
||||
return this.bus.read2(this.address, cmd)
|
||||
}
|
||||
|
||||
write2(cmd, bytes) {
|
||||
return this.bus.write2(this.address, cmd, bytes)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
Device
|
||||
}
|
|
@ -21,10 +21,9 @@
|
|||
},
|
||||
"homepage": "https://github.com/uCOMmandIt/i2c#readme",
|
||||
"dependencies": {
|
||||
"bluebird": "^3.4.7",
|
||||
"i2c-bus": "^1.2.0",
|
||||
"p-queue": "^1.0.0",
|
||||
"pify": "^2.3.0",
|
||||
"promisqueue": "^1.0.3",
|
||||
"require-all": "git+https://github.com/dkebler/node-require-all.git#merge"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
'use strict'
|
||||
|
||||
const chai = require('chai'),
|
||||
chaiAsPromised = require("chai-as-promised"),
|
||||
Bus = require('../lib/bus').Bus,
|
||||
Device = require('../lib/device').Device
|
||||
// _ = require('uci-utils')
|
||||
|
||||
chai.use(chaiAsPromised);
|
||||
|
||||
const expect = chai.expect
|
||||
|
||||
let bus = new Bus()
|
||||
let device = new Device(bus, 0x20)
|
||||
|
||||
describe('Device Class - ', function () {
|
||||
|
||||
let SET = 0xff
|
||||
it('Can write and read to actual device', function () {
|
||||
|
||||
device.write(0x09, SET).then(expect(device.read(0x0A)).to.eventually.equal(SET))
|
||||
.then(setTimeout(() => device.write(0x09, 0), 3000))
|
||||
.catch(err => console.log("an error", err))
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue