add try catch to init

master
David Kebler 2017-01-21 12:21:16 -08:00
parent 9b1173f007
commit 66034b09cb
3 changed files with 129 additions and 59 deletions

View File

@ -1,32 +1,79 @@
'use strict' "use strict";
const Gpio = require('onoff').Gpio const fs = require('fs'),
EventEmitter = require('events'),
Epoll = require('epoll').Epoll
// ********************************** const GPIO_ROOT_PATH = '/sys/class/gpio/'
class Interrupt { class Interrupt extends EventEmitter {
// bus is i2c-bus bus object // bus is i2c-bus bus object
constructor(pin_number, processor, opts = {}) { constructor(pin_number, opts = {}) {
let dtimeout = opts.debounceTimeout ? opts.debounceTimeout : 200 super()
this.pin = new Gpio(pin_number, 'in', 'falling', { debounceTimeout: dtimeout }) this.num = pin_number;
this.processor = processor this.hook = opts.hook // will be passed back with the emit
this.path = GPIO_ROOT_PATH + 'gpio' + this.num + '/'
this.edge = opts.edge ? opts.edge : 'falling'
this.debounce = opts.debounce ? opts.debounce : 200
this.poller = new Epoll(function (err, fd, events) {
if (err) { this.emit('error', err) }
this.clear()
this.emit('fired', this.hook)
}.bind(this))
} }
init() { init() {
this.addListener(this.processor)
try {
if (fs.existsSync(this.path)) { this.exit() }
fs.writeFileSync(GPIO_ROOT_PATH + 'export', this.gpio);
fs.writeFileSync(this.path + 'direction', 'in');
fs.writeFileSync(this.path + 'edge', this.edge);
this.valueFd = fs.openSync(this.path + 'value', 'r+'); // Cache fd for performance.
this.clear();
this.start()
process.on('SIGINT', function () { process.on('SIGINT', function () {
this.pin.unexport(); console.log('\ncleaning up interrupt before exiting')
this.exit();
}) })
} catch (err) { return Promise.reject(err) }
return Promise.resolve('interrupt ready')
} }
addListener(processor) { fire(name = 'fired') {
this.pin.watch((err, value) => { this.emit(name, this.hook)
if (err) { return Promise.reject(err) } }
return processor
}); clear() {
fs.readSync(this.valuefd, null, 0, 1, 0);
}
exit() {
if (this.valuefd) {
this.stop()
fs.closeSync(this.valueFd)
}
try {
fs.writeFileSync(GPIO_ROOT_PATH + 'unexport', this.gpio);
} catch (ignore) {}
}
start() {
this.poller.add(this.valuefd, Epoll.EPOLLPRI);
}
stop() {
this.poller.remove(this.valuefd).close();
} }
} }
module.exports.Interrupt = Interrupt module.exports = {
Interrupt
}

View File

@ -3,9 +3,13 @@
"version": "0.0.1", "version": "0.0.1",
"description": "a class for adding interrupt processesing via sysfs and gpio pins on Raspberry and similar", "description": "a class for adding interrupt processesing via sysfs and gpio pins on Raspberry and similar",
"main": "index.js", "main": "index.js",
"watch": {
"testw": "{lib,test}/*.js"
},
"scripts": { "scripts": {
"testw": "./node_modules/.bin/mocha --reporter list --recursive --watch", "testw": "./node_modules/.bin/mocha --reporter list --recursive ",
"test": "istanbul cover ./node_modules/.bin/_mocha test/ --report lcovonly -- -R spec --recursive && codecov || true" "test": "istanbul cover ./node_modules/.bin/_mocha test/ --report lcovonly -- -R spec --recursive && codecov || true",
"watch": "./node_modules/.bin/npm-watch"
}, },
"author": "David Kebler", "author": "David Kebler",
"license": "MIT", "license": "MIT",
@ -33,6 +37,7 @@
"chai-as-promised": "^6.0.0", "chai-as-promised": "^6.0.0",
"codecov": "^1.0.1", "codecov": "^1.0.1",
"istanbul": "^0.4.5", "istanbul": "^0.4.5",
"mocha": "^3.2.0" "mocha": "^3.2.0",
"npm-watch": "^0.1.7"
} }
} }

View File

@ -1,17 +1,35 @@
'use strict' 'use strict'
const expect = require('chai').expect, const expect = require('chai').expect,
lib = require('../') Interrupt = require('../').Interrupt
//time-stamp for use when watching to distinguish reruns in console //time-stamp for use when watching to distinguish reruns in console
// place in alpha first file only // place in alpha first file only
let date = new Date(Date.now()) let date = new Date(Date.now())
console.log(date.getMinutes(), "\:", date.getSeconds()) console.log(date.getMinutes(), "\:", date.getSeconds())
describe('Test a template module ', function () { let inter17 = new Interrupt(17, { hook: 'a hook to something to do' })
it('Should test all methods', function () { // console.log('inter17', inter17)
expect(lib.hello('Forest Gump')).to.equal('Hello Forest Gump')
inter17.on('fired', hook => {
console.log('Listener fired and returned:', hook)
}) })
inter17.init()
.then((resp) => {
console.log('return from init()', resp)
setInterval(function () {
inter17.fire()
}, 1000)
}) })
.catch(err => console.log("error:", err))
// describe('Interrupt Class', function () {
//
// it('can be manually fired', function () {
// expect(hello('Forest Gump')).to.equal()
// })
//
// })
// })