Last active
December 20, 2015 20:18
-
-
Save Fabryz/6189177 to your computer and use it in GitHub Desktop.
Control the Pi Lite with Node.js $ npm install serialport
$ node server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Description: Control the Pi Lite via Node.js | |
* Author: Fabrizio Codello (fabryz.com) | |
* | |
* TODO: Check if writing out of bounds | |
*/ | |
var SerialPort = require("serialport").SerialPort; | |
// http://openmicros.org/index.php/articles/94-ciseco-product-documentation/raspberry-pi/280#Using the Pi-Lite pre-loaded software | |
var PiLite = { | |
device: "/dev/ttyAMA0", | |
baudrate: 9600, | |
client: null, | |
init: function() { | |
this.client = new SerialPort(this.device, { | |
baudrate: this.baudrate | |
}, false); | |
}, | |
connect: function(callback) { | |
this.init(); | |
this.client.open(function() { | |
console.log('Connected to Pi Lite'); | |
callback(); | |
}); | |
}, | |
write: function(data) { | |
// console.log('Writing: '+ data); | |
this.client.write(data, function(err, results) { | |
if (err) { | |
console.log('Error: '+ err); | |
} | |
// console.log('Result: '+ results); | |
}); | |
}, | |
// Set scrolling delay in ms 1 is scrolling very fast, 1000 is scrolling very slow. Default speed is 80. | |
setSpeed: function(value) { | |
this.write("$$$SPEED"+ value +"\r"); | |
}, | |
// (value) is a 126 character string in one's and zeros to represent each LED state (on or off) | |
frameBuffer: function(value) { | |
this.write("$$$F"+ value +"\r"); | |
}, | |
// Column X to Y% | |
barGraph: function(column, value) { | |
this.write("$$$B"+ column +","+ value +"\r"); | |
}, | |
// Create graph from an array (max 14 columns) | |
chart: function(data) { | |
var that = this; | |
data.forEach(function(graphValue, index) { | |
that.barGraph(index + 1, graphValue); | |
}); | |
}, | |
// Row X (1 | 2) to Y% | |
vuMeter: function(row, value) { | |
this.write("$$$V"+ row +","+ value +"\r"); | |
}, | |
// Pixel set a pixel state | |
// column 1 to 14 | |
// row 1 to 9 | |
// action ON,OFF,TOGGLE | |
pixel: function(column, row, action) { | |
this.write("$$$P"+ column +","+ row +","+ action +"\r"); | |
}, | |
// Text display at location x,y a text character | |
randomPixel: function(time) { | |
var that = this; | |
var timer = setInterval(function() { | |
column = Math.floor(Math.random() * 15), | |
row = Math.floor(Math.random() * 10); | |
that.pixel(column, row, "TOGGLE"); | |
}, time); | |
}, | |
// set all the pixels on or off | |
all: function(value) { | |
this.write("$$$ALL,"+ value +"\r"); | |
}, | |
// alias to ALL,OFF | |
clear: function() { | |
this.all("OFF"); | |
}, | |
// Scroll value columns left (+) or right (-) | |
scroll: function(value) { | |
this.write("$$$SCROLL"+ value +"\r"); | |
}, | |
// Text display at location x,y a text character | |
text: function(column, row, character) { | |
this.write("$$$T"+ column +","+ row +","+ character +"\r"); | |
}, | |
// Display each character of the text after time ms, column and row are optional | |
timed: function(text, time, column, row) { | |
var that = this; | |
column = column | 5, | |
row = row | 2; | |
var timer = setInterval(function() { | |
character = text[0]; | |
text = text.substring(1); | |
// Skip a turn on spaces | |
if (character !== " ") { | |
that.clear(); | |
that.text(column, row, character); | |
} | |
// TODO: Manually center short letters like I/J? | |
//if (character == "I" || character == "I") { | |
// that.clear(); | |
// that.text(column + 1, row, character); | |
//} | |
if (text.length <= 0) { | |
// that.clear(); | |
clearInterval(timer); | |
} | |
}, time); | |
} | |
}; | |
exports.PiLite = PiLite; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pilite = require('./node-pilite.js').PiLite; | |
pilite.connect(function() { | |
pilite.clear(); | |
// pilite.setSpeed(500); | |
pilite.randomPixel(50); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I'm keen to make this an npm module. Would you be happy if I did that linking to this gist to give you credit?