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";
const fs = require('fs'),
const fs = require('mz/fs'),
EventEmitter = require('events'),
Epoll = require('epoll').Epoll
Epoll = require('epoll').Epoll,
pSeries = require('p-series')
const GPIO_ROOT_PATH = '/sys/class/gpio/'
@ -24,24 +25,17 @@ class Interrupt extends EventEmitter {
init() {
try {
if (fs.existsSync(this.path)) { this.exit() }
let tasks = [
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);
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')
return this.exit()
.then(() => { return pSeries(tasks) })
// .then(() => { return this.exit() })
}
@ -50,18 +44,24 @@ class Interrupt extends EventEmitter {
}
clear() {
fs.readSync(this.valuefd, null, 0, 1, 0);
let buffer = Buffer.from([0x00])
return fs.read(this.valuefd, buffer, 0, 1, 0);
}
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() {

View File

@ -1,43 +1,45 @@
{
"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": {
"testw": "{lib,test}/*.js"
},
"scripts": {
"testw": "./node_modules/.bin/mocha --reporter list --recursive ",
"test": "istanbul cover ./node_modules/.bin/_mocha test/ --report lcovonly -- -R spec --recursive && codecov || true",
"watch": "./node_modules/.bin/npm-watch"
},
"author": "David Kebler",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/uCOMmandIt/uci-interrrupt.git"
},
"keywords": [
"node.js",
"communication",
"serial",
"utilities",
"helpers"
],
"bugs": {
"url": "https://github.com/uCOMmandIt/uci-interrrupt/issues"
},
"homepage": "https://github.com/uCOMmandIt/uci-interrrupt#readme",
"dependencies": {
"onoff": "^1.1.1",
"require-all": "git+https://github.com/dkebler/node-require-all.git#merge"
},
"devDependencies": {
"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"
}
"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": {
"testw": "{lib,test}/*.js"
},
"scripts": {
"testw": "./node_modules/.bin/mocha --reporter list --recursive ",
"test": "istanbul cover ./node_modules/.bin/_mocha test/ --report lcovonly -- -R spec --recursive && codecov || true",
"watch": "./node_modules/.bin/npm-watch"
},
"author": "David Kebler",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/uCOMmandIt/uci-interrrupt.git"
},
"keywords": [
"node.js",
"communication",
"serial",
"utilities",
"helpers"
],
"bugs": {
"url": "https://github.com/uCOMmandIt/uci-interrrupt/issues"
},
"homepage": "https://github.com/uCOMmandIt/uci-interrrupt#readme",
"dependencies": {
"mz": "^2.6.0",
"onoff": "^1.1.1",
"p-series": "^1.0.0",
"require-all": "git+https://github.com/dkebler/node-require-all.git#merge"
},
"devDependencies": {
"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,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'
const expect = require('chai').expect,
Interrupt = require('../').Interrupt
// const expect = require('chai').expect
const Interrupt = require('../').Interrupt
//time-stamp for use when watching to distinguish reruns in console
// place in alpha first file only
@ -19,11 +20,11 @@ inter17.on('fired', hook => {
inter17.init()
.then((resp) => {
console.log('return from init()', resp)
setInterval(function () {
inter17.fire()
}, 1000)
// setInterval(function () {
// inter17.fire()
// }, 1000)
})
.catch(err => console.log("error:", err))
.catch(err => console.log("returned error:", err))
// describe('Interrupt Class', function () {
//