From 8bede723040d518a8125226775a0f9012be4f6b5 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Sat, 21 Jan 2017 12:23:45 -0800 Subject: [PATCH] refactoring for new interrupt class --- lib/interrupt.js | 54 ++++++++++++------------- package.json | 84 ++++++++++++++++++++------------------- test/apromise.test.js.off | 19 --------- test/interrupt.test.js | 13 +++--- 4 files changed, 77 insertions(+), 93 deletions(-) delete mode 100644 test/apromise.test.js.off diff --git a/lib/interrupt.js b/lib/interrupt.js index 23b7479..7cf6466 100644 --- a/lib/interrupt.js +++ b/lib/interrupt.js @@ -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() { diff --git a/package.json b/package.json index 47a3f4b..454ea82 100644 --- a/package.json +++ b/package.json @@ -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" + } } diff --git a/test/apromise.test.js.off b/test/apromise.test.js.off deleted file mode 100644 index 6fa66b6..0000000 --- a/test/apromise.test.js.off +++ /dev/null @@ -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')) - - }) -}) diff --git a/test/interrupt.test.js b/test/interrupt.test.js index 342a874..a7dbf92 100644 --- a/test/interrupt.test.js +++ b/test/interrupt.test.js @@ -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 () { //