From 4d2b7ced716ca5f7c526b3aebe8dfd1b1417dfc6 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Wed, 29 Nov 2017 21:41:00 -0800 Subject: [PATCH] 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" ],