threshold query api

This commit is contained in:
Peli de Halleux 2018-01-30 08:27:23 -08:00
parent 16c67f0e30
commit 188d5b3aa7
10 changed files with 68 additions and 13 deletions

View File

@ -15,5 +15,7 @@
"sensors.ColorSensor.pauseForLight": "Waits for the given color to be detected", "sensors.ColorSensor.pauseForLight": "Waits for the given color to be detected",
"sensors.ColorSensor.setThreshold": "Sets a threshold value", "sensors.ColorSensor.setThreshold": "Sets a threshold value",
"sensors.ColorSensor.setThreshold|param|condition": "the dark or bright light condition", "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"
} }

View File

@ -21,6 +21,7 @@
"sensors.ColorSensor.pauseForColor|block": "pause %sensor|for color %color", "sensors.ColorSensor.pauseForColor|block": "pause %sensor|for color %color",
"sensors.ColorSensor.pauseForLight|block": "pause %sensor|for %mode|%condition", "sensors.ColorSensor.pauseForLight|block": "pause %sensor|for %mode|%condition",
"sensors.ColorSensor.setThreshold|block": "set %sensor|%condition|to %value", "sensors.ColorSensor.setThreshold|block": "set %sensor|%condition|to %value",
"sensors.ColorSensor.threshold|block": "%sensor|%condition",
"sensors.color1|block": "color 1", "sensors.color1|block": "color 1",
"sensors.color2|block": "color 2", "sensors.color2|block": "color 2",
"sensors.color3|block": "color 3", "sensors.color3|block": "color 3",

View File

@ -241,6 +241,17 @@ namespace sensors {
this.thresholdDetector.setHighThreshold(value); 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(<ThresholdState><number>LightCondition.Dark);
}
/** /**
* Collects measurement of the light condition and adjusts the threshold to 10% / 90%. * Collects measurement of the light condition and adjusts the threshold to 10% / 90%.
*/ */

View File

@ -432,11 +432,11 @@ namespace sensors {
export class ThresholdDetector { export class ThresholdDetector {
public id: number; public id: number;
private min: number; public min: number;
private max: number; public max: number;
private lowThreshold: number; public lowThreshold: number;
private highThreshold: number; public highThreshold: number;
private level: number; public level: number;
public state: ThresholdState; public state: ThresholdState;
constructor(id: number, min = 0, max = 100, lowThreshold = 20, highThreshold = 80) { 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) { public setLowThreshold(value: number) {
this.lowThreshold = this.clampValue(value); this.lowThreshold = this.clampValue(value);
this.highThreshold = Math.max(this.lowThreshold + 1, this.highThreshold); this.highThreshold = Math.max(this.lowThreshold + 1, this.highThreshold);

View File

@ -7,6 +7,8 @@
"sensors.InfraredSensor.setThreshold": "Sets a threshold value", "sensors.InfraredSensor.setThreshold": "Sets a threshold value",
"sensors.InfraredSensor.setThreshold|param|condition": "the dark or bright light condition", "sensors.InfraredSensor.setThreshold|param|condition": "the dark or bright light condition",
"sensors.InfraredSensor.setThreshold|param|value": "the value threshold", "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.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": "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", "sensors.RemoteInfraredBeaconButton.onEvent|param|body": "code to run when the event is raised",

View File

@ -6,6 +6,7 @@
"sensors.InfraredSensor.proximity|block": "%sensor|proximity", "sensors.InfraredSensor.proximity|block": "%sensor|proximity",
"sensors.InfraredSensor.remoteCommand|block": "%sensor|remote command", "sensors.InfraredSensor.remoteCommand|block": "%sensor|remote command",
"sensors.InfraredSensor.setThreshold|block": "set %sensor|%condition|to %value", "sensors.InfraredSensor.setThreshold|block": "set %sensor|%condition|to %value",
"sensors.InfraredSensor.threshold|block": "%sensor|%condition",
"sensors.RemoteInfraredBeaconButton.isPressed|block": "%button|is pressed", "sensors.RemoteInfraredBeaconButton.isPressed|block": "%button|is pressed",
"sensors.RemoteInfraredBeaconButton.onEvent|block": "on %button|%event", "sensors.RemoteInfraredBeaconButton.onEvent|block": "on %button|%event",
"sensors.RemoteInfraredBeaconButton.wasPressed|block": "%button|was pressed", "sensors.RemoteInfraredBeaconButton.wasPressed|block": "%button|was pressed",

View File

@ -258,7 +258,7 @@ namespace sensors {
* @param value the value threshold * @param value the value threshold
*/ */
//% blockId=irSetThreshold block="set %sensor|%condition|to %value" //% blockId=irSetThreshold block="set %sensor|%condition|to %value"
//% group="Threshold" blockGap=8 //% group="Threshold" blockGap=8 weight=49
//% value.min=0 value.max=100 //% value.min=0 value.max=100
setThreshold(condition: InfraredSensorEvent, value: number) { setThreshold(condition: InfraredSensorEvent, value: number) {
if (condition == InfraredSensorEvent.ObjectNear) if (condition == InfraredSensorEvent.ObjectNear)
@ -266,6 +266,17 @@ namespace sensors {
else else
this.proximityThreshold.setHighThreshold(value); 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(<ThresholdState><number>LightCondition.Dark);
}
} }
//% fixedInstance whenUsed block="infrared 1" jres=icons.port1 //% fixedInstance whenUsed block="infrared 1" jres=icons.port1

View File

@ -5,5 +5,7 @@
"sensors.UltraSonicSensor.pauseUntil": "Waits for the event to occur", "sensors.UltraSonicSensor.pauseUntil": "Waits for the event to occur",
"sensors.UltraSonicSensor.setThreshold": "Sets a threshold value", "sensors.UltraSonicSensor.setThreshold": "Sets a threshold value",
"sensors.UltraSonicSensor.setThreshold|param|condition": "the dark or bright light condition", "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"
} }

View File

@ -6,6 +6,7 @@
"sensors.UltraSonicSensor.onEvent|block": "on %sensor|%event", "sensors.UltraSonicSensor.onEvent|block": "on %sensor|%event",
"sensors.UltraSonicSensor.pauseUntil|block": "pause until %sensor| %event", "sensors.UltraSonicSensor.pauseUntil|block": "pause until %sensor| %event",
"sensors.UltraSonicSensor.setThreshold|block": "set %sensor|%condition|to %value", "sensors.UltraSonicSensor.setThreshold|block": "set %sensor|%condition|to %value",
"sensors.UltraSonicSensor.threshold|block": "%sensor|%condition",
"sensors.ultrasonic1|block": "ultrasonic 1", "sensors.ultrasonic1|block": "ultrasonic 1",
"sensors.ultrasonic2|block": "ultrasonic 2", "sensors.ultrasonic2|block": "ultrasonic 2",
"sensors.ultrasonic3|block": "ultrasonic 3", "sensors.ultrasonic3|block": "ultrasonic 3",

View File

@ -93,15 +93,31 @@ namespace sensors {
* @param value the value threshold * @param value the value threshold
*/ */
//% blockId=ultrasonicSetThreshold block="set %sensor|%condition|to %value" //% blockId=ultrasonicSetThreshold block="set %sensor|%condition|to %value"
//% group="Threshold" blockGap=8 //% group="Threshold" blockGap=8 weight=80
//% value.min=0 value.max=255 //% value.min=0 value.max=255
setThreshold(condition: UltrasonicSensorEvent, value: number) { setThreshold(condition: UltrasonicSensorEvent, value: number) {
switch(condition) { switch (condition) {
case UltrasonicSensorEvent.ObjectNear: this.promixityThreshold.setLowThreshold(value); break; case UltrasonicSensorEvent.ObjectNear: this.promixityThreshold.setLowThreshold(value); break;
case UltrasonicSensorEvent.ObjectFar: this.promixityThreshold.setHighThreshold(value); break; case UltrasonicSensorEvent.ObjectFar: this.promixityThreshold.setHighThreshold(value); break;
case UltrasonicSensorEvent.ObjectDetected: this.movementThreshold = 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 //% fixedInstance whenUsed block="ultrasonic 4" jres=icons.port4