From df70d8a8a4bdbb664fc4777173a88b226afb45e4 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 16 Dec 2019 21:19:56 -0800 Subject: [PATCH] Renamed devices --- src/devices/colordistancesensor.ts | 22 +++++++++++++--------- src/devices/motionsensor.ts | 10 ++++++++-- src/devices/tachomotor.ts | 12 +++++++++--- src/devices/tiltsensor.ts | 10 ++++++++-- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/devices/colordistancesensor.ts b/src/devices/colordistancesensor.ts index 7589759..84018e6 100644 --- a/src/devices/colordistancesensor.ts +++ b/src/devices/colordistancesensor.ts @@ -16,17 +16,13 @@ export class ColorDistanceSensor extends Device { if (this.autoSubscribe) { switch (event) { case "color": - if (this._isWeDo2) { - this.subscribe(0x00); - } else { - this.subscribe(0x08); - } + this.subscribe(ColorDistanceSensor.Mode.COLOR); break; case "distance": - this.subscribe(0x08); + this.subscribe(ColorDistanceSensor.Mode.DISTANCE); break; case "colorAndDistance": - this.subscribe(0x08); + this.subscribe(ColorDistanceSensor.Mode.COLOR_AND_DISTANCE); break; } } @@ -39,13 +35,13 @@ export class ColorDistanceSensor extends Device { console.log(message); switch (mode) { - case 0x00: + case ColorDistanceSensor.Mode.COLOR: if (this._isWeDo2 && message[2] <= 10) { const color = message[2]; this.emit("color", color); } break; - case 0x08: + case ColorDistanceSensor.Mode.COLOR_AND_DISTANCE: /** * Emits when a color sensor is activated. * @event ColorDistanceSensor#color @@ -90,3 +86,11 @@ export class ColorDistanceSensor extends Device { } } + +export namespace ColorDistanceSensor { + export enum Mode { + COLOR = 0x00, + DISTANCE = 0x01, + COLOR_AND_DISTANCE = 0x08 + } +} \ No newline at end of file diff --git a/src/devices/motionsensor.ts b/src/devices/motionsensor.ts index 0bb1717..0c44e57 100644 --- a/src/devices/motionsensor.ts +++ b/src/devices/motionsensor.ts @@ -13,7 +13,7 @@ export class MotionSensor extends Device { if (this.autoSubscribe) { switch (event) { case "distance": - this.subscribe(0x00); + this.subscribe(MotionSensor.Mode.DISTANCE); break; } } @@ -25,7 +25,7 @@ export class MotionSensor extends Device { const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); switch (mode) { - case 0x00: + case MotionSensor.Mode.DISTANCE: let distance = message[isWeDo2 ? 2 : 4]; if (message[isWeDo2 ? 3 : 5] === 1) { distance = distance + 255; @@ -41,3 +41,9 @@ export class MotionSensor extends Device { } } + +export namespace MotionSensor { + export enum Mode { + DISTANCE = 0x00 + } +} \ No newline at end of file diff --git a/src/devices/tachomotor.ts b/src/devices/tachomotor.ts index cae01c6..72a6f00 100644 --- a/src/devices/tachomotor.ts +++ b/src/devices/tachomotor.ts @@ -14,7 +14,7 @@ export class TachoMotor extends BasicMotor { if (this.autoSubscribe) { switch (event) { case "rotate": - this.subscribe(0x02); + this.subscribe(TachoMotor.Mode.ROTATION); break; } } @@ -26,7 +26,7 @@ export class TachoMotor extends BasicMotor { const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); switch (mode) { - case 0x02: + case TachoMotor.Mode.ROTATION: const rotation = message.readInt32LE(isWeDo2 ? 2 : 4); /** * Emits when a rotation sensor is activated. @@ -48,7 +48,7 @@ export class TachoMotor extends BasicMotor { public rotateByAngle (angle: number, power: number = 100) { const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); if (isWeDo2) { - throw new Error("Rotating by angle is not available on the WeDo 2.0 Smart Hub"); + throw new Error("Angle rotation is not available on the WeDo 2.0 Smart Hub"); } return new Promise((resolve) => { this._busy = true; @@ -62,3 +62,9 @@ export class TachoMotor extends BasicMotor { } } + +export namespace TachoMotor { + export enum Mode { + ROTATION = 0x02 + } +} \ No newline at end of file diff --git a/src/devices/tiltsensor.ts b/src/devices/tiltsensor.ts index a0e7676..4d5a964 100644 --- a/src/devices/tiltsensor.ts +++ b/src/devices/tiltsensor.ts @@ -13,7 +13,7 @@ export class TiltSensor extends Device { if (this.autoSubscribe) { switch (event) { case "tilt": - this.subscribe(0x00); + this.subscribe(TiltSensor.Mode.TILT); break; } } @@ -25,7 +25,7 @@ export class TiltSensor extends Device { const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); switch (mode) { - case 0x00: + case TiltSensor.Mode.TILT: const tiltX = message.readInt8(isWeDo2 ? 2 : 4); const tiltY = message.readInt8(isWeDo2 ? 3 : 5); /** @@ -40,3 +40,9 @@ export class TiltSensor extends Device { } } + +export namespace TiltSensor { + export enum Mode { + TILT = 0x00 + } +} \ No newline at end of file