From 28fd0b557885be5e368cbf7cf724719cb6ea5340 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 24 Feb 2020 21:43:52 -0800 Subject: [PATCH] Added support for Duplo color sensor RGB values, reflectivity, and Duplo train system sounds --- src/devices/duplotrainbasecolorsensor.ts | 36 ++++++++++++++++++++++-- src/devices/duplotrainbasespeaker.ts | 8 +++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/devices/duplotrainbasecolorsensor.ts b/src/devices/duplotrainbasecolorsensor.ts index ddc2737..4018365 100644 --- a/src/devices/duplotrainbasecolorsensor.ts +++ b/src/devices/duplotrainbasecolorsensor.ts @@ -32,15 +32,47 @@ export class DuploTrainBaseColorSensor extends Device { } break; + case Mode.REFLECTIVITY: + const reflect = message[4]; + + /** + * Emits when the light reflectivity changes. + * @event DuploTrainBaseColorSensor#reflect + * @type {object} + * @param {number} reflect Percentage, from 0 to 100. + */ + this.notify("reflect", { reflect }); + break; + + case Mode.RGB: + const red = Math.floor(message.readUInt16LE(4) / 4); + const green = Math.floor(message.readUInt16LE(6) / 4); + const blue = Math.floor(message.readUInt16LE(8) / 4); + + /** + * Emits when the light reflectivity changes. + * @event DuploTrainBaseColorSensor#rgb + * @type {object} + * @param {number} red + * @param {number} green + * @param {number} blue + */ + this.notify("rgb", { red, green, blue }); + break; + } } } export enum Mode { - COLOR = 0x00 + COLOR = 0x00, + REFLECTIVITY = 0x02, + RGB = 0x03 } export const ModeMap: {[event: string]: number} = { - "color": Mode.COLOR + "color": Mode.COLOR, + "reflect": Mode.REFLECTIVITY, + "rgb": Mode.RGB }; diff --git a/src/devices/duplotrainbasespeaker.ts b/src/devices/duplotrainbasespeaker.ts index 0e52537..d2a67de 100644 --- a/src/devices/duplotrainbasespeaker.ts +++ b/src/devices/duplotrainbasespeaker.ts @@ -28,8 +28,14 @@ export class DuploTrainBaseSpeaker extends Device { }); } + public playTone (tone: number) { + this.subscribe(Mode.TONE); + this.writeDirect(0x02, Buffer.from([tone])); + } + } export enum Mode { - SOUND = 0x01 + SOUND = 0x01, + TONE = 0x02 }