From 1a5992408b183fd1db71b4dec13b7dd0c5b78863 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Tue, 24 Oct 2017 23:10:27 -0700 Subject: [PATCH] added event for color changes --- libs/core/_locales/core-jsdoc-strings.json | 3 ++ libs/core/_locales/core-strings.json | 9 +++++ libs/core/color.ts | 39 ++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 1f6d96c3..d6881d47 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -26,6 +26,9 @@ "input.Button.wasPressed": "See if the button was pressed again since the last time you checked.", "input.ColorSensor.ambientLight": "Get current ambient light value from the color sensor.", "input.ColorSensor.color": "Get the current color from the color sensor.", + "input.ColorSensor.onColorDetected": "Registers code to run when the given color is detected", + "input.ColorSensor.onColorDetected|param|color": "the color to dtect", + "input.ColorSensor.onColorDetected|param|handler": "the code to run when detected", "input.ColorSensor.reflectedLight": "Get current reflected light value from the color sensor.", "input.GyroSensor.angle": "Get the current angle from the gyroscope.", "input.GyroSensor.rate": "Get the current rotation rate from the gyroscope.", diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index 7bb52492..10c9dfb9 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -2,6 +2,14 @@ "ButtonEvent.Click|block": "click", "ButtonEvent.Down|block": "down", "ButtonEvent.Up|block": "up", + "ColorSensorColor.Black|block": "black", + "ColorSensorColor.Blue|block": "blue", + "ColorSensorColor.Brown|block": "brown", + "ColorSensorColor.Green|block": "green", + "ColorSensorColor.None|block": "none", + "ColorSensorColor.Red|block": "red", + "ColorSensorColor.White|block": "white", + "ColorSensorColor.Yellow|block": "yellow", "LightsPattern.GreenFlash|block": "Flashing Green", "LightsPattern.GreenPulse|block": "Pulsing Green", "LightsPattern.Green|block": "Green", @@ -27,6 +35,7 @@ "input.Button.wasPressed|block": "%button|was pressed", "input.ColorSensor.ambientLight|block": "%color| ambient light", "input.ColorSensor.color|block": "%color| color", + "input.ColorSensor.onColorDetected|block": "on %sensor|detected %color", "input.ColorSensor.reflectedLight|block": "%color| reflected light", "input.GyroSensor.angle|block": "%sensor|angle", "input.GyroSensor.rate|block": "%sensor|rotation rate", diff --git a/libs/core/color.ts b/libs/core/color.ts index a7051e11..84706d2f 100644 --- a/libs/core/color.ts +++ b/libs/core/color.ts @@ -9,13 +9,21 @@ const enum ColorSensorMode { } const enum ColorSensorColor { + //% block="none" None, + //% block="black" Black, + //% block="blue" Blue, + //% block="green" Green, + //% block="yellow" Yellow, + //% block="red" Red, + //% block="white" White, + //% block="brown" Brown, } @@ -23,8 +31,11 @@ namespace input { //% fixedInstances export class ColorSensor extends internal.UartSensor { + polling: boolean; + constructor(port: number) { super(port) + this.polling = false; } _deviceType() { @@ -35,6 +46,34 @@ namespace input { this._setMode(m) } + _initPolling() { + if (!this.polling) { + input.internal.unsafePollForChanges( + 50, + () => this.color(), + (prev, curr) => { + control.raiseEvent(this._id, curr); + }) + } + } + + /** + * Registers code to run when the given color is detected + * @param color the color to dtect + * @param handler the code to run when detected + */ + //% help=input/color/on-color-detected + //% block="on %sensor|detected %color" + //% blockId=colorOnColorDetected + //% parts="colorsensor" + //% blockNamespace=input + //% weight=100 blockGap=8 + //% group="Color Sensor" + onColorDetected(color: ColorSensorColor, handler: () => void) { + this._initPolling(); + control.onEvent(this._id, color, handler); + } + /** * Get current ambient light value from the color sensor. * @param color the color sensor to query the request