refactoring for new interrupt class

master
David Kebler 2017-01-21 12:23:45 -08:00
parent 5b6c97e2b1
commit 8bede72304
4 changed files with 77 additions and 93 deletions

View File

@ -1,8 +1,9 @@
"use strict"; "use strict";
const fs = require('fs'), const fs = require('mz/fs'),
EventEmitter = require('events'), EventEmitter = require('events'),
Epoll = require('epoll').Epoll Epoll = require('epoll').Epoll,
pSeries = require('p-series')
const GPIO_ROOT_PATH = '/sys/class/gpio/' const GPIO_ROOT_PATH = '/sys/class/gpio/'
@ -24,24 +25,17 @@ class Interrupt extends EventEmitter {
init() { init() {
try { let tasks = [
if (fs.existsSync(this.path)) { this.exit() } fs.writeFile(GPIO_ROOT_PATH + 'export', this.num)
// fs.writeFile(this.path + 'direction', 'in'),
// fs.writeFile(this.path + 'edge', this.edge),
// fs.open(this.path + 'value', 'r+').then(fd => { this.valuefd = fd }),
// this.clear().then(() => { this.start() })
]
fs.writeFileSync(GPIO_ROOT_PATH + 'export', this.gpio); return this.exit()
fs.writeFileSync(this.path + 'direction', 'in'); .then(() => { return pSeries(tasks) })
fs.writeFileSync(this.path + 'edge', this.edge); // .then(() => { return this.exit() })
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')
} }
@ -50,18 +44,24 @@ class Interrupt extends EventEmitter {
} }
clear() { clear() {
fs.readSync(this.valuefd, null, 0, 1, 0); let buffer = Buffer.from([0x00])
return fs.read(this.valuefd, buffer, 0, 1, 0);
} }
exit() { exit() {
if (this.valuefd) {
this.stop()
fs.closeSync(this.valueFd)
}
try {
fs.writeFileSync(GPIO_ROOT_PATH + 'unexport', this.gpio);
} catch (ignore) {}
return fs.access(this.path)
.then(() => {
console.log('unexporting')
return fs.writeFile(GPIO_ROOT_PATH + 'unexport', this.num)
})
.then(() => {
if (this.valuefd) {
this.stop()
return fs.close(this.valuefd)
}
})
.catch(err => { return Promise.resolve('already exited') })
} }
start() { start() {

View File

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

View File

@ -1,19 +0,0 @@
'use strict'
const chai = require('chai'),
chaiAsPromised = require("chai-as-promised"),
lib = require('../')
chai.use(chaiAsPromised);
const expect = chai.expect
describe('Promise Stuff - ', function () {
it('Can test a promise', function () {
return expect(lib.apromise).to.eventually.equal('some promise hey')
return Promise.resolve().then(() => expect(lib.apromise).to.eventually.equal('some promise hey'))
})
})

View File

@ -1,7 +1,8 @@
'use strict' 'use strict'
const expect = require('chai').expect, // const expect = require('chai').expect
Interrupt = require('../').Interrupt
const 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
@ -19,11 +20,11 @@ inter17.on('fired', hook => {
inter17.init() inter17.init()
.then((resp) => { .then((resp) => {
console.log('return from init()', resp) console.log('return from init()', resp)
setInterval(function () { // setInterval(function () {
inter17.fire() // inter17.fire()
}, 1000) // }, 1000)
}) })
.catch(err => console.log("error:", err)) .catch(err => console.log("returned error:", err))
// describe('Interrupt Class', function () { // describe('Interrupt Class', function () {
// //