initial commit
This commit is contained in:
commit
f2d3961686
|
@ -0,0 +1 @@
|
|||
node_modules
|
|
@ -0,0 +1,30 @@
|
|||
# GPIO-mock test nodejs
|
||||
|
||||
test gpio in a container (without them being physically available)
|
||||
|
||||
idea/initial mock forked from: https://github.com/sitepoint-editors/TddWithGpio
|
||||
|
||||
Article: https://www.sitepoint.com/getting-started-with-the-raspberry-pi-gpio-pins-in-node-js/
|
||||
|
||||
## run dev
|
||||
|
||||
~~~shell
|
||||
docker run -it --rm -v $(pwd):/app -w /app node:lts bash
|
||||
~~~
|
||||
|
||||
followed by
|
||||
|
||||
~~~shell
|
||||
npm install
|
||||
npm run test
|
||||
~~~
|
||||
|
||||
## Jest
|
||||
|
||||
https://jestjs.io/docs/using-matchers
|
||||
|
||||
## TODO
|
||||
|
||||
check this out: https://stackoverflow.com/questions/50066138/mock-fs-function-with-jest
|
||||
|
||||
is this going to be a problem? https://stackoverflow.com/questions/62568294/nodejs-fs-watch-inside-docker-container-detects-changes-twice
|
|
@ -0,0 +1,17 @@
|
|||
var gpio = require('./gpio');
|
||||
|
||||
gpio.open(16, 'out', function () {
|
||||
var on = 0;
|
||||
|
||||
var blinker = setInterval(function () {
|
||||
gpio.write(16, on, function () {
|
||||
on = (on + 1) % 2;
|
||||
|
||||
console.log('ON = ' + on);
|
||||
});
|
||||
}, 1000);
|
||||
|
||||
setTimeout(function () {
|
||||
clearInterval(blinker);
|
||||
}, 12000);
|
||||
});
|
|
@ -0,0 +1,27 @@
|
|||
var fs = require('fs');
|
||||
|
||||
// Post 3.18.x kernel
|
||||
var sysFsPath = '/sys/class/gpio/gpio';
|
||||
var pinMapping = {
|
||||
'16': 23
|
||||
};
|
||||
|
||||
function open(pinNumber, direction, callback) {
|
||||
const path = sysFsPath + pinMapping[pinNumber] + '/direction';
|
||||
|
||||
fs.writeFile(path, direction, (callback || noOp));
|
||||
}
|
||||
|
||||
function write(pinNumber, value, callback) {
|
||||
const path = sysFsPath + pinMapping[pinNumber] + '/value';
|
||||
value = !!value ? '1' : '0';
|
||||
|
||||
fs.writeFile(path, value, 'utf8', callback);
|
||||
}
|
||||
|
||||
function noOp() {}
|
||||
|
||||
module.exports = {
|
||||
open: open,
|
||||
write: write
|
||||
};
|
|
@ -0,0 +1,22 @@
|
|||
{
|
||||
"name": "tdd-with-gpio",
|
||||
"version": "0.0.1",
|
||||
"main": "app.js",
|
||||
"scripts": {
|
||||
"start": "node app.js",
|
||||
"test": "jest"
|
||||
|
||||
},
|
||||
"author": {
|
||||
"name": "CR",
|
||||
"email": "reyesc@gmail.com"
|
||||
},
|
||||
"devDependencies": {
|
||||
"jest": "^29.3.1",
|
||||
"mock-fs": "^5.2.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"fix": "^0.0.6",
|
||||
"mqtt": "^4.3.7"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
var mock = require('mock-fs');
|
||||
var fs = require('fs');
|
||||
|
||||
var gpio = require('../gpio');
|
||||
|
||||
describe('GPIO API', function () {
|
||||
it('opens a pin with out', function (done) {
|
||||
mock({
|
||||
'/sys/class/gpio/gpio23/direction': ''
|
||||
});
|
||||
|
||||
gpio.open(16, 'out', function () {
|
||||
const direction = fs.readFileSync('/sys/class/gpio/gpio23/direction').toString();
|
||||
|
||||
expect(direction).toEqual('out');
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('writes to a pin with a high value', function (done) {
|
||||
mock({
|
||||
'/sys/class/gpio/gpio23/value': '0'
|
||||
});
|
||||
|
||||
gpio.write(16, 5, function () {
|
||||
const value = fs.readFileSync('/sys/class/gpio/gpio23/value').toString();
|
||||
|
||||
expect(value).toEqual('1');
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('writes to a pin with a low value', function (done) {
|
||||
mock({
|
||||
'/sys/class/gpio/gpio23/value': '1'
|
||||
});
|
||||
|
||||
gpio.write(16, 0, function () {
|
||||
const value = fs.readFileSync('/sys/class/gpio/gpio23/value').toString();
|
||||
|
||||
expect(value).toEqual('0');
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
mock.restore();
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue