Skip to content

Instantly share code, notes, and snippets.

@wayspurrchen
Created October 15, 2014 03:02
Show Gist options
  • Save wayspurrchen/0ce87ee10608a8eaee2b to your computer and use it in GitHub Desktop.
Save wayspurrchen/0ce87ee10608a8eaee2b to your computer and use it in GitHub Desktop.
Johnny-Five Arduino UNO Pixel Iterator RGB LED Code
var five = require('johnny-five');
var keypress = require('keypress');
var gm = require('gm');
var board = new five.Board();
var fs = require('fs');
var PNG = require('pngjs').PNG;
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
board.on('ready', function() {
// for my information
(new five.Led(8)).strobe();
var led = new five.Led.RGB({
pins: {
red: 11,
green: 10,
blue: 9
}
});
fs.createReadStream('crazy.png')
.pipe(new PNG({
filterType: 4
}))
.on('parsed', function() {
var me = this;
var i = 0;
var j = 0;
for (var y = 0; y < this.height; y++) {
for (var x = 0; x < this.width; x++) {
if (i % 100 == 0) {
(function(j, x, y) {
setTimeout(function() {
var idx = (me.width * y + x) << 2;
// invert color
var r = me.data[idx];
var g = me.data[idx+1];
var b = me.data[idx+2];
// console.log(r, g, b);
var hex = rgbToHex(r, g, b);
console.log(j, ':', hex);
led.color(hex);
}, 70 * j);
})(j, x, y);
j++
}
i++;
}
}
console.log('loop done');
});
// Expose to REPL
this.repl.inject({
led: led
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment