-
Notifications
You must be signed in to change notification settings - Fork 0
/
spi_led.js
65 lines (60 loc) · 1.38 KB
/
spi_led.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const spi = require('spi-device');
const LED_NUM = 1;
const args = process.argv;
if (args.length == 5){
rgb = [args[2], args[3], args[4]]
}else{
rgb = [0, 0, 0]
}
const spiPort = spi.open(0, 0, err => {
// An SPI message is an array of one or more read+write transfers
const message = [{
//sendBuffer: Buffer.from(messageParser(rgb)), // use this if more leds are used
sendBuffer: Buffer.from(colorParser(rgb)), // use this to speedup if only one led is used
byteLength: 24 * LED_NUM,
speedHz: 8000000,
microSecondsDelay: 0
}];
spiPort.transfer(message, (err, message) => {
if (err) throw err;
});
});
function messageParser(rgb) {
message = []
basemessage=colorParser(rgb);
i=1;
while (i<=LED_NUM){
message = message.concat(basemessage);
i++;
}
return message;
};
function colorParser(rgb){
r = rgb[0];
g = rgb[1];
b = rgb[2];
//Create byte array with color data
color_data = []
for(let i=0; i<8; i++){
if ( (g >> (7-i)) & 0b1 ){
color_data.push(0b1110);
}else{
color_data.push(0b1000);
}
}
for(let i=0; i<8; i++){
if ( (r >> (7-i)) & 0b1 ){
color_data.push(0b1110);
}else{
color_data.push(0b1000);
}
}
for(let i=0; i<8; i++){
if ( (b >> (7-i)) & 0b1 ){
color_data.push(0b1110);
}else{
color_data.push(0b1000);
}
}
return color_data;
};