From 892a2d585fecc485e7b52cfba679b13d1896ecff Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Tue, 9 Jan 2018 12:46:48 -0800 Subject: [PATCH] Thresholds (#222) * add threshold blocks * updated strings * added gap * reorg color/gyro blocks * fixing sound names * adding stop block * fixing exanmples --- docs/coding/cruise-control-3.md | 2 +- docs/coding/reverse-beeper-1.md | 2 +- docs/coding/reverse-beeper-2.md | 2 +- docs/coding/reverse-beeper-3.md | 5 +- .../_locales/color-sensor-jsdoc-strings.json | 5 +- .../_locales/color-sensor-strings.json | 4 +- libs/color-sensor/color.ts | 16 +++++- libs/core/_locales/core-jsdoc-strings.json | 2 +- libs/core/_locales/core-strings.json | 1 + libs/core/output.ts | 6 ++- libs/ev3/ns.ts | 2 +- libs/gyro-sensor/gyro.ts | 4 +- .../infrared-sensor-jsdoc-strings.json | 3 ++ .../_locales/infrared-sensor-strings.json | 4 +- libs/infrared-sensor/ir.ts | 15 ++++++ libs/music/_locales/music-strings.json | 52 +++++++++---------- libs/music/sounds.ts | 52 +++++++++---------- .../ultrasonic-sensor-jsdoc-strings.json | 5 +- .../_locales/ultrasonic-sensor-strings.json | 2 + libs/ultrasonic-sensor/ultrasonic.ts | 16 ++++++ 20 files changed, 131 insertions(+), 69 deletions(-) diff --git a/docs/coding/cruise-control-3.md b/docs/coding/cruise-control-3.md index edc8776a..95f1526e 100644 --- a/docs/coding/cruise-control-3.md +++ b/docs/coding/cruise-control-3.md @@ -14,7 +14,7 @@ function accelerate() { } function update() { brick.clearScreen() - brick.printLine("speed: " + speed, 1) + brick.showString("speed: " + speed, 1) motors.largeBC.setSpeed(speed) } sensors.touch2.onEvent(TouchSensorEvent.Pressed, function () { diff --git a/docs/coding/reverse-beeper-1.md b/docs/coding/reverse-beeper-1.md index 54f10c53..daf4588f 100644 --- a/docs/coding/reverse-beeper-1.md +++ b/docs/coding/reverse-beeper-1.md @@ -7,5 +7,5 @@ loops.forever(function () { }) motors.largeBC.setSpeed(-20); sensors.ultrasonic4.pauseUntil(UltrasonicSensorEvent.ObjectNear); -motors.stopAllMotors(); +motors.largeBC.stop(); ``` diff --git a/docs/coding/reverse-beeper-2.md b/docs/coding/reverse-beeper-2.md index 9c3df90f..fea694b7 100644 --- a/docs/coding/reverse-beeper-2.md +++ b/docs/coding/reverse-beeper-2.md @@ -9,5 +9,5 @@ loops.forever(function () { }) motors.largeBC.setSpeed(-20); sensors.ultrasonic4.pauseUntil(UltrasonicSensorEvent.ObjectNear); -motors.stopAllMotors(); +motors.largeBC.stop(); ``` diff --git a/docs/coding/reverse-beeper-3.md b/docs/coding/reverse-beeper-3.md index 557883e6..634485b2 100644 --- a/docs/coding/reverse-beeper-3.md +++ b/docs/coding/reverse-beeper-3.md @@ -4,10 +4,9 @@ let beep = false beep = true control.runInBackground(function () { - motors.largeB.setSpeed(-20) - motors.largeC.setSpeed(-20) + motors.largeBC.setSpeed(-20) sensors.ultrasonic4.pauseUntil(UltrasonicSensorEvent.ObjectNear) - motors.stopAllMotors() + motors.largeBC.stop() beep = false }) control.runInBackground(function () { diff --git a/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json b/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json index 0cfdbf31..be9c607b 100644 --- a/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json +++ b/libs/color-sensor/_locales/color-sensor-jsdoc-strings.json @@ -11,5 +11,8 @@ "sensors.ColorSensor.onLightChanged|param|handler": "the code to run when detected", "sensors.ColorSensor.pauseForColor": "Waits for the given color to be detected", "sensors.ColorSensor.pauseForColor|param|color": "the color to detect", - "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|param|condition": "the dark or bright light condition", + "sensors.ColorSensor.setThreshold|param|value": "the value threshold" } \ 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 bd243aa9..cb650083 100644 --- a/libs/color-sensor/_locales/color-sensor-strings.json +++ b/libs/color-sensor/_locales/color-sensor-strings.json @@ -19,11 +19,13 @@ "sensors.ColorSensor.onLightChanged|block": "on %sensor|%mode|%condition", "sensors.ColorSensor.pauseForColor|block": "pause %sensor|for color %color", "sensors.ColorSensor.pauseForLight|block": "pause %sensor|for %mode|%condition", + "sensors.ColorSensor.setThreshold|block": "set %condition|to %value", "sensors.color1|block": "color 1", "sensors.color2|block": "color 2", "sensors.color3|block": "color 3", "sensors.color4|block": "color 4", "sensors|block": "sensors", "{id:category}Sensors": "Sensors", - "{id:group}Color Sensor": "Color Sensor" + "{id:group}Color Sensor": "Color Sensor", + "{id:group}Threshold": "Threshold" } \ No newline at end of file diff --git a/libs/color-sensor/color.ts b/libs/color-sensor/color.ts index 2346298b..f6590e9f 100644 --- a/libs/color-sensor/color.ts +++ b/libs/color-sensor/color.ts @@ -152,7 +152,7 @@ namespace sensors { //% parts="colorsensor" //% blockNamespace=sensors //% sensor.fieldEditor="ports" - //% weight=99 + //% weight=98 //% group="Color Sensor" color(): ColorSensorColor { this.setMode(ColorSensorMode.Color) @@ -221,6 +221,20 @@ namespace sensors { reflectedLight() { return this.light(LightIntensityMode.Reflected); } + + /** + * Sets a threshold value + * @param condition the dark or bright light condition + * @param value the value threshold + */ + //% blockId=colorSetThreshold block="set %condition|to %value" + //% group="Threshold" blockGap=8 weight=90 + setThreshold(condition: LightCondition, value: number) { + if (condition == LightCondition.Dark) + this.thresholdDetector.setLowThreshold(value) + else + this.thresholdDetector.setHighThreshold(value); + } } //% whenUsed block="color 3" weight=90 fixedInstance jres=icons.port3 diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 15ad475e..ba749e94 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -71,7 +71,7 @@ "motors.MotorBase.setBrake": "Sets the automatic brake on or off when the motor is off", "motors.MotorBase.setBrake|param|brake": "a value indicating if the motor should break when off", "motors.MotorBase.setReversed": "Reverses the motor polarity", - "motors.MotorBase.setSpeed": "Sets the motor speed for limited time or distance", + "motors.MotorBase.setSpeed": "Sets the motor speed for limited time or distance.", "motors.MotorBase.setSpeed|param|speed": "the speed from ``100`` full forward to ``-100`` full backward, eg: 50", "motors.MotorBase.setSpeed|param|unit": "(optional) unit of the value", "motors.MotorBase.setSpeed|param|value": "(optional) measured distance or rotation", diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index aa4d65d0..c2a8fc3a 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -58,6 +58,7 @@ "motors.MotorBase.setBrake|block": "set %motor|brake %brake", "motors.MotorBase.setReversed|block": "set %motor|reversed %reversed", "motors.MotorBase.setSpeed|block": "set %motor|speed to %speed=motorSpeedPicker|%", + "motors.MotorBase.stop|block": "%motors|stop", "motors.SynchedMotorPair.steer|block": "steer %chassis|turn ratio %turnRatio|speed %speed=motorSpeedPicker|%", "motors.SynchedMotorPair.tank|block": "tank %motors|%speedLeft=motorSpeedPicker|%|%speedRight=motorSpeedPicker|%", "motors.largeAB|block": "large A+B", diff --git a/libs/core/output.ts b/libs/core/output.ts index cacfc32a..b62106a4 100644 --- a/libs/core/output.ts +++ b/libs/core/output.ts @@ -187,7 +187,9 @@ namespace motors { /** * Stops the motor(s). */ - //% + //% weight=6 blockGap=8 + //% group="Move" + //% blockId=motorStop block="%motors|stop" stop() { this.init(); stop(this._port, this._brake); @@ -203,7 +205,7 @@ namespace motors { } /** - * Sets the motor speed for limited time or distance + * Sets the motor speed for limited time or distance. * @param speed the speed from ``100`` full forward to ``-100`` full backward, eg: 50 * @param value (optional) measured distance or rotation * @param unit (optional) unit of the value diff --git a/libs/ev3/ns.ts b/libs/ev3/ns.ts index 2b16c053..d238066e 100644 --- a/libs/ev3/ns.ts +++ b/libs/ev3/ns.ts @@ -8,7 +8,7 @@ namespace brick { //% color="#C8509B" weight=95 icon="\uf10f" //% labelLineWidth=0 -//% groups='["Ultrasonic Sensor", "Touch Sensor", "Color Sensor", "Infrared Sensor", "Remote Infrared Beacon", "Gyro Sensor"]' +//% groups='["Touch Sensor", "Color Sensor", "Ultrasonic Sensor", "Gyro Sensor", "Infrared Sensor", "Remote Infrared Beacon", "Threshold"]' //% groupIcons='["\uf101","\uf103","\uf102","","","\uf104"]' namespace sensors { } diff --git a/libs/gyro-sensor/gyro.ts b/libs/gyro-sensor/gyro.ts index 4b169792..889f643d 100644 --- a/libs/gyro-sensor/gyro.ts +++ b/libs/gyro-sensor/gyro.ts @@ -31,7 +31,7 @@ namespace sensors { //% parts="gyroscope" //% blockNamespace=sensors //% sensor.fieldEditor="ports" - //% weight=65 blockGap=8 + //% weight=64 blockGap=8 //% group="Gyro Sensor" angle(): number { if (this.calibrating) @@ -70,7 +70,7 @@ namespace sensors { //% parts="gyroscope" //% blockNamespace=sensors //% sensor.fieldEditor="ports" - //% weight=65 blockGap=8 + //% weight=50 blockGap=8 //% group="Gyro Sensor" calibrate(): void { if (this.calibrating) return; // already in calibration mode diff --git a/libs/infrared-sensor/_locales/infrared-sensor-jsdoc-strings.json b/libs/infrared-sensor/_locales/infrared-sensor-jsdoc-strings.json index 2e81f649..e9e23697 100644 --- a/libs/infrared-sensor/_locales/infrared-sensor-jsdoc-strings.json +++ b/libs/infrared-sensor/_locales/infrared-sensor-jsdoc-strings.json @@ -4,6 +4,9 @@ "sensors.InfraredSensor.pauseUntil": "Waits for the event to occur", "sensors.InfraredSensor.proximity": "Get the promixity measured by the infrared sensor, from ``0`` (close) to ``100`` (far)", "sensors.InfraredSensor.remoteCommand": "Get the remote commandreceived the infrared sensor.", + "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.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 efb06cff..1d53da6d 100644 --- a/libs/infrared-sensor/_locales/infrared-sensor-strings.json +++ b/libs/infrared-sensor/_locales/infrared-sensor-strings.json @@ -5,6 +5,7 @@ "sensors.InfraredSensor.pauseUntil|block": "pause until %sensor| %event", "sensors.InfraredSensor.proximity|block": "%sensor|proximity", "sensors.InfraredSensor.remoteCommand|block": "%sensor|remote command", + "sensors.InfraredSensor.setThreshold|block": "set %condition|to %value", "sensors.RemoteInfraredBeaconButton.isPressed|block": "%button|is pressed", "sensors.RemoteInfraredBeaconButton.onEvent|block": "on %button|%event", "sensors.RemoteInfraredBeaconButton.wasPressed|block": "%button|was pressed", @@ -20,5 +21,6 @@ "sensors|block": "sensors", "{id:category}Sensors": "Sensors", "{id:group}Infrared Sensor": "Infrared Sensor", - "{id:group}Remote Infrared Beacon": "Remote Infrared Beacon" + "{id:group}Remote Infrared Beacon": "Remote Infrared Beacon", + "{id:group}Threshold": "Threshold" } \ No newline at end of file diff --git a/libs/infrared-sensor/ir.ts b/libs/infrared-sensor/ir.ts index 1fd9288b..79da5f07 100644 --- a/libs/infrared-sensor/ir.ts +++ b/libs/infrared-sensor/ir.ts @@ -250,6 +250,21 @@ namespace sensors { this._setMode(IrSensorMode.Seek) return this.getNumber(NumberFormat.UInt16LE, this.channel * 2) } + + + /** + * Sets a threshold value + * @param condition the dark or bright light condition + * @param value the value threshold + */ + //% blockId=irSetThreshold block="set %condition|to %value" + //% group="Threshold" blockGap=8 + setThreshold(condition: InfraredSensorEvent, value: number) { + if (condition == InfraredSensorEvent.ObjectNear) + this.proximityThreshold.setLowThreshold(value) + else + this.proximityThreshold.setHighThreshold(value); + } } //% fixedInstance whenUsed block="infrared 1" jres=icons.port1 diff --git a/libs/music/_locales/music-strings.json b/libs/music/_locales/music-strings.json index 84820d72..882c158f 100644 --- a/libs/music/_locales/music-strings.json +++ b/libs/music/_locales/music-strings.json @@ -33,34 +33,34 @@ "music.stopAllSounds|block": "stop all sounds", "music.tempo|block": "tempo (bpm)", "music|block": "music", - "sounds.animalsCatPurr|block": "Animals cat purr", - "sounds.animalsDogBark1|block": "Animals dog bark 1", - "sounds.animalsDogBark2|block": "Animals dog bark 2", - "sounds.animalsDogGrowl|block": "Animals dog growl", - "sounds.animalsDogSniff|block": "Animals dog sniff", - "sounds.animalsDogWhine|block": "Animals dog whine", - "sounds.animalsElephantCall|block": "Animals elephant call", - "sounds.animalsInsectBuzz1|block": "Animals insect buzz1", - "sounds.animalsInsectBuzz2|block": "Animals insect buzz2", - "sounds.animalsInsectChirp|block": "Animals insect chirp", - "sounds.animalsSnakeHiss|block": "Animals snake hiss", - "sounds.animalsSnakeRattle|block": "Animals snake rattle", - "sounds.animalsTRexRoar|block": "Animals trex roar", - "sounds.colorsBlack|block": "Colors black", - "sounds.colorsBlue|block": "Colors blue", - "sounds.colorsBrown|block": "Colors brown", - "sounds.colorsGreen|block": "Colors green", - "sounds.colorsRed|block": "Colors red", - "sounds.colorsWhite|block": "Colors white", - "sounds.colorsYellow|block": "Colors yellow", - "sounds.communicationBravo|block": "Communication bravo", - "sounds.communicationEv3|block": "Communication ev3", - "sounds.communicationFantastic|block": "Communication fantastic", - "sounds.communicationGameOver|block": "Communication game over", + "sounds.animalsCatPurr|block": "animals cat purr", + "sounds.animalsDogBark1|block": "animals dog bark 1", + "sounds.animalsDogBark2|block": "animals dog bark 2", + "sounds.animalsDogGrowl|block": "animals dog growl", + "sounds.animalsDogSniff|block": "animals dog sniff", + "sounds.animalsDogWhine|block": "animals dog whine", + "sounds.animalsElephantCall|block": "animals elephant call", + "sounds.animalsInsectBuzz1|block": "animals insect buzz1", + "sounds.animalsInsectBuzz2|block": "animals insect buzz2", + "sounds.animalsInsectChirp|block": "animals insect chirp", + "sounds.animalsSnakeHiss|block": "animals snake hiss", + "sounds.animalsSnakeRattle|block": "animals snake rattle", + "sounds.animalsTRexRoar|block": "animals trex roar", + "sounds.colorsBlack|block": "colors black", + "sounds.colorsBlue|block": "colors blue", + "sounds.colorsBrown|block": "colors brown", + "sounds.colorsGreen|block": "colors green", + "sounds.colorsRed|block": "colors red", + "sounds.colorsWhite|block": "colors white", + "sounds.colorsYellow|block": "colors yellow", + "sounds.communicationBravo|block": "communication bravo", + "sounds.communicationEv3|block": "communication ev3", + "sounds.communicationFantastic|block": "communication fantastic", + "sounds.communicationGameOver|block": "communication game over", "sounds.communicationGoodJob|block": "communicationGoodJob", "sounds.communicationGoodbye|block": "communication goodbye", "sounds.communicationGood|block": "communication good", - "sounds.communicationGo|block": "Communication go", + "sounds.communicationGo|block": "communication go", "sounds.communicationHello|block": "communication hello", "sounds.communicationHi|block": "communication hi", "sounds.communicationLego|block": "communication lego", @@ -159,7 +159,7 @@ "sounds.systemOverpower|block": "system overpower", "sounds.systemPowerDown|block": "system power down", "sounds.systemReady|block": "system ready", - "sounds.systemStartUp|block": "S", + "sounds.systemStartUp|block": "system start up", "{id:category}Music": "Music", "{id:category}Sound": "Sound", "{id:category}Sounds": "Sounds" diff --git a/libs/music/sounds.ts b/libs/music/sounds.ts index a5f41c7d..36bdd2f9 100644 --- a/libs/music/sounds.ts +++ b/libs/music/sounds.ts @@ -1,53 +1,53 @@ namespace sounds { - //% fixedInstance jres block="Animals cat purr" + //% fixedInstance jres block="animals cat purr" export const animalsCatPurr = music.fromWAV(hex``); - //% fixedInstance jres block="Animals dog bark 1" + //% fixedInstance jres block="animals dog bark 1" export const animalsDogBark1 = music.fromWAV(hex``); - //% fixedInstance jres block="Animals dog bark 2" + //% fixedInstance jres block="animals dog bark 2" export const animalsDogBark2 = music.fromWAV(hex``); - //% fixedInstance jres block="Animals dog growl" + //% fixedInstance jres block="animals dog growl" export const animalsDogGrowl = music.fromWAV(hex``); - //% fixedInstance jres block="Animals dog sniff" + //% fixedInstance jres block="animals dog sniff" export const animalsDogSniff = music.fromWAV(hex``); - //% fixedInstance jres block="Animals dog whine" + //% fixedInstance jres block="animals dog whine" export const animalsDogWhine = music.fromWAV(hex``); - //% fixedInstance jres block="Animals elephant call" + //% fixedInstance jres block="animals elephant call" export const animalsElephantCall = music.fromWAV(hex``); - //% fixedInstance jres block="Animals insect buzz1" + //% fixedInstance jres block="animals insect buzz1" export const animalsInsectBuzz1 = music.fromWAV(hex``); - //% fixedInstance jres block="Animals insect buzz2" + //% fixedInstance jres block="animals insect buzz2" export const animalsInsectBuzz2 = music.fromWAV(hex``); - //% fixedInstance jres block="Animals insect chirp" + //% fixedInstance jres block="animals insect chirp" export const animalsInsectChirp = music.fromWAV(hex``); - //% fixedInstance jres block="Animals snake hiss" + //% fixedInstance jres block="animals snake hiss" export const animalsSnakeHiss = music.fromWAV(hex``); - //% fixedInstance jres block="Animals snake rattle" + //% fixedInstance jres block="animals snake rattle" export const animalsSnakeRattle = music.fromWAV(hex``); - //% fixedInstance jres block="Animals trex roar" + //% fixedInstance jres block="animals trex roar" export const animalsTRexRoar = music.fromWAV(hex``); - //% fixedInstance jres block="Colors black" + //% fixedInstance jres block="colors black" export const colorsBlack = music.fromWAV(hex``); - //% fixedInstance jres block="Colors blue" + //% fixedInstance jres block="colors blue" export const colorsBlue = music.fromWAV(hex``); - //% fixedInstance jres block="Colors brown" + //% fixedInstance jres block="colors brown" export const colorsBrown = music.fromWAV(hex``); - //% fixedInstance jres block="Colors green" + //% fixedInstance jres block="colors green" export const colorsGreen = music.fromWAV(hex``); - //% fixedInstance jres block="Colors red" + //% fixedInstance jres block="colors red" export const colorsRed = music.fromWAV(hex``); - //% fixedInstance jres block="Colors white" + //% fixedInstance jres block="colors white" export const colorsWhite = music.fromWAV(hex``); - //% fixedInstance jres block="Colors yellow" + //% fixedInstance jres block="colors yellow" export const colorsYellow = music.fromWAV(hex``); - //% fixedInstance jres block="Communication bravo" + //% fixedInstance jres block="communication bravo" export const communicationBravo = music.fromWAV(hex``); - //% fixedInstance jres block="Communication ev3" + //% fixedInstance jres block="communication ev3" export const communicationEv3 = music.fromWAV(hex``); - //% fixedInstance jres block="Communication fantastic" + //% fixedInstance jres block="communication fantastic" export const communicationFantastic = music.fromWAV(hex``); - //% fixedInstance jres block="Communication game over" + //% fixedInstance jres block="communication game over" export const communicationGameOver = music.fromWAV(hex``); - //% fixedInstance jres block="Communication go" + //% fixedInstance jres block="communication go" export const communicationGo = music.fromWAV(hex``); //% fixedInstance jres block="communicationGoodJob" export const communicationGoodJob = music.fromWAV(hex``); @@ -251,7 +251,7 @@ namespace sounds { export const systemPowerDown = music.fromWAV(hex``); //% fixedInstance jres block="system ready" export const systemReady = music.fromWAV(hex``); - //% fixedInstance jres block=S + //% fixedInstance jres block="system start up" export const systemStartUp = music.fromWAV(hex``); } diff --git a/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-jsdoc-strings.json b/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-jsdoc-strings.json index 3f21b280..d5ea29ec 100644 --- a/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-jsdoc-strings.json +++ b/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-jsdoc-strings.json @@ -2,5 +2,8 @@ "sensors.UltraSonicSensor.distance": "Gets the distance from the sonar in centimeters", "sensors.UltraSonicSensor.onEvent": "Registers code to run when the given color is close", "sensors.UltraSonicSensor.onEvent|param|handler": "the code to run when detected", - "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|param|condition": "the dark or bright light condition", + "sensors.UltraSonicSensor.setThreshold|param|value": "the value threshold" } \ 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 467001ec..d5ffd898 100644 --- a/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-strings.json +++ b/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-strings.json @@ -5,10 +5,12 @@ "sensors.UltraSonicSensor.distance|block": "%sensor|distance", "sensors.UltraSonicSensor.onEvent|block": "on %sensor|%event", "sensors.UltraSonicSensor.pauseUntil|block": "pause until %sensor| %event", + "sensors.UltraSonicSensor.setThreshold|block": "set %condition|to %value", "sensors.ultrasonic1|block": "ultrasonic 1", "sensors.ultrasonic2|block": "ultrasonic 2", "sensors.ultrasonic3|block": "ultrasonic 3", "sensors.ultrasonic4|block": "ultrasonic 4", "{id:category}Sensors": "Sensors", + "{id:group}Threshold": "Threshold", "{id:group}Ultrasonic Sensor": "Ultrasonic Sensor" } \ No newline at end of file diff --git a/libs/ultrasonic-sensor/ultrasonic.ts b/libs/ultrasonic-sensor/ultrasonic.ts index 17df8007..fa879b04 100644 --- a/libs/ultrasonic-sensor/ultrasonic.ts +++ b/libs/ultrasonic-sensor/ultrasonic.ts @@ -85,6 +85,22 @@ namespace sensors { this._setMode(0) return this._query(); } + + + /** + * Sets a threshold value + * @param condition the dark or bright light condition + * @param value the value threshold + */ + //% blockId=ultrasonicSetThreshold block="set %condition|to %value" + //% group="Threshold" blockGap=8 + 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; + } + } } //% fixedInstance whenUsed block="ultrasonic 4" jres=icons.port4