From f2d396168614492c19228cb75e05aedcdc98e853 Mon Sep 17 00:00:00 2001 From: Frieder Schlesier Date: Wed, 9 Nov 2022 20:28:42 +0100 Subject: [PATCH] initial commit --- .gitignore | 1 + README.md | 30 +++++++++++++++++++++++++++ app.js | 17 ++++++++++++++++ gpio.js | 27 ++++++++++++++++++++++++ package.json | 22 ++++++++++++++++++++ test/gpio.test.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 149 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 app.js create mode 100644 gpio.js create mode 100644 package.json create mode 100644 test/gpio.test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..843f7a9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/README.md b/README.md new file mode 100644 index 0000000..802d397 --- /dev/null +++ b/README.md @@ -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 diff --git a/app.js b/app.js new file mode 100644 index 0000000..19a13c8 --- /dev/null +++ b/app.js @@ -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); +}); diff --git a/gpio.js b/gpio.js new file mode 100644 index 0000000..a5ffbc7 --- /dev/null +++ b/gpio.js @@ -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 +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..6b536b7 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/test/gpio.test.js b/test/gpio.test.js new file mode 100644 index 0000000..d226c0e --- /dev/null +++ b/test/gpio.test.js @@ -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(); + }); +});