finished refactoring bus and device classes and making tests

master
David Kebler 2017-01-15 23:16:14 -08:00
parent 193b003d07
commit 459e59bc90
4 changed files with 47 additions and 16 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/node_modules/ /node_modules/
/coverage/ /coverage/
/syncd/

View File

@ -22,31 +22,31 @@ class Bus {
// //
// } // }
scan() { return pify(this.bus.scan_p)() } scan() { return pify(this.bus.scan).bind(this.bus)() }
close() { return pify(this.bus.close_p)() } close() { return pify(this.bus.close).bind(this.bus)() }
readRaw(address, length, buffer) { readRaw(address, length, buffer) {
return pify(this.bus.i2cRead)(address, length, buffer) return pify(this.bus.i2cRead).bind(this.bus)(address, length, buffer)
} }
writeRaw(address, length, buffer) { writeRaw(address, length, buffer) {
return pify(this.bus.i2cWrite)(address, length, buffer) return pify(this.bus.i2cWrite).bind(this.bus)(address, length, buffer)
} }
read(address, cmd) { read(address, cmd) {
return pify(this.bus.readByte)(address, cmd) return pify(this.bus.readByte).bind(this.bus)(address, cmd)
} }
write(address, cmd, byte) { write(address, cmd, byte) {
return pify(this.bus.writeByte)(address, cmd, byte) return pify(this.bus.writeByte.bind(this.bus))(address, cmd, byte)
} }
read2(address, cmd) { read2(address, cmd) {
return pify(this.bus.readWord)(address, cmd) return pify(this.bus.readWord.bind(this.bus))(address, cmd)
} }
write2(address, cmd, bytes) { write2(address, cmd, bytes) {
return pify(this.bus.writeWord)(address, cmd, bytes) return pify(this.bus.writeWord.bind(this.bus))(address, cmd, bytes)
} }
} // end of Bus Class } // end of Bus Class

View File

@ -1,18 +1,23 @@
'use strict' 'use strict'
const expect = require('chai').expect, const chai = require('chai'),
Bus = require('../lib/bus').Bus chaiAsPromised = require("chai-as-promised"),
// _ = require('uci-utils') Bus = require('../lib/bus').Bus,
Device = require('../lib/device').Device
chai.use(chaiAsPromised);
const expect = chai.expect
let bus = new Bus() let bus = new Bus()
// console.log(bus.write.toString())
bus.write(0x20, 0x09, 0xFF) let addresses = [0x20, 0x23, 0x27]
describe('Bus Class - ', function () { describe('Bus Class - ', function () {
it('Promise Methods are exposed ', function () { it('Can scan the bus for devices', function () {
// expect(pin.cfg, "config getter failed").to.deep.equal(gpio.configs('output'))
}) return expect(bus.scan().catch(err => console.log("an error", err))).to.eventually.deep.equal(addresses)
}) })
})

25
test/device.test.js Normal file
View File

@ -0,0 +1,25 @@
'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))
})
})