node-poweredup 10.0.0 → 10.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -1
- package/dist/browser/poweredup.js +6193 -0
- package/dist/browser/poweredup.js.map +7 -0
- package/dist/consts.d.ts +40 -0
- package/dist/consts.d.ts.map +1 -1
- package/dist/consts.js +42 -0
- package/dist/consts.js.map +1 -1
- package/dist/devices/absolutemotor.d.ts +8 -6
- package/dist/devices/absolutemotor.d.ts.map +1 -1
- package/dist/devices/absolutemotor.js +24 -31
- package/dist/devices/absolutemotor.js.map +1 -1
- package/dist/devices/basicmotor.d.ts +11 -10
- package/dist/devices/basicmotor.d.ts.map +1 -1
- package/dist/devices/basicmotor.js +16 -22
- package/dist/devices/basicmotor.js.map +1 -1
- package/dist/devices/colordistancesensor.d.ts +13 -12
- package/dist/devices/colordistancesensor.d.ts.map +1 -1
- package/dist/devices/colordistancesensor.js +25 -31
- package/dist/devices/colordistancesensor.js.map +1 -1
- package/dist/devices/device.d.ts +15 -6
- package/dist/devices/device.d.ts.map +1 -1
- package/dist/devices/device.js +209 -21
- package/dist/devices/device.js.map +1 -1
- package/dist/devices/duplotrainbasespeaker.d.ts +4 -4
- package/dist/devices/duplotrainbasespeaker.d.ts.map +1 -1
- package/dist/devices/duplotrainbasespeaker.js +5 -8
- package/dist/devices/duplotrainbasespeaker.js.map +1 -1
- package/dist/devices/hubled.d.ts +5 -4
- package/dist/devices/hubled.d.ts.map +1 -1
- package/dist/devices/hubled.js +24 -24
- package/dist/devices/hubled.js.map +1 -1
- package/dist/devices/light.d.ts +7 -4
- package/dist/devices/light.d.ts.map +1 -1
- package/dist/devices/light.js +11 -17
- package/dist/devices/light.js.map +1 -1
- package/dist/devices/piezobuzzer.d.ts +3 -2
- package/dist/devices/piezobuzzer.d.ts.map +1 -1
- package/dist/devices/piezobuzzer.js +2 -2
- package/dist/devices/piezobuzzer.js.map +1 -1
- package/dist/devices/tachomotor.d.ts +14 -8
- package/dist/devices/tachomotor.d.ts.map +1 -1
- package/dist/devices/tachomotor.js +50 -56
- package/dist/devices/tachomotor.js.map +1 -1
- package/dist/devices/technic3x3colorlightmatrix.d.ts +3 -2
- package/dist/devices/technic3x3colorlightmatrix.d.ts.map +1 -1
- package/dist/devices/technic3x3colorlightmatrix.js +20 -23
- package/dist/devices/technic3x3colorlightmatrix.js.map +1 -1
- package/dist/devices/techniccolorsensor.d.ts +9 -4
- package/dist/devices/techniccolorsensor.d.ts.map +1 -1
- package/dist/devices/techniccolorsensor.js +99 -8
- package/dist/devices/techniccolorsensor.js.map +1 -1
- package/dist/devices/technicdistancesensor.d.ts +1 -1
- package/dist/devices/technicdistancesensor.js +1 -1
- package/dist/devices/technicmediumhubtiltsensor.d.ts +7 -6
- package/dist/devices/technicmediumhubtiltsensor.d.ts.map +1 -1
- package/dist/devices/technicmediumhubtiltsensor.js +9 -18
- package/dist/devices/technicmediumhubtiltsensor.js.map +1 -1
- package/dist/devices/tiltsensor.d.ts +4 -1
- package/dist/devices/tiltsensor.d.ts.map +1 -1
- package/dist/devices/tiltsensor.js +84 -3
- package/dist/devices/tiltsensor.js.map +1 -1
- package/dist/hubs/basehub.d.ts +1 -0
- package/dist/hubs/basehub.d.ts.map +1 -1
- package/dist/hubs/basehub.js +17 -0
- package/dist/hubs/basehub.js.map +1 -1
- package/dist/hubs/lpf2hub.d.ts +1 -0
- package/dist/hubs/lpf2hub.d.ts.map +1 -1
- package/dist/hubs/lpf2hub.js +11 -0
- package/dist/hubs/lpf2hub.js.map +1 -1
- package/dist/portoutputcommand.d.ts +13 -0
- package/dist/portoutputcommand.d.ts.map +1 -0
- package/dist/portoutputcommand.js +30 -0
- package/dist/portoutputcommand.js.map +1 -0
- package/dist/portoutputsleep.d.ts +6 -0
- package/dist/portoutputsleep.d.ts.map +1 -0
- package/dist/portoutputsleep.js +10 -0
- package/dist/portoutputsleep.js.map +1 -0
- package/dist/utils.d.ts +1 -3
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +13 -19
- package/dist/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/consts.ts +44 -0
- package/src/devices/absolutemotor.ts +24 -31
- package/src/devices/basicmotor.ts +16 -22
- package/src/devices/colordistancesensor.ts +23 -29
- package/src/devices/device.ts +198 -20
- package/src/devices/duplotrainbasespeaker.ts +5 -8
- package/src/devices/hubled.ts +22 -22
- package/src/devices/light.ts +11 -18
- package/src/devices/piezobuzzer.ts +3 -3
- package/src/devices/tachomotor.ts +47 -53
- package/src/devices/technic3x3colorlightmatrix.ts +20 -23
- package/src/devices/techniccolorsensor.ts +102 -10
- package/src/devices/technicdistancesensor.ts +1 -1
- package/src/devices/technicmediumhubtiltsensor.ts +9 -18
- package/src/devices/tiltsensor.ts +85 -5
- package/src/hubs/basehub.ts +18 -0
- package/src/hubs/lpf2hub.ts +12 -5
- package/src/portoutputcommand.ts +37 -0
- package/src/portoutputsleep.ts +11 -0
- package/src/utils.ts +14 -19
- package/tsconfig.tsbuildinfo +1 -1
- package/webpack.config.cjs +35 -0
- package/dist/node/color.d.ts +0 -6
- package/dist/node/color.js +0 -15
- package/dist/node/color.js.map +0 -1
- package/dist/node/commands.d.ts +0 -299
- package/dist/node/commands.js +0 -672
- package/dist/node/commands.js.map +0 -1
- package/dist/node/connection.d.ts +0 -187
- package/dist/node/connection.js +0 -478
- package/dist/node/connection.js.map +0 -1
- package/dist/node/consts.d.ts +0 -644
- package/dist/node/consts.js +0 -675
- package/dist/node/consts.js.map +0 -1
- package/dist/node/devices/absolutemotor.d.ts +0 -43
- package/dist/node/devices/absolutemotor.js +0 -118
- package/dist/node/devices/absolutemotor.js.map +0 -1
- package/dist/node/devices/basicmotor.d.ts +0 -40
- package/dist/node/devices/basicmotor.js +0 -61
- package/dist/node/devices/basicmotor.js.map +0 -1
- package/dist/node/devices/colordistancesensor.d.ts +0 -79
- package/dist/node/devices/colordistancesensor.js +0 -284
- package/dist/node/devices/colordistancesensor.js.map +0 -1
- package/dist/node/devices/currentsensor.d.ts +0 -16
- package/dist/node/devices/currentsensor.js +0 -55
- package/dist/node/devices/currentsensor.js.map +0 -1
- package/dist/node/devices/device.d.ts +0 -71
- package/dist/node/devices/device.js +0 -162
- package/dist/node/devices/device.js.map +0 -1
- package/dist/node/devices/duplotrainbasecolorsensor.d.ts +0 -19
- package/dist/node/devices/duplotrainbasecolorsensor.js +0 -77
- package/dist/node/devices/duplotrainbasecolorsensor.js.map +0 -1
- package/dist/node/devices/duplotrainbasemotor.d.ts +0 -9
- package/dist/node/devices/duplotrainbasemotor.js +0 -12
- package/dist/node/devices/duplotrainbasemotor.js.map +0 -1
- package/dist/node/devices/duplotrainbasespeaker.d.ts +0 -28
- package/dist/node/devices/duplotrainbasespeaker.js +0 -40
- package/dist/node/devices/duplotrainbasespeaker.js.map +0 -1
- package/dist/node/devices/duplotrainbasespeedometer.d.ts +0 -16
- package/dist/node/devices/duplotrainbasespeedometer.js +0 -34
- package/dist/node/devices/duplotrainbasespeedometer.js.map +0 -1
- package/dist/node/devices/hubled.d.ts +0 -29
- package/dist/node/devices/hubled.js +0 -60
- package/dist/node/devices/hubled.js.map +0 -1
- package/dist/node/devices/index.d.ts +0 -14
- package/dist/node/devices/index.js +0 -154
- package/dist/node/devices/index.js.map +0 -1
- package/dist/node/devices/led.d.ts +0 -140
- package/dist/node/devices/led.js +0 -295
- package/dist/node/devices/led.js.map +0 -1
- package/dist/node/devices/light.d.ts +0 -25
- package/dist/node/devices/light.js +0 -46
- package/dist/node/devices/light.js.map +0 -1
- package/dist/node/devices/marioaccelerometer.d.ts +0 -17
- package/dist/node/devices/marioaccelerometer.js +0 -50
- package/dist/node/devices/marioaccelerometer.js.map +0 -1
- package/dist/node/devices/mariobarcodesensor.d.ts +0 -17
- package/dist/node/devices/mariobarcodesensor.js +0 -58
- package/dist/node/devices/mariobarcodesensor.js.map +0 -1
- package/dist/node/devices/mariopantssensor.d.ts +0 -16
- package/dist/node/devices/mariopantssensor.js +0 -34
- package/dist/node/devices/mariopantssensor.js.map +0 -1
- package/dist/node/devices/mediumlinearmotor.d.ts +0 -9
- package/dist/node/devices/mediumlinearmotor.js +0 -12
- package/dist/node/devices/mediumlinearmotor.js.map +0 -1
- package/dist/node/devices/motionsensor.d.ts +0 -16
- package/dist/node/devices/motionsensor.js +0 -38
- package/dist/node/devices/motionsensor.js.map +0 -1
- package/dist/node/devices/motor.d.ts +0 -182
- package/dist/node/devices/motor.js +0 -367
- package/dist/node/devices/motor.js.map +0 -1
- package/dist/node/devices/movehubmediumlinearmotor.d.ts +0 -9
- package/dist/node/devices/movehubmediumlinearmotor.js +0 -12
- package/dist/node/devices/movehubmediumlinearmotor.js.map +0 -1
- package/dist/node/devices/movehubtiltsensor.d.ts +0 -16
- package/dist/node/devices/movehubtiltsensor.js +0 -36
- package/dist/node/devices/movehubtiltsensor.js.map +0 -1
- package/dist/node/devices/piezobuzzer.d.ts +0 -17
- package/dist/node/devices/piezobuzzer.js +0 -28
- package/dist/node/devices/piezobuzzer.js.map +0 -1
- package/dist/node/devices/remotecontrolbutton.d.ts +0 -19
- package/dist/node/devices/remotecontrolbutton.js +0 -40
- package/dist/node/devices/remotecontrolbutton.js.map +0 -1
- package/dist/node/devices/sensor.d.ts +0 -305
- package/dist/node/devices/sensor.js +0 -662
- package/dist/node/devices/sensor.js.map +0 -1
- package/dist/node/devices/simplemediumlinearmotor.d.ts +0 -9
- package/dist/node/devices/simplemediumlinearmotor.js +0 -12
- package/dist/node/devices/simplemediumlinearmotor.js.map +0 -1
- package/dist/node/devices/tachomotor.d.ts +0 -70
- package/dist/node/devices/tachomotor.js +0 -167
- package/dist/node/devices/tachomotor.js.map +0 -1
- package/dist/node/devices/technic3x3colorlightmatrix.d.ts +0 -22
- package/dist/node/devices/technic3x3colorlightmatrix.js +0 -54
- package/dist/node/devices/technic3x3colorlightmatrix.js.map +0 -1
- package/dist/node/devices/techniccolorsensor.d.ts +0 -27
- package/dist/node/devices/techniccolorsensor.js +0 -72
- package/dist/node/devices/techniccolorsensor.js.map +0 -1
- package/dist/node/devices/technicdistancesensor.d.ts +0 -27
- package/dist/node/devices/technicdistancesensor.js +0 -58
- package/dist/node/devices/technicdistancesensor.js.map +0 -1
- package/dist/node/devices/technicforcesensor.d.ts +0 -18
- package/dist/node/devices/technicforcesensor.js +0 -58
- package/dist/node/devices/technicforcesensor.js.map +0 -1
- package/dist/node/devices/techniclargeangularmotor.d.ts +0 -12
- package/dist/node/devices/techniclargeangularmotor.js +0 -12
- package/dist/node/devices/techniclargeangularmotor.js.map +0 -1
- package/dist/node/devices/techniclargelinearmotor.d.ts +0 -9
- package/dist/node/devices/techniclargelinearmotor.js +0 -12
- package/dist/node/devices/techniclargelinearmotor.js.map +0 -1
- package/dist/node/devices/technicmediumangularmotor.d.ts +0 -12
- package/dist/node/devices/technicmediumangularmotor.js +0 -12
- package/dist/node/devices/technicmediumangularmotor.js.map +0 -1
- package/dist/node/devices/technicmediumhubaccelerometersensor.d.ts +0 -16
- package/dist/node/devices/technicmediumhubaccelerometersensor.js +0 -38
- package/dist/node/devices/technicmediumhubaccelerometersensor.js.map +0 -1
- package/dist/node/devices/technicmediumhubgyrosensor.d.ts +0 -16
- package/dist/node/devices/technicmediumhubgyrosensor.js +0 -38
- package/dist/node/devices/technicmediumhubgyrosensor.js.map +0 -1
- package/dist/node/devices/technicmediumhubtiltsensor.d.ts +0 -40
- package/dist/node/devices/technicmediumhubtiltsensor.js +0 -106
- package/dist/node/devices/technicmediumhubtiltsensor.js.map +0 -1
- package/dist/node/devices/technicsmallangularmotor.d.ts +0 -12
- package/dist/node/devices/technicsmallangularmotor.js +0 -12
- package/dist/node/devices/technicsmallangularmotor.js.map +0 -1
- package/dist/node/devices/technicxlargelinearmotor.d.ts +0 -9
- package/dist/node/devices/technicxlargelinearmotor.js +0 -12
- package/dist/node/devices/technicxlargelinearmotor.js.map +0 -1
- package/dist/node/devices/tiltsensor.d.ts +0 -16
- package/dist/node/devices/tiltsensor.js +0 -36
- package/dist/node/devices/tiltsensor.js.map +0 -1
- package/dist/node/devices/trainmotor.d.ts +0 -9
- package/dist/node/devices/trainmotor.js +0 -12
- package/dist/node/devices/trainmotor.js.map +0 -1
- package/dist/node/devices/voltagesensor.d.ts +0 -16
- package/dist/node/devices/voltagesensor.js +0 -59
- package/dist/node/devices/voltagesensor.js.map +0 -1
- package/dist/node/hub.d.ts +0 -291
- package/dist/node/hub.js +0 -778
- package/dist/node/hub.js.map +0 -1
- package/dist/node/hubs/basehub.d.ts +0 -162
- package/dist/node/hubs/basehub.js +0 -392
- package/dist/node/hubs/basehub.js.map +0 -1
- package/dist/node/hubs/duplotrainbase.d.ts +0 -17
- package/dist/node/hubs/duplotrainbase.js +0 -35
- package/dist/node/hubs/duplotrainbase.js.map +0 -1
- package/dist/node/hubs/hub.d.ts +0 -19
- package/dist/node/hubs/hub.js +0 -44
- package/dist/node/hubs/hub.js.map +0 -1
- package/dist/node/hubs/lpf2hub.d.ts +0 -48
- package/dist/node/hubs/lpf2hub.js +0 -314
- package/dist/node/hubs/lpf2hub.js.map +0 -1
- package/dist/node/hubs/mario.d.ts +0 -17
- package/dist/node/hubs/mario.js +0 -31
- package/dist/node/hubs/mario.js.map +0 -1
- package/dist/node/hubs/movehub.d.ts +0 -18
- package/dist/node/hubs/movehub.js +0 -46
- package/dist/node/hubs/movehub.js.map +0 -1
- package/dist/node/hubs/remotecontrol.d.ts +0 -17
- package/dist/node/hubs/remotecontrol.js +0 -37
- package/dist/node/hubs/remotecontrol.js.map +0 -1
- package/dist/node/hubs/technicmediumhub.d.ts +0 -17
- package/dist/node/hubs/technicmediumhub.js +0 -42
- package/dist/node/hubs/technicmediumhub.js.map +0 -1
- package/dist/node/hubs/technicsmallhub.d.ts +0 -18
- package/dist/node/hubs/technicsmallhub.js +0 -41
- package/dist/node/hubs/technicsmallhub.js.map +0 -1
- package/dist/node/hubs/wedo2smarthub.d.ts +0 -40
- package/dist/node/hubs/wedo2smarthub.js +0 -188
- package/dist/node/hubs/wedo2smarthub.js.map +0 -1
- package/dist/node/index-browser.d.ts +0 -1
- package/dist/node/index-browser.js +0 -96
- package/dist/node/index-browser.js.map +0 -1
- package/dist/node/index-node.d.ts +0 -49
- package/dist/node/index-node.js +0 -50
- package/dist/node/index-node.js.map +0 -1
- package/dist/node/index.d.ts +0 -31
- package/dist/node/index.js +0 -93
- package/dist/node/index.js.map +0 -1
- package/dist/node/index.mjs +0 -0
- package/dist/node/interfaces.d.ts +0 -23
- package/dist/node/interfaces.js +0 -2
- package/dist/node/interfaces.js.map +0 -1
- package/dist/node/nobleabstraction.d.ts +0 -26
- package/dist/node/nobleabstraction.js +0 -123
- package/dist/node/nobleabstraction.js.map +0 -1
- package/dist/node/poweredup-browser.d.ts +0 -50
- package/dist/node/poweredup-browser.js +0 -216
- package/dist/node/poweredup-browser.js.map +0 -1
- package/dist/node/poweredup-node.d.ts +0 -54
- package/dist/node/poweredup-node.js +0 -173
- package/dist/node/poweredup-node.js.map +0 -1
- package/dist/node/poweredup.d.ts +0 -83
- package/dist/node/poweredup.js +0 -542
- package/dist/node/poweredup.js.map +0 -1
- package/dist/node/protocol-wedo.d.ts +0 -158
- package/dist/node/protocol-wedo.js +0 -342
- package/dist/node/protocol-wedo.js.map +0 -1
- package/dist/node/protocol.d.ts +0 -340
- package/dist/node/protocol.js +0 -730
- package/dist/node/protocol.js.map +0 -1
- package/dist/node/types.d.ts +0 -408
- package/dist/node/types.js +0 -291
- package/dist/node/types.js.map +0 -1
- package/dist/node/utils.d.ts +0 -12
- package/dist/node/utils.js +0 -90
- package/dist/node/utils.js.map +0 -1
- package/dist/node/webbleabstraction.d.ts +0 -26
- package/dist/node/webbleabstraction.js +0 -104
- package/dist/node/webbleabstraction.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
package/src/devices/device.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { EventEmitter } from "events";
|
|
2
2
|
|
|
3
3
|
import { IDeviceInterface } from "../interfaces.js";
|
|
4
|
+
import { PortOutputCommand } from "../portoutputcommand.js";
|
|
5
|
+
import { PortOutputSleep } from "../portoutputsleep.js";
|
|
4
6
|
|
|
5
7
|
import * as Consts from "../consts.js";
|
|
6
8
|
|
|
9
|
+
import Debug = require("debug");
|
|
10
|
+
const debug = Debug("device");
|
|
11
|
+
|
|
7
12
|
/**
|
|
8
13
|
* @class Device
|
|
9
14
|
* @extends EventEmitter
|
|
@@ -14,8 +19,9 @@ export class Device extends EventEmitter {
|
|
|
14
19
|
public values: {[event: string]: any} = {};
|
|
15
20
|
|
|
16
21
|
protected _mode: number | undefined;
|
|
17
|
-
protected
|
|
18
|
-
protected
|
|
22
|
+
protected _bufferLength: number = 0;
|
|
23
|
+
protected _nextPortOutputCommands: (PortOutputCommand | PortOutputSleep)[] = [];
|
|
24
|
+
protected _transmittedPortOutputCommands: PortOutputCommand[] = [];
|
|
19
25
|
|
|
20
26
|
private _hub: IDeviceInterface;
|
|
21
27
|
private _portId: number;
|
|
@@ -25,7 +31,6 @@ export class Device extends EventEmitter {
|
|
|
25
31
|
|
|
26
32
|
private _isWeDo2SmartHub: boolean;
|
|
27
33
|
private _isVirtualPort: boolean = false;
|
|
28
|
-
private _eventTimer: NodeJS.Timeout | null = null;
|
|
29
34
|
|
|
30
35
|
constructor (hub: IDeviceInterface, portId: number, modeMap: {[event: string]: number} = {}, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) {
|
|
31
36
|
super();
|
|
@@ -126,11 +131,11 @@ export class Device extends EventEmitter {
|
|
|
126
131
|
return this._isVirtualPort;
|
|
127
132
|
}
|
|
128
133
|
|
|
129
|
-
public writeDirect (mode: number, data: Buffer) {
|
|
134
|
+
public writeDirect (mode: number, data: Buffer, interrupt: boolean = false) {
|
|
130
135
|
if (this.isWeDo2SmartHub) {
|
|
131
|
-
return this.send(Buffer.concat([Buffer.from([this.portId, 0x01, 0x02]), data]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);
|
|
136
|
+
return this.send(Buffer.concat([Buffer.from([this.portId, 0x01, 0x02]), data]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE).then(() => { return Consts.CommandFeedback.FEEDBACK_DISABLED; });
|
|
132
137
|
} else {
|
|
133
|
-
return this.
|
|
138
|
+
return this.sendPortOutputCommand(Buffer.concat([Buffer.from([0x51, mode]), data]), interrupt);
|
|
134
139
|
}
|
|
135
140
|
}
|
|
136
141
|
|
|
@@ -142,7 +147,6 @@ export class Device extends EventEmitter {
|
|
|
142
147
|
public subscribe (mode: number) {
|
|
143
148
|
this._ensureConnected();
|
|
144
149
|
if (mode !== this._mode) {
|
|
145
|
-
this._mode = mode;
|
|
146
150
|
this.hub.subscribe(this.portId, this.type, mode);
|
|
147
151
|
}
|
|
148
152
|
}
|
|
@@ -167,26 +171,180 @@ export class Device extends EventEmitter {
|
|
|
167
171
|
this.send(Buffer.from([0x21, this.portId, 0x00]));
|
|
168
172
|
}
|
|
169
173
|
|
|
170
|
-
public
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
174
|
+
public setMode (mode: number) {
|
|
175
|
+
this._mode = mode;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
protected transmitNextPortOutputCommand() {
|
|
179
|
+
if(!this.connected) {
|
|
180
|
+
this._transmittedPortOutputCommands.forEach(command => command.resolve(Consts.CommandFeedback.FEEDBACK_MISSING));
|
|
181
|
+
this._transmittedPortOutputCommands = [];
|
|
182
|
+
this._nextPortOutputCommands.forEach(command => command.resolve(Consts.CommandFeedback.TRANSMISSION_DISCARDED));
|
|
183
|
+
this._nextPortOutputCommands = [];
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if(!this._nextPortOutputCommands.length) return;
|
|
187
|
+
const nextCommand = this._nextPortOutputCommands[0];
|
|
188
|
+
if(nextCommand instanceof PortOutputSleep) {
|
|
189
|
+
if(nextCommand.state === Consts.CommandFeedback.EXECUTION_PENDING) {
|
|
190
|
+
nextCommand.state = Consts.CommandFeedback.EXECUTION_BUSY;
|
|
191
|
+
debug("sleep command ", nextCommand.duration);
|
|
192
|
+
setTimeout(() => {
|
|
193
|
+
if(nextCommand.state !== Consts.CommandFeedback.EXECUTION_BUSY) return;
|
|
194
|
+
const command = this._nextPortOutputCommands.shift();
|
|
195
|
+
if(command) command.resolve(Consts.CommandFeedback.EXECUTION_COMPLETED);
|
|
196
|
+
this.transmitNextPortOutputCommand();
|
|
197
|
+
}, nextCommand.duration);
|
|
177
198
|
}
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
if(this._bufferLength !== this._transmittedPortOutputCommands.length) return;
|
|
202
|
+
if(this._bufferLength < 2 || nextCommand.interrupt) {
|
|
203
|
+
if(nextCommand.state === Consts.CommandFeedback.TRANSMISSION_PENDING) {
|
|
204
|
+
nextCommand.state = Consts.CommandFeedback.TRANSMISSION_BUSY;
|
|
205
|
+
debug("transmit command ", nextCommand.startupAndCompletion, nextCommand.data);
|
|
206
|
+
this.send(Buffer.concat([Buffer.from([0x81, this.portId, nextCommand.startupAndCompletion]), nextCommand.data])).then(() => {
|
|
207
|
+
if(nextCommand.state !== Consts.CommandFeedback.TRANSMISSION_BUSY) return;
|
|
208
|
+
const command = this._nextPortOutputCommands.shift();
|
|
209
|
+
if(command instanceof PortOutputCommand) this._transmittedPortOutputCommands.push(command);
|
|
210
|
+
});
|
|
211
|
+
this.transmitNextPortOutputCommand(); // if the next command is PortOutputSleep this starts sleep timeout
|
|
212
|
+
// one could start a timer here to ensure finish function is called
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
public sendPortOutputCommand(data: Buffer, interrupt: boolean = false) {
|
|
218
|
+
if (this.isWeDo2SmartHub) {
|
|
219
|
+
throw new Error("PortOutputCommands are not available on the WeDo 2.0 Smart Hub");
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
const command = new PortOutputCommand(data, interrupt);
|
|
223
|
+
if(interrupt) {
|
|
224
|
+
this._nextPortOutputCommands.forEach(command => {
|
|
225
|
+
if(command.state !== Consts.CommandFeedback.TRANSMISSION_BUSY) {
|
|
226
|
+
command.resolve(Consts.CommandFeedback.TRANSMISSION_DISCARDED);
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
this._nextPortOutputCommands = this._nextPortOutputCommands.filter(command => command.state === Consts.CommandFeedback.TRANSMISSION_BUSY);
|
|
178
230
|
}
|
|
231
|
+
this._nextPortOutputCommands.push(command);
|
|
232
|
+
process.nextTick(() => this.transmitNextPortOutputCommand());
|
|
233
|
+
return command.promise;
|
|
179
234
|
}
|
|
180
235
|
|
|
181
|
-
public
|
|
182
|
-
|
|
236
|
+
public addPortOutputSleep(duration: number) {
|
|
237
|
+
const command = new PortOutputSleep(duration);
|
|
238
|
+
this._nextPortOutputCommands.push(command);
|
|
239
|
+
process.nextTick(() => this.transmitNextPortOutputCommand());
|
|
240
|
+
return command.promise;
|
|
183
241
|
}
|
|
184
242
|
|
|
185
|
-
public
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
243
|
+
public finish (message: number) {
|
|
244
|
+
debug("recieved command feedback ", message);
|
|
245
|
+
if((message & 0x08) === 0x08) this._bufferLength = 0;
|
|
246
|
+
else if((message & 0x01) === 0x01) this._bufferLength = 1;
|
|
247
|
+
else if((message & 0x10) === 0x10) this._bufferLength = 2;
|
|
248
|
+
const completed = ((message & 0x02) === 0x02);
|
|
249
|
+
const discarded = ((message & 0x04) === 0x04);
|
|
250
|
+
|
|
251
|
+
switch(this._transmittedPortOutputCommands.length) {
|
|
252
|
+
case 0:
|
|
253
|
+
break;
|
|
254
|
+
case 1:
|
|
255
|
+
if(!this._bufferLength && completed && !discarded) {
|
|
256
|
+
this._complete();
|
|
257
|
+
}
|
|
258
|
+
else if(!this._bufferLength && !completed && discarded) {
|
|
259
|
+
this._discard();
|
|
260
|
+
}
|
|
261
|
+
else if(this._bufferLength && !completed && !discarded) {
|
|
262
|
+
this._busy();
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
this._missing();
|
|
266
|
+
}
|
|
267
|
+
break;
|
|
268
|
+
case 2:
|
|
269
|
+
if(!this._bufferLength && completed && discarded) {
|
|
270
|
+
this._discard();
|
|
271
|
+
this._complete();
|
|
272
|
+
}
|
|
273
|
+
else if(!this._bufferLength && completed && !discarded) {
|
|
274
|
+
this._complete();
|
|
275
|
+
this._complete();
|
|
276
|
+
}
|
|
277
|
+
else if(!this._bufferLength && !completed && discarded) {
|
|
278
|
+
this._discard();
|
|
279
|
+
this._discard();
|
|
280
|
+
}
|
|
281
|
+
else if(this._bufferLength === 1 && completed && !discarded) {
|
|
282
|
+
this._complete();
|
|
283
|
+
this._busy();
|
|
284
|
+
}
|
|
285
|
+
else if(this._bufferLength === 1 && !completed && discarded) {
|
|
286
|
+
this._discard();
|
|
287
|
+
this._busy();
|
|
288
|
+
}
|
|
289
|
+
else if(this._bufferLength === 1 && completed && discarded) {
|
|
290
|
+
this._missing();
|
|
291
|
+
this._busy();
|
|
292
|
+
}
|
|
293
|
+
else if(this._bufferLength === 2 && !completed && !discarded) {
|
|
294
|
+
this._busy();
|
|
295
|
+
this._pending();
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
this._missing();
|
|
299
|
+
this._missing();
|
|
300
|
+
}
|
|
301
|
+
break;
|
|
302
|
+
case 3:
|
|
303
|
+
if(!this._bufferLength && completed && discarded) {
|
|
304
|
+
this._discard();
|
|
305
|
+
this._discard();
|
|
306
|
+
this._complete();
|
|
307
|
+
}
|
|
308
|
+
else if(!this._bufferLength && completed && !discarded) {
|
|
309
|
+
this._complete();
|
|
310
|
+
this._complete();
|
|
311
|
+
this._complete();
|
|
312
|
+
}
|
|
313
|
+
else if(!this._bufferLength && !completed && discarded) {
|
|
314
|
+
this._discard();
|
|
315
|
+
this._discard();
|
|
316
|
+
this._discard();
|
|
317
|
+
}
|
|
318
|
+
else if(this._bufferLength === 1 && completed && discarded) {
|
|
319
|
+
this._discard();
|
|
320
|
+
this._complete();
|
|
321
|
+
this._busy();
|
|
322
|
+
}
|
|
323
|
+
else if(this._bufferLength === 1 && completed && !discarded) {
|
|
324
|
+
this._complete();
|
|
325
|
+
this._complete();
|
|
326
|
+
this._busy();
|
|
327
|
+
}
|
|
328
|
+
else if(this._bufferLength === 1 && !completed && discarded) {
|
|
329
|
+
this._discard();
|
|
330
|
+
this._discard();
|
|
331
|
+
this._busy();
|
|
332
|
+
}
|
|
333
|
+
else if(this._bufferLength === 1 && !completed && !discarded) {
|
|
334
|
+
this._missing();
|
|
335
|
+
this._missing();
|
|
336
|
+
this._busy();
|
|
337
|
+
}
|
|
338
|
+
// third command can only be interrupt, if this._bufferLength === 2 it was queued
|
|
339
|
+
else {
|
|
340
|
+
this._missing();
|
|
341
|
+
this._missing();
|
|
342
|
+
this._missing();
|
|
343
|
+
}
|
|
344
|
+
break;
|
|
189
345
|
}
|
|
346
|
+
|
|
347
|
+
this.transmitNextPortOutputCommand();
|
|
190
348
|
}
|
|
191
349
|
|
|
192
350
|
private _ensureConnected () {
|
|
@@ -195,4 +353,24 @@ export class Device extends EventEmitter {
|
|
|
195
353
|
}
|
|
196
354
|
}
|
|
197
355
|
|
|
356
|
+
private _complete () {
|
|
357
|
+
const command = this._transmittedPortOutputCommands.shift();
|
|
358
|
+
if(command) command.resolve(Consts.CommandFeedback.EXECUTION_COMPLETED);
|
|
359
|
+
}
|
|
360
|
+
private _discard () {
|
|
361
|
+
const command = this._transmittedPortOutputCommands.shift();
|
|
362
|
+
if(command) command.resolve(Consts.CommandFeedback.EXECUTION_DISCARDED);
|
|
363
|
+
}
|
|
364
|
+
private _missing () {
|
|
365
|
+
const command = this._transmittedPortOutputCommands.shift();
|
|
366
|
+
if(command) command.resolve(Consts.CommandFeedback.FEEDBACK_MISSING);
|
|
367
|
+
}
|
|
368
|
+
private _busy () {
|
|
369
|
+
const command = this._transmittedPortOutputCommands[0];
|
|
370
|
+
if(command) command.state = Consts.CommandFeedback.EXECUTION_BUSY;
|
|
371
|
+
}
|
|
372
|
+
private _pending () {
|
|
373
|
+
const command = this._transmittedPortOutputCommands[1];
|
|
374
|
+
if(command) command.state = Consts.CommandFeedback.EXECUTION_PENDING;
|
|
375
|
+
}
|
|
198
376
|
}
|
|
@@ -17,24 +17,21 @@ export class DuploTrainBaseSpeaker extends Device {
|
|
|
17
17
|
/**
|
|
18
18
|
* Play a built-in train sound.
|
|
19
19
|
* @param {DuploTrainBaseSound} sound
|
|
20
|
-
* @returns {Promise} Resolved upon
|
|
20
|
+
* @returns {Promise<CommandFeedback>} Resolved upon completion of command.
|
|
21
21
|
*/
|
|
22
22
|
public playSound (sound: Consts.DuploTrainBaseSound) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
this.writeDirect(0x01, Buffer.from([sound]));
|
|
26
|
-
return resolve();
|
|
27
|
-
});
|
|
23
|
+
this.subscribe(Mode.SOUND);
|
|
24
|
+
return this.writeDirect(0x01, Buffer.from([sound]));
|
|
28
25
|
}
|
|
29
26
|
|
|
30
27
|
/**
|
|
31
28
|
* Play a built-in system tone.
|
|
32
29
|
* @param {number} tone
|
|
33
|
-
* @returns {Promise} Resolved upon
|
|
30
|
+
* @returns {Promise<CommandFeedback>} Resolved upon completion of command.
|
|
34
31
|
*/
|
|
35
32
|
public playTone (tone: number) {
|
|
36
33
|
this.subscribe(Mode.TONE);
|
|
37
|
-
this.writeDirect(0x02, Buffer.from([tone]));
|
|
34
|
+
return this.writeDirect(0x02, Buffer.from([tone]));
|
|
38
35
|
}
|
|
39
36
|
|
|
40
37
|
}
|
package/src/devices/hubled.ts
CHANGED
|
@@ -19,22 +19,22 @@ export class HubLED extends Device {
|
|
|
19
19
|
/**
|
|
20
20
|
* Set the color of the LED on the Hub via a color value.
|
|
21
21
|
* @param {Color} color
|
|
22
|
-
* @returns {Promise} Resolved upon
|
|
22
|
+
* @returns {Promise<CommandFeedback>} Resolved upon completion of command.
|
|
23
23
|
*/
|
|
24
24
|
public setColor (color: number | boolean) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
if (typeof color === "boolean") {
|
|
26
|
+
color = 0;
|
|
27
|
+
}
|
|
28
|
+
if (this.isWeDo2SmartHub) {
|
|
29
|
+
return new Promise<Consts.CommandFeedback>((resolve) => {
|
|
30
30
|
this.send(Buffer.from([0x06, 0x17, 0x01, 0x01]), Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE);
|
|
31
|
-
this.send(Buffer.from([0x06, 0x04, 0x01, color]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
return
|
|
37
|
-
}
|
|
31
|
+
this.send(Buffer.from([0x06, 0x04, 0x01, Number(color)]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);
|
|
32
|
+
return resolve(Consts.CommandFeedback.FEEDBACK_DISABLED);
|
|
33
|
+
})
|
|
34
|
+
} else {
|
|
35
|
+
this.subscribe(Mode.COLOR);
|
|
36
|
+
return this.writeDirect(0x00, Buffer.from([color]));
|
|
37
|
+
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
|
|
@@ -43,19 +43,19 @@ export class HubLED extends Device {
|
|
|
43
43
|
* @param {number} red
|
|
44
44
|
* @param {number} green
|
|
45
45
|
* @param {number} blue
|
|
46
|
-
* @returns {Promise} Resolved upon
|
|
46
|
+
* @returns {Promise<CommandFeedback>} Resolved upon completion of command.
|
|
47
47
|
*/
|
|
48
48
|
public setRGB (red: number, green: number, blue: number) {
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
if (this.isWeDo2SmartHub) {
|
|
50
|
+
return new Promise<Consts.CommandFeedback>((resolve) => {
|
|
51
51
|
this.send(Buffer.from([0x06, 0x17, 0x01, 0x02]), Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE);
|
|
52
52
|
this.send(Buffer.from([0x06, 0x04, 0x03, red, green, blue]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return
|
|
58
|
-
}
|
|
53
|
+
resolve(Consts.CommandFeedback.FEEDBACK_DISABLED);
|
|
54
|
+
});
|
|
55
|
+
} else {
|
|
56
|
+
this.subscribe(Mode.RGB);
|
|
57
|
+
return this.writeDirect(0x01, Buffer.from([red, green, blue]));
|
|
58
|
+
}
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
|
package/src/devices/light.ts
CHANGED
|
@@ -20,16 +20,12 @@ export class Light extends Device {
|
|
|
20
20
|
/**
|
|
21
21
|
* Set the light brightness.
|
|
22
22
|
* @param {number} brightness Brightness value between 0-100 (0 is off)
|
|
23
|
-
* @
|
|
23
|
+
* @param {number} brightness Brightness value between 0-100 (0 is off)
|
|
24
|
+
* @param {boolean} [interrupt=false] If true, previous commands are discarded.
|
|
25
|
+
* @returns {Promise<CommandFeedback>} Resolved upon completion of command.
|
|
24
26
|
*/
|
|
25
|
-
public setBrightness (brightness: number, interrupt: boolean =
|
|
26
|
-
|
|
27
|
-
this.cancelEventTimer();
|
|
28
|
-
}
|
|
29
|
-
return new Promise<void>((resolve) => {
|
|
30
|
-
this.writeDirect(0x00, Buffer.from([brightness]));
|
|
31
|
-
return resolve();
|
|
32
|
-
});
|
|
27
|
+
public setBrightness (brightness: number, interrupt: boolean = false) {
|
|
28
|
+
return this.writeDirect(0x00, Buffer.from([brightness]), interrupt);
|
|
33
29
|
}
|
|
34
30
|
|
|
35
31
|
|
|
@@ -38,18 +34,15 @@ export class Light extends Device {
|
|
|
38
34
|
* @param {number} fromBrightness Brightness value between 0-100 (0 is off)
|
|
39
35
|
* @param {number} toBrightness Brightness value between 0-100 (0 is off)
|
|
40
36
|
* @param {number} time How long the ramp should last (in milliseconds).
|
|
41
|
-
* @returns {Promise} Resolved upon
|
|
37
|
+
* @returns {Promise<CommandFeedback>} Resolved upon completion of command.
|
|
42
38
|
*/
|
|
43
39
|
public rampBrightness (fromBrightness: number, toBrightness: number, time: number) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
.
|
|
48
|
-
this.setBrightness(power, false);
|
|
49
|
-
})
|
|
50
|
-
.on("finished", resolve);
|
|
40
|
+
const powerValues = calculateRamp(fromBrightness, toBrightness, time);
|
|
41
|
+
powerValues.forEach(value => {
|
|
42
|
+
this.setBrightness(value);
|
|
43
|
+
this.addPortOutputSleep(Math.round(time/powerValues.length));
|
|
51
44
|
});
|
|
45
|
+
return this.setBrightness(toBrightness);
|
|
52
46
|
}
|
|
53
47
|
|
|
54
|
-
|
|
55
48
|
}
|
|
@@ -20,15 +20,15 @@ export class PiezoBuzzer extends Device {
|
|
|
20
20
|
* Play a tone on the Hub's in-built buzzer
|
|
21
21
|
* @param {number} frequency
|
|
22
22
|
* @param {number} time How long the tone should play for (in milliseconds).
|
|
23
|
-
* @returns {Promise} Resolved upon
|
|
23
|
+
* @returns {Promise<CommandFeedback>} Resolved upon completion of command (i.e. once the tone has finished playing).
|
|
24
24
|
*/
|
|
25
25
|
public playTone (frequency: number, time: number) {
|
|
26
|
-
return new Promise((resolve) => {
|
|
26
|
+
return new Promise<Consts.CommandFeedback>((resolve) => {
|
|
27
27
|
const data = Buffer.from([0x05, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00]);
|
|
28
28
|
data.writeUInt16LE(frequency, 3);
|
|
29
29
|
data.writeUInt16LE(time, 5);
|
|
30
30
|
this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);
|
|
31
|
-
global.setTimeout(resolve, time);
|
|
31
|
+
global.setTimeout(() => {return resolve(Consts.CommandFeedback.FEEDBACK_DISABLED)}, time);
|
|
32
32
|
});
|
|
33
33
|
}
|
|
34
34
|
|
|
@@ -63,24 +63,28 @@ export class TachoMotor extends BasicMotor {
|
|
|
63
63
|
/**
|
|
64
64
|
* Set the global acceleration time
|
|
65
65
|
* @param {number} time How long acceleration should last (in milliseconds).
|
|
66
|
-
* @
|
|
66
|
+
* @param {number} profile 0 by default
|
|
67
|
+
* @param {boolean} [interrupt=false] If true, previous commands are discarded.
|
|
68
|
+
* @returns {Promise<CommandFeedback>} Resolved upon completion of command (i.e. once the motor is finished).
|
|
67
69
|
*/
|
|
68
|
-
public setAccelerationTime (time: number, profile: number = 0x00) {
|
|
69
|
-
const message = Buffer.from([
|
|
70
|
-
message.writeUInt16LE(time,
|
|
71
|
-
this.
|
|
70
|
+
public setAccelerationTime (time: number, profile: number = 0x00, interrupt: boolean = false) {
|
|
71
|
+
const message = Buffer.from([0x05, 0x00, 0x00, profile]);
|
|
72
|
+
message.writeUInt16LE(time, 1);
|
|
73
|
+
return this.sendPortOutputCommand(message, interrupt);
|
|
72
74
|
}
|
|
73
75
|
|
|
74
76
|
|
|
75
77
|
/**
|
|
76
78
|
* Set the global deceleration time
|
|
77
79
|
* @param {number} time How long deceleration should last (in milliseconds).
|
|
78
|
-
* @
|
|
80
|
+
* @param {number} profile 0 by default
|
|
81
|
+
* @param {boolean} [interrupt=false] If true, previous commands are discarded.
|
|
82
|
+
* @returns {Promise<CommandFeedback>} Resolved upon completion of command (i.e. once the motor is finished).
|
|
79
83
|
*/
|
|
80
|
-
public setDecelerationTime (time: number, profile: number = 0x00) {
|
|
81
|
-
const message = Buffer.from([
|
|
82
|
-
message.writeUInt16LE(time,
|
|
83
|
-
this.
|
|
84
|
+
public setDecelerationTime (time: number, profile: number = 0x00, interrupt: boolean = true) {
|
|
85
|
+
const message = Buffer.from([0x06, 0x00, 0x00, profile]);
|
|
86
|
+
message.writeUInt16LE(time, 1);
|
|
87
|
+
return this.sendPortOutputCommand(message, interrupt);
|
|
84
88
|
}
|
|
85
89
|
|
|
86
90
|
|
|
@@ -88,72 +92,62 @@ export class TachoMotor extends BasicMotor {
|
|
|
88
92
|
* Set the motor speed.
|
|
89
93
|
* @param {number} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
|
|
90
94
|
* @param {number} time How long the motor should run for (in milliseconds).
|
|
91
|
-
* @
|
|
95
|
+
* @param {boolean} [interrupt=false] If true, previous commands are discarded.
|
|
96
|
+
* @returns {Promise<CommandFeedback>} Resolved upon completion of command (i.e. once the motor is finished).
|
|
92
97
|
*/
|
|
93
|
-
public setSpeed (speed: [number, number] | number, time: number | undefined) {
|
|
98
|
+
public setSpeed (speed: [number, number] | number, time: number | undefined, interrupt: boolean = false) {
|
|
94
99
|
if (!this.isVirtualPort && speed instanceof Array) {
|
|
95
100
|
throw new Error("Only virtual ports can accept multiple speeds");
|
|
96
101
|
}
|
|
97
102
|
if (this.isWeDo2SmartHub) {
|
|
98
103
|
throw new Error("Motor speed is not available on the WeDo 2.0 Smart Hub");
|
|
99
104
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
105
|
+
if (speed === undefined || speed === null) {
|
|
106
|
+
speed = 100;
|
|
107
|
+
}
|
|
108
|
+
let message;
|
|
109
|
+
if (time !== undefined) {
|
|
110
|
+
if (speed instanceof Array) {
|
|
111
|
+
message = Buffer.from([0x0a, 0x00, 0x00, mapSpeed(speed[0]), mapSpeed(speed[1]), this._maxPower, this._brakeStyle, this.useProfile()]);
|
|
112
|
+
} else {
|
|
113
|
+
message = Buffer.from([0x09, 0x00, 0x00, mapSpeed(speed), this._maxPower, this._brakeStyle, this.useProfile()]);
|
|
104
114
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
} else {
|
|
110
|
-
message = Buffer.from([0x81, this.portId, 0x11, 0x09, 0x00, 0x00, mapSpeed(speed), this._maxPower, this._brakeStyle, this.useProfile()]);
|
|
111
|
-
}
|
|
112
|
-
message.writeUInt16LE(time, 4);
|
|
115
|
+
message.writeUInt16LE(time, 1);
|
|
116
|
+
} else {
|
|
117
|
+
if (speed instanceof Array) {
|
|
118
|
+
message = Buffer.from([0x08, mapSpeed(speed[0]), mapSpeed(speed[1]), this._maxPower, this.useProfile()]);
|
|
113
119
|
} else {
|
|
114
|
-
|
|
115
|
-
message = Buffer.from([0x81, this.portId, 0x11, 0x08, mapSpeed(speed[0]), mapSpeed(speed[1]), this._maxPower, this.useProfile()]);
|
|
116
|
-
} else {
|
|
117
|
-
message = Buffer.from([0x81, this.portId, 0x11, 0x07, mapSpeed(speed), this._maxPower, this.useProfile()]);
|
|
118
|
-
}
|
|
120
|
+
message = Buffer.from([0x07, mapSpeed(speed), this._maxPower, this.useProfile()]);
|
|
119
121
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
return resolve();
|
|
123
|
-
});
|
|
124
|
-
});
|
|
122
|
+
}
|
|
123
|
+
return this.sendPortOutputCommand(message, interrupt);
|
|
125
124
|
}
|
|
126
125
|
|
|
127
126
|
/**
|
|
128
127
|
* Rotate a motor by a given amount of degrees.
|
|
129
128
|
* @param {number} degrees How much the motor should be rotated (in degrees).
|
|
130
129
|
* @param {number} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.
|
|
131
|
-
* @
|
|
130
|
+
* @param {boolean} [interrupt=false] If true, previous commands are discarded.
|
|
131
|
+
* @returns {Promise<CommandFeedback>} Resolved upon completion of command (i.e. once the motor is finished).
|
|
132
132
|
*/
|
|
133
|
-
public rotateByDegrees (degrees: number, speed: [number, number] | number) {
|
|
133
|
+
public rotateByDegrees (degrees: number, speed: [number, number] | number, interrupt: boolean = false) {
|
|
134
134
|
if (!this.isVirtualPort && speed instanceof Array) {
|
|
135
135
|
throw new Error("Only virtual ports can accept multiple speeds");
|
|
136
136
|
}
|
|
137
137
|
if (this.isWeDo2SmartHub) {
|
|
138
138
|
throw new Error("Rotation is not available on the WeDo 2.0 Smart Hub");
|
|
139
139
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
message.writeUInt32LE(degrees, 4);
|
|
152
|
-
this.send(message);
|
|
153
|
-
this._finishedCallbacks.push(() => {
|
|
154
|
-
return resolve();
|
|
155
|
-
});
|
|
156
|
-
});
|
|
140
|
+
if (speed === undefined || speed === null) {
|
|
141
|
+
speed = 100;
|
|
142
|
+
}
|
|
143
|
+
let message;
|
|
144
|
+
if (speed instanceof Array) {
|
|
145
|
+
message = Buffer.from([0x0c, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed[0]), mapSpeed(speed[1]), this._maxPower, this._brakeStyle, this.useProfile()]);
|
|
146
|
+
} else {
|
|
147
|
+
message = Buffer.from([0x0b, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), this._maxPower, this._brakeStyle, this.useProfile()]);
|
|
148
|
+
}
|
|
149
|
+
message.writeUInt32LE(degrees, 1);
|
|
150
|
+
return this.sendPortOutputCommand(message, interrupt);
|
|
157
151
|
}
|
|
158
152
|
|
|
159
153
|
|
|
@@ -21,35 +21,32 @@ export class Technic3x3ColorLightMatrix extends Device {
|
|
|
21
21
|
/**
|
|
22
22
|
* Set the LED matrix, one color per LED
|
|
23
23
|
* @param {Color[] | Color} colors Array of 9 colors, 9 Color objects, or a single color
|
|
24
|
-
* @returns {Promise} Resolved upon
|
|
24
|
+
* @returns {Promise<CommandFeedback>} Resolved upon completion of command.
|
|
25
25
|
*/
|
|
26
26
|
public setMatrix (colors: number[] | number) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (typeof colors === 'number') {
|
|
32
|
-
// @ts-ignore
|
|
33
|
-
colorArray[i] = colors + (10 << 4);
|
|
34
|
-
}
|
|
27
|
+
this.subscribe(Mode.PIX_0);
|
|
28
|
+
const colorArray = new Array(9);
|
|
29
|
+
for (let i = 0; i < colorArray.length; i++) {
|
|
30
|
+
if (typeof colors === 'number') {
|
|
35
31
|
// @ts-ignore
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
32
|
+
colorArray[i] = colors + (10 << 4);
|
|
33
|
+
}
|
|
34
|
+
// @ts-ignore
|
|
35
|
+
if (colors[i] instanceof Color) {
|
|
40
36
|
// @ts-ignore
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
37
|
+
colorArray[i] = colors[i].toValue();
|
|
38
|
+
}
|
|
39
|
+
// @ts-ignore
|
|
40
|
+
if (colors[i] === Consts.Color.NONE) {
|
|
41
|
+
colorArray[i] = Consts.Color.NONE;
|
|
42
|
+
}
|
|
43
|
+
// @ts-ignore
|
|
44
|
+
if (colors[i] <= 10) {
|
|
44
45
|
// @ts-ignore
|
|
45
|
-
|
|
46
|
-
// @ts-ignore
|
|
47
|
-
colorArray[i] = colors[i] + (10 << 4); // If a raw color value, set it to max brightness (10)
|
|
48
|
-
}
|
|
46
|
+
colorArray[i] = colors[i] + (10 << 4); // If a raw color value, set it to max brightness (10)
|
|
49
47
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
});
|
|
48
|
+
}
|
|
49
|
+
return this.writeDirect(Mode.PIX_0, Buffer.from(colorArray));
|
|
53
50
|
}
|
|
54
51
|
|
|
55
52
|
|