forked from bofh69/FlyLight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmilightRGBController.js
73 lines (62 loc) · 1.31 KB
/
milightRGBController.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
//
// # mightControl.
//
// A component to control a milight lamp.
//
var dgram = require('dgram');
var socket = dgram.createSocket('udp4');
var lampBuffer = new Buffer(2);
var port;
var addr;
exports.setDestination = function(cfg)
{
port = cfg.port;
addr = cfg.addr;
socket.bind(function() {
if(addr == '255.255.255.255') {
socket.setBroadcast(true);
}
});
}
exports.sendCommand = function(c1, c2, cb) {
lampBuffer.writeUInt8(c1, 0);
lampBuffer.writeUInt8(c2, 1);
socket.send(lampBuffer, 0, lampBuffer.length, port, addr);
if (cb !== null) {
setTimeout(cb, 1000);
}
}
exports.setColour = function(name, c) {
console.log("Setting lamp to ", name);
var cmd = 0x20;
if(c == 256) {
cmd = 0xa2;
c = 0;
}
exports.sendCommand(0x25, 0x00, // Speed up/link.
function() {
for(i = 0; i < 20; i++) {
exports.sendCommand(cmd, c);
}
});
}
exports.increaseLight = function() {
for(i = 0; i < 20; i++) {
exports.sendCommand(0x23, 0);
}
}
exports.decreaseLight = function() {
for(i = 0; i < 20; i++) {
exports.sendCommand(0x23, 0);
}
}
exports.turnOn = function() {
for(i = 0; i < 20; i++) {
exports.sendCommand(0x22, 0);
}
}
exports.turnOff = function() {
for(i = 0; i < 20; i++) {
exports.sendCommand(0x23, 0);
}
}