From 7f9b54e2c76520b45db87a61ae0bdf99257be0ea Mon Sep 17 00:00:00 2001 From: David Kebler Date: Mon, 16 Jan 2017 21:37:57 -0800 Subject: [PATCH] split device class into a separate repo from bus (i2c) --- lib/bus.js | 15 ++++----------- lib/device.js | 45 --------------------------------------------- package.json | 3 +-- test/device.test.js | 25 ------------------------- 4 files changed, 5 insertions(+), 83 deletions(-) delete mode 100644 lib/device.js delete mode 100644 test/device.test.js diff --git a/lib/bus.js b/lib/bus.js index a5dd9dd..3eddb04 100644 --- a/lib/bus.js +++ b/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)() } diff --git a/lib/device.js b/lib/device.js deleted file mode 100644 index acfa043..0000000 --- a/lib/device.js +++ /dev/null @@ -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 -} diff --git a/package.json b/package.json index 2fd32c0..9382213 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/device.test.js b/test/device.test.js deleted file mode 100644 index 73b28d4..0000000 --- a/test/device.test.js +++ /dev/null @@ -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)) - }) -})