Skip to content

Instantly share code, notes, and snippets.

@creationix
Last active January 12, 2019 01:29
Show Gist options
  • Save creationix/5938578 to your computer and use it in GitHub Desktop.
Save creationix/5938578 to your computer and use it in GitHub Desktop.
A sample program showing how to control a tiny arduino using a gamepad, linux, node.js, and firmata. https://www.youtube.com/watch?v=XjCsDosGghA

Instructions

  • npm install johnny-five
  • Plug in Logitech Gamepad to linux laptop
  • plug in tinyduino with lightring shield.
  • run sudo node move.js
  • move the gamepad to see lights move.
var FS = require('fs');
var EventEmitter = require('events').EventEmitter;
function parse(buffer) {
var event = {
time: buffer.readUInt32LE(0),
value: buffer.readInt16LE(4),
number: buffer[7],
}
var type = buffer[6];
if (type & 0x80) event.init = true;
if (type & 0x01) event.type = "button";
if (type & 0x02) event.type = "axis";
return event;
}
// Expose as a nice JavaScript API
function Joystick(id) {
this.wrap("onOpen");
this.wrap("onRead");
this.id = id;
this.buffer = new Buffer(8);
FS.open("/dev/input/js" + id, "r", this.onOpen);
}
Joystick.prototype = Object.create(EventEmitter.prototype, {
constructor: {value: Joystick}
});
// Register a bound version of a method and route errors
Joystick.prototype.wrap = function (name) {
var self = this;
var fn = this[name];
this[name] = function (err) {
if (err) return self.emit("error", err);
return fn.apply(self, Array.prototype.slice.call(arguments, 1));
};
};
Joystick.prototype.onOpen = function (fd) {
this.fd = fd;
this.startRead();
};
Joystick.prototype.startRead = function () {
FS.read(this.fd, this.buffer, 0, 8, null, this.onRead);
};
Joystick.prototype.onRead = function (bytesRead) {
var event = parse(this.buffer);
event.id = this.id;
this.emit(event.type, event);
if (this.fd) this.startRead();
};
Joystick.prototype.close = function (callback) {
FS.close(this.fd, callback);
this.fd = undefined;
};
module.exports = Joystick;
////////////////////////////////////////////////////////////////////////////////
// Sample usage
//var js = new Joystick(0);
//js.on('button', console.log);
//js.on('axis', console.log);
// Close after 5 seconds
//setTimeout(function () {
// js.close();
//}, 5000);
var five = require('johnny-five');
var Joystick = require('./joystick.js');
var js = new Joystick(0);
var board = new five.Board();
var firmata;
var HIGH, LOW, OUTPUT, INPUT, digitalWrite, pinMode;
board.on("ready", function () {
firmata = board.firmata;
HIGH = firmata.HIGH;
LOW = firmata.LOW;
OUTPUT = firmata.MODES.OUTPUT;
INPUT = firmata.MODES.INPUT;
digitalWrite = firmata.digitalWrite.bind(firmata);
pinMode = firmata.pinMode.bind(firmata);
console.log("move axis 0 on joystick");
var old;
js.on('axis', function (evt) {
if (!(evt.type === "axis" && evt.number === 0)) return;
var value = ((evt.value + 0x8000) >> 12) + 1;
if (value !== old) {
old = value;
setPosition(value);
}
});
});
function setPosition(iLedNum) {
console.log(iLedNum);
if( iLedNum == 1 )
{
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
digitalWrite( 5, HIGH );
digitalWrite( 6, LOW );
}
else if( iLedNum == 2 )
{
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
digitalWrite( 5, LOW );
digitalWrite( 6, HIGH );
}
else if( iLedNum == 3 )
{
pinMode(5, OUTPUT);
pinMode(6, INPUT);
pinMode(7, OUTPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
digitalWrite( 5, HIGH );
digitalWrite( 7, LOW );
}
else if( iLedNum == 4 )
{
pinMode(5, OUTPUT);
pinMode(6, INPUT);
pinMode(7, OUTPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
digitalWrite( 7, HIGH );
digitalWrite( 5, LOW );
}
else if( iLedNum == 5 )
{
pinMode(5, INPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
digitalWrite( 6, HIGH );
digitalWrite( 7, LOW );
}
else if( iLedNum == 6 )
{
pinMode(5, INPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
digitalWrite( 7, HIGH );
digitalWrite( 6, LOW );
}
else if( iLedNum == 7 )
{
pinMode(5, INPUT);
pinMode(6, OUTPUT);
pinMode(7, INPUT);
pinMode(8, OUTPUT);
pinMode(9, INPUT);
digitalWrite( 6, HIGH );
digitalWrite( 8, LOW );
}
else if( iLedNum == 8 )
{
pinMode(5, INPUT);
pinMode(6, OUTPUT);
pinMode(7, INPUT);
pinMode(8, OUTPUT);
pinMode(9, INPUT);
digitalWrite( 8, HIGH );
digitalWrite( 6, LOW );
}
else if( iLedNum == 9 )
{
pinMode(5, OUTPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, OUTPUT);
pinMode(9, INPUT);
digitalWrite( 5, HIGH );
digitalWrite( 8, LOW );
}
else if( iLedNum == 10 )
{
pinMode(5, OUTPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, OUTPUT);
pinMode(9, INPUT);
digitalWrite( 8, HIGH );
digitalWrite( 5, LOW );
}
else if( iLedNum == 11 )
{
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, INPUT);
digitalWrite( 8, HIGH );
digitalWrite( 7, LOW );
}
else if( iLedNum == 12 )
{
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, INPUT);
digitalWrite( 7, HIGH );
digitalWrite( 8, LOW );
}
else if( iLedNum == 13 )
{
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, OUTPUT);
pinMode(8, INPUT);
pinMode(9, OUTPUT);
digitalWrite( 9, HIGH );
digitalWrite( 7, LOW );
}
else if( iLedNum == 14 )
{
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, OUTPUT);
pinMode(8, INPUT);
pinMode(9, OUTPUT);
digitalWrite( 7, HIGH );
digitalWrite( 9, LOW );
}
else if( iLedNum == 15 )
{
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite( 9, HIGH );
digitalWrite( 8, LOW );
}
else if( iLedNum == 16 )
{
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite( 8, HIGH );
digitalWrite( 9, LOW );
}
}
var five = require('johnny-five');
var board = new five.Board();
board.on("ready", function () {
var leds = new Array(5);
for (var i = 0; i < 5; i++) {
leds[i] = new five.Led({pin: i + 5});
}
setInterval(function () {
for (var i = 0; i < 5; i++) {
var on = Math.random() > 0.5;
leds[i][on ? "on" : "off"]();
}
}, 33);
});
@creationix
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment