/
index.js
97 lines (89 loc) · 3.47 KB
/
index.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/node
/*
* index.js - RPi LED Controller
*
* The server to control the LED Strip. Accepts commands.
*/
/*
* Initialize Variables
*/
//var fs = require('fs');
//var Schema = require('protobuf').Schema;
//var schema = new Schema(fs.readFileSync('protobuf/lifx.desc'));
//var Command = schema['lifx.Command'];
var net = require('net');
var http = require('http');
// Create server & socket
var server = http.createServer(function(req, res){
// Send HTML headers and message
res.writeHead(404, {'Content-Type': 'text/html'});
res.end('<h1>Aw, snap! 404</h1>');
});
server.listen(8533);
var colors = {
1: { index: 1, red: 0, green: 0, blue: 0, alpha: 0 },
2: { index: 2, red: 0, green: 0, blue: 0, alpha: 0 },
3: { index: 3, red: 0, green: 0, blue: 0, alpha: 0 },
4: { index: 4, red: 0, green: 0, blue: 0, alpha: 0 }
},
count = 0;
var io = require('socket.io').listen(server);
io.set('log level', 0);
// Load LED Controlling libraries here!
var LEDDRIVER = require("leddriver");
var driver = new LEDDRIVER(24, 12, "/dev/spidev0.0");
function decToHex(d) {
var hex = Number(d).toString(16);
padding = typeof (padding) === "undefined" || padding === null ? padding = 2 : padding;
while (hex.length < padding) {
hex = "0" + hex;
}
return hex;
}
function changeColor(r, g, b) {
console.log("#" + decToHex(r) + decToHex(g) + decToHex(b));
driver.setRGB("#" + decToHex(r) + decToHex(g) + decToHex(b), 0, 1, 2);
driver.send();
}
/*
* Socket (Back end)
* Process the commands from the socket connections
*/
io.sockets.on('connection', function(socket) {
// Initialize Client with current variables
socket.emit('color', { index: 1, red: colors[1].red, green: colors[1].green, blue: colors[1].blue, alpha: colors[1].alpha});
console.log('emit: ' + JSON.stringify({ index: 1, red: colors[1].red, green: colors[1].green, blue: colors[1].blue, alpha: colors[1].alpha}));
socket.emit('color', { index: 2, red: colors[2].red, green: colors[2].green, blue: colors[2].blue, alpha: colors[2].alpha});
console.log('emit: ' + JSON.stringify({ index: 2, red: colors[2].red, green: colors[2].green, blue: colors[2].blue, alpha: colors[2].alpha}));
socket.emit('color', { index: 3, red: colors[3].red, green: colors[3].green, blue: colors[3].blue, alpha: colors[3].alpha});
console.log('emit: ' + JSON.stringify({ index: 3, red: colors[3].red, green: colors[3].green, blue: colors[3].blue, alpha: colors[3].alpha}));
socket.emit('color', { index: 4, red: colors[4].red, green: colors[4].green, blue: colors[4].blue, alpha: colors[4].alpha});
console.log('emit: ' + JSON.stringify({ index: 4, red: colors[4].red, green: colors[4].green, blue: colors[4].blue, alpha: colors[4].alpha}));
// Process data
socket.on('set', function(data) {
count++;
//var command = Command.parse(data);
var cmd = data;
colors[cmd.index].red = cmd.red;
colors[cmd.index].green = cmd.green;
colors[cmd.index].blue = cmd.blue;
colors[cmd.index].alpha = cmd.alpha;
console.log('Sent: ' + JSON.stringify({
index: cmd.index,
red: colors[cmd.index].red,
green: colors[cmd.index].green,
blue: colors[cmd.index].blue,
alpha: colors[cmd.index].alpha
}));
changeColor(cmd.red, cmd.green, cmd.blue);
io.sockets.emit('color', {
index: cmd.index,
red: cmd.red,
green: cmd.green,
blue: cmd.blue,
alpha: cmd.alpha
});
});
// Do something here
});
changeColor(0, 0, 255);