Skip to content

Commit

Permalink
Merge pull request #4 from bouiboui/feature/noAsync
Browse files Browse the repository at this point in the history
Feature/no async
  • Loading branch information
bouiboui committed Dec 5, 2015
2 parents 73f841a + 635e260 commit 2bda062
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 240 deletions.
115 changes: 52 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,108 +3,97 @@
![](http://i.imgur.com/ef7aNZi.png)

## Introduction
This library makes it easy to control your UDOO Neo from node.
**TL;DR**
With this library, you can do ``require("udooneo").GPIO(37).out().val(1);``

``var udooneo = require("./udooneo")`` and you have access to the GPIOs and motion sensors.
``var neo = require("./udooneo")`` and you have access to all your UDOO Neo GPIOs and motion sensors from Node.js.

You can get started by running the test example: ``sudo node examples/test``

You can get started by plugging a LED in the GPIO port 37 and running the test example: ``sudo node examples/test``.
The LED will blink every second.

## GPIOs

### Get access to a GPIO
You can either get access to the GPIO from its Extended Pinout number (the white number on pink background in the illustration above) or from its real, internal number.

# Most common case
var gpio = new udooneo.GPIO().fromPin(pinNum);
var gpio = new neo.GPIO(pinNum);

# For geeks only
var gpio = new udooneo.GPIO(gpioNum);
var gpio = new neo.GPIO().fromGpio(gpioNum);

### Read or change direction
Then you have to set the direction, either ``udooneo.DIRECTION.INPUT`` or ``udooneo.DIRECTION.OUTPUT``. Like most methods, ``setDirection(direction, callback)`` is asynchronous and takes a lambda function as a second parameter.

gpio.setDirection(udooneo.DIRECTION.OUTPUT, function() {
console.log("The direction is set!");
});
To set the direction, you can use the shorthand methods ``gpio.in()`` and ``gpio.out()``.

You can also get the current direction with ``getDirection()``

switch (gpio.getDirection()) {
case neo.DIRECTION.INPUT:
console.log("Direction = input!");
break;
case neo.DIRECTION.OUTPUT:
console.log("Direction = output!");
break;
}

You can also get the current direction with ``getDirection(callback)``

gpio.getDirection(function(direction) {
switch (direction) {
case udooneo.DIRECTION.INPUT:
console.log("Direction = input!");
break;
case udooneo.DIRECTION.OUTPUT:
console.log("Direction = output!");
break;
}
});

### Read or change value
You can now either use ``gpio.getValue(callback)`` or ``gpio.setValue(value, callback)``

The ``callback`` function will be executed when the operation is done. In the case of ``getValue``, the first parameter of the callback function is fed the value of the GPIO.

gpio.getValue(function(gpioValue) {
switch (gpioValue) {
case udooneo.VALUE.HIGH:
console.log("Value = High!");
break;
case udooneo.VALUE.LOW:
console.log("Value = Low!");
break;
default:
console.log("Value = " + gpioValue + "!");
break;
}
});
To read a value, use ``gpio.val()``. To set the value, use ``gpio.val(value)``.

# Read value
switch (gpio.val()) {
case neo.VALUE.HIGH:
console.log("Value = High!");
break;
case neo.VALUE.LOW:
console.log("Value = Low!");
break;
default:
console.log("Value = " + gpio.val() + "!");
break;
}

gpio.setValue(udooneo.VALUE.HIGH, function() {
console.log("The value is now high!");
});
# Change value
gpio.val(neo.VALUE.HIGH);

### Watch value changes
Another nice method to know and use is ``gpio.watchValue(callback)``. It works like an event listener, ``callback`` will be executed everytime the value of the GPIO changes, in real time.
You can make your script react to value changes by using ``gpio.watch(callback)``. It works like an event listener, ``callback`` will be executed everytime the value of the GPIO changes, in real time.

gpio.watchValue(function() {
gpio.getValue(function(gpioValue) {
console.log("The value has changed! The new value is " + gpioValue);
});
gpio.watch(function() {
console.log("The value has changed! The new value is " + gpio.val());
});

###Realease the GPIO access when you're done
Though the access to the GPIO is abstracted by the library, it has to be undone manually by calling ``gpio.unexport(callback)`` when you're done with the GPIO. I don't know the implications of failing to do this, so I wouldn't risk it. Consider it a good practice. The callback parameter/function is not mandatory.
Though the access to the GPIO is abstracted by the library, it has to be undone manually by calling ``gpio.unexport()`` when you're done with the GPIO. I don't know the implications of failing to do this, so I wouldn't risk it. Consider it a good practice. The callback parameter/function is not mandatory.

## Motion sensors

The 3 motion sensors (depending on your Neo version) are accessible via ``udooneo.Accelerometer``, ``udooneo.Magnetometer`` and ``udooneo.Gyroscope``.
The 3 motion sensors (depending on your Neo version) are accessible via ``neo.sensors`` (``.Accelerometer``, ``.Magnetometer`` and ``.Gyroscope``). To make it easier, you can access them via ``var sensors = require("udooneo").sensors``.

### Enable or disable

This couldn't be more straightforward, just ``.enable(callback)`` or ``.disable(calback)`` the sensor. As usual, these methods are asynchronous and take an optional callback function as a parameter.
This couldn't be more straightforward, just ``.enable()`` or ``.disable()`` the sensor.

udooneo.Accelerometer.enable();
udooneo.Magnetometer.enable();
udooneo.Gyroscope.enable(function() {
console.log("The Gyroscope is enabled!");
udooneo.Gyroscope.disable(function() {
console.log("The Gyroscope is disabled!");
});
});
sensors.Accelerometer.enable();
sensors.Magnetometer.enable();
sensors.Gyroscope.enable();
console.log("All the sensors are enabled");

sensors.Gyroscope.disable();
console.log("Gyroscope is disabled");

![](https://38.media.tumblr.com/tumblr_lkx3onuocM1qzvwy3.gif)

*I hope someone gets that joke.*

### Get data

To get the current data for a motion sensor, use its ``.getData(callback)`` method.
To get the current data for a motion sensor, use its ``.data()`` method.

udooneo.Magnetometer.getData(function(data) {
console.log("Magnometer data: " + data);
});
console.log("Magnometer data: " + sensors.Magnetometer.data();

Due to the way the value is returned, there's no ``watchData(callback)`` method, you have to use a ``setInterval`` or ``setTimeout`` to know if the value of a motion sensor has changed (hint: it's changing constantly).
Due to the way the value is returned, there's no ``.watch(callback)`` method, you have to use a ``setInterval`` or ``setTimeout`` to know if the value of a motion sensor has changed (hint: it's changing constantly).

The data is returned in the form of a string, someting like: ``data = "1.0,1.0,1.0"``.
I decided to return this bluntly as a string and not to convert it to a special kind of object for the moment for performance purposes. It's up to you.
Expand Down
62 changes: 24 additions & 38 deletions examples/motion.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,29 @@
var udooneo = require("../udooneo");
var sensors = require("../udooneo").sensors;

udooneo.Accelerometer.enable();
udooneo.Magnetometer.enable();
udooneo.Gyroscope.enable();
var acc = sensors.Accelerometer.enable();
var mag = sensors.Magnetometer.enable();
var gyr = sensors.Gyroscope.enable();

var values = {
Accelerometer: "",
Magnetometer: "",
Gyroscope: ""
};
var int = setInterval(function () {

// Write values
process.stdout.write('\033c');
console.log([

"Accelerometer: " + acc.data(),
"Magnetometer: " + mag.data(),
"Gyroscope: " + gyr.data()

].join("\r\n"));

}, 100);

// Disable to preserve battery
setTimeout(function () {
clearInterval(int);

acc.disable();
mag.disable();
gyr.disable();

var int = setInterval(function () {

udooneo.Accelerometer.getData(function (data) {
values.Accelerometer = data;
});
udooneo.Magnetometer.getData(function (data) {
values.Magnetometer = data;
});
udooneo.Gyroscope.getData(function (data) {
values.Gyroscope = data;
});

process.stdout.write('\033c');
console.log([
"Accelerometer: " + values.Accelerometer,
"Magnetometer: " + values.Magnetometer,
"Gyroscope: " + values.Gyroscope,
].join("\r\n"));

}, 100);

setTimeout(function () {
clearInterval(int);
udooneo.Accelerometer.disable();
udooneo.Magnetometer.disable();
udooneo.Gyroscope.disable();
}, 10000);

}, 500);
}, 10000);
24 changes: 5 additions & 19 deletions examples/test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
var udooneo = require("../udooneo");
var neo = require("../udooneo");

udooneo.gpioNumbers.forEach(function (gpioNum) {
var gpio = new udooneo.GPIO(gpioNum);
gpio.getValue(function (value) {
console.log("GPIO " + gpioNum + " current value: " + value);
});
gpio.watchValue(function () {
gpio.getValue(function (value) {
console.log("GPIO " + gpioNum + " change - new value: " + value);
});
});
});
var gpio = new neo.GPIO(37).out();

var x = 0;
var targetGpio = new udooneo.GPIO().fromPin(34);
targetGpio.setDirection(udooneo.DIRECTION.OUTPUT, function () {
var int = setInterval(function () {
if (x > 4) clearInterval(int);
targetGpio.setValue(x++ % 2 ? udooneo.VALUE.LOW : udooneo.VALUE.HIGH);
}, 2000);
});
setInterval(function () {
gpio.val(x++ % 2 ? neo.VALUE.LOW : neo.VALUE.HIGH);
}, 1000);
10 changes: 4 additions & 6 deletions examples/unexport-all.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
var udooneo = require("../udooneo");
var neo = require("../udooneo");

udooneo.gpioNumbers.forEach(function (gpioNum) {
var gpio = new udooneo.GPIO(gpioNum);
gpio.unexport(function () {
console.log("GPIO " + gpioNum + " unexported.");
});
neo.gpios.each(function (gpio) {
gpio.unexport();
console.log(gpio.num() + " unexported.");
});
Loading

0 comments on commit 2bda062

Please sign in to comment.