If you have a new amazing Acme Systems Aria board or a FoxBoad G20, you can easy manage GPIO using Node.js and my last aria_fox_gpio node module and also the daisy_gpio to manage Daisy board for fast prototyping. The aria_fox_gpio package is a full asynchronous module that handle all GPIO using hardware interrupt to notify all GPIO changes without waste your cpu. To debounce your input you can define a debounce time to avoid incorrect reports of change of state.
Let’s see how.
First you must install Node.js on your board following this instructions (tested with v0.8.14). Now you can install daisy_gpio as usual:
debarm:~# npm install daisy_gpio
Done! Now you can write a simple program to turn on a led when push a button using two prototype boards: Daisy5 and Daisy11. Assuming you connect Daisy5 to D5 connector and Daisy11 to D2 connector of your FoxBoard, your first program seems like this:
var daisy = require('daisy')({ model: 'fox', // set to aria if you have this board test: false, // set true if you want use a fake gpio path. // Note that the change, falling and rising events can't fires debug: false // set true if you want see debug messages into terminal window }); // create a new Daisy5 instance (8 buttons array) var daisy5 = new daisy.Daisy5(); // attach the init event (fired when ALL buttons are ready) daisy5.attach('init', function(event) { console.log('init daisy5'); }); // create a new Daisy11 instance (8 leds array) var daisy11 = new daisy.Daisy11(); // attach the init event (fired when ALL leds are ready) daisy11.attach('init', function(event) { console.log('init daisy11'); }); // attach the rising event fired for EVERY button when pressed daisy5.attach('rising', function(event) { daisy11.instances[event.data.sender.index].setHigh(); }); // attach the falling event fired for EVERY button when released daisy5.attach('falling', function(event) { daisy11.instances[event.data.sender.index].setLow(); }); // attach the rising event fired for EVERY led is turned on daisy11.attach('rising', function(event) { console.log('turned on led #' + event.data.sender.index); }); // attach the falling event fired for EVERY led is turned off daisy11.attach('falling', function(event) { console.log('turned off led #' + event.data.sender.index); });
Save your file as presstolight.js and run in your terminal:
debarm:~# node presstolight.js
Now if you push a button, the corresponding led is turned on. Easy, right? Note that the init and free events are fired only when ALL leds (buttons) of Daisy11 (Daisy5) are ready to use and available respectively, whereas the rising and falling events are fired by every led (button). Note also that all events have a data property that contains two properties:
- err: the error if occurred
- sender: the Gpio instance that has fired the event
The event.data for a change event has also a value property with the last value.
In general if you want manage your GPIO as input or output without a daisy prototype board, you can use the generic aria_fox_gpio module. Install it:
debarm:~# npm install aria_fox_gpio
and write a simple program:
// define the OutGpio class from the aria_fox_gpio module var Led = require('aria_fox_gpio')({ model: 'fox', debug: true }).OutGpio; // create a new Led instance var led = new Led('D2', 2, function() { // use callback to handle the init console.log('init callback button #1'); var isOn = false; setInterval(function(){ isOn = !isOn; if (isOn) { led.setHigh(); } else { led.setLow(); } }, 500); }); // attach the init event fired (after the callback) when the led is ready led.attach('init', function(event) { console.log('init event button #1'); }); // attach the rising event fired when the led is turned on led.attach('rising', function(event) { console.log('led is turned on'); }); // attach the rising event fired when the led is turned off led.attach('falling', function(event) { console.log('led is turned off'); });
Save your file as blinking.js and run in your terminal:
debarm:~# node blinking.js
Your led is blinking.
See full documentation online or into doc folder and some examples into test folder within the aria_fox_gpio package and daisy_gpio package.
UPDATE: Now the module works both with Linux kernel 2.6.39 and 3.11.6.
Arm , Node.js
About Marcello Gesmundo
I'm the founder of Yoovant company. I'm an engineer and an artist: what could be better than combine the technologies with the arts?