-
Notifications
You must be signed in to change notification settings - Fork 0
/
krawatte.backup
89 lines (70 loc) · 1.8 KB
/
krawatte.backup
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
var ws281x = require('rpi-ws281x-native');
var WebSocketServer = require('ws').Server;
var wss= new WebSocketServer({port:8080});
var path = require('path');
var mode = 0;
var NUM_LEDS = 32;
var express = require('express');
var app = express();
//var server = app.listen(8888);
app.use(express.static(__dirname + '/static'));
//app.use(express.static(path.join(__dirname, 'static')));
app.listen(8888);
//32 LEDs for Krawatte
ws281x.init(32);
process.on('SIGINT', function(){
ws281x.reset();
process.nextTick(function() { process.exit(0);})
});
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message, flags) {
if(flags.binary)
{
if(message == null)
{
return;
}
var len = message.length;
if(len< 4)
{
return;
}
var uint8 = new Uint8Array(message);
var uint32 = new Uint32Array(32);
var count = 0;
for(var i=4; i<uint8.length;i=i+3)
{
uint32[count] = (uint8[i] << 16) | (uint8[i+1] <<8) | uint8[i+2];
count++;
}
mode = 1;
ws281x.render(uint32);
}
else
{
console.log('received: %s', message);
}
});
ws.send('something');
});
var pixelData = new Uint32Array(32);
var offset =0;
setInterval(function () {
if(mode ==0)
{
for (var i = 0; i < NUM_LEDS; i++) {
pixelData[i] = colorwheel((offset + i) % 256);
}
offset = (offset + 1) % 256;
ws281x.render(pixelData);
}}, 1000 / 30);
// rainbow-colors, taken from http://goo.gl/Cs3H0v
function colorwheel(pos) {
pos = 255 - pos;
if (pos < 85) { return rgb2Int(255 - pos * 3, 0, pos * 3); }
else if (pos < 170) { pos -= 85; return rgb2Int(0, pos * 3, 255 - pos * 3); }
else { pos -= 170; return rgb2Int(pos * 3, 255 - pos * 3, 0); }
}
function rgb2Int(r, g, b) {
return ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff);
}