-
Notifications
You must be signed in to change notification settings - Fork 4
/
lightBulb.js
159 lines (141 loc) · 4.27 KB
/
lightBulb.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
var lightBulb_Device = null;
var lightBulb_Characteristic = null;
function lightBulb_connect() { // Connect
let serviceUuid = '00007777-0000-1000-8000-00805f9b34fb';
let characteristicUuid = '00008877-0000-1000-8000-00805f9b34fb';
navigator.bluetooth.requestDevice
({
acceptAllDevices: true,
optionalServices: [serviceUuid]
})
.then(device => {
lightBulb_Device = device;
return device.gatt.connect();
})
.then(server => { return server.getPrimaryService(serviceUuid); })
.then(service => { return service.getCharacteristic(characteristicUuid); })
.then(characteristic => { // Save characteristic
lightBulb_Characteristic = characteristic;
return characteristic;
})
.then(characteristic => { // Set colors
lightBulb_traffic(characteristic);
return characteristic;
})
.catch(error => { // Handle Errors
log('Error! ' + error);
});
}
function getPayload(r, g, b) { // Create the payload
var data = [
0x01, 0xfe, 0x00, 0x00, 0x53, 0x83, 0x10, 0x00,
g, // Green
b, // Blue
r, // Red
0x00, 0x50, 0x00, 0x00, 0x00
];
return Uint8Array.from(data);
}
function lightBulb_traffic(characteristic) { // Set a traffic light pattern
characteristic = characteristic || lightBulb_Characteristic;
lightBulb_green(); sleep(2000);
lightBulb_yellow(); sleep(1000);
lightBulb_red(); sleep(1000);
}
function lightBulb_common(r, g, b) {
setColorValue(r, g, b);
if (!lightBulb_Characteristic) return;
var send = getPayload(r, g, b); // Color
lightBulb_Characteristic.writeValue(send);
sleep(50);
}
function lightBulb_red() { // Red
lightBulb_common(0xff, 0x00, 0x00); // Red
}
function lightBulb_yellow() { // Yellow
lightBulb_common(0xff, 0xff, 0x00); // Yellow
}
function lightBulb_green() { // Green
lightBulb_common(0x00, 0xff, 0x00); // Green
}
function lightBulb_blue() { // Blue
lightBulb_common(0x00, 0x00, 0xff); // Blue
}
function lightBulb_black() { // Black
lightBulb_common(0x00, 0x00, 0x00); // Black
}
function lightBulb_countdown() {
var a = new Date();
var length = 2000; // 2 min
var length = 4000; // 8 min
for (var i = length; i > 0; i -= 100) {
lightBulb_green();
sleep(i);
lightBulb_black();
sleep(i);
lightBulb_yellow();
sleep(i);
lightBulb_black();
sleep(i);
lightBulb_red();
sleep(i);
lightBulb_black();
sleep(i);
}
lightBulb_blue();
var b = new Date();
console.log(a.toString() + ', ' + b.toString())
}
function lightBulb_disconnect() { // Disconnect
lightBulb_black();
if (lightBulb_Device.gatt.connected) {
lightBulb_Device.gatt.disconnect();
}
}
function initButtons() {
var msg = document.getElementById('lightBulbColorKey');
document.body.addEventListener('keydown', function (e) {
msg.textContent = 'keydown:' + e.keyCode;
switch (e.keyCode) {
case 49: // 1
//case 27: // Escape
lightBulb_red();
break;
case 81: // Q
//case 9: // Tab
lightBulb_green();
break;
case 65: // 1
//case 20: // Caps
lightBulb_blue();
break;
case 90: // Z
//case 16: // Shift
lightBulb_black();
break;
case 50: // 2
lightBulb_green();
break;
case 87: // W
lightBulb_yellow();
break;
case 83: // S
lightBulb_red();
break;
case 88: // X
lightBulb_black();
break;
case 51: // 3
//lightBulb_traffic();
break;
case 52: // 4
lightBulb_black();
break;
case 54: // 6
playToggleAudio();
break;
default:
break;
}
});
}