forked from nathankellenicki/node-poweredup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wedo2smarthub.ts
360 lines (304 loc) · 13.6 KB
/
wedo2smarthub.ts
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import { Peripheral } from "noble";
import { Hub } from "./hub";
import { Port } from "./port";
import * as Consts from "./consts";
import Debug = require("debug");
const debug = Debug("wedo2smarthub");
/**
* The WeDo2SmartHub is emitted if the discovered device is a WeDo 2.0 Smart Hub.
* @class WeDo2SmartHub
* @extends Hub
*/
export class WeDo2SmartHub extends Hub {
// We set JSDoc to ignore these events as a WeDo 2.0 Smart Hub will never emit them.
/**
* @event WeDo2SmartHub#speed
* @ignore
*/
public static IsWeDo2SmartHub (peripheral: Peripheral) {
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.WEDO2_SMART_HUB.replace(/-/g, "")) >= 0);
}
private _lastTiltX: number = 0;
private _lastTiltY: number = 0;
constructor (peripheral: Peripheral, autoSubscribe: boolean = true) {
super(peripheral, autoSubscribe);
this.type = Consts.Hubs.WEDO2_SMART_HUB;
this._ports = {
"A": new Port("A", 1),
"B": new Port("B", 2)
};
debug("Discovered WeDo 2.0 Smart Hub");
}
public connect () {
return new Promise(async (resolve, reject) => {
debug("Connecting to WeDo 2.0 Smart Hub");
await super.connect();
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_PORT_TYPE), this._parsePortMessage.bind(this));
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_SENSOR_VALUE), this._parseSensorMessage.bind(this));
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_BUTTON), this._parseSensorMessage.bind(this));
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_BATTERY), this._parseBatteryMessage.bind(this));
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_HIGH_CURRENT_ALERT), this._parseHighCurrentAlert.bind(this));
this._getCharacteristic(Consts.BLECharacteristics.WEDO2_BATTERY).read((err, data) => {
this._parseBatteryMessage(data);
});
debug("Connect completed");
return resolve();
});
}
/**
* Set the name of the Hub.
* @method WeDo2SmartHub#setName
* @param {string} name New name of the hub (14 characters or less, ASCII only).
* @returns {Promise} Resolved upon successful issuance of command.
*/
public setName (name: string) {
if (name.length > 14) {
throw new Error("Name must be 14 characters or less");
}
return new Promise((resolve, reject) => {
const data = Buffer.from(name, "ascii");
// Send this twice, as sometimes the first time doesn't take
this._writeMessage(Consts.BLECharacteristics.WEDO2_NAME_ID, data);
this._writeMessage(Consts.BLECharacteristics.WEDO2_NAME_ID, data);
this._name = name;
return resolve();
});
}
/**
* Set the color of the LED on the Hub via a color value.
* @method WeDo2SmartHub#setLEDColor
* @param {number} color A number representing one of the LED color consts.
* @returns {Promise} Resolved upon successful issuance of command.
*/
public setLEDColor (color: number | boolean) {
return new Promise((resolve, reject) => {
let data = Buffer.from([0x06, 0x17, 0x01, 0x01]);
this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, data);
if (color === false) {
color = 0;
}
data = Buffer.from([0x06, 0x04, 0x01, color]);
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
return resolve();
});
}
/**
* Set the color of the LED on the Hub via RGB values.
* @method WeDo2SmartHub#setLEDRGB
* @param {number} red
* @param {number} green
* @param {number} blue
* @returns {Promise} Resolved upon successful issuance of command.
*/
public setLEDRGB (red: number, green: number, blue: number) {
return new Promise((resolve, reject) => {
let data = Buffer.from([0x06, 0x17, 0x01, 0x02]);
this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, data);
data = Buffer.from([0x06, 0x04, 0x03, red, green, blue]);
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
return resolve();
});
}
/**
* Set the motor speed on a given port.
* @method WeDo2SmartHub#setMotorSpeed
* @param {string} port
* @param {number} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
* @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely.
* @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
*/
public setMotorSpeed (port: string, speed: number, time?: number | boolean) {
const portObj = this._portLookup(port);
let cancelEventTimer = true;
if (typeof time === "boolean") {
if (time === true) {
cancelEventTimer = false;
}
time = undefined;
}
if (cancelEventTimer) {
portObj.cancelEventTimer();
}
return new Promise((resolve, reject) => {
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, this._mapSpeed(speed)]));
if (time && typeof time === "number") {
const timeout = global.setTimeout(() => {
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, 0x00]));
return resolve();
}, time);
portObj.setEventTimer(timeout);
} else {
return resolve();
}
});
}
/**
* Ramp the motor speed on a given port.
* @method WeDo2SmartHub#rampMotorSpeed
* @param {string} port
* @param {number} fromSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
* @param {number} toSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
* @param {number} time How long the ramp should last (in milliseconds).
* @returns {Promise} Resolved upon successful completion of command.
*/
public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) {
const portObj = this._portLookup(port);
portObj.cancelEventTimer();
return new Promise((resolve, reject) => {
this._calculateRamp(fromSpeed, toSpeed, time, portObj)
.on("changeSpeed", (speed) => {
this.setMotorSpeed(port, speed, true);
})
.on("finished", resolve);
});
}
/**
* Play a tone on the Hub's in-built buzzer
* @method WeDo2SmartHub#playTone
* @param {number} frequency
* @param {number} time How long the tone should play for (in milliseconds).
* @returns {Promise} Resolved upon successful completion of command (ie. once the tone has finished playing).
*/
public playTone (frequency: number, time: number) {
return new Promise((resolve, reject) => {
const data = Buffer.from([0x05, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00]);
data.writeUInt16LE(frequency, 3);
data.writeUInt16LE(time, 5);
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
global.setTimeout(resolve, time);
});
}
/**
* Set the light brightness on a given port.
* @method WeDo2SmartHub#setLightBrightness
* @param {string} port
* @param {number} brightness Brightness value between 0-100 (0 is off)
* @param {number} [time] How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely.
* @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the light is turned off.
*/
public setLightBrightness (port: string, brightness: number, time?: number) {
const portObj = this._portLookup(port);
portObj.cancelEventTimer();
return new Promise((resolve, reject) => {
const data = Buffer.from([portObj.value, 0x01, 0x02, brightness]);
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
if (time) {
const timeout = global.setTimeout(() => {
const data = Buffer.from([portObj.value, 0x01, 0x02, 0x00]);
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
return resolve();
}, time);
portObj.setEventTimer(timeout);
} else {
return resolve();
}
});
}
protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) {
this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), callback);
}
protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) {
this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), callback);
}
private _writeMessage (uuid: string, message: Buffer, callback?: () => void) {
const characteristic = this._getCharacteristic(uuid);
if (characteristic) {
characteristic.write(message, false, callback);
}
}
private _parseHighCurrentAlert (data: Buffer) {
// console.log(data);
}
private _parseBatteryMessage (data: Buffer) {
this._batteryLevel = data[0];
}
private _parsePortMessage (data: Buffer) {
const port = this._getPortForPortNumber(data[0]);
if (!port) {
return;
}
port.connected = data[1] === 1 ? true : false;
this._registerDeviceAttachment(port, data[3]);
}
private _parseSensorMessage (data: Buffer) {
if (data[0] === 0x01) {
/**
* Emits when a button is pressed.
* @event WeDo2SmartHub#button
* @param {string} button
* @param {number} state A number representing one of the button state consts.
*/
this.emit("button", "GREEN", Consts.ButtonStates.PRESSED);
return;
} else if (data[0] === 0x00) {
this.emit("button", "GREEN", Consts.ButtonStates.RELEASED);
return;
}
const port = this._getPortForPortNumber(data[1]);
if (!port) {
return;
}
if (port && port.connected) {
switch (port.type) {
case Consts.Devices.WEDO2_DISTANCE:
{
let distance = data[2];
if (data[3] === 1) {
distance = data[2] + 255;
}
/**
* Emits when a distance sensor is activated.
* @event WeDo2SmartHub#distance
* @param {string} port
* @param {number} distance Distance, in millimeters.
*/
this.emit("distance", port.id, distance * 10);
break;
}
case Consts.Devices.BOOST_DISTANCE:
{
const distance = data[2];
/**
* Emits when a color sensor is activated.
* @event WeDo2SmartHub#color
* @param {string} port
* @param {number} color A number representing one of the LED color consts.
*/
this.emit("color", port.id, distance);
break;
}
case Consts.Devices.WEDO2_TILT:
{
this._lastTiltX = data[2];
if (this._lastTiltX > 100) {
this._lastTiltX = -(255 - this._lastTiltX);
}
this._lastTiltY = data[3];
if (this._lastTiltY > 100) {
this._lastTiltY = -(255 - this._lastTiltY);
}
/**
* Emits when a tilt sensor is activated.
* @event WeDo2SmartHub#tilt
* @param {string} port
* @param {number} x
* @param {number} y
*/
this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY);
break;
}
case Consts.Devices.BOOST_TACHO_MOTOR:
{
const rotation = data.readInt32LE(2);
/**
* Emits when a rotation sensor is activated.
* @event WeDo2SmartHub#rotate
* @param {string} port
* @param {number} rotation
*/
this.emit("rotate", port.id, rotation);
}
}
}
}
}