GithubHelp home page GithubHelp logo

udooneo's Introduction

udooneo

Introduction

TL;DR With this library, you can do require("udooneo").GPIO(37).out().val(1);

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 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 neo.GPIO(pinNum);

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

Read or change direction

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;
}

Read or change value

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;
}

// Change value
gpio.val(neo.VALUE.HIGH);
    

Watch value changes

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.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() 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 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() or .disable() the sensor.

sensors.Accelerometer.enable();
sensors.Magnetometer.enable();
sensors.Gyroscope.enable();
console.log('All the sensors are enabled');

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

I hope someone gets that joke.

Get data

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

console.log('Magnometer data: ' + sensors.Magnetometer.data());

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.

License

MIT

udooneo's People

Contributors

bouiboui avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.