diff --git a/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json b/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json index 7739e3fe..d12427a6 100644 --- a/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json +++ b/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json @@ -15,5 +15,7 @@ "sensors.ColorSensor.pauseForLight": "Waits for the given color to be detected", "sensors.ColorSensor.setThreshold": "Sets a threshold value", "sensors.ColorSensor.setThreshold|param|condition": "the dark or bright light condition", - "sensors.ColorSensor.setThreshold|param|value": "the value threshold" + "sensors.ColorSensor.setThreshold|param|value": "the value threshold", + "sensors.ColorSensor.threshold": "Gets the threshold value", + "sensors.ColorSensor.threshold|param|condition": "the light condition" } \ 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 6c530eab..4994c892 100644 --- a/libs/color-sensor/_locales/color-sensor-strings.json +++ b/libs/color-sensor/_locales/color-sensor-strings.json @@ -21,6 +21,7 @@ "sensors.ColorSensor.pauseForColor|block": "pause %sensor|for color %color", "sensors.ColorSensor.pauseForLight|block": "pause %sensor|for %mode|%condition", "sensors.ColorSensor.setThreshold|block": "set %sensor|%condition|to %value", + "sensors.ColorSensor.threshold|block": "%sensor|%condition", "sensors.color1|block": "color 1", "sensors.color2|block": "color 2", "sensors.color3|block": "color 3", diff --git a/libs/color-sensor/color.ts b/libs/color-sensor/color.ts index 54d7881d..8d9b1365 100644 --- a/libs/color-sensor/color.ts +++ b/libs/color-sensor/color.ts @@ -241,6 +241,17 @@ namespace sensors { this.thresholdDetector.setHighThreshold(value); } + /** + * Gets the threshold value + * @param condition the light condition + */ + //% blockId=colorGetThreshold block="%sensor|%condition" + //% group="Threshold" blockGap=8 weight=89 + //% sensor.fieldEditor="ports" + threshold(condition: LightCondition): number { + return this.thresholdDetector.threshold(LightCondition.Dark); + } + /** * Collects measurement of the light condition and adjusts the threshold to 10% / 90%. */ diff --git a/libs/core/input.ts b/libs/core/input.ts index e0290f3a..0f33da20 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -432,11 +432,11 @@ namespace sensors { export class ThresholdDetector { public id: number; - private min: number; - private max: number; - private lowThreshold: number; - private highThreshold: number; - private level: number; + public min: number; + public max: number; + public lowThreshold: number; + public highThreshold: number; + public level: number; public state: ThresholdState; constructor(id: number, min = 0, max = 100, lowThreshold = 20, highThreshold = 80) { @@ -467,6 +467,14 @@ namespace sensors { } } + public threshold(t: ThresholdState): number { + switch(t) { + case ThresholdState.High: return this.highThreshold; + case ThresholdState.Low: return this.lowThreshold; + default: return (this.max - this.min) / 2; + } + } + public setLowThreshold(value: number) { this.lowThreshold = this.clampValue(value); this.highThreshold = Math.max(this.lowThreshold + 1, this.highThreshold); diff --git a/libs/infrared-sensor/_locales/infrared-sensor-jsdoc-strings.json b/libs/infrared-sensor/_locales/infrared-sensor-jsdoc-strings.json index e9e23697..abd97d50 100644 --- a/libs/infrared-sensor/_locales/infrared-sensor-jsdoc-strings.json +++ b/libs/infrared-sensor/_locales/infrared-sensor-jsdoc-strings.json @@ -7,6 +7,8 @@ "sensors.InfraredSensor.setThreshold": "Sets a threshold value", "sensors.InfraredSensor.setThreshold|param|condition": "the dark or bright light condition", "sensors.InfraredSensor.setThreshold|param|value": "the value threshold", + "sensors.InfraredSensor.threshold": "Gets the threshold value", + "sensors.InfraredSensor.threshold|param|condition": "the proximity condition", "sensors.RemoteInfraredBeaconButton.isPressed": "Check if a remote button is currently pressed or not.", "sensors.RemoteInfraredBeaconButton.onEvent": "Do something when a button or sensor is clicked, up or down", "sensors.RemoteInfraredBeaconButton.onEvent|param|body": "code to run when the event is raised", diff --git a/libs/infrared-sensor/_locales/infrared-sensor-strings.json b/libs/infrared-sensor/_locales/infrared-sensor-strings.json index b00bc03f..6e81890c 100644 --- a/libs/infrared-sensor/_locales/infrared-sensor-strings.json +++ b/libs/infrared-sensor/_locales/infrared-sensor-strings.json @@ -6,6 +6,7 @@ "sensors.InfraredSensor.proximity|block": "%sensor|proximity", "sensors.InfraredSensor.remoteCommand|block": "%sensor|remote command", "sensors.InfraredSensor.setThreshold|block": "set %sensor|%condition|to %value", + "sensors.InfraredSensor.threshold|block": "%sensor|%condition", "sensors.RemoteInfraredBeaconButton.isPressed|block": "%button|is pressed", "sensors.RemoteInfraredBeaconButton.onEvent|block": "on %button|%event", "sensors.RemoteInfraredBeaconButton.wasPressed|block": "%button|was pressed", diff --git a/libs/infrared-sensor/ir.ts b/libs/infrared-sensor/ir.ts index 4b1439e6..208e61c0 100644 --- a/libs/infrared-sensor/ir.ts +++ b/libs/infrared-sensor/ir.ts @@ -258,7 +258,7 @@ namespace sensors { * @param value the value threshold */ //% blockId=irSetThreshold block="set %sensor|%condition|to %value" - //% group="Threshold" blockGap=8 + //% group="Threshold" blockGap=8 weight=49 //% value.min=0 value.max=100 setThreshold(condition: InfraredSensorEvent, value: number) { if (condition == InfraredSensorEvent.ObjectNear) @@ -266,6 +266,17 @@ namespace sensors { else this.proximityThreshold.setHighThreshold(value); } + + /** + * Gets the threshold value + * @param condition the proximity condition + */ + //% blockId=irGetThreshold block="%sensor|%condition" + //% group="Threshold" blockGap=8 weight=49 + //% sensor.fieldEditor="ports" + threshold(condition: InfraredSensorEvent): number { + return this.proximityThreshold.threshold(LightCondition.Dark); + } } //% fixedInstance whenUsed block="infrared 1" jres=icons.port1 diff --git a/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-jsdoc-strings.json b/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-jsdoc-strings.json index d5ea29ec..031c5321 100644 --- a/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-jsdoc-strings.json +++ b/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-jsdoc-strings.json @@ -5,5 +5,7 @@ "sensors.UltraSonicSensor.pauseUntil": "Waits for the event to occur", "sensors.UltraSonicSensor.setThreshold": "Sets a threshold value", "sensors.UltraSonicSensor.setThreshold|param|condition": "the dark or bright light condition", - "sensors.UltraSonicSensor.setThreshold|param|value": "the value threshold" + "sensors.UltraSonicSensor.setThreshold|param|value": "the value threshold", + "sensors.UltraSonicSensor.threshold": "Gets the threshold value", + "sensors.UltraSonicSensor.threshold|param|condition": "the proximity condition" } \ No newline at end of file diff --git a/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-strings.json b/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-strings.json index 8059b245..8003ec37 100644 --- a/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-strings.json +++ b/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-strings.json @@ -6,6 +6,7 @@ "sensors.UltraSonicSensor.onEvent|block": "on %sensor|%event", "sensors.UltraSonicSensor.pauseUntil|block": "pause until %sensor| %event", "sensors.UltraSonicSensor.setThreshold|block": "set %sensor|%condition|to %value", + "sensors.UltraSonicSensor.threshold|block": "%sensor|%condition", "sensors.ultrasonic1|block": "ultrasonic 1", "sensors.ultrasonic2|block": "ultrasonic 2", "sensors.ultrasonic3|block": "ultrasonic 3", diff --git a/libs/ultrasonic-sensor/ultrasonic.ts b/libs/ultrasonic-sensor/ultrasonic.ts index 3fd069cd..36452e38 100644 --- a/libs/ultrasonic-sensor/ultrasonic.ts +++ b/libs/ultrasonic-sensor/ultrasonic.ts @@ -93,20 +93,36 @@ namespace sensors { * @param value the value threshold */ //% blockId=ultrasonicSetThreshold block="set %sensor|%condition|to %value" - //% group="Threshold" blockGap=8 + //% group="Threshold" blockGap=8 weight=80 //% value.min=0 value.max=255 - setThreshold(condition: UltrasonicSensorEvent, value: number) { - switch(condition) { + setThreshold(condition: UltrasonicSensorEvent, value: number) { + switch (condition) { case UltrasonicSensorEvent.ObjectNear: this.promixityThreshold.setLowThreshold(value); break; case UltrasonicSensorEvent.ObjectFar: this.promixityThreshold.setHighThreshold(value); break; case UltrasonicSensorEvent.ObjectDetected: this.movementThreshold = value; break; } } + + /** + * Gets the threshold value + * @param condition the proximity condition + */ + //% blockId=ultrasonicGetThreshold block="%sensor|%condition" + //% group="Threshold" blockGap=8 weight=79 + //% sensor.fieldEditor="ports" + threshold(condition: UltrasonicSensorEvent): number { + switch (condition) { + case UltrasonicSensorEvent.ObjectNear: this.promixityThreshold.threshold(ThresholdState.Low); break; + case UltrasonicSensorEvent.ObjectFar: this.promixityThreshold.threshold(ThresholdState.Low); break; + case UltrasonicSensorEvent.ObjectDetected: this.movementThreshold; break; + } + return 0; + } } - + //% fixedInstance whenUsed block="ultrasonic 4" jres=icons.port4 export const ultrasonic4: UltraSonicSensor = new UltraSonicSensor(4) - + //% fixedInstance whenUsed block="ultrasonic 1" jres=icons.port1 export const ultrasonic1: UltraSonicSensor = new UltraSonicSensor(1)