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

View File

@ -29,7 +29,9 @@
},
"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": {

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