add device class back to this package - remove promise q from bus module.

master
David Kebler 2017-05-25 09:56:34 -07:00
parent c4575be271
commit 1cdbb171d3
9 changed files with 92 additions and 104 deletions

View File

@ -1,13 +0,0 @@
{
"globals": {
"Debug" : true,
/* MOCHA */
"describe" : false,
"it" : false,
"xit" : false,
"before" : false,
"beforeEach" : false,
"after" : false,
"afterEach" : false
}
}

View File

@ -1,70 +0,0 @@
{
// https://gist.github.com/connor/1597131
// Globals - import repo spectific globals
"extends": "./.jshglobals",
// Settings
"passfail" : false, // Stop on first error.
"maxerr" : 100, // Maximum error before stopping.
// Predefined globals whom JSHint will ignore.
"browser" : true, // Standard browser globals e.g. `window`, `document`.
"node" : true,
"rhino" : false,
"couch" : false,
"wsh" : true, // Windows Scripting Host.
"jquery" : true,
"prototypejs" : false,
"mootools" : false,
"dojo" : false,
// Development.
"debug" : false, // Allow debugger statements e.g. browser breakpoints.
"devel" : true, // Allow developments statements e.g. `console.log();`.
// ECMAScript
"esversion" : 6, //use this in new version of jshint
"strict" : false, // Require `use strict` pragma in every file.
"globalstrict" : false, // Allow global "use strict" (also enables 'strict').
// The Good Parts.
"asi" : true, // Tolerate Automatic Semicolon Insertion (no semicolons).
"laxbreak" : true, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons.
"bitwise" : true, // Allow bitwise operators (&, |, ^, etc.).
"boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments.
"curly" : true, // Require {} for every new block or scope.
"eqeqeq" : true, // Require triple equals i.e. `===`.
"eqnull" : false, // Tolerate use of `== null`.
"evil" : false, // Tolerate use of `eval`.
"expr" : false, // Tolerate `ExpressionStatement` as Programs.
"forin" : false, // Tolerate `for in` loops without `hasOwnPrototype`.
"immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );`
"latedef" : false, // Prohipit variable use before definition.
"loopfunc" : false, // Allow functions to be defined within loops.
"noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`.
"regexp" : true, // Prohibit `.` and `[^...]` in regular expressions.
"regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`.
"scripturl" : true, // Tolerate script-targeted URLs.
"shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`.
"supernew" : false, // Tolerate `new function () { ... };` and `new Object;`.
"undef" : true, // Require all non-global variables be declared before they are used.
// Personal styling preferences.
"newcap" : false, // Require capitalization of all constructor functions e.g. `new F()`.
"noempty" : true, // Prohibit use of empty blocks.
"nonew" : true, // Prohibit use of constructors for side-effects.
"nomen" : true, // Prohibit use of initial or trailing underbars in names.
"onevar" : false, // Allow only one `var` statement per function.
"plusplus" : false, // Prohibit use of `++` & `--`.
"sub" : false, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`.
"trailing" : true, // Prohibit trailing whitespaces.
"white" : false, // Check against strict whitespace and indentation rules.
"indent" : 2 // Specify indentation spacing
}

View File

@ -1,9 +1,7 @@
language: node_js language: node_js
node_js: node_js:
- '4.0' - '7.10'
- '5.0'
- '6.0'
- 'node' - 'node'
sudo: false sudo: false

View File

@ -5,4 +5,4 @@ let opts = {
recursive: false, recursive: false,
merge: true // remove or comment to have each file in /lib be a prop/key in library...see node-require-all merge: true // remove or comment to have each file in /lib be a prop/key in library...see node-require-all
} }
module.exports = require('require-all')(opts); module.exports = require('@uci/require-all')(opts)

View File

@ -1,17 +1,15 @@
// Solely a promise wrapper module for the basic async functions of the fivdi/i2c-bus javascript bindings C API
// https://github.com/fivdi/i2c-bus
'use strict' 'use strict'
const i2c = require('i2c-bus'), const i2c = require('i2c-bus'),
pify = require('pify'), pify = require('pify')
PQueue = require('p-queue')
// TODO Create a Bus master class then BusRPi classes etc for actual hardware.
class Bus { class Bus {
constructor(busnum = 1) { constructor(busnum = 1) {
this.busnum = busnum this.busnum = busnum
this.queue = new PQueue({ concurrency: 1 });
this.qAdd = (p) => { this.queue.add(() => p) } // p is a promise
this.bus = i2c.open(this.busnum, () => {}) this.bus = i2c.open(this.busnum, () => {})
} }
@ -48,4 +46,4 @@ class Bus {
module.exports = { module.exports = {
Bus Bus
}; }

47
lib/device.js Normal file
View File

@ -0,0 +1,47 @@
'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
}
}
// for devices needing a buffer/stream
readRaw(length, buffer) {
return this.bus.readRaw(this.address, length, buffer)
}
writeRaw(length, buffer) {
return this.bus.writeRaw(this.address, length, buffer)
}
// both cmd and byte should be a single byte as a decimal or hex
read(cmd) {
return this.bus.read(this.address, cmd)
}
write(cmd, byte) {
return this.bus.write(this.address, cmd, byte)
}
// for I2C devices that use a word length packackage
read2(cmd) {
return this.bus.read2(this.address, cmd)
}
write2(cmd, bytes) {
return this.bus.write2(this.address, cmd, bytes)
}
}
module.exports = {
Device
}

View File

@ -1,5 +1,5 @@
{ {
"name": "uci-i2c", "name": "@uci/i2c",
"version": "0.0.1", "version": "0.0.1",
"description": "Bus and Device Classes for I2C Interfacing", "description": "Bus and Device Classes for I2C Interfacing",
"main": "index.js", "main": "index.js",
@ -14,17 +14,19 @@
"url": "git+https://github.com/uCOMmandIt/i2c.git" "url": "git+https://github.com/uCOMmandIt/i2c.git"
}, },
"keywords": [ "keywords": [
"node.js" "node.js",
"i2c",
"rpi",
"raspberrypi"
], ],
"bugs": { "bugs": {
"url": "https://github.com/uCOMmandIt/i2c/issues" "url": "https://github.com/uCOMmandIt/i2c/issues"
}, },
"homepage": "https://github.com/uCOMmandIt/i2c#readme", "homepage": "https://github.com/uCOMmandIt/i2c#readme",
"dependencies": { "dependencies": {
"@uci/require-all": "^2.3.0",
"i2c-bus": "^1.2.0", "i2c-bus": "^1.2.0",
"p-queue": "^1.0.0", "pify": "^2.3.0"
"pify": "^2.3.0",
"require-all": "git+https://github.com/dkebler/node-require-all.git#merge"
}, },
"devDependencies": { "devDependencies": {
"chai": "^3.5.0", "chai": "^3.5.0",

View File

@ -6,3 +6,5 @@
[![Dependencies](https://img.shields.io/david/uCOMmandIt/uci-pkg-template.svg)](https://david-dm.org/uCOMmandIt/uci-pkg-template) [![Dependencies](https://img.shields.io/david/uCOMmandIt/uci-pkg-template.svg)](https://david-dm.org/uCOMmandIt/uci-pkg-template)
[![devDependencies](https://img.shields.io/david/dev/uCOMmandIt/uci-pkg-template.svg)](https://david-dm.org/uCOMmandIt/uci-pkg-template?type=dev) [![devDependencies](https://img.shields.io/david/dev/uCOMmandIt/uci-pkg-template.svg)](https://david-dm.org/uCOMmandIt/uci-pkg-template?type=dev)
[![codecov](https://img.shields.io/codecov/c/github/uCOMmandIt/uci-pkg-template/master.svg)](https://codecov.io/gh/uCOMmandIt/uci-pkg-template) [![codecov](https://img.shields.io/codecov/c/github/uCOMmandIt/uci-pkg-template/master.svg)](https://codecov.io/gh/uCOMmandIt/uci-pkg-template)
A promise wrapper for an I2C arm C library methods and a Device Class that is the base class for an I2C device

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

@ -0,0 +1,24 @@
'use strict'
const chai = require('chai'),
chaiAsPromised = require('chai-as-promised'),
Bus = require('@uci/i2c').Bus,
Device = require('../lib/device').Device
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))
})
})