-
Notifications
You must be signed in to change notification settings - Fork 4
/
stressDisplay.js
120 lines (104 loc) · 3.94 KB
/
stressDisplay.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
var stressDisplayLightBulb_Device = null;
var stressDisplayLightBulb_Characteristic = null;
var stressDisplayHeartRate_Characteristic = null;
function stressDisplay_lightBulb_connect() {
let serviceUuid = '00007777-0000-1000-8000-00805f9b34fb';
let characteristicUuid = '00008877-0000-1000-8000-00805f9b34fb';
navigator.bluetooth.requestDevice
({
acceptAllDevices: true,
optionalServices: [serviceUuid]
})
.then(device => {
stressDisplayLightBulb_Device = device;
return device.gatt.connect();
})
.then(server => { return server.getPrimaryService(serviceUuid); })
.then(service => { return service.getCharacteristic(characteristicUuid); })
.then(characteristic => { // Save characteristic
stressDisplayLightBulb_Characteristic = characteristic;
return characteristic;
})
.then(characteristic => { // Set to blue when starting
var send = getPayload(0x00, 0x00, 0xff);
characteristic.writeValue(send);
return characteristic;
})
.catch(error => { // Handle Errors
log('Error! ' + error);
});
}
function stressDisplay_heartRate_connect() {
let serviceUuid = "heart_rate";
let characteristicUuid = "heart_rate_measurement"
navigator.bluetooth.requestDevice
({
filters: [{ services: [serviceUuid] }]
})
.then(device => { return device.gatt.connect(); })
.then(server => { return server.getPrimaryService(serviceUuid); })
.then(service => { return service.getCharacteristic(characteristicUuid); })
.then(characteristic => { // Save characteristic
stressDisplayHeartRate_Characteristic = characteristic;
return characteristic;
})
.then(characteristic => { // Start notification
return characteristic.startNotifications()
.then(_ => {
characteristic.addEventListener(
'characteristicvaluechanged',
stressDisplay_read);
});
})
.catch(error => { // Handle Errors
log('Error! ' + error);
});
}
function stressDisplay_read(event) {
let value = event.target.value;
var bpm = value.getUint8(1);
setHeartRateValue(bpm);
var lut = colorLUT(bpm);
setColorValue(lut.r, lut.g, lut.b);
// log('{'
// + '0x' + ('0' + (lut.r & 0xFF).toString(16)).slice(-2) + ', '
// + '0x' + ('0' + (lut.g & 0xFF).toString(16)).slice(-2) + ', '
// + '0x' + ('0' + (lut.b & 0xFF).toString(16)).slice(-2)
// + '}, '
// + bpm.toString().padStart(3) + '|' + '-'.repeat(bpm - 40) + '>'
// );
log(bpm.toString().padStart(3) + '|' + '-'.repeat(bpm - 40) + '>');
var send = getPayload(lut.r, lut.g, lut.b);
stressDisplayLightBulb_Characteristic.writeValue(send);
sleep(50);
}
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 stressDisplay_disconnect() { // Disconnect
setColorValue(0x00, 0x00, 0x00);
var send = getPayload(0x00, 0x00, 0x00);
stressDisplayLightBulb_Characteristic.writeValue(send);
sleep(50);
if (stressDisplayLightBulb_Device.gatt.connected) {
stressDisplayLightBulb_Device.gatt.disconnect();
}
if (stressDisplayHeartRate_Characteristic) {
stressDisplayHeartRate_Characteristic.stopNotifications()
.then(_ => {
stressDisplayHeartRate_Characteristic.removeEventListener(
'characteristicvaluechanged',
stressDisplay_read);
})
.catch(error => {
log('Error! ' + error);
});
}
}