-
Notifications
You must be signed in to change notification settings - Fork 0
/
led-bar.js
51 lines (41 loc) · 1.57 KB
/
led-bar.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
//Mit WebsocketServer verbinden
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost:8080');
//GPIO Bibliothek laden
const Gpio = require('onoff').Gpio;
//LEDs anlegen
const led_10 = new Gpio(22, 'out');
const led_20 = new Gpio(23, 'out');
const led_30 = new Gpio(24, 'out');
const led_40 = new Gpio(5, 'out');
const led_50 = new Gpio(12, 'out');
const led_60 = new Gpio(6, 'out');
const led_70 = new Gpio(13, 'out');
const led_80 = new Gpio(19, 'out');
const led_90 = new Gpio(26, 'out');
const led_100 = new Gpio(21, 'out');
//LEDs in Array verwalten
const ledArray = [led_10, led_20, led_30, led_40, led_50, led_60, led_70, led_80, led_90, led_100];
//Wenn Verbindung mit WSS hergestellt wird
ws.on('open', function open() {
console.log("connected to wss");
//Wenn WS eine Nachricht von WSS empfaengt
ws.on('message', function incoming(message) {
//Nachricht kommt als String -> in JSON Objekt konvertieren
var obj = JSON.parse(message);
//Wenn aktueller Lautstaerke-Wert geliefert wird
if (obj.type === "change-volume") {
//erstmal alle LEDs ausmachen
ledArray.forEach(led => {
led.writeSync(0);
});
//wie viele der LEDs anmachen? 60 / 10 = 6 => LED 1-6 anschalten
let steps = obj.value / 10;
console.log("turn on until LED: " + steps);
//Ueber die LEDs gehen, die an sein sollen und diese anschalten
for (let i = 0; i < steps; i++) {
ledArray[i].writeSync(1);
}
}
});
});