From 4d2b7ced716ca5f7c526b3aebe8dfd1b1417dfc6 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Wed, 29 Nov 2017 21:41:00 -0800 Subject: [PATCH 1/7] moving color sensor to separate project --- libs/color-sensor/README.md | 3 +++ .../_locales/color-sensor-jsdoc-strings.json | 9 +++++++ .../_locales/color-sensor-strings.json | 20 ++++++++++++++ libs/{core => color-sensor}/color.ts | 26 ++++++++++++------- libs/color-sensor/pxt.json | 15 +++++++++++ libs/color-sensor/test.ts | 0 libs/core/_locales/core-jsdoc-strings.json | 6 ----- libs/core/_locales/core-strings.json | 17 ------------ libs/core/pxt.json | 1 - libs/ev3/pxt.json | 1 + pxtarget.json | 1 + 11 files changed, 66 insertions(+), 33 deletions(-) create mode 100644 libs/color-sensor/README.md create mode 100644 libs/color-sensor/_locales/color-sensor-jsdoc-strings.json create mode 100644 libs/color-sensor/_locales/color-sensor-strings.json rename libs/{core => color-sensor}/color.ts (83%) create mode 100644 libs/color-sensor/pxt.json create mode 100644 libs/color-sensor/test.ts diff --git a/libs/color-sensor/README.md b/libs/color-sensor/README.md new file mode 100644 index 00000000..dda9366e --- /dev/null +++ b/libs/color-sensor/README.md @@ -0,0 +1,3 @@ +# Color sensor + +The library to interact with the Touch Sensor. \ No newline at end of file diff --git a/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json b/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json new file mode 100644 index 00000000..f7f09026 --- /dev/null +++ b/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json @@ -0,0 +1,9 @@ +{ + "sensors.ColorSensor.ambientLight": "Get current ambient light value from the color sensor.", + "sensors.ColorSensor.color": "Get the current color from the color sensor.", + "sensors.ColorSensor.colorMode": "Gets the current color mode", + "sensors.ColorSensor.onColorDetected": "Registers code to run when the given color is detected", + "sensors.ColorSensor.onColorDetected|param|color": "the color to detect, eg: ColorSensorColor.Blue", + "sensors.ColorSensor.onColorDetected|param|handler": "the code to run when detected", + "sensors.ColorSensor.reflectedLight": "Get current reflected light value from the color sensor." +} \ No newline at end of file diff --git a/libs/color-sensor/_locales/color-sensor-strings.json b/libs/color-sensor/_locales/color-sensor-strings.json new file mode 100644 index 00000000..5b232c78 --- /dev/null +++ b/libs/color-sensor/_locales/color-sensor-strings.json @@ -0,0 +1,20 @@ +{ + "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", + "sensors.ColorSensor.ambientLight|block": "`icons.colorSensor` %color| ambient light", + "sensors.ColorSensor.color|block": "`icons.colorSensor` %color| color", + "sensors.ColorSensor.onColorDetected|block": "on `icons.colorSensor` %sensor|detected color %color", + "sensors.ColorSensor.reflectedLight|block": "`icons.colorSensor` %color| reflected light", + "sensors.color1|block": "1", + "sensors.color2|block": "2", + "sensors.color3|block": "3", + "sensors.color4|block": "4", + "{id:category}Sensors": "Sensors", + "{id:group}Color Sensor": "Color Sensor" +} \ No newline at end of file diff --git a/libs/core/color.ts b/libs/color-sensor/color.ts similarity index 83% rename from libs/core/color.ts rename to libs/color-sensor/color.ts index 77308481..5c2da498 100644 --- a/libs/core/color.ts +++ b/libs/color-sensor/color.ts @@ -39,10 +39,17 @@ namespace sensors { return DAL.DEVICE_TYPE_COLOR } - setMode(m: ColorSensorMode) { + setColorMode(m: ColorSensorMode) { this._setMode(m) } + /** + * Gets the current color mode + */ + colorMode() { + return this.mode; + } + _query() { if (this.mode == ColorSensorMode.Color) return this.getNumber(NumberFormat.UInt8LE, 0) @@ -50,16 +57,17 @@ namespace sensors { } _update(prev: number, curr: number) { - control.raiseEvent(this._id, curr); + if (this.mode == ColorSensorMode.Color) + control.raiseEvent(this._id, curr); } /** * Registers code to run when the given color is detected - * @param color the color to dtect + * @param color the color to detect, eg: ColorSensorColor.Blue * @param handler the code to run when detected */ //% help=input/color/on-color-detected - //% block="on `icons.colorSensor` %sensor|detected %color" + //% block="on `icons.colorSensor` %sensor|detected color %color" //% blockId=colorOnColorDetected //% parts="colorsensor" //% blockNamespace=sensors @@ -67,9 +75,9 @@ namespace sensors { //% group="Color Sensor" onColorDetected(color: ColorSensorColor, handler: () => void) { control.onEvent(this._id, color, handler); - this.setMode(ColorSensorMode.Color) + this.setColorMode(ColorSensorMode.Color) if (this.color() == color) - control.runInBackground(handler) + control.raiseEvent(this._id, color); } /** @@ -84,7 +92,7 @@ namespace sensors { //% weight=65 blockGap=8 //% group="Color Sensor" ambientLight() { - this.setMode(ColorSensorMode.Ambient) + this.setColorMode(ColorSensorMode.Ambient) return this.getNumber(NumberFormat.UInt8LE, 0) } @@ -100,7 +108,7 @@ namespace sensors { //% weight=64 blockGap=8 //% group="Color Sensor" reflectedLight(): number { - this.setMode(ColorSensorMode.Reflect) + this.setColorMode(ColorSensorMode.Reflect) return this.getNumber(NumberFormat.UInt8LE, 0) } @@ -116,7 +124,7 @@ namespace sensors { //% weight=66 blockGap=8 //% group="Color Sensor" color(): ColorSensorColor { - this.setMode(ColorSensorMode.Color) + this.setColorMode(ColorSensorMode.Color) return this.getNumber(NumberFormat.UInt8LE, 0) } } diff --git a/libs/color-sensor/pxt.json b/libs/color-sensor/pxt.json new file mode 100644 index 00000000..0465015e --- /dev/null +++ b/libs/color-sensor/pxt.json @@ -0,0 +1,15 @@ +{ + "name": "color-sensor", + "description": "Color Sensor support", + "files": [ + "README.md", + "color.ts" + ], + "testFiles": [ + "test.ts" + ], + "public": true, + "dependencies": { + "core": "file:../core" + } +} \ No newline at end of file diff --git a/libs/color-sensor/test.ts b/libs/color-sensor/test.ts new file mode 100644 index 00000000..e69de29b diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 5e9221e1..63126edd 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -68,12 +68,6 @@ "screen.clear": "Clear screen and reset font to normal.", "screen.imageOf": "Makes an image bound to a buffer.", "screen.unpackPNG": "Decompresses a 1-bit gray scale PNG image to image format.", - "sensors.ColorSensor.ambientLight": "Get current ambient light value from the color sensor.", - "sensors.ColorSensor.color": "Get the current color from the color sensor.", - "sensors.ColorSensor.onColorDetected": "Registers code to run when the given color is detected", - "sensors.ColorSensor.onColorDetected|param|color": "the color to dtect", - "sensors.ColorSensor.onColorDetected|param|handler": "the code to run when detected", - "sensors.ColorSensor.reflectedLight": "Get current reflected light value from the color sensor.", "sensors.GyroSensor.angle": "Get the current angle from the gyroscope.", "sensors.GyroSensor.rate": "Get the current rotation rate from the gyroscope.", "sensors.InfraredSensor.on": "Registers code to run when an object is getting near.", diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index 1ec3eb36..ad23c521 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -2,14 +2,6 @@ "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", "InfraredSensorEvent.ObjectDetected|block": "object detected", "InfraredSensorEvent.ObjectNear|block": "object near", "LightsPattern.GreenFlash|block": "Flashing Green", @@ -66,10 +58,6 @@ "motors|block": "motors", "output|block": "output", "screen|block": "screen", - "sensors.ColorSensor.ambientLight|block": "`icons.colorSensor` %color| ambient light", - "sensors.ColorSensor.color|block": "`icons.colorSensor` %color| color", - "sensors.ColorSensor.onColorDetected|block": "on `icons.colorSensor` %sensor|detected %color", - "sensors.ColorSensor.reflectedLight|block": "`icons.colorSensor` %color| reflected light", "sensors.GyroSensor.angle|block": "`icons.gyroSensor` %sensor|angle", "sensors.GyroSensor.rate|block": "`icons.gyroSensor` %sensor|rotation rate", "sensors.InfraredSensor.on|block": "on `icons.infraredSensor` %sensor|%event", @@ -82,10 +70,6 @@ "sensors.UltraSonicSensor.distance|block": "`icons.ultrasonicSensor` %sensor|distance", "sensors.UltraSonicSensor.on|block": "on `icons.ultrasonicSensor` %sensor|%event", "sensors.UltraSonicSensor.wait|block": "wait `icons.ultrasonicSensor` %sensor|for %event", - "sensors.color1|block": "1", - "sensors.color2|block": "2", - "sensors.color3|block": "3", - "sensors.color4|block": "4", "sensors.gyro1|block": "1", "sensors.gyro2|block": "2", "sensors.gyro3|block": "3", @@ -116,7 +100,6 @@ "{id:category}Sensors": "Sensors", "{id:category}Serial": "Serial", "{id:group}Buttons": "Buttons", - "{id:group}Color Sensor": "Color Sensor", "{id:group}Gyro Sensor": "Gyro Sensor", "{id:group}Infrared Sensor": "Infrared Sensor", "{id:group}Light": "Light", diff --git a/libs/core/pxt.json b/libs/core/pxt.json index dbaf051d..de5a8fb6 100644 --- a/libs/core/pxt.json +++ b/libs/core/pxt.json @@ -19,7 +19,6 @@ "core.ts", "input.ts", "ir.ts", - "color.ts", "gyro.ts", "ultrasonic.ts", "shims.d.ts", diff --git a/libs/ev3/pxt.json b/libs/ev3/pxt.json index 67ba5f99..c99726f7 100644 --- a/libs/ev3/pxt.json +++ b/libs/ev3/pxt.json @@ -9,6 +9,7 @@ "base": "file:../base", "core": "file:../core", "music": "file:../music", + "color-sensor": "file:../color-sensor", "touch-sensor": "file:../touch-sensor" }, "public": true diff --git a/pxtarget.json b/pxtarget.json index 9ba18d32..7cc3ec93 100644 --- a/pxtarget.json +++ b/pxtarget.json @@ -10,6 +10,7 @@ "libs/base", "libs/core", "libs/music", + "libs/color-sensor", "libs/touch-sensor", "libs/ev3" ], From 97663d2b83406be7eac2ec27fe4ecb30223e9a76 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Wed, 29 Nov 2017 21:57:05 -0800 Subject: [PATCH 2/7] updated naming --- .../_locales/color-sensor-jsdoc-strings.json | 3 +- .../_locales/color-sensor-strings.json | 12 ++- libs/color-sensor/color.ts | 89 ++++++++++--------- 3 files changed, 58 insertions(+), 46 deletions(-) diff --git a/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json b/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json index f7f09026..6c637a94 100644 --- a/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json +++ b/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json @@ -1,8 +1,9 @@ { + "sensors.ColorSensor": "The color sensor is a digital sensor that can detect the color or intensity\nof light that enters the small window on the face of the sensor.", "sensors.ColorSensor.ambientLight": "Get current ambient light value from the color sensor.", "sensors.ColorSensor.color": "Get the current color from the color sensor.", "sensors.ColorSensor.colorMode": "Gets the current color mode", - "sensors.ColorSensor.onColorDetected": "Registers code to run when the given color is detected", + "sensors.ColorSensor.onColorDetected": "Registers code to run when the given color is detected.", "sensors.ColorSensor.onColorDetected|param|color": "the color to detect, eg: ColorSensorColor.Blue", "sensors.ColorSensor.onColorDetected|param|handler": "the code to run when detected", "sensors.ColorSensor.reflectedLight": "Get current reflected light value from the color sensor." diff --git a/libs/color-sensor/_locales/color-sensor-strings.json b/libs/color-sensor/_locales/color-sensor-strings.json index 5b232c78..a74db2d9 100644 --- a/libs/color-sensor/_locales/color-sensor-strings.json +++ b/libs/color-sensor/_locales/color-sensor-strings.json @@ -7,14 +7,18 @@ "ColorSensorColor.Red|block": "red", "ColorSensorColor.White|block": "white", "ColorSensorColor.Yellow|block": "yellow", + "ColorSensorMode.AmbientLightIntensity|block": "ambient light intensity", + "ColorSensorMode.Color|block": "color", + "ColorSensorMode.ReflectedLightIntensity|block": "reflected light intensity", "sensors.ColorSensor.ambientLight|block": "`icons.colorSensor` %color| ambient light", "sensors.ColorSensor.color|block": "`icons.colorSensor` %color| color", "sensors.ColorSensor.onColorDetected|block": "on `icons.colorSensor` %sensor|detected color %color", "sensors.ColorSensor.reflectedLight|block": "`icons.colorSensor` %color| reflected light", - "sensors.color1|block": "1", - "sensors.color2|block": "2", - "sensors.color3|block": "3", - "sensors.color4|block": "4", + "sensors.colorSensor1|block": "1", + "sensors.colorSensor2|block": "2", + "sensors.colorSensor3|block": "3", + "sensors.colorSensor4|block": "4", + "sensors|block": "sensors", "{id:category}Sensors": "Sensors", "{id:group}Color Sensor": "Color Sensor" } \ No newline at end of file diff --git a/libs/color-sensor/color.ts b/libs/color-sensor/color.ts index 5c2da498..b03f717c 100644 --- a/libs/color-sensor/color.ts +++ b/libs/color-sensor/color.ts @@ -1,7 +1,10 @@ const enum ColorSensorMode { None = -1, - Reflect = 0, - Ambient = 1, + //% block="reflected light intensity" + ReflectedLightIntensity = 0, + //% block="ambient light intensity" + AmbientLightIntensity = 1, + //% block="color" Color = 2, RefRaw = 3, RgbRaw = 4, @@ -29,6 +32,10 @@ const enum ColorSensorColor { namespace sensors { + /** + * The color sensor is a digital sensor that can detect the color or intensity + * of light that enters the small window on the face of the sensor. + */ //% fixedInstances export class ColorSensor extends internal.UartSensor { constructor(port: number) { @@ -62,11 +69,11 @@ namespace sensors { } /** - * Registers code to run when the given color is detected + * Registers code to run when the given color is detected. * @param color the color to detect, eg: ColorSensorColor.Blue * @param handler the code to run when detected */ - //% help=input/color/on-color-detected + //% help=sensors/color-sensor/on-color-detected //% block="on `icons.colorSensor` %sensor|detected color %color" //% blockId=colorOnColorDetected //% parts="colorsensor" @@ -80,43 +87,11 @@ namespace sensors { control.raiseEvent(this._id, color); } - /** - * Get current ambient light value from the color sensor. - * @param color the color sensor to query the request - */ - //% help=input/color/ambient-light - //% block="`icons.colorSensor` %color| ambient light" - //% blockId=colorGetAmbient - //% parts="colorsensor" - //% blockNamespace=sensors - //% weight=65 blockGap=8 - //% group="Color Sensor" - ambientLight() { - this.setColorMode(ColorSensorMode.Ambient) - return this.getNumber(NumberFormat.UInt8LE, 0) - } - - /** - * Get current reflected light value from the color sensor. - * @param color the color sensor to query the request - */ - //% help=input/color/refelected-light - //% block="`icons.colorSensor` %color| reflected light" - //% blockId=colorGetReflected - //% parts="colorsensor" - //% blockNamespace=sensors - //% weight=64 blockGap=8 - //% group="Color Sensor" - reflectedLight(): number { - this.setColorMode(ColorSensorMode.Reflect) - return this.getNumber(NumberFormat.UInt8LE, 0) - } - /** * Get the current color from the color sensor. * @param color the color sensor to query the request */ - //% help=input/color/color + //% help=sensors/color-sensor/color //% block="`icons.colorSensor` %color| color" //% blockId=colorGetColor //% parts="colorsensor" @@ -127,17 +102,49 @@ namespace sensors { this.setColorMode(ColorSensorMode.Color) return this.getNumber(NumberFormat.UInt8LE, 0) } + + /** + * Get current ambient light value from the color sensor. + * @param color the color sensor to query the request + */ + //% help=sensors/color-sensor/ambient-light + //% block="`icons.colorSensor` %color| ambient light" + //% blockId=colorGetAmbient + //% parts="colorsensor" + //% blockNamespace=sensors + //% weight=65 blockGap=8 + //% group="Color Sensor" + ambientLight() { + this.setColorMode(ColorSensorMode.AmbientLightIntensity) + return this.getNumber(NumberFormat.UInt8LE, 0) + } + + /** + * Get current reflected light value from the color sensor. + * @param color the color sensor to query the request + */ + //% help=sensors/color-sensor/reflected-light + //% block="`icons.colorSensor` %color| reflected light" + //% blockId=colorGetReflected + //% parts="colorsensor" + //% blockNamespace=sensors + //% weight=64 blockGap=8 + //% group="Color Sensor" + reflectedLight(): number { + this.setColorMode(ColorSensorMode.ReflectedLightIntensity) + return this.getNumber(NumberFormat.UInt8LE, 0) + } } //% whenUsed block="1" weight=95 fixedInstance - export const color1: ColorSensor = new ColorSensor(1) + export const colorSensor1: ColorSensor = new ColorSensor(1) //% whenUsed block="3" weight=90 fixedInstance - export const color3: ColorSensor = new ColorSensor(3) + export const colorSensor3: ColorSensor = new ColorSensor(3) //% whenUsed block="2" weight=90 fixedInstance - export const color2: ColorSensor = new ColorSensor(2) + export const colorSensor2: ColorSensor = new ColorSensor(2) //% whenUsed block="4" weight=90 fixedInstance - export const color4: ColorSensor = new ColorSensor(4) + export const colorSensor4: ColorSensor = new ColorSensor(4) } From 3690f409b0ae20a706843b31b4bac65fbe6ae430 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Wed, 29 Nov 2017 22:09:12 -0800 Subject: [PATCH 3/7] docs skeleton --- docs/reference.md | 3 ++- .../docs/reference/sensors/color-sensor.md | 16 ++++++++++++++++ .../sensors/color-sensor/ambient-light.md | 11 +++++++++++ .../docs/reference/sensors/color-sensor/color.md | 11 +++++++++++ .../sensors/color-sensor/on-color-detected.md | 16 ++++++++++++++++ .../sensors/color-sensor/reflected-light.md | 11 +++++++++++ 6 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 libs/color-sensor/docs/reference/sensors/color-sensor.md create mode 100644 libs/color-sensor/docs/reference/sensors/color-sensor/ambient-light.md create mode 100644 libs/color-sensor/docs/reference/sensors/color-sensor/color.md create mode 100644 libs/color-sensor/docs/reference/sensors/color-sensor/on-color-detected.md create mode 100644 libs/color-sensor/docs/reference/sensors/color-sensor/reflected-light.md diff --git a/docs/reference.md b/docs/reference.md index 70f479f4..110f23ff 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -4,4 +4,5 @@ ## See Also -[touch sensor](/reference/sensors/touch-sensor) \ No newline at end of file +[touch sensor](/reference/sensors/touch-sensor), +[color sensor](/reference/sensors/color-sensor) \ No newline at end of file diff --git a/libs/color-sensor/docs/reference/sensors/color-sensor.md b/libs/color-sensor/docs/reference/sensors/color-sensor.md new file mode 100644 index 00000000..a47b1cc9 --- /dev/null +++ b/libs/color-sensor/docs/reference/sensors/color-sensor.md @@ -0,0 +1,16 @@ +# Color Sensor + +```cards +sensors.colorSensor1.onColorDetected(ColorSensorColor.Blue, function () { +}) +sensors.colorSensor1.color(); +sensors.colorSensor1.ambientLight(); +sensors.colorSensor1.reflectedLight(); +``` + +## See Also + +[on color detected](/reference/sensors/color-sensor/on-color-detected), +[color](/reference/sensors/color-sensor/color), +[ambient light](/reference/sensors/color-sensor/ambient-light), +[reflected light](/reference/sensors/color-sensor/reflected-light), diff --git a/libs/color-sensor/docs/reference/sensors/color-sensor/ambient-light.md b/libs/color-sensor/docs/reference/sensors/color-sensor/ambient-light.md new file mode 100644 index 00000000..5e82ac7e --- /dev/null +++ b/libs/color-sensor/docs/reference/sensors/color-sensor/ambient-light.md @@ -0,0 +1,11 @@ +# Ambient Light + +```blocks +loops.forever(function () { + if (sensors.colorSensor1.ambientLight() > 20) { + brick.setStatusLight(LightsPattern.Green) + } else { + brick.setStatusLight(LightsPattern.Orange) + } +}) +``` \ No newline at end of file diff --git a/libs/color-sensor/docs/reference/sensors/color-sensor/color.md b/libs/color-sensor/docs/reference/sensors/color-sensor/color.md new file mode 100644 index 00000000..7bc8f3de --- /dev/null +++ b/libs/color-sensor/docs/reference/sensors/color-sensor/color.md @@ -0,0 +1,11 @@ +# color + +```blocks +loops.forever(function () { + if (sensors.colorSensor1.color() == ColorSensorColor.Green) { + brick.setStatusLight(LightsPattern.Green) + } else { + brick.setStatusLight(LightsPattern.Orange) + } +}) +``` \ No newline at end of file diff --git a/libs/color-sensor/docs/reference/sensors/color-sensor/on-color-detected.md b/libs/color-sensor/docs/reference/sensors/color-sensor/on-color-detected.md new file mode 100644 index 00000000..d69e5ff5 --- /dev/null +++ b/libs/color-sensor/docs/reference/sensors/color-sensor/on-color-detected.md @@ -0,0 +1,16 @@ +# On Color Detected + +```sig +sensors.colorSensor1.onColorDetected(ColorSensorColor.Blue, function () { }) +``` + +# Parameters + +## Examples + + +```blocks +sensors.colorSensor1.onColorDetected(ColorSensorColor.Blue, function () { + brick.showImage(images.expressionsSick) +}) +``` diff --git a/libs/color-sensor/docs/reference/sensors/color-sensor/reflected-light.md b/libs/color-sensor/docs/reference/sensors/color-sensor/reflected-light.md new file mode 100644 index 00000000..fec938b1 --- /dev/null +++ b/libs/color-sensor/docs/reference/sensors/color-sensor/reflected-light.md @@ -0,0 +1,11 @@ +# Reflected Light + +```blocks +loops.forever(function () { + if (sensors.colorSensor1.reflectedLight() > 20) { + brick.setStatusLight(LightsPattern.Green) + } else { + brick.setStatusLight(LightsPattern.Orange) + } +}) +``` \ No newline at end of file From 4a8633f5069696c7ac1b8f076b1a515f490ab137 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Wed, 29 Nov 2017 22:11:59 -0800 Subject: [PATCH 4/7] updated descriptions --- .../color-sensor/_locales/color-sensor-jsdoc-strings.json | 4 ++-- libs/color-sensor/color.ts | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json b/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json index 6c637a94..bcd25d8f 100644 --- a/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json +++ b/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json @@ -1,10 +1,10 @@ { "sensors.ColorSensor": "The color sensor is a digital sensor that can detect the color or intensity\nof light that enters the small window on the face of the sensor.", - "sensors.ColorSensor.ambientLight": "Get current ambient light value from the color sensor.", + "sensors.ColorSensor.ambientLight": "Measures the ambient light value from 0 (darkest) to 100 (brightest).", "sensors.ColorSensor.color": "Get the current color from the color sensor.", "sensors.ColorSensor.colorMode": "Gets the current color mode", "sensors.ColorSensor.onColorDetected": "Registers code to run when the given color is detected.", "sensors.ColorSensor.onColorDetected|param|color": "the color to detect, eg: ColorSensorColor.Blue", "sensors.ColorSensor.onColorDetected|param|handler": "the code to run when detected", - "sensors.ColorSensor.reflectedLight": "Get current reflected light value from the color sensor." + "sensors.ColorSensor.reflectedLight": "Measures the reflected light value from 0 (darkest) to 100 (brightest)." } \ No newline at end of file diff --git a/libs/color-sensor/color.ts b/libs/color-sensor/color.ts index b03f717c..8f4b143c 100644 --- a/libs/color-sensor/color.ts +++ b/libs/color-sensor/color.ts @@ -104,8 +104,8 @@ namespace sensors { } /** - * Get current ambient light value from the color sensor. - * @param color the color sensor to query the request + * Measures the ambient light value from 0 (darkest) to 100 (brightest). + * @param color the color sensor port */ //% help=sensors/color-sensor/ambient-light //% block="`icons.colorSensor` %color| ambient light" @@ -120,8 +120,8 @@ namespace sensors { } /** - * Get current reflected light value from the color sensor. - * @param color the color sensor to query the request + * Measures the reflected light value from 0 (darkest) to 100 (brightest). + * @param color the color sensor port */ //% help=sensors/color-sensor/reflected-light //% block="`icons.colorSensor` %color| reflected light" From ad17191ae2c4ce5752a455b9c32118a3cffe5ef5 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Wed, 29 Nov 2017 22:47:17 -0800 Subject: [PATCH 5/7] collapse reflected/ambient blocks --- .../_locales/color-sensor-jsdoc-strings.json | 6 +- .../_locales/color-sensor-strings.json | 7 +- libs/color-sensor/color.ts | 82 +++++++++++++------ libs/core/input.ts | 74 +++++++++++++++++ 4 files changed, 139 insertions(+), 30 deletions(-) diff --git a/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json b/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json index bcd25d8f..3b3ab874 100644 --- a/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json +++ b/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json @@ -1,10 +1,12 @@ { "sensors.ColorSensor": "The color sensor is a digital sensor that can detect the color or intensity\nof light that enters the small window on the face of the sensor.", - "sensors.ColorSensor.ambientLight": "Measures the ambient light value from 0 (darkest) to 100 (brightest).", "sensors.ColorSensor.color": "Get the current color from the color sensor.", "sensors.ColorSensor.colorMode": "Gets the current color mode", + "sensors.ColorSensor.light": "Measures the ambient or reflected light value from 0 (darkest) to 100 (brightest).", "sensors.ColorSensor.onColorDetected": "Registers code to run when the given color is detected.", "sensors.ColorSensor.onColorDetected|param|color": "the color to detect, eg: ColorSensorColor.Blue", "sensors.ColorSensor.onColorDetected|param|handler": "the code to run when detected", - "sensors.ColorSensor.reflectedLight": "Measures the reflected light value from 0 (darkest) to 100 (brightest)." + "sensors.ColorSensor.onLightChanged": "Registers code to run when the ambient light changes.", + "sensors.ColorSensor.onLightChanged|param|condition": "the light condition", + "sensors.ColorSensor.onLightChanged|param|handler": "the code to run when detected" } \ No newline at end of file diff --git a/libs/color-sensor/_locales/color-sensor-strings.json b/libs/color-sensor/_locales/color-sensor-strings.json index a74db2d9..3211eddc 100644 --- a/libs/color-sensor/_locales/color-sensor-strings.json +++ b/libs/color-sensor/_locales/color-sensor-strings.json @@ -10,10 +10,13 @@ "ColorSensorMode.AmbientLightIntensity|block": "ambient light intensity", "ColorSensorMode.Color|block": "color", "ColorSensorMode.ReflectedLightIntensity|block": "reflected light intensity", - "sensors.ColorSensor.ambientLight|block": "`icons.colorSensor` %color| ambient light", + "LightCondition.Dark|block": "dark", + "LightIntensityMode.Ambient|block": "ambient light", + "LightIntensityMode.Reflected|block": "reflected light", "sensors.ColorSensor.color|block": "`icons.colorSensor` %color| color", + "sensors.ColorSensor.light|block": "`icons.colorSensor` %color|%mode", "sensors.ColorSensor.onColorDetected|block": "on `icons.colorSensor` %sensor|detected color %color", - "sensors.ColorSensor.reflectedLight|block": "`icons.colorSensor` %color| reflected light", + "sensors.ColorSensor.onLightChanged|block": "on `icons.colorSensor` %sensor|%mode|%condition", "sensors.colorSensor1|block": "1", "sensors.colorSensor2|block": "2", "sensors.colorSensor3|block": "3", diff --git a/libs/color-sensor/color.ts b/libs/color-sensor/color.ts index 8f4b143c..c6f0bdb5 100644 --- a/libs/color-sensor/color.ts +++ b/libs/color-sensor/color.ts @@ -11,6 +11,13 @@ const enum ColorSensorMode { ColorCal = 5, } +enum LightIntensityMode { + //% block="reflected light" + Reflected = ColorSensorMode.ReflectedLightIntensity, + //% block="ambient light" + Ambient = ColorSensorMode.AmbientLightIntensity +} + const enum ColorSensorColor { //% block="none" None, @@ -30,6 +37,13 @@ const enum ColorSensorColor { Brown, } +enum LightCondition { + //% block="dark" + Dark = sensors.internal.ThresholdState.Low, + //$ block="bright" + Bright = sensors.internal.ThresholdState.High +} + namespace sensors { /** @@ -38,15 +52,18 @@ namespace sensors { */ //% fixedInstances export class ColorSensor extends internal.UartSensor { + thresholdDetector: sensors.internal.ThresholdDetector; + constructor(port: number) { super(port) + this.thresholdDetector = new sensors.internal.ThresholdDetector(this.id()); } _deviceType() { return DAL.DEVICE_TYPE_COLOR } - setColorMode(m: ColorSensorMode) { + setMode(m: ColorSensorMode) { this._setMode(m) } @@ -66,6 +83,8 @@ namespace sensors { _update(prev: number, curr: number) { if (this.mode == ColorSensorMode.Color) control.raiseEvent(this._id, curr); + else + this.thresholdDetector.setLevel(curr); } /** @@ -82,7 +101,7 @@ namespace sensors { //% group="Color Sensor" onColorDetected(color: ColorSensorColor, handler: () => void) { control.onEvent(this._id, color, handler); - this.setColorMode(ColorSensorMode.Color) + this.setMode(ColorSensorMode.Color) if (this.color() == color) control.raiseEvent(this._id, color); } @@ -96,43 +115,54 @@ namespace sensors { //% blockId=colorGetColor //% parts="colorsensor" //% blockNamespace=sensors - //% weight=66 blockGap=8 + //% weight=99 //% group="Color Sensor" color(): ColorSensorColor { - this.setColorMode(ColorSensorMode.Color) + this.setMode(ColorSensorMode.Color) return this.getNumber(NumberFormat.UInt8LE, 0) } /** - * Measures the ambient light value from 0 (darkest) to 100 (brightest). - * @param color the color sensor port + * Registers code to run when the ambient light changes. + * @param condition the light condition + * @param handler the code to run when detected */ - //% help=sensors/color-sensor/ambient-light - //% block="`icons.colorSensor` %color| ambient light" - //% blockId=colorGetAmbient + //% help=sensors/color-sensor/on-light-changed + //% block="on `icons.colorSensor` %sensor|%mode|%condition" + //% blockId=colorOnLightChanged //% parts="colorsensor" //% blockNamespace=sensors - //% weight=65 blockGap=8 + //% weight=89 blockGap=8 //% group="Color Sensor" + onLightChanged(mode: LightIntensityMode, condition: LightCondition, handler: () => void) { + control.onEvent(this._id, condition, handler); + this.setMode(ColorSensorMode.AmbientLightIntensity) + } + + /** + * Measures the ambient or reflected light value from 0 (darkest) to 100 (brightest). + * @param color the color sensor port + */ + //% help=sensors/color-sensor/light + //% block="`icons.colorSensor` %color|%mode" + //% blockId=colorLight + //% parts="colorsensor" + //% blockNamespace=sensors + //% weight=88 + //% group="Color Sensor" + light(mode: LightIntensityMode) { + this.setMode(mode) + return this.getNumber(NumberFormat.UInt8LE, 0) + } + + //% ambientLight() { - this.setColorMode(ColorSensorMode.AmbientLightIntensity) - return this.getNumber(NumberFormat.UInt8LE, 0) + return this.light(LightIntensityMode.Ambient); } - /** - * Measures the reflected light value from 0 (darkest) to 100 (brightest). - * @param color the color sensor port - */ - //% help=sensors/color-sensor/reflected-light - //% block="`icons.colorSensor` %color| reflected light" - //% blockId=colorGetReflected - //% parts="colorsensor" - //% blockNamespace=sensors - //% weight=64 blockGap=8 - //% group="Color Sensor" - reflectedLight(): number { - this.setColorMode(ColorSensorMode.ReflectedLightIntensity) - return this.getNumber(NumberFormat.UInt8LE, 0) + //% + reflectedLight() { + return this.light(LightIntensityMode.Reflected); } } diff --git a/libs/core/input.ts b/libs/core/input.ts index 894c9203..05f39e89 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -177,7 +177,81 @@ namespace sensors.internal { } } + export enum ThresholdState { + Normal = 1, + High = 2, + Low = 3, + } + export class ThresholdDetector { + public id: number; + private min: number; + private max: number; + private lowThreshold: number; + private highThreshold: number; + private level: number; + private state: ThresholdState; + + constructor(id: number, min = 0, max = 100, lowThreshold = 20, highThreshold = 80) { + this.id = id; + this.min = min; + this.max = max; + this.lowThreshold = lowThreshold; + this.highThreshold = highThreshold; + this.level = Math.ceil((max - min) / 2); + this.state = ThresholdState.Normal; + } + + public setLevel(level: number) { + this.level = this.clampValue(level); + + if (this.level >= this.highThreshold) { + this.setState(ThresholdState.High); + } + else if (this.level <= this.lowThreshold) { + this.setState(ThresholdState.Low); + } + else { + this.setState(ThresholdState.Normal); + } + } + + public setLowThreshold(value: number) { + this.lowThreshold = this.clampValue(value); + this.highThreshold = Math.max(this.lowThreshold + 1, this.highThreshold); + } + + public setHighThreshold(value: number) { + this.highThreshold = this.clampValue(value); + this.lowThreshold = Math.min(this.highThreshold - 1, this.lowThreshold); + } + + private clampValue(value: number) { + if (value < this.min) { + return this.min; + } + else if (value > this.max) { + return this.max; + } + return value; + } + + private setState(state: ThresholdState) { + if (this.state == state) return; + + this.state = state; + switch (state) { + case ThresholdState.High: + control.raiseEvent(this.id, ThresholdState.High); + break; + case ThresholdState.Low: + control.raiseEvent(this.id, ThresholdState.Low); + break; + case ThresholdState.Normal: + break; + } + } + } export class UartSensor extends Sensor { protected mode: number // the mode user asked for From 39b7f99741e358aa2f0f594f0f7b42fdf21cc3ab Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Thu, 30 Nov 2017 08:20:00 -0800 Subject: [PATCH 6/7] update mode selection --- libs/color-sensor/color.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/color-sensor/color.ts b/libs/color-sensor/color.ts index c6f0bdb5..bd9e7fcb 100644 --- a/libs/color-sensor/color.ts +++ b/libs/color-sensor/color.ts @@ -136,7 +136,7 @@ namespace sensors { //% group="Color Sensor" onLightChanged(mode: LightIntensityMode, condition: LightCondition, handler: () => void) { control.onEvent(this._id, condition, handler); - this.setMode(ColorSensorMode.AmbientLightIntensity) + this.setMode(mode) } /** From 4ed41adb6aecd897a8b62031ca05309b99393c15 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Thu, 30 Nov 2017 08:31:31 -0800 Subject: [PATCH 7/7] avoid conflicts of event values --- libs/color-sensor/color.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/libs/color-sensor/color.ts b/libs/color-sensor/color.ts index bd9e7fcb..2db3c455 100644 --- a/libs/color-sensor/color.ts +++ b/libs/color-sensor/color.ts @@ -59,6 +59,10 @@ namespace sensors { this.thresholdDetector = new sensors.internal.ThresholdDetector(this.id()); } + _colorEventValue(value: number) { + return 0xff00 | value; + } + _deviceType() { return DAL.DEVICE_TYPE_COLOR } @@ -82,7 +86,7 @@ namespace sensors { _update(prev: number, curr: number) { if (this.mode == ColorSensorMode.Color) - control.raiseEvent(this._id, curr); + control.raiseEvent(this._id, this._colorEventValue(curr)); else this.thresholdDetector.setLevel(curr); } @@ -100,10 +104,11 @@ namespace sensors { //% weight=100 blockGap=8 //% group="Color Sensor" onColorDetected(color: ColorSensorColor, handler: () => void) { - control.onEvent(this._id, color, handler); + const v = this._colorEventValue(color); + control.onEvent(this._id, v, handler); this.setMode(ColorSensorMode.Color) if (this.color() == color) - control.raiseEvent(this._id, color); + control.raiseEvent(this._id, v); } /** @@ -134,7 +139,7 @@ namespace sensors { //% blockNamespace=sensors //% weight=89 blockGap=8 //% group="Color Sensor" - onLightChanged(mode: LightIntensityMode, condition: LightCondition, handler: () => void) { + onLightChanged(mode: LightIntensityMode, condition: LightCondition, handler: () => void) { control.onEvent(this._id, condition, handler); this.setMode(mode) }