Last active
February 28, 2016 01:28
-
-
Save sofroniewn/12a39565833170c660cb to your computer and use it in GitHub Desktop.
Using https://github.com/nebrius/raspi-gpio on a Raspberry Pi to control digital pin states at 2 kHz from node. In this example a switch was placed on pin 2 and an led was hooked up to pin 0. When the switch (yellow trace) was flipped, the led turned on (blue trace) within 500 us
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| console.log('Hello Pi') | |
| var raspi = require('raspi') | |
| var gpio = require('raspi-gpio') | |
| var NanoTimer = require('nanotimer') | |
| var fs = require('fs') | |
| var now = require('performance-now') | |
| var path = require('path') | |
| var timer = new NanoTimer() | |
| var interval = process.argv[2] | |
| var repeats = process.argv[3] | |
| var filename = path.basename(process.argv[1], '.js') | |
| var ws = fs.createWriteStream('./logs/' + filename + '-' + interval + '-' + repeats +'.log' ) | |
| var iteration = 0 | |
| raspi.init(function() { | |
| obj = { | |
| 'board': true, | |
| 'date': new Date(), | |
| 'time': now().toFixed(3) | |
| } | |
| console.log(JSON.stringify(obj)) | |
| // ws.write(JSON.stringify(obj)) | |
| var input = new gpio.DigitalInput(2) | |
| var output = new gpio.DigitalOutput(0) | |
| var curTime = 0 | |
| var prevTime = 0 | |
| var value = 0 | |
| timer.setInterval(function() { | |
| curTime = now() | |
| for (i=0; i<repeats; i++) { | |
| value = input.read() | |
| } | |
| for (i=0; i<repeats; i++) { | |
| output.write(value) | |
| } | |
| obj = { | |
| 'sw': value, | |
| 'it': iteration, | |
| 'ts': curTime.toFixed(3), | |
| 'df': (curTime - prevTime).toFixed(3) | |
| } | |
| prevTime = curTime | |
| iteration++ | |
| // console.log(JSON.stringify(obj)) | |
| ws.write(JSON.stringify(obj) + '\n') | |
| }, '', interval) | |
| }) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$sudo node switch-multi.js '500u' 20
20 pin read, 20 pin writes, and a block of JSON at 2 kHz controlling the switching on and off of an LED