threshold query api
This commit is contained in:
		@@ -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"
 | 
			
		||||
}
 | 
			
		||||
@@ -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",
 | 
			
		||||
 
 | 
			
		||||
@@ -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(<ThresholdState><number>LightCondition.Dark);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * Collects measurement of the light condition and adjusts the threshold to 10% / 90%.
 | 
			
		||||
         */
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
 
 | 
			
		||||
@@ -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",
 | 
			
		||||
 
 | 
			
		||||
@@ -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",
 | 
			
		||||
 
 | 
			
		||||
@@ -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(<ThresholdState><number>LightCondition.Dark);
 | 
			
		||||
        }        
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //% fixedInstance whenUsed block="infrared 1" jres=icons.port1
 | 
			
		||||
 
 | 
			
		||||
@@ -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"
 | 
			
		||||
}
 | 
			
		||||
@@ -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",
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user