-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32.js
218 lines (207 loc) · 9.71 KB
/
esp32.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
(function () {
'use strict';
const SERVICE_UUID = "4fafc201-1fb5-459e-8fcc-c5c9c331914b";
const LED_UUID = "beb5483e-36e1-4688-b7f5-ea07361b26a8";
const MOVE_MOUNT = "b5489b57-fd0c-4889-a942-861f39906a45";
const RESET_UUID = "27e5eb71-4b57-4a18-873c-aa272f770ca7";
const TRACKING_UUID = "1043dbcc-f3a0-4fa7-b77c-d7598a3868b4";
const TUNE_START_UUID = "e15d4ed0-d255-465c-a6af-33ce7b7ecd69";
const TUNE_END_UUID = "e36a814d-017e-4d96-99a1-3fd3d86fa194";
class Esp32 {
constructor() {
this.device = null;
this.tracking = null;
this.characteristics = new Map();
}
// ------ Connection ------
request() {
let options = {
filters: [
{ services: [SERVICE_UUID] }
]
};
return navigator.bluetooth.requestDevice(options)
.then(device => {
this.device = device;
//Check for device found
if (!this.device) {
return Promise.reject('Device not available.');
}
//Listen for disconnect
this.device.addEventListener('gattserverdisconnected', onDisconnected);
})
}
connect() {
let options = {
filters: [
{ services: [SERVICE_UUID] }
]
};
return navigator.bluetooth.requestDevice(options)
.then(device => {
this.device = device;
//Check for device found
if (!this.device) {
return Promise.reject('Device not available.');
}
//listen for disconnect
this.device.addEventListener('gattserverdisconnected', this.onDisconnected);
//add loading elements
document.querySelector('#loader').style.display = 'block';
document.querySelector('#connect').innerHTML = 'Connecting';
//TODO attempt to connect multiple times if failed first attempts
return this.device.gatt.connect()
.then(server => {
this.server = server;
//can remove promise.all
return Promise.all([
server.getPrimaryService(SERVICE_UUID).then(service => {
return Promise.all([
this.cacheCharacteristic(service, LED_UUID),
this.cacheCharacteristic(service, MOVE_MOUNT),
this.cacheCharacteristic(service, RESET_UUID),
this.cacheCharacteristic(service, TRACKING_UUID),
this.cacheCharacteristic(service, TUNE_START_UUID),
this.cacheCharacteristic(service, TUNE_END_UUID)
])
}),
]);
}, () => {
console.log('Error connecting. Try again')
})
.then(() => {
return this.getTracking();
})
.then((data) => {
this.tracking = data.getUint8(0) - 48;
console.log("tracking recorded as " + this.tracking)
if (this.tracking == 1) {
document.querySelector('#trackingStellar').checked = true;
} else if (this.tracking == 2) {
document.querySelector('#trackingLunar').checked = true;
} else {
document.querySelector('#trackingOff').checked = true;
}
})
.then(() => {
// document.querySelector('disconnected-page').style.display = 'none';
// document.querySelector('home-page').syle.display = 'inherit';
changePage('#disconnected-page', '#home-page');
})
.finally(() => {
//remove loading elements
document.querySelector('#loader').style.display = 'none';
document.querySelector('#connect').innerHTML = 'Connect';
})
})
// if (!this.device) {
// return Promise.reject('Device not available.');
// }
// //TODO attempt to connect multiple times if failed first attempts
// return this.device.gatt.connect()
// .then(server => {
// this.server = server;
// return Promise.all([
// server.getPrimaryService(SERVICE_UUID).then(service => {
// return Promise.all([
// this._cacheCharacteristic(service, LED_UUID),
// this._cacheCharacteristic(service, MOVE_MOUNT),
// ])
// }),
// ]);
// })
}
connect2() {
let options = {
filters: [
{ services: [SERVICE_UUID] }
]
};
//Start loader
document.querySelector('#loader').style.display = 'block';
document.querySelector('#connect').innerHTML = 'connecting';
//TODO attempt to connect multiple times if failed first attempts
return this.device.gatt.connect()
.then(server => {
this.server = server;
return Promise.all([
server.getPrimaryService(SERVICE_UUID).then(service => {
return Promise.all([
this.cacheCharacteristic(service, LED_UUID),
this.cacheCharacteristic(service, MOVE_MOUNT),
this.cacheCharacteristic(service, RESET_UUID),
this.cacheCharacteristic(service, TRACKING_MOUNT),
this.cacheCharacteristic(service, TUNE_START_UUID),
this.cacheCharacteristic(service, TUNE_END_MOUNT)
])
}),
]);
})
}
onDisconnected() {
console.log('disconnected');
document.querySelector('#disconnected-label').style.visibility = 'visible';
// document.querySelector('#state').classList.remove('connected');
document.querySelector('#disconnected-page').style.display = 'inherit';
document.querySelector('#home-page').style.display = 'none';
document.querySelector('#advanced-page').style.display = 'none';
document.querySelector('#tuning-page').style.display = 'none';
document.querySelector('#tuning-info-page').style.display = 'none';
document.querySelector('#align-page').style.display = 'none';
document.querySelector('#align-info-page').style.display = 'none';
setTimeout(() => {
document.querySelector('#disconnected-label').style.visibility = 'hidden';
}, 2000);
}
disconnect() {
return this.device.gatt.disconnect();
}
//------ Controls ------
moveBackwards() {
// let characteristic = this.characteristics.get(MOVE_MOUNT);
// return characteristic.writeValue(new Int8Array([-1]));
return this.writeCharacteristicValue(MOVE_MOUNT, new Int8Array([-1]))
}
moveForwards() {
return this.writeCharacteristicValue(MOVE_MOUNT, new Int8Array([1]))
}
standStill() {
return this.writeCharacteristicValue(MOVE_MOUNT, new Int8Array([0]))
}
changeLed() {
return this.writeCharacteristicValue(LED_UUID, new Int8Array([0]))
}
getTracking() {
return this.readCharacteristicValue(TRACKING_UUID);
}
setTracking(tracking) {
return this.writeCharacteristicValue(TRACKING_UUID, new Int8Array([tracking]))
}
reset() {
console.log("writing reset");
return this.writeCharacteristicValue(RESET_UUID, new Int8Array([0]))
}
tuneStart() {
return this.writeCharacteristicValue(TUNE_START_UUID, new Int8Array([0]))
}
tuneEnd() {
return this.writeCharacteristicValue(TUNE_END_UUID, new Int8Array([0]))
}
// ------ Utils ------
cacheCharacteristic(service, characteristicUuid) {
return service.getCharacteristic(characteristicUuid)
.then(characteristic => {
this.characteristics.set(characteristicUuid, characteristic)
})
}
writeCharacteristicValue(characteristicUuid, value) {
let characteristic = this.characteristics.get(characteristicUuid);
return characteristic.writeValue(value);
}
readCharacteristicValue(characteristicUuid) {
let characteristic = this.characteristics.get(characteristicUuid);
return characteristic.readValue();
}
}
window.esp32 = new Esp32();
})();