From 3d73f193a8d5183a22b082df74424545d133a59c Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Tue, 30 Jan 2018 16:14:54 -0800 Subject: [PATCH] fix for #260 (#279) --- .../_locales/color-sensor-jsdoc-strings.json | 3 +- .../_locales/color-sensor-strings.json | 1 + libs/color-sensor/color.ts | 45 ++++++++++++------- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json b/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json index d12427a6..1cd1c1d9 100644 --- a/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json +++ b/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json @@ -17,5 +17,6 @@ "sensors.ColorSensor.setThreshold|param|condition": "the dark or bright light condition", "sensors.ColorSensor.setThreshold|param|value": "the value threshold", "sensors.ColorSensor.threshold": "Gets the threshold value", - "sensors.ColorSensor.threshold|param|condition": "the light condition" + "sensors.ColorSensor.threshold|param|condition": "the light condition", + "sensors.color": "Returns a color that the sensor can detect" } \ 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 4994c892..8ec31604 100644 --- a/libs/color-sensor/_locales/color-sensor-strings.json +++ b/libs/color-sensor/_locales/color-sensor-strings.json @@ -26,6 +26,7 @@ "sensors.color2|block": "color 2", "sensors.color3|block": "color 3", "sensors.color4|block": "color 4", + "sensors.color|block": "color %color", "sensors|block": "sensors", "{id:category}Sensors": "Sensors", "{id:group}Color Sensor": "Color Sensor", diff --git a/libs/color-sensor/color.ts b/libs/color-sensor/color.ts index 8d9b1365..c542bea7 100644 --- a/libs/color-sensor/color.ts +++ b/libs/color-sensor/color.ts @@ -19,21 +19,21 @@ enum LightIntensityMode { } const enum ColorSensorColor { - //% block="none" + //% block="none" blockIdentity=sensors.color None, - //% block="black" + //% block="black" blockIdentity=sensors.color Black, - //% block="blue" + //% block="blue" blockIdentity=sensors.color Blue, - //% block="green" + //% block="green" blockIdentity=sensors.color Green, - //% block="yellow" + //% block="yellow" blockIdentity=sensors.color Yellow, - //% block="red" + //% block="red" blockIdentity=sensors.color Red, - //% block="white" + //% block="white" blockIdentity=sensors.color White, - //% block="brown" + //% block="brown" blockIdentity=sensors.color Brown, } @@ -157,6 +157,7 @@ namespace sensors { //% sensor.fieldEditor="ports" //% weight=98 //% group="Color Sensor" + //% blockGap=8 color(): ColorSensorColor { this.setMode(ColorSensorMode.Color) return this.getNumber(NumberFormat.UInt8LE, 0) @@ -263,45 +264,57 @@ namespace sensors { this.light(mode); // trigger a read pauseUntil(() => this.isActive()); // ensure sensor is live - + let vold = 0; let vcount = 0; let min = 200; - let max = -200; - let k = 0; - while(k++ < 1000 && vcount < 50) { + let max = -200; + let k = 0; + while (k++ < 1000 && vcount < 50) { let v = this.light(mode); min = Math.min(min, v); max = Math.max(max, v); // detect if nothing has changed and stop calibration if (Math.abs(v - vold) <= 2) - vcount ++; + vcount++; else { vold = v; vcount = 1; } // wait a bit - loops.pause(50); + loops.pause(50); } // apply tolerance const minDist = 10; min = Math.max(minDist / 2, Math.min(min + deviation / 2, max - deviation / 2 - minDist / 2)); max = Math.min(100 - minDist / 2, Math.max(min + minDist, max - deviation / 2)); - + // apply thresholds this.thresholdDetector.setLowThreshold(min); this.thresholdDetector.setHighThreshold(max); this.calibrating = false; } + + } + + /** + * Returns a color that the sensor can detect + */ + //% shim=TD_ID + //% blockId=colorSensorColor block="color %color" + //% group="Color Sensor" + //% weight=97 + export function color(color: ColorSensorColor): ColorSensorColor { + return color; } //% whenUsed block="color 3" weight=95 fixedInstance jres=icons.port3 export const color3: ColorSensor = new ColorSensor(3) - + //% whenUsed block="color 1" weight=90 fixedInstance jres=icons.port1 export const color1: ColorSensor = new ColorSensor(1)