udoo
v0.2.0
Published
UDOO GPIO abstraction library for Node.js & command line tool. (callback, promise, and synchronous styles supported)
Downloads
19
Maintainers
Readme
node-udoo
is a UDOO GPIO abstraction library for Node.js & command line tool. All shared GPIO pinouts described in the UDOO Pinout Diagram are supported.
This library provides user an option to develop UDOO apps on Node.js using asynchronous (both callback & promise styles) and synchronous patterns. However, it is strongly recommended to stick to Node.js' asynchronous (non-blocking I/O) style.
Demo Video
Installation
$ npm install udoo
GPIOs Warnings
Please pay attention to the GPIOs Warnings
section of the UDOO Starting Manual:
When changing i.MX6 GPIOs directions, it is necessary to pay special attention. New direction must be compatible with SAM3x8E pinout configuration and/or with the load of the physical pin.
If you are not interacting with SAM3X (or you don't really understand what this is all about), just reset all shared pinouts to INPUT
mode on SAM3X
(Arduino Due
microcontroller) using this sketch file so you can move the potential direction conflict issue out of the way.
Usage
Blink example from Arduino Tutorials implemented in different styles:
Asynchronous Version w/ Callbacks
var udoo = require('udoo');
var led = udoo.outputPin(13),
on = false;
(function loop() {
led.set(on = !on, function () {
setTimeout(loop, 1000);
});
}());
Asynchronous Version w/ Promise (Q)
var udoo = require('udoo');
var led = udoo.outputPin(13),
on = false;
(function loop() {
led
.set(on = !on)
.done(function () {
setTimeout(loop, 1000);
});
}());
Asynchronous Version w/ Async.js
var udoo = require('udoo');
var led = udoo.outputPin(13),
on = false;
udoo.async.forever(function (cb) {
udoo.async.series([
function (cb) {
led.set(on = !on, cb);
},
function (cb) {
setTimeout(cb, 1000);
}
], cb);
});
Synchronous Version (not recommended)
var udoo = require('udoo');
var led = udoo.outputPin(13),
on = false;
(function loop() {
led.setSync(on = !on);
setTimeout(loop, 1000);
}());
API
node-udoo
API provides all these patterns: asynchronous style using callbacks, asynchronous style using promise, and synchronous style.You will be tempted to use synchronous style because the code looks simpler and easier, however it is not the recommended way to program in Node.js.
All asynchronous API functions accept callback as the last parameter. Whether you pass a callback function or not, they will always return a promise object built using Q. You can do whatever you want with the returned promise, or stick with the traditional callback style.
Pin name can be any of key values in PIN_MAPPING.
// List all supported pinouts
.gpioNumbers()
.pinNames()
// Create new pin
.inputPin(pinName)
.outputPin(pinName)
// Pin getter/setter (append `Sync` for synchronous calls)
.get(callback) // Returns `true` for high/1, `false` for low/0
.setHigh(callback) // Sets true/high/1
.setLow(callback) // Sets false/low/0
.set(value, callback) // Sets (boolean) value
// Pin mode (append `Sync` for synchronous calls)
.getMode(callback) // Returns one of `udoo.PIN_MODE.*`
.setInputMode(callback) // Change to input mode
.setOutputMode(callback) // Change to output mode
// Reset (calling on `udoo` resets all pinouts to `INPUT` mode)
.reset(callback)
.resetSync()
// Constants
.PIN_MODE
.PIN_MODE_INVERT
// Libraries exported
._ // Lodash (underscore.js)
.Q // Q (promise)
.async // Async.js
Command Line Tool
node-udoo
comes with a convenient command line tool (udoo
) for quick and easy control of UDOO GPIO pins. The command line tool can be installed using the following command:
$ sudo npm install udoo -g
Available Commands
$ udoo help
$ udoo high [<pinName> <pinName> ...] # aliases: `udoo on`, `udoo 1`
$ udoo low [<pinName> <pinName> ...] # aliases: `udoo off`, `udoo 0`
$ udoo reset [<pinName> <pinName> ...]
$ udoo blink [<pinName> <pinName> ...]
TODOs
Here is a list of various ports need to be supported. Please contribute to the node-udoo
project by sending Pull Requests.
- ~~GPIO (all 51 shared GPIO pinouts described in the UDOO Pinout Diagram)~~
- PWM
- SPI
- UARTs
- I2C
- Can Bus
- USB-OTG
- DAC
- JTAG
- DMA
Credits
See the contributors.