From 8303cc3cc86f99e781645161e2b8fdaf7d54ca48 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Fri, 26 May 2017 08:15:54 -0700 Subject: [PATCH] bit of cleanup and documentation --- .jshglobals | 13 ------- .jshintrc | 70 ------------------------------------- index.js | 8 ----- lib/interrupt.js | 76 ---------------------------------------- package.json | 12 +++---- readme.md | 20 +++++------ src/interrupt.js | 79 ++++++++++++++++++++++++++++++++++++++++++ temp/.gitignore | 3 -- test/interrupt.test.js | 69 +++++++++++++++++++----------------- 9 files changed, 130 insertions(+), 220 deletions(-) delete mode 100644 .jshglobals delete mode 100644 .jshintrc delete mode 100644 index.js delete mode 100644 lib/interrupt.js create mode 100644 src/interrupt.js delete mode 100644 temp/.gitignore diff --git a/.jshglobals b/.jshglobals deleted file mode 100644 index db4fe74..0000000 --- a/.jshglobals +++ /dev/null @@ -1,13 +0,0 @@ -{ -"globals": { - "Debug" : true, - /* MOCHA */ -"describe" : false, -"it" : false, -"xit" : false, -"before" : false, -"beforeEach" : false, -"after" : false, -"afterEach" : false - } -} diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 6ef7c9f..0000000 --- a/.jshintrc +++ /dev/null @@ -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" : false, // 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 -} diff --git a/index.js b/index.js deleted file mode 100644 index 90ae21c..0000000 --- a/index.js +++ /dev/null @@ -1,8 +0,0 @@ -let opts = { - dirname: __dirname + '/lib', - // http://stackoverflow.com/questions/2078915/a-regular-expression-to-exclude-a-word-string - filter: /^(?!index)([^\.].*)\.js?$/, - recursive: false, - 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); diff --git a/lib/interrupt.js b/lib/interrupt.js deleted file mode 100644 index ab3a60f..0000000 --- a/lib/interrupt.js +++ /dev/null @@ -1,76 +0,0 @@ -"use strict"; - -const - pigpio = require('pigpio'), - Gpio = pigpio.Gpio, - EventEmitter = require('events') - //,_ = require('uci-utils') - //const debounce = require('debounce'); - -class Interrupt extends EventEmitter { - constructor(pin_number, handler, opts = {}) { - super() - this.pin_number = pin_number - this.handler = handler - this.mode = Gpio.INPUT - this.pull = opts.pull ? opts.pull : Gpio.PUD_DOWN - this.edge = opts.edge ? opts.edge : Gpio.RISING_EDGE - this.sbc_interrupt = new Gpio( - this.pin_number, { - mode: this.mode, - pullUpDown: this.pull, - // edge: this.edge // don't set edge here as it will start the emitter -- see pigio js - } - ) - } - - init() { - - // console.log(`initialize interrupt ${this.pin_number}`) - return Promise.resolve() - - } - - get pin() { - return this.pin_number - } - - async start() { - - console.log(`starting interrupt on pin ${ this.pin_number}`) - - // for cntrl-c exit of interrupt - process.on('SIGINT', () => { - this.exit().then((resp) => console.log("\n", resp)) // unexport on cntrl-c - .catch(err => console.log("error:", err)) - }) - // There are two interrupts here. One on the I2c mcp board and one on the sbc/rpi - // The mcp interrupt trips the sbc interrupt which emits an event to run handler attached to the mcp class interrupt - let interrupt = this // scope `this` for use in the listener for each interrupt - console.log(`interrupt listener set for rpi ${interrupt.pin_number}`) - this.sbc_interrupt.on('interrupt', function () { - console.log(`rpi interrupt tripped by rpi pin ${interrupt.pin_number}`) - interrupt.emit('fired') - }) - - // rock n roll!!, turn on the pigpio interrupt - this.sbc_interrupt.enableInterrupt(this.edge) - - // return Promise.resolve("interrupt on pin listener") - } - - // manual firing for testing - fire(name = 'fired') { - console.log('firing interrupt handler', this.handler) - this.emit(name, this.handler) - } - - exit() { - pigpio.terminate() - return Promise.reject(`keyboard termination...terminating interrupt on pin ${this.pin_number}`) - } -} - -module.exports = { - Interrupt -} diff --git a/package.json b/package.json index f543b1b..273e269 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,8 @@ { - "name": "uci-interrupt", - "version": "0.0.1", - "description": "a class for adding interrupt processesing via sysfs and gpio pins on Raspberry and similar", - "main": "index.js", - "watch": { - "testi": "{lib,test}/*.js" - }, + "name": "@uci/interrupt", + "main": "src/interrupt.js", + "version": "0.1.0", + "description": "a class for adding interrupt processesing for gpio pins on Raspberry Pi and Similar SBCs", "scripts": { "testi": "node test/interrupt.test.js", "testw": "./node_modules/.bin/mocha --reporter list --recursive ", @@ -30,7 +27,6 @@ }, "homepage": "https://github.com/uCOMmandIt/uci-interrrupt#readme", "dependencies": { - "require-all": "git+https://github.com/dkebler/node-require-all.git#merge", "pigpio": "^0.x" }, "devDependencies": { diff --git a/readme.md b/readme.md index 4930a05..f00fc52 100644 --- a/readme.md +++ b/readme.md @@ -1,16 +1,14 @@ -# uCOMmandIt Template Package Repository +# uCOMmandIt Interrupt Package -[![Build Status](https://img.shields.io/travis/uCOMmandIt/uci-pkg-template.svg?branch=master)](https://travis-ci.org/uCOMmandIt/uci-pkg-template) -[![Inline docs](http://inch-ci.org/github/uCOMmandIt/uci-pkg-template.svg?branch=master)](http://inch-ci.org/github/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) -[![codecov](https://img.shields.io/codecov/c/github/uCOMmandIt/uci-pkg-template/master.svg)](https://codecov.io/gh/uCOMmandIt/uci-pkg-template) +[![Build Status](https://img.shields.io/travis/uCOMmandIt/uci-interrupt.svg?branch=master)](https://travis-ci.org/uCOMmandIt/uci-interrupt) +[![Inline docs](http://inch-ci.org/github/uCOMmandIt/uci-interrupt.svg?branch=master)](http://inch-ci.org/github/uCOMmandIt/uci-interrupt) +[![devDependencies](https://img.shields.io/david/dev/uCOMmandIt/uci-interrupt.svg)](https://david-dm.org/uCOMmandIt/uci-interrupt?type=dev) +[![codecov](https://img.shields.io/codecov/c/github/uCOMmandIt/uci-interrupt/master.svg)](https://codecov.io/gh/uCOMmandIt/uci-interrupt) -Clone this to get a quick start on a new uci package. It has all the testing ready to go with Travis-CI and code coverage. All the readme badges are included as well +This module creates an ES6 Interrupt class by extending the event emitter class for use . It calls uses the pigio C library via pigpio javascript bindings package in order to set up one or more pins on an SBC as interrupts that are then listened for. -Clone it for as a starting place for your own package! +The pigipio C library is specifically coded for the Raspberry Pi but might be adapatable to any single board computer (sbc) with Gpios +assuming they would be pigio C, compatiable. You must have pigpio C library installed first. -You'll need codecov and travis-ci accounts - -Interrupt Class +When a gpio pin so set up is tripped this class emits a "fired" message. If you pass a handler function it will be called when the "fired" message is emitted or you can set up your own "fired" listener to do as you please. diff --git a/src/interrupt.js b/src/interrupt.js new file mode 100644 index 0000000..8eab7bd --- /dev/null +++ b/src/interrupt.js @@ -0,0 +1,79 @@ +'use strict' + +// Creates an ES6 Interrupt class wrapper using the pigio C library and pigpio javascript wrapper package +// The pigipio C library is specifically coded for the Raspberry Pi but might be adapatable to any single board computer (sbc) with Gpios +// assuming they would be pigio C, compatiable. +// Must have pigpio C library installed first. +// On interrupt emits "fired" which can be caught by the code calling this interrupt instance for handling +// Understand there are two interrupts here. One is this class and one on the sbc/rpi via pigpio +// The sbc/pigpio interrupt emits an event which is listened for by this class which then emits +// a "fired" event which can be can be listed to by having a handle to an instance this class. + +const + pigpio = require('pigpio'), + sbcGpio = pigpio.Gpio, + EventEmitter = require('events') + +class Interrupt extends EventEmitter { + constructor(pin_number, handler, opts = {}) { + super() + this.pin_number = pin_number + this.handler = handler + this.mode = sbcGpio.INPUT + this.pull = opts.pull ? opts.pull : sbcGpio.PUD_DOWN + this.edge = opts.edge ? opts.edge : sbcGpio.RISING_EDGE + this.sbc_interrupt = new sbcGpio( + this.pin_number, { + mode: this.mode, + pullUpDown: this.pull, + // do not! set edge here as it will start the emitter -- see pigio js + } + ) + } + + // init() { + // + // // console.log(`initialize interrupt ${this.pin_number}`) + // return Promise.resolve() + // + // } + + get pin() { + return this.pin_number + } + + async start() { + + console.log(`starting interrupt on pin ${ this.pin_number}`) + + // for cntrl-c exit of interrupt + process.on('SIGINT', () => { + this.exit().then((resp) => console.log('\n', resp)) // unexport on cntrl-c + .catch(err => console.log('error:', err)) + }) + let interrupt = this // scope `this` for use in the listener for each interrupt + // console.log(`interrupt listener set for rpi ${interrupt.pin_number}`) + this.sbc_interrupt.on('interrupt', function () { + // console.log(`the sbc interrupt tripped by sbc pin ${interrupt.pin_number}`) + interrupt.emit('fired') // emit an event that can be listened for by the device that created the interrupt instance + }) + + // rock n roll!!, start the pigpio interrupt + this.sbc_interrupt.enableInterrupt(this.edge) + + } + + // manual firing for testing + fire() { + let interrupt = this + console.log('manually firing interrupt', this.handler) + this.emit('fired') + } + + exit() { + pigpio.terminate() + return Promise.reject(`keyboard termination...terminating interrupt on pin ${this.pin_number}`) + } +} + +module.exports = Interrupt diff --git a/temp/.gitignore b/temp/.gitignore deleted file mode 100644 index fa2d181..0000000 --- a/temp/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -/epoll.js -/interrupt.js.old -/onoff.js.off diff --git a/test/interrupt.test.js b/test/interrupt.test.js index 05ade38..7a3d233 100644 --- a/test/interrupt.test.js +++ b/test/interrupt.test.js @@ -1,39 +1,46 @@ 'use strict' -// const expect = require('chai').expect +const + Interrupt = require('../src/interrupt'), + expect = require('chai').expect -const Interrupt = require('../').Interrupt +// TODO finish simple test of a pin as interrupt -//time-stamp for use when watching to distinguish reruns in console -// place in alpha first file only -let date = new Date(Date.now()) -console.log(date.getMinutes(), "\:", date.getSeconds()) +describe( + `Testing `, + function () { + hooks() + interrupt() + someothertests() + }) -let inter17 = new Interrupt(17, { - hook: 'a hook to something to do' - // pullup: (num, state) => { return `someccommand using num and state` } // if rpi then specify a function that returns a command line line pullup tool using num and state - // pullup: 'external' // pullup not set with a command line utility - use alternative method such as device tree overlays -}) +//****************** TESTS ********************** +function interrupt() { + it('==> tests an interrupt', async function () { -// inter17.on('fired', hook => { -// console.log('Listener fired and returned:', hook) -// }) + expect(result, `test failed`).to.equal('expectedresult') + + }) +} + +function someothertests() { + it('==> test something', async function () { + + let result = await someasyncfunction() + expect(result, `test failed`).to.equal('expectedresult') + await pause(1000) + + }) +} + +function hooks() { + + before(async() => { + + }) + + beforeEach(async() => {}) + + after(async() => {}) -inter17.init() - .then((resp) => { - console.log(resp) - console.log('initialzed, waiting for interrupt to fire') - - }) - .catch(err => console.log("returned error:\n", err)) - -inter17.on('fired', hook => { counter(hook) }) - -let count = 0 - -function counter(hook) { - count++ - console.log(`******${count}********`) - console.log(hook) - console.log(`----------------------`) }