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)
process.on('SIGINT', function () { try {
this.pin.unexport(); 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 () {
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

@ -1,38 +1,43 @@
{ {
"name": "uci-interrupt", "name": "uci-interrupt",
"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",
"scripts": { "watch": {
"testw": "./node_modules/.bin/mocha --reporter list --recursive --watch", "testw": "{lib,test}/*.js"
"test": "istanbul cover ./node_modules/.bin/_mocha test/ --report lcovonly -- -R spec --recursive && codecov || true" },
}, "scripts": {
"author": "David Kebler", "testw": "./node_modules/.bin/mocha --reporter list --recursive ",
"license": "MIT", "test": "istanbul cover ./node_modules/.bin/_mocha test/ --report lcovonly -- -R spec --recursive && codecov || true",
"repository": { "watch": "./node_modules/.bin/npm-watch"
"type": "git", },
"url": "git+https://github.com/uCOMmandIt/uci-interrrupt.git" "author": "David Kebler",
}, "license": "MIT",
"keywords": [ "repository": {
"node.js", "type": "git",
"communication", "url": "git+https://github.com/uCOMmandIt/uci-interrrupt.git"
"serial", },
"utilities", "keywords": [
"helpers" "node.js",
], "communication",
"bugs": { "serial",
"url": "https://github.com/uCOMmandIt/uci-interrrupt/issues" "utilities",
}, "helpers"
"homepage": "https://github.com/uCOMmandIt/uci-interrrupt#readme", ],
"dependencies": { "bugs": {
"onoff": "^1.1.1", "url": "https://github.com/uCOMmandIt/uci-interrrupt/issues"
"require-all": "git+https://github.com/dkebler/node-require-all.git#merge" },
}, "homepage": "https://github.com/uCOMmandIt/uci-interrrupt#readme",
"devDependencies": { "dependencies": {
"chai": "^3.5.0", "onoff": "^1.1.1",
"chai-as-promised": "^6.0.0", "require-all": "git+https://github.com/dkebler/node-require-all.git#merge"
"codecov": "^1.0.1", },
"istanbul": "^0.4.5", "devDependencies": {
"mocha": "^3.2.0" "chai": "^3.5.0",
} "chai-as-promised": "^6.0.0",
"codecov": "^1.0.1",
"istanbul": "^0.4.5",
"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()
// })
//
// })
// })