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() {
return fs.access(this.path)
.then(() => {
console.log('unexporting')
return fs.writeFile(GPIO_ROOT_PATH + 'unexport', this.num)
})
.then(() => {
if (this.valuefd) { if (this.valuefd) {
this.stop() this.stop()
fs.closeSync(this.valueFd) return fs.close(this.valuefd)
} }
try { })
fs.writeFileSync(GPIO_ROOT_PATH + 'unexport', this.gpio); .catch(err => { return Promise.resolve('already exited') })
} catch (ignore) {}
} }
start() { start() {

View File

@ -29,7 +29,9 @@
}, },
"homepage": "https://github.com/uCOMmandIt/uci-interrrupt#readme", "homepage": "https://github.com/uCOMmandIt/uci-interrrupt#readme",
"dependencies": { "dependencies": {
"mz": "^2.6.0",
"onoff": "^1.1.1", "onoff": "^1.1.1",
"p-series": "^1.0.0",
"require-all": "git+https://github.com/dkebler/node-require-all.git#merge" "require-all": "git+https://github.com/dkebler/node-require-all.git#merge"
}, },
"devDependencies": { "devDependencies": {

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 () {
// //