Duplo motors and sensors
This commit is contained in:
parent
705fbd341a
commit
bc644c85b2
@ -67,17 +67,17 @@ export enum DeviceType {
|
||||
MOVE_HUB_TILT_SENSOR = 40,
|
||||
DUPLO_TRAIN_BASE_MOTOR = 41,
|
||||
DUPLO_TRAIN_BASE_SPEAKER = 42,
|
||||
DUPLO_TRAIN_BASE_COLOR = 43,
|
||||
DUPLO_TRAIN_BASE_COLOR_SENSOR = 43,
|
||||
DUPLO_TRAIN_BASE_SPEEDOMETER = 44,
|
||||
TECHNIC_LARGE_LINEAR_MOTOR = 46,
|
||||
TECHNIC_XLARGE_LINEAR_MOTOR = 47,
|
||||
CONTROL_PLUS_GEST = 54,
|
||||
PUP_REMOTE_BUTTON = 55,
|
||||
RSSI = 56,
|
||||
TECHNIC_MEDIUM_HUB_GEST_SENSOR = 54,
|
||||
REMOTE_CONTROL_BUTTON = 55,
|
||||
REMOTE_CONTROL_RSSI = 56,
|
||||
TECHNIC_MEDIUM_HUB_ACCELEROMETER = 57,
|
||||
TECHNIC_MEDIUM_HUB_GYRO_SENSOR = 58,
|
||||
TECHNIC_MEDIUM_HUB_TILT_SENSOR = 59,
|
||||
TEMPERATURE = 60,
|
||||
TECHNIC_MEDIUM_HUB_TEMPERATURE_SENSOR = 60,
|
||||
}
|
||||
|
||||
|
||||
|
45
src/devices/duplotrainbasecolorsensor.ts
Normal file
45
src/devices/duplotrainbasecolorsensor.ts
Normal file
@ -0,0 +1,45 @@
|
||||
import { Device } from "./device";
|
||||
|
||||
import { IDeviceInterface } from "../interfaces";
|
||||
|
||||
import * as Consts from "../consts";
|
||||
|
||||
export class DuploTrainBaseColorSensor extends Device {
|
||||
|
||||
constructor (hub: IDeviceInterface, portId: number) {
|
||||
super(hub, portId, DuploTrainBaseColorSensor.ModeMap, Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR_SENSOR);
|
||||
}
|
||||
|
||||
public receive (message: Buffer) {
|
||||
const mode = this._mode;
|
||||
|
||||
switch (mode) {
|
||||
case DuploTrainBaseColorSensor.Mode.COLOR:
|
||||
if (message[4] <= 10) {
|
||||
const color = message[4];
|
||||
|
||||
/**
|
||||
* Emits when a color sensor is activated.
|
||||
* @event DuploTrainBaseColorSensor#color
|
||||
* @param {Color} color
|
||||
*/
|
||||
this.emit("color", color);
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export namespace DuploTrainBaseColorSensor {
|
||||
|
||||
export enum Mode {
|
||||
COLOR = 0x00
|
||||
}
|
||||
|
||||
export const ModeMap: {[event: string]: number} = {
|
||||
"color": DuploTrainBaseColorSensor.Mode.COLOR
|
||||
}
|
||||
|
||||
}
|
13
src/devices/duplotrainbasemotor.ts
Normal file
13
src/devices/duplotrainbasemotor.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { BasicMotor } from "./basicmotor";
|
||||
|
||||
import { IDeviceInterface } from "../interfaces";
|
||||
|
||||
import * as Consts from "../consts";
|
||||
|
||||
export class DuploTrainBaseMotor extends BasicMotor {
|
||||
|
||||
constructor (hub: IDeviceInterface, portId: number) {
|
||||
super(hub, portId, {}, Consts.DeviceType.DUPLO_TRAIN_BASE_MOTOR);
|
||||
}
|
||||
|
||||
}
|
38
src/devices/duplotrainbasespeaker.ts
Normal file
38
src/devices/duplotrainbasespeaker.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import { Device } from "./device";
|
||||
|
||||
import { IDeviceInterface } from "../interfaces";
|
||||
|
||||
import * as Consts from "../consts";
|
||||
|
||||
export class DuploTrainBaseSpeaker extends Device {
|
||||
|
||||
|
||||
constructor (hub: IDeviceInterface, portId: number) {
|
||||
super(hub, portId, {}, Consts.DeviceType.DUPLO_TRAIN_BASE_SPEAKER);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Play a built-in train sound.
|
||||
* @method DuploTrainBaseSpeaker#playSound
|
||||
* @param {DuploTrainBaseSound} sound
|
||||
* @returns {Promise} Resolved upon successful issuance of command.
|
||||
*/
|
||||
public playSound (sound: Consts.DuploTrainBaseSound) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.subscribe(DuploTrainBaseSpeaker.Mode.SOUND);
|
||||
this.writeDirect(0x01, Buffer.from([sound]));
|
||||
return resolve();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
export namespace DuploTrainBaseSpeaker {
|
||||
|
||||
export enum Mode {
|
||||
SOUND = 0x01
|
||||
}
|
||||
|
||||
}
|
43
src/devices/duplotrainbasespeedometer.ts
Normal file
43
src/devices/duplotrainbasespeedometer.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import { Device } from "./device";
|
||||
|
||||
import { IDeviceInterface } from "../interfaces";
|
||||
|
||||
import * as Consts from "../consts";
|
||||
|
||||
export class DuploTrainBaseSpeedometer extends Device {
|
||||
|
||||
constructor (hub: IDeviceInterface, portId: number) {
|
||||
super(hub, portId, DuploTrainBaseSpeedometer.ModeMap, Consts.DeviceType.DUPLO_TRAIN_BASE_SPEEDOMETER);
|
||||
}
|
||||
|
||||
public receive (message: Buffer) {
|
||||
const mode = this._mode;
|
||||
|
||||
switch (mode) {
|
||||
case DuploTrainBaseSpeedometer.Mode.SPEED:
|
||||
const speed = message.readInt16LE(4);
|
||||
|
||||
/**
|
||||
* Emits on a speed change.
|
||||
* @event DuploTrainBaseSpeedometer#speed
|
||||
* @param {number} speed
|
||||
*/
|
||||
this.emit("speed", speed);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export namespace DuploTrainBaseSpeedometer {
|
||||
|
||||
export enum Mode {
|
||||
SPEED = 0x00
|
||||
}
|
||||
|
||||
export const ModeMap: {[event: string]: number} = {
|
||||
"speed": DuploTrainBaseSpeedometer.Mode.SPEED
|
||||
}
|
||||
|
||||
}
|
@ -20,12 +20,10 @@ export class HubLED extends Device {
|
||||
*/
|
||||
public setColor (color: number | boolean) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.mode !== HubLED.Mode.COLOR) {
|
||||
this.subscribe(HubLED.Mode.COLOR);
|
||||
}
|
||||
if (typeof color === "boolean") {
|
||||
color = 0;
|
||||
}
|
||||
this.subscribe(HubLED.Mode.COLOR);
|
||||
this.writeDirect(0x00, Buffer.from([color]));
|
||||
return resolve();
|
||||
});
|
||||
@ -42,9 +40,7 @@ export class HubLED extends Device {
|
||||
*/
|
||||
public setRGB (red: number, green: number, blue: number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.mode !== HubLED.Mode.RGB) {
|
||||
this.subscribe(HubLED.Mode.RGB);
|
||||
}
|
||||
this.writeDirect(0x00, Buffer.from([red, green, blue]));
|
||||
return resolve();
|
||||
});
|
||||
|
@ -7,7 +7,7 @@ import * as Consts from "../consts";
|
||||
export class RemoteControlButton extends Device {
|
||||
|
||||
constructor (hub: IDeviceInterface, portId: number) {
|
||||
super(hub, portId, RemoteControlButton.ModeMap, Consts.DeviceType.PUP_REMOTE_BUTTON);
|
||||
super(hub, portId, RemoteControlButton.ModeMap, Consts.DeviceType.REMOTE_CONTROL_BUTTON);
|
||||
}
|
||||
|
||||
public receive (message: Buffer) {
|
||||
|
@ -5,6 +5,12 @@ import { IBLEAbstraction } from "../interfaces";
|
||||
import { ColorDistanceSensor } from "../devices/colordistancesensor";
|
||||
import { CurrentSensor } from "../devices/currentsensor";
|
||||
import { Device } from "../devices/device";
|
||||
|
||||
import { DuploTrainBaseColorSensor } from "../devices/duplotrainbasecolorsensor";
|
||||
import { DuploTrainBaseMotor } from "../devices/duplotrainbasemotor";
|
||||
import { DuploTrainBaseSpeaker } from "../devices/duplotrainbasespeaker";
|
||||
import { DuploTrainBaseSpeedometer } from "../devices/duplotrainbasespeedometer";
|
||||
|
||||
import { HubLED } from "../devices/hubled";
|
||||
import { Light } from "../devices/light";
|
||||
import { MediumLinearMotor } from "../devices/mediumlinearmotor";
|
||||
@ -352,12 +358,24 @@ export class BaseHub extends EventEmitter {
|
||||
case Consts.DeviceType.CURRENT_SENSOR:
|
||||
device = new CurrentSensor(this, portId);
|
||||
break;
|
||||
case Consts.DeviceType.PUP_REMOTE_BUTTON:
|
||||
case Consts.DeviceType.REMOTE_CONTROL_BUTTON:
|
||||
device = new RemoteControlButton(this, portId);
|
||||
break;
|
||||
case Consts.DeviceType.HUB_LED:
|
||||
device = new HubLED(this, portId);
|
||||
break;
|
||||
case Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR_SENSOR:
|
||||
device = new DuploTrainBaseColorSensor(this, portId);
|
||||
break;
|
||||
case Consts.DeviceType.DUPLO_TRAIN_BASE_MOTOR:
|
||||
device = new DuploTrainBaseMotor(this, portId);
|
||||
break;
|
||||
case Consts.DeviceType.DUPLO_TRAIN_BASE_SPEAKER:
|
||||
device = new DuploTrainBaseSpeaker(this, portId);
|
||||
break;
|
||||
case Consts.DeviceType.DUPLO_TRAIN_BASE_SPEEDOMETER:
|
||||
device = new DuploTrainBaseSpeedometer(this, portId);
|
||||
break;
|
||||
default:
|
||||
device = new Device(this, portId, undefined, deviceType);
|
||||
break;
|
||||
|
@ -40,28 +40,12 @@ export class DuploTrainBase extends LPF2Hub {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
debug("Connecting to Duplo Train Base");
|
||||
await super.connect();
|
||||
// this.subscribe(0x01, Consts.DeviceType.DUPLO_TRAIN_BASE_SPEAKER, 0x01);
|
||||
debug("Connect completed");
|
||||
return resolve();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Play a built-in train sound.
|
||||
* @method DuploTrainBase#playSound
|
||||
* @param {DuploTrainBaseSound} sound
|
||||
* @returns {Promise} Resolved upon successful issuance of command.
|
||||
*/
|
||||
public playSound (sound: number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const data = Buffer.from([0x81, 0x01, 0x11, 0x51, 0x01, sound]);
|
||||
this.send(data, Consts.BLECharacteristic.LPF2_ALL);
|
||||
return resolve();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
export namespace DuploTrainBase {
|
||||
|
@ -13,6 +13,10 @@ import { WeDo2SmartHub } from "./hubs/wedo2smarthub";
|
||||
import { ColorDistanceSensor } from "./devices/colordistancesensor";
|
||||
import { CurrentSensor } from "./devices/currentsensor";
|
||||
import { Device } from "./devices/device";
|
||||
import { DuploTrainBaseColorSensor } from "./devices/duplotrainbasecolorsensor";
|
||||
import { DuploTrainBaseMotor } from "./devices/duplotrainbasemotor";
|
||||
import { DuploTrainBaseSpeaker } from "./devices/duplotrainbasespeaker";
|
||||
import { DuploTrainBaseSpeedometer } from "./devices/duplotrainbasespeedometer";
|
||||
import { HubLED } from "./devices/hubled";
|
||||
import { Light } from "./devices/light";
|
||||
import { MediumLinearMotor } from "./devices/mediumlinearmotor";
|
||||
@ -44,6 +48,10 @@ window.PoweredUP = {
|
||||
Consts,
|
||||
ColorDistanceSensor,
|
||||
Device,
|
||||
DuploTrainBaseColorSensor,
|
||||
DuploTrainBaseMotor,
|
||||
DuploTrainBaseSpeaker,
|
||||
DuploTrainBaseSpeedometer,
|
||||
HubLED,
|
||||
Light,
|
||||
MediumLinearMotor,
|
||||
|
@ -13,6 +13,10 @@ import { WeDo2SmartHub } from "./hubs/wedo2smarthub";
|
||||
import { ColorDistanceSensor } from "./devices/colordistancesensor";
|
||||
import { CurrentSensor } from "./devices/currentsensor";
|
||||
import { Device } from "./devices/device";
|
||||
import { DuploTrainBaseColorSensor } from "./devices/duplotrainbasecolorsensor";
|
||||
import { DuploTrainBaseMotor } from "./devices/duplotrainbasemotor";
|
||||
import { DuploTrainBaseSpeaker } from "./devices/duplotrainbasespeaker";
|
||||
import { DuploTrainBaseSpeedometer } from "./devices/duplotrainbasespeedometer";
|
||||
import { HubLED } from "./devices/hubled";
|
||||
import { Light } from "./devices/light";
|
||||
import { MediumLinearMotor } from "./devices/mediumlinearmotor";
|
||||
@ -44,6 +48,10 @@ export {
|
||||
Consts,
|
||||
ColorDistanceSensor,
|
||||
Device,
|
||||
DuploTrainBaseColorSensor,
|
||||
DuploTrainBaseMotor,
|
||||
DuploTrainBaseSpeaker,
|
||||
DuploTrainBaseSpeedometer,
|
||||
HubLED,
|
||||
Light,
|
||||
MediumLinearMotor,
|
||||
|
Loading…
x
Reference in New Issue
Block a user