Created
June 25, 2013 20:49
-
-
Save crumpled/5862233 to your computer and use it in GitHub Desktop.
Use Node to power TCL light strands through Arduino over the serial port.
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 SerialPort = require("serialport2").SerialPort; | |
var _ = require("underscore"); | |
var port = new SerialPort(); | |
var t = true; | |
var f = false; | |
var stringInput = 'OX'; | |
var colors = { | |
red: [0xFF, 0x00, 0x00], | |
orange:[0xFF, 0x2B, 0x00], | |
yellow: [0xFF, 0xFF, 0x00], | |
green: [0x00, 0xFF, 0x00], | |
teal: [0x00, 0xFF, 0xFF], | |
blue: [0x00, 0x00, 0xFF], | |
violet: [0xFF, 0x00, 0xFF], | |
white: [0xFF, 0xFF, 0xFF], | |
black: [0x00, 0x00, 0x00], | |
}; | |
var hLetters = { | |
A: [ | |
[f,t,t,t,t], | |
[t,f,t,f,f], | |
[t,f,t,f,f], | |
[f,t,t,t,t] | |
], | |
B: [ | |
[t,t,t,t,t], | |
[t,f,t,f,t], | |
[t,f,t,f,t], | |
[f,t,f,t,f] | |
], | |
C: [ | |
[f,t,t,t,f], | |
[t,f,f,f,t], | |
[t,f,f,f,t], | |
[f,t,f,t,f] | |
], | |
D: [ | |
[t,t,t,t,t], | |
[t,f,f,f,t], | |
[t,f,f,f,t], | |
[f,t,t,t,f] | |
], | |
E: [ | |
[t,t,t,t,t], | |
[t,f,t,f,t], | |
[t,f,t,f,t], | |
[t,f,f,f,t] | |
], | |
F: [ | |
[t,t,t,t,t], | |
[f,f,t,f,t], | |
[f,f,t,f,t], | |
[f,f,f,f,t] | |
], | |
G: [ | |
[f,t,t,t,f], | |
[t,f,f,f,t], | |
[t,f,t,f,t], | |
[t,f,t,f,t], | |
[f,t,t,f,t] | |
], | |
H: [ | |
[t,t,t,t,t], | |
[f,f,t,f,f], | |
[f,f,t,f,f], | |
[t,t,t,t,t] | |
], | |
I: [ | |
[t,f,f,f,t], | |
[t,t,t,t,t], | |
[t,f,f,f,t], | |
], | |
J: [ | |
[f,t,f,f,f], | |
[t,f,f,f,f], | |
[t,f,f,f,f], | |
[f,t,t,t,t] | |
], | |
K: [ | |
[t,t,t,t,t], | |
[f,f,t,f,f], | |
[f,t,f,t,f], | |
[t,f,f,f,t] | |
], | |
L: [ | |
[t,t,t,t,t], | |
[t,f,f,f,f], | |
[t,f,f,f,f], | |
[t,f,f,f,f], | |
[t,f,f,f,f] | |
], | |
M: [ | |
[t,t,t,t,f], | |
[f,f,f,f,t], | |
[t,t,t,t,f], | |
[f,f,f,f,t], | |
[t,t,t,t,f] | |
], | |
N: [ | |
[t,t,t,t,t], | |
[f,f,f,t,f], | |
[f,f,t,f,f], | |
[f,t,f,f,f], | |
[t,t,t,t,t] | |
], | |
O: [ | |
[f,t,t,t,f], | |
[t,f,f,f,t], | |
[t,f,f,f,t], | |
[t,f,f,f,t], | |
[f,t,t,t,f] | |
], | |
P: [ | |
[t,t,t,t,f], | |
[f,f,t,f,t], | |
[f,f,t,f,t], | |
[f,f,f,t,f] | |
], | |
Q: [ | |
[f,t,t,t,f], | |
[t,f,f,f,t], | |
[t,f,f,f,t], | |
[f,t,f,f,t], | |
[t,f,t,t,f] | |
], | |
R: [ | |
[t,t,t,t,f], | |
[f,f,t,f,t], | |
[f,t,t,f,t], | |
[t,f,f,t,f] | |
], | |
S: [ | |
[t,f,f,t,f], | |
[t,f,t,f,t], | |
[t,f,t,f,t], | |
[f,t,f,f,t] | |
], | |
T: [ | |
[f,f,f,f,t], | |
[f,f,f,f,t], | |
[t,t,t,t,t], | |
[f,f,f,f,t], | |
[f,f,f,f,t], | |
], | |
U: [ | |
[f,t,t,t,t], | |
[t,f,f,f,f], | |
[t,f,f,f,f], | |
[f,t,t,t,t] | |
], | |
V: [ | |
[f,f,t,t,t], | |
[f,t,f,f,f], | |
[t,f,f,f,f], | |
[f,t,f,f,f], | |
[f,f,t,t,t] | |
], | |
W: [ | |
[f,t,t,t,t], | |
[t,f,f,f,f], | |
[f,t,t,t,t], | |
[t,f,f,f,f], | |
[f,t,t,t,t] | |
], | |
X: [ | |
[t,f,f,f,t], | |
[f,t,f,t,f], | |
[f,f,t,f,f], | |
[f,t,f,t,f], | |
[t,f,f,f,t], | |
], | |
Y: [ | |
[f,f,f,f,t], | |
[f,f,f,t,f], | |
[t,t,t,f,f], | |
[f,f,f,t,f], | |
[f,f,f,f,t] | |
], | |
Z: [ | |
[t,f,f,f,t], | |
[t,t,f,f,t], | |
[t,f,t,f,t], | |
[t,f,f,t,t], | |
[t,f,f,f,t] | |
], | |
' ': [ | |
[f,f,f,f,f], | |
[f,f,f,f,f], | |
[f,f,f,f,f], | |
] | |
}; | |
// Give a text string, or array of chars, | |
//[colors.color], | |
//[column characters will start from (index begins with 1)] | |
//returns an array of bytes: triplets = RGB pixel values | |
var mapHLetters = function(hLetterArray, color, columnStart){ | |
var fixedLetterArray = [], | |
pixelArray = [], | |
idx = 0; | |
color = color || colors.white; | |
columnStart = columnStart || 1; | |
_.each(hLetterArray, function(va, ke){ | |
var letterMap = hLetters[va]; | |
_.each(letterMap, function(val, key) { | |
var flipVal = _.clone(val); | |
flipVal.reverse(); | |
if (columnStart % 2 == 1){ | |
fixedLetterArray.push(val); | |
} | |
else { | |
fixedLetterArray.push(flipVal); | |
} | |
columnStart++; | |
} ); | |
//blank column kern | |
fixedLetterArray.push([false,false,false,false,false]); | |
columnStart++; | |
}); | |
fixedLetterArray = _.flatten(fixedLetterArray, false); | |
_.map(fixedLetterArray, function(va, ke){ | |
if (va == false){ | |
pixelArray.push(colors.black); | |
} | |
else { | |
pixelArray.push(color); | |
} | |
}); | |
return _.flatten(pixelArray, false); | |
}; | |
// Give a byte array of triplets, (like the return from mapHletters) | |
// [delay] | |
// [number of times to show] 0 for infinite, default = 1 | |
// writes bytes to serial port. | |
var scroll = function(byteArray, delay, repeat){ | |
var arrayOfArrays = [], | |
tinyArray = [], | |
countToThree = 0, | |
smallArray = [], | |
countToFive = 0, | |
repeat = repeat || 1, | |
delay = delay || 500, | |
columnCount; | |
console.log(JSON.stringify(byteArray)); | |
//split array into arrays of five triplets (3D array) | |
_.each(byteArray, function(v,k){ | |
myByte = byteArray.shift(); | |
countToFive = countToFive % 5; | |
countToThree = countToThree % 3; | |
if (countToThree == 0) { | |
tinyArray = [myByte]; | |
} | |
else { | |
tinyArray.push(myByte); | |
} | |
if (countToThree == 2){ | |
if (countToFive == 0){ | |
smallArray = [tinyArray]; | |
} | |
else { | |
smallArray.push(tinyArray); | |
} | |
countToFive++; | |
} | |
if (countToFive == 4){ | |
arrayOfArrays.push(smallArray); | |
} | |
countToThree++; | |
}); | |
columnCount = arrayOfArrays.length; | |
function writeAndMove(cols, array, dlay, rept){ | |
var pixels = _.flatten(arrayOfArrays, true).lenth, | |
columnCount = cols, | |
arrayOfArrays = array, | |
delay = dlay, | |
repeat = rept; | |
port.write("DATA " + pixels + "\n"); | |
port.write(_.flatten(arrayOfArrays, false)); | |
setTimeout(writeAndMove(columnCount, arrayOfArrays, delay, repeat), dlay); | |
} | |
writeAndMove(columnCount, arrayOfArrays, delay, repeat); | |
}; | |
//-------------------- | |
port.on('data', function(data) { | |
console.log(data.toString()); | |
}); | |
port.on('error', function(err) { | |
console.log(err); | |
}); | |
port.open('COM4', { | |
baudRate: 115200, | |
dataBits: 8, | |
parity: 'none', | |
stopBits: 1 | |
}, function(err) { | |
port.write("CHECK\n"); | |
scroll(mapHLetters(stringInput, [0x21, 0x22, 0x23])); | |
// port.write("DATA "+ writeMe.length / 3 +"\n"); | |
// port.write(writeMe); | |
port.write("\n"); | |
port.close(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment