From 3d8c697586e6a37da7534ba8f4ee377fca86bbfb Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Thu, 30 Nov 2017 09:38:04 -0800 Subject: [PATCH 1/7] moving into separate project --- .../_locales/color-sensor-strings.json | 8 ++--- libs/color-sensor/color.ts | 8 ++--- .../docs/reference/sensors/color-sensor.md | 8 ++--- .../sensors/color-sensor/ambient-light.md | 2 +- .../reference/sensors/color-sensor/color.md | 2 +- .../sensors/color-sensor/on-color-detected.md | 4 +-- .../sensors/color-sensor/reflected-light.md | 2 +- libs/core/_locales/core-jsdoc-strings.json | 6 +--- libs/core/_locales/core-strings.json | 14 ++------ libs/core/ir.ts | 6 ++-- libs/core/pxt.json | 1 - libs/ev3/pxt.json | 3 +- libs/ultrasonic-sensor/README.md | 3 ++ .../ultrasonic-sensor-jsdoc-strings.json | 6 ++++ .../_locales/ultrasonic-sensor-strings.json | 13 +++++++ libs/ultrasonic-sensor/pxt.json | 15 ++++++++ libs/ultrasonic-sensor/test.ts | 0 .../{core => ultrasonic-sensor}/ultrasonic.ts | 34 +++++++++---------- pxtarget.json | 1 + 19 files changed, 79 insertions(+), 57 deletions(-) create mode 100644 libs/ultrasonic-sensor/README.md create mode 100644 libs/ultrasonic-sensor/_locales/ultrasonic-sensor-jsdoc-strings.json create mode 100644 libs/ultrasonic-sensor/_locales/ultrasonic-sensor-strings.json create mode 100644 libs/ultrasonic-sensor/pxt.json create mode 100644 libs/ultrasonic-sensor/test.ts rename libs/{core => ultrasonic-sensor}/ultrasonic.ts (77%) diff --git a/libs/color-sensor/_locales/color-sensor-strings.json b/libs/color-sensor/_locales/color-sensor-strings.json index 3211eddc..bfa0eca8 100644 --- a/libs/color-sensor/_locales/color-sensor-strings.json +++ b/libs/color-sensor/_locales/color-sensor-strings.json @@ -17,10 +17,10 @@ "sensors.ColorSensor.light|block": "`icons.colorSensor` %color|%mode", "sensors.ColorSensor.onColorDetected|block": "on `icons.colorSensor` %sensor|detected color %color", "sensors.ColorSensor.onLightChanged|block": "on `icons.colorSensor` %sensor|%mode|%condition", - "sensors.colorSensor1|block": "1", - "sensors.colorSensor2|block": "2", - "sensors.colorSensor3|block": "3", - "sensors.colorSensor4|block": "4", + "sensors.color1|block": "1", + "sensors.color2|block": "2", + "sensors.color3|block": "3", + "sensors.color4|block": "4", "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 2db3c455..708ea066 100644 --- a/libs/color-sensor/color.ts +++ b/libs/color-sensor/color.ts @@ -172,14 +172,14 @@ namespace sensors { } //% whenUsed block="1" weight=95 fixedInstance - export const colorSensor1: ColorSensor = new ColorSensor(1) + export const color1: ColorSensor = new ColorSensor(1) //% whenUsed block="3" weight=90 fixedInstance - export const colorSensor3: ColorSensor = new ColorSensor(3) + export const color3: ColorSensor = new ColorSensor(3) //% whenUsed block="2" weight=90 fixedInstance - export const colorSensor2: ColorSensor = new ColorSensor(2) + export const color2: ColorSensor = new ColorSensor(2) //% whenUsed block="4" weight=90 fixedInstance - export const colorSensor4: ColorSensor = new ColorSensor(4) + export const color4: ColorSensor = new ColorSensor(4) } diff --git a/libs/color-sensor/docs/reference/sensors/color-sensor.md b/libs/color-sensor/docs/reference/sensors/color-sensor.md index a47b1cc9..f8298b50 100644 --- a/libs/color-sensor/docs/reference/sensors/color-sensor.md +++ b/libs/color-sensor/docs/reference/sensors/color-sensor.md @@ -1,11 +1,11 @@ # Color Sensor ```cards -sensors.colorSensor1.onColorDetected(ColorSensorColor.Blue, function () { +sensors.color1.onColorDetected(ColorSensorColor.Blue, function () { }) -sensors.colorSensor1.color(); -sensors.colorSensor1.ambientLight(); -sensors.colorSensor1.reflectedLight(); +sensors.color1.color(); +sensors.color1.ambientLight(); +sensors.color1.reflectedLight(); ``` ## See Also diff --git a/libs/color-sensor/docs/reference/sensors/color-sensor/ambient-light.md b/libs/color-sensor/docs/reference/sensors/color-sensor/ambient-light.md index 5e82ac7e..cc48c745 100644 --- a/libs/color-sensor/docs/reference/sensors/color-sensor/ambient-light.md +++ b/libs/color-sensor/docs/reference/sensors/color-sensor/ambient-light.md @@ -2,7 +2,7 @@ ```blocks loops.forever(function () { - if (sensors.colorSensor1.ambientLight() > 20) { + if (sensors.color1.ambientLight() > 20) { brick.setStatusLight(LightsPattern.Green) } else { brick.setStatusLight(LightsPattern.Orange) diff --git a/libs/color-sensor/docs/reference/sensors/color-sensor/color.md b/libs/color-sensor/docs/reference/sensors/color-sensor/color.md index 7bc8f3de..f3fe99f9 100644 --- a/libs/color-sensor/docs/reference/sensors/color-sensor/color.md +++ b/libs/color-sensor/docs/reference/sensors/color-sensor/color.md @@ -2,7 +2,7 @@ ```blocks loops.forever(function () { - if (sensors.colorSensor1.color() == ColorSensorColor.Green) { + if (sensors.color1.color() == ColorSensorColor.Green) { brick.setStatusLight(LightsPattern.Green) } else { brick.setStatusLight(LightsPattern.Orange) diff --git a/libs/color-sensor/docs/reference/sensors/color-sensor/on-color-detected.md b/libs/color-sensor/docs/reference/sensors/color-sensor/on-color-detected.md index d69e5ff5..4ded99f4 100644 --- a/libs/color-sensor/docs/reference/sensors/color-sensor/on-color-detected.md +++ b/libs/color-sensor/docs/reference/sensors/color-sensor/on-color-detected.md @@ -1,7 +1,7 @@ # On Color Detected ```sig -sensors.colorSensor1.onColorDetected(ColorSensorColor.Blue, function () { }) +sensors.color1.onColorDetected(ColorSensorColor.Blue, function () { }) ``` # Parameters @@ -10,7 +10,7 @@ sensors.colorSensor1.onColorDetected(ColorSensorColor.Blue, function () { }) ```blocks -sensors.colorSensor1.onColorDetected(ColorSensorColor.Blue, function () { +sensors.color1.onColorDetected(ColorSensorColor.Blue, function () { brick.showImage(images.expressionsSick) }) ``` diff --git a/libs/color-sensor/docs/reference/sensors/color-sensor/reflected-light.md b/libs/color-sensor/docs/reference/sensors/color-sensor/reflected-light.md index fec938b1..e772654a 100644 --- a/libs/color-sensor/docs/reference/sensors/color-sensor/reflected-light.md +++ b/libs/color-sensor/docs/reference/sensors/color-sensor/reflected-light.md @@ -2,7 +2,7 @@ ```blocks loops.forever(function () { - if (sensors.colorSensor1.reflectedLight() > 20) { + if (sensors.color1.reflectedLight() > 20) { brick.setStatusLight(LightsPattern.Green) } else { brick.setStatusLight(LightsPattern.Orange) diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 63126edd..6f82c463 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -74,15 +74,11 @@ "sensors.InfraredSensor.on|param|handler": "the code to run when detected", "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.wait": "Waits for the event to occur", + "sensors.InfraredSensor.waitUntil": "Waits for the event to occur", "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", "sensors.RemoteInfraredBeaconButton.wasPressed": "See if the remote button was pressed again since the last time you checked.", - "sensors.UltraSonicSensor.distance": "Gets the distance from the sonar in millimeters", - "sensors.UltraSonicSensor.on": "Registers code to run when the given color is close", - "sensors.UltraSonicSensor.on|param|handler": "the code to run when detected", - "sensors.UltraSonicSensor.wait": "Waits for the event to occur", "sensors.remoteButtonBottomLeft": "Remote bottom-left button.", "sensors.remoteButtonBottomRight": "Remote bottom-right button.", "sensors.remoteButtonCenter": "Remote beacon (center) button.", diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index ad23c521..0f0ae8bb 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -19,8 +19,6 @@ "Output.B|block": "B", "Output.C|block": "C", "Output.D|block": "D", - "UltrasonicSensorEvent.ObjectDetected|block": "object detected", - "UltrasonicSensorEvent.ObjectNear|block": "object near", "brick.Button.isPressed|block": "`icons.brickButtons` %button|is pressed", "brick.Button.onEvent|block": "on `icons.brickButtons` %button|%event", "brick.Button.wasPressed|block": "`icons.brickButtons` %button|was pressed", @@ -63,13 +61,10 @@ "sensors.InfraredSensor.on|block": "on `icons.infraredSensor` %sensor|%event", "sensors.InfraredSensor.proximity|block": "`icons.infraredSensor` %infrared|proximity", "sensors.InfraredSensor.remoteCommand|block": "`icons.infraredSensor` %infrared|remote command", - "sensors.InfraredSensor.wait|block": "wait `icons.infraredSensor` %sensor|for %event", + "sensors.InfraredSensor.waitUntil|block": "wait `icons.infraredSensor` %sensor|until %event", "sensors.RemoteInfraredBeaconButton.isPressed|block": "`icons.infraredSensor` %button|is pressed", "sensors.RemoteInfraredBeaconButton.onEvent|block": "on `icons.infraredSensor` %button|%event", "sensors.RemoteInfraredBeaconButton.wasPressed|block": "`icons.infraredSensor` %button|was pressed", - "sensors.UltraSonicSensor.distance|block": "`icons.ultrasonicSensor` %sensor|distance", - "sensors.UltraSonicSensor.on|block": "on `icons.ultrasonicSensor` %sensor|%event", - "sensors.UltraSonicSensor.wait|block": "wait `icons.ultrasonicSensor` %sensor|for %event", "sensors.gyro1|block": "1", "sensors.gyro2|block": "2", "sensors.gyro3|block": "3", @@ -83,10 +78,6 @@ "sensors.remoteButtonCenter|block": "center", "sensors.remoteButtonTopLeft|block": "top-left", "sensors.remoteButtonTopRight|block": "top-right", - "sensors.ultrasonic1|block": "1", - "sensors.ultrasonic2|block": "2", - "sensors.ultrasonic3|block": "3", - "sensors.ultrasonic4|block": "4", "sensors|block": "sensors", "serial|block": "serial", "{id:category}Brick": "Brick", @@ -105,6 +96,5 @@ "{id:group}Light": "Light", "{id:group}Motors": "Motors", "{id:group}Remote Infrared Beacon": "Remote Infrared Beacon", - "{id:group}Screen": "Screen", - "{id:group}Ultrasonic Sensor": "Ultrasonic Sensor" + "{id:group}Screen": "Screen" } \ No newline at end of file diff --git a/libs/core/ir.ts b/libs/core/ir.ts index 0129aa0f..17e77f2f 100644 --- a/libs/core/ir.ts +++ b/libs/core/ir.ts @@ -209,14 +209,14 @@ namespace sensors { * Waits for the event to occur */ //% help=input/ultrasonic/wait - //% block="wait `icons.infraredSensor` %sensor|for %event" + //% block="wait `icons.infraredSensor` %sensor|until %event" //% blockId=infraredwait //% parts="infraredsensor" //% blockNamespace=sensors //% weight=99 blockGap=8 //% group="Infrared Sensor" - wait(event: InfraredSensorEvent) { - // TODO + waitUntil(event: InfraredSensorEvent) { + control.waitForEvent(this._id, event); } /** diff --git a/libs/core/pxt.json b/libs/core/pxt.json index de5a8fb6..250ecca5 100644 --- a/libs/core/pxt.json +++ b/libs/core/pxt.json @@ -20,7 +20,6 @@ "input.ts", "ir.ts", "gyro.ts", - "ultrasonic.ts", "shims.d.ts", "enums.d.ts", "dal.d.ts", diff --git a/libs/ev3/pxt.json b/libs/ev3/pxt.json index c99726f7..dcd28778 100644 --- a/libs/ev3/pxt.json +++ b/libs/ev3/pxt.json @@ -10,7 +10,8 @@ "core": "file:../core", "music": "file:../music", "color-sensor": "file:../color-sensor", - "touch-sensor": "file:../touch-sensor" + "touch-sensor": "file:../touch-sensor", + "ultrasonic-sensor": "file:../ultrasonic-sensor" }, "public": true } diff --git a/libs/ultrasonic-sensor/README.md b/libs/ultrasonic-sensor/README.md new file mode 100644 index 00000000..18721304 --- /dev/null +++ b/libs/ultrasonic-sensor/README.md @@ -0,0 +1,3 @@ +# Touch sensor + +The library to interact with the Touch Sensor. \ No newline at end of file diff --git a/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-jsdoc-strings.json b/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-jsdoc-strings.json new file mode 100644 index 00000000..3bb5fc2a --- /dev/null +++ b/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-jsdoc-strings.json @@ -0,0 +1,6 @@ +{ + "sensors.UltraSonicSensor.distance": "Gets the distance from the sonar in millimeters", + "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.waitUntil": "Waits for the event to occur" +} \ 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 new file mode 100644 index 00000000..96d7a0ff --- /dev/null +++ b/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-strings.json @@ -0,0 +1,13 @@ +{ + "UltrasonicSensorEvent.ObjectDetected|block": "object detected", + "UltrasonicSensorEvent.ObjectNear|block": "object near", + "sensors.UltraSonicSensor.distance|block": "`icons.ultrasonicSensor` %sensor|distance", + "sensors.UltraSonicSensor.onEvent|block": "on `icons.ultrasonicSensor` %sensor|%event", + "sensors.UltraSonicSensor.waitUntil|block": "wait `icons.ultrasonicSensor` %sensor|until %event", + "sensors.ultrasonic1|block": "1", + "sensors.ultrasonic2|block": "2", + "sensors.ultrasonic3|block": "3", + "sensors.ultrasonic4|block": "4", + "{id:category}Sensors": "Sensors", + "{id:group}Ultrasonic Sensor": "Ultrasonic Sensor" +} \ No newline at end of file diff --git a/libs/ultrasonic-sensor/pxt.json b/libs/ultrasonic-sensor/pxt.json new file mode 100644 index 00000000..f435a357 --- /dev/null +++ b/libs/ultrasonic-sensor/pxt.json @@ -0,0 +1,15 @@ +{ + "name": "ultrasonic-sensor", + "description": "Ultrasonic Sensor support", + "files": [ + "README.md", + "ultrasonic.ts" + ], + "testFiles": [ + "test.ts" + ], + "public": true, + "dependencies": { + "core": "file:../core" + } +} \ No newline at end of file diff --git a/libs/ultrasonic-sensor/test.ts b/libs/ultrasonic-sensor/test.ts new file mode 100644 index 00000000..e69de29b diff --git a/libs/core/ultrasonic.ts b/libs/ultrasonic-sensor/ultrasonic.ts similarity index 77% rename from libs/core/ultrasonic.ts rename to libs/ultrasonic-sensor/ultrasonic.ts index 069bb664..5466e366 100644 --- a/libs/core/ultrasonic.ts +++ b/libs/ultrasonic-sensor/ultrasonic.ts @@ -1,20 +1,22 @@ -const enum UltrasonicSensorEvent { - //% block="object near" - ObjectNear = 1, +enum UltrasonicSensorEvent { //% block="object detected" - ObjectDetected = 2 + ObjectDetected = 10, + //% block="object near" + ObjectNear = sensors.internal.ThresholdState.Low, + //% block="object far" + ObjectFar = sensors.internal.ThresholdState.High } namespace sensors { //% fixedInstances export class UltraSonicSensor extends internal.UartSensor { - private promixityThreshold: number; + private promixityThreshold: sensors.internal.ThresholdDetector; private movementThreshold: number; constructor(port: number) { super(port) - this.promixityThreshold = 10; + this.promixityThreshold = new sensors.internal.ThresholdDetector(this.id(), 0, 255, 10, 100); // range is 0..255cm this.movementThreshold = 1; } @@ -28,12 +30,11 @@ namespace sensors { _update(prev: number, curr: number) { // is there an object near? - if (prev >= this.promixityThreshold && curr < this.promixityThreshold) - control.raiseEvent(this._id, UltrasonicSensorEvent.ObjectNear); // TODO proper HI-LO sensor + this.promixityThreshold.setLevel(curr); // did something change? if (Math.abs(prev - curr) > this.movementThreshold) - control.raiseEvent(this._id, UltrasonicSensorEvent.ObjectDetected); // TODO debouncing + control.raiseEvent(this._id, UltrasonicSensorEvent.ObjectDetected); } /** @@ -47,25 +48,22 @@ namespace sensors { //% blockNamespace=sensors //% weight=100 blockGap=8 //% group="Ultrasonic Sensor" - on(event: UltrasonicSensorEvent, handler: () => void) { + onEvent(event: UltrasonicSensorEvent, handler: () => void) { control.onEvent(this._id, event, handler); - if (event == UltrasonicSensorEvent.ObjectNear - && this.distance() < this.promixityThreshold) - control.runInBackground(handler); } /** * Waits for the event to occur */ //% help=input/ultrasonic/wait - //% block="wait `icons.ultrasonicSensor` %sensor|for %event" + //% block="wait `icons.ultrasonicSensor` %sensor|until %event" //% blockId=ultrasonicWait //% parts="ultrasonicsensor" //% blockNamespace=sensors //% weight=99 blockGap=8 //% group="Ultrasonic Sensor" - wait(event: UltrasonicSensorEvent) { - // TODO + waitUntil(event: UltrasonicSensorEvent) { + control.waitForEvent(this._id, event); } /** @@ -79,10 +77,10 @@ namespace sensors { //% blockNamespace=sensors //% weight=65 blockGap=8 //% group="Ultrasonic Sensor" - distance() { + distance(): number { // it supposedly also has an inch mode, but we stick to cm this._setMode(0) - return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff; + return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff; // range is 0..255 } } diff --git a/pxtarget.json b/pxtarget.json index 7cc3ec93..cabbb4e5 100644 --- a/pxtarget.json +++ b/pxtarget.json @@ -12,6 +12,7 @@ "libs/music", "libs/color-sensor", "libs/touch-sensor", + "libs/ultrasonic-sensor", "libs/ev3" ], "simulator": { From 808984189209e3e55e71036f510b05aa11f89c01 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Thu, 30 Nov 2017 09:39:24 -0800 Subject: [PATCH 2/7] wait until --- libs/core/_locales/core-strings.json | 2 +- libs/core/ir.ts | 2 +- libs/ultrasonic-sensor/_locales/ultrasonic-sensor-strings.json | 3 ++- libs/ultrasonic-sensor/ultrasonic.ts | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index 0f0ae8bb..2448ecc8 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -61,7 +61,7 @@ "sensors.InfraredSensor.on|block": "on `icons.infraredSensor` %sensor|%event", "sensors.InfraredSensor.proximity|block": "`icons.infraredSensor` %infrared|proximity", "sensors.InfraredSensor.remoteCommand|block": "`icons.infraredSensor` %infrared|remote command", - "sensors.InfraredSensor.waitUntil|block": "wait `icons.infraredSensor` %sensor|until %event", + "sensors.InfraredSensor.waitUntil|block": "wait until `icons.infraredSensor` %sensor| %event", "sensors.RemoteInfraredBeaconButton.isPressed|block": "`icons.infraredSensor` %button|is pressed", "sensors.RemoteInfraredBeaconButton.onEvent|block": "on `icons.infraredSensor` %button|%event", "sensors.RemoteInfraredBeaconButton.wasPressed|block": "`icons.infraredSensor` %button|was pressed", diff --git a/libs/core/ir.ts b/libs/core/ir.ts index 17e77f2f..5235876a 100644 --- a/libs/core/ir.ts +++ b/libs/core/ir.ts @@ -209,7 +209,7 @@ namespace sensors { * Waits for the event to occur */ //% help=input/ultrasonic/wait - //% block="wait `icons.infraredSensor` %sensor|until %event" + //% block="wait until `icons.infraredSensor` %sensor| %event" //% blockId=infraredwait //% parts="infraredsensor" //% blockNamespace=sensors diff --git a/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-strings.json b/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-strings.json index 96d7a0ff..5ec2abf0 100644 --- a/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-strings.json +++ b/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-strings.json @@ -1,9 +1,10 @@ { "UltrasonicSensorEvent.ObjectDetected|block": "object detected", + "UltrasonicSensorEvent.ObjectFar|block": "object far", "UltrasonicSensorEvent.ObjectNear|block": "object near", "sensors.UltraSonicSensor.distance|block": "`icons.ultrasonicSensor` %sensor|distance", "sensors.UltraSonicSensor.onEvent|block": "on `icons.ultrasonicSensor` %sensor|%event", - "sensors.UltraSonicSensor.waitUntil|block": "wait `icons.ultrasonicSensor` %sensor|until %event", + "sensors.UltraSonicSensor.waitUntil|block": "wait until `icons.ultrasonicSensor` %sensor| %event", "sensors.ultrasonic1|block": "1", "sensors.ultrasonic2|block": "2", "sensors.ultrasonic3|block": "3", diff --git a/libs/ultrasonic-sensor/ultrasonic.ts b/libs/ultrasonic-sensor/ultrasonic.ts index 5466e366..e7d8826d 100644 --- a/libs/ultrasonic-sensor/ultrasonic.ts +++ b/libs/ultrasonic-sensor/ultrasonic.ts @@ -56,7 +56,7 @@ namespace sensors { * Waits for the event to occur */ //% help=input/ultrasonic/wait - //% block="wait `icons.ultrasonicSensor` %sensor|until %event" + //% block="wait until `icons.ultrasonicSensor` %sensor| %event" //% blockId=ultrasonicWait //% parts="ultrasonicsensor" //% blockNamespace=sensors From b690c1634bd65982f63fd2a070fc436818149214 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Thu, 30 Nov 2017 09:41:34 -0800 Subject: [PATCH 3/7] updated distance detection --- .../_locales/ultrasonic-sensor-jsdoc-strings.json | 2 +- libs/ultrasonic-sensor/ultrasonic.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-jsdoc-strings.json b/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-jsdoc-strings.json index 3bb5fc2a..9aa4671b 100644 --- a/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-jsdoc-strings.json +++ b/libs/ultrasonic-sensor/_locales/ultrasonic-sensor-jsdoc-strings.json @@ -1,5 +1,5 @@ { - "sensors.UltraSonicSensor.distance": "Gets the distance from the sonar in millimeters", + "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.waitUntil": "Waits for the event to occur" diff --git a/libs/ultrasonic-sensor/ultrasonic.ts b/libs/ultrasonic-sensor/ultrasonic.ts index e7d8826d..ef6628af 100644 --- a/libs/ultrasonic-sensor/ultrasonic.ts +++ b/libs/ultrasonic-sensor/ultrasonic.ts @@ -67,7 +67,7 @@ namespace sensors { } /** - * Gets the distance from the sonar in millimeters + * Gets the distance from the sonar in centimeters * @param sensor the ultrasonic sensor port */ //% help=input/ultrasonic/distance From 32f524ddd8bd30fe8180ff64e52b3f1e80079afb Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Thu, 30 Nov 2017 09:48:43 -0800 Subject: [PATCH 4/7] fixing build --- libs/core/ir.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/core/ir.ts b/libs/core/ir.ts index 5235876a..31af128a 100644 --- a/libs/core/ir.ts +++ b/libs/core/ir.ts @@ -155,8 +155,8 @@ namespace sensors { return mapButton(this.getNumber(NumberFormat.UInt8LE, this.channel)); else if (this.mode == IrSensorMode.Proximity) { const d = this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff; - return d < this.proximityThreshold ? UltrasonicSensorEvent.ObjectNear - : d > this.proximityThreshold + 5 ? UltrasonicSensorEvent.ObjectDetected + return d < this.proximityThreshold ? InfraredSensorEvent.ObjectNear + : d > this.proximityThreshold + 5 ? InfraredSensorEvent.ObjectDetected : 0; } return 0 From 34a5aeb7d2e7b4afbb357571ea1cdf53a776c58f Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Thu, 30 Nov 2017 09:53:43 -0800 Subject: [PATCH 5/7] moving IR as well --- libs/core/_locales/core-jsdoc-strings.json | 14 ----------- libs/core/_locales/core-strings.json | 21 ---------------- libs/core/pxt.json | 1 - libs/ev3/pxt.json | 3 ++- libs/infrared-sensor/README.md | 3 +++ .../infrared-sensor-jsdoc-strings.json | 16 +++++++++++++ .../_locales/infrared-sensor-strings.json | 24 +++++++++++++++++++ libs/{core => infrared-sensor}/ir.ts | 8 +++---- libs/infrared-sensor/pxt.json | 15 ++++++++++++ libs/infrared-sensor/test.ts | 0 libs/ultrasonic-sensor/README.md | 4 ++-- pxtarget.json | 1 + 12 files changed, 66 insertions(+), 44 deletions(-) create mode 100644 libs/infrared-sensor/README.md create mode 100644 libs/infrared-sensor/_locales/infrared-sensor-jsdoc-strings.json create mode 100644 libs/infrared-sensor/_locales/infrared-sensor-strings.json rename libs/{core => infrared-sensor}/ir.ts (97%) create mode 100644 libs/infrared-sensor/pxt.json create mode 100644 libs/infrared-sensor/test.ts diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 6f82c463..7d672f03 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -70,20 +70,6 @@ "screen.unpackPNG": "Decompresses a 1-bit gray scale PNG image to image format.", "sensors.GyroSensor.angle": "Get the current angle from the gyroscope.", "sensors.GyroSensor.rate": "Get the current rotation rate from the gyroscope.", - "sensors.InfraredSensor.on": "Registers code to run when an object is getting near.", - "sensors.InfraredSensor.on|param|handler": "the code to run when detected", - "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.waitUntil": "Waits for the event to occur", - "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", - "sensors.RemoteInfraredBeaconButton.wasPressed": "See if the remote button was pressed again since the last time you checked.", - "sensors.remoteButtonBottomLeft": "Remote bottom-left button.", - "sensors.remoteButtonBottomRight": "Remote bottom-right button.", - "sensors.remoteButtonCenter": "Remote beacon (center) button.", - "sensors.remoteButtonTopLeft": "Remote top-left button.", - "sensors.remoteButtonTopRight": "Remote top-right button.", "serial": "Reading and writing data over a serial connection.", "serial.writeDmesg": "Send DMESG debug buffer over serial." } \ No newline at end of file diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index 2448ecc8..3da8c2f7 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -2,8 +2,6 @@ "ButtonEvent.Click|block": "click", "ButtonEvent.Down|block": "down", "ButtonEvent.Up|block": "up", - "InfraredSensorEvent.ObjectDetected|block": "object detected", - "InfraredSensorEvent.ObjectNear|block": "object near", "LightsPattern.GreenFlash|block": "Flashing Green", "LightsPattern.GreenPulse|block": "Pulsing Green", "LightsPattern.Green|block": "Green", @@ -58,27 +56,10 @@ "screen|block": "screen", "sensors.GyroSensor.angle|block": "`icons.gyroSensor` %sensor|angle", "sensors.GyroSensor.rate|block": "`icons.gyroSensor` %sensor|rotation rate", - "sensors.InfraredSensor.on|block": "on `icons.infraredSensor` %sensor|%event", - "sensors.InfraredSensor.proximity|block": "`icons.infraredSensor` %infrared|proximity", - "sensors.InfraredSensor.remoteCommand|block": "`icons.infraredSensor` %infrared|remote command", - "sensors.InfraredSensor.waitUntil|block": "wait until `icons.infraredSensor` %sensor| %event", - "sensors.RemoteInfraredBeaconButton.isPressed|block": "`icons.infraredSensor` %button|is pressed", - "sensors.RemoteInfraredBeaconButton.onEvent|block": "on `icons.infraredSensor` %button|%event", - "sensors.RemoteInfraredBeaconButton.wasPressed|block": "`icons.infraredSensor` %button|was pressed", "sensors.gyro1|block": "1", "sensors.gyro2|block": "2", "sensors.gyro3|block": "3", "sensors.gyro4|block": "4", - "sensors.infraredSensor1|block": "1", - "sensors.infraredSensor2|block": "2", - "sensors.infraredSensor3|block": "3", - "sensors.infraredSensor4|block": "4", - "sensors.remoteButtonBottomLeft|block": "bottom-left", - "sensors.remoteButtonBottomRight|block": "bottom-right", - "sensors.remoteButtonCenter|block": "center", - "sensors.remoteButtonTopLeft|block": "top-left", - "sensors.remoteButtonTopRight|block": "top-right", - "sensors|block": "sensors", "serial|block": "serial", "{id:category}Brick": "Brick", "{id:category}Control": "Control", @@ -92,9 +73,7 @@ "{id:category}Serial": "Serial", "{id:group}Buttons": "Buttons", "{id:group}Gyro Sensor": "Gyro Sensor", - "{id:group}Infrared Sensor": "Infrared Sensor", "{id:group}Light": "Light", "{id:group}Motors": "Motors", - "{id:group}Remote Infrared Beacon": "Remote Infrared Beacon", "{id:group}Screen": "Screen" } \ No newline at end of file diff --git a/libs/core/pxt.json b/libs/core/pxt.json index 250ecca5..203eb3b7 100644 --- a/libs/core/pxt.json +++ b/libs/core/pxt.json @@ -18,7 +18,6 @@ "output.ts", "core.ts", "input.ts", - "ir.ts", "gyro.ts", "shims.d.ts", "enums.d.ts", diff --git a/libs/ev3/pxt.json b/libs/ev3/pxt.json index dcd28778..6d5ca312 100644 --- a/libs/ev3/pxt.json +++ b/libs/ev3/pxt.json @@ -11,7 +11,8 @@ "music": "file:../music", "color-sensor": "file:../color-sensor", "touch-sensor": "file:../touch-sensor", - "ultrasonic-sensor": "file:../ultrasonic-sensor" + "ultrasonic-sensor": "file:../ultrasonic-sensor", + "infrared-sensor": "file:../infrared-sensor" }, "public": true } diff --git a/libs/infrared-sensor/README.md b/libs/infrared-sensor/README.md new file mode 100644 index 00000000..dc893e0f --- /dev/null +++ b/libs/infrared-sensor/README.md @@ -0,0 +1,3 @@ +# Infrared sensor + +The library to interact with the Infrared Sensor. \ No newline at end of file diff --git a/libs/infrared-sensor/_locales/infrared-sensor-jsdoc-strings.json b/libs/infrared-sensor/_locales/infrared-sensor-jsdoc-strings.json new file mode 100644 index 00000000..85472c80 --- /dev/null +++ b/libs/infrared-sensor/_locales/infrared-sensor-jsdoc-strings.json @@ -0,0 +1,16 @@ +{ + "sensors.InfraredSensor.onEvent": "Registers code to run when an object is getting near.", + "sensors.InfraredSensor.onEvent|param|handler": "the code to run when detected", + "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.waitUntil": "Waits for the event to occur", + "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", + "sensors.RemoteInfraredBeaconButton.wasPressed": "See if the remote button was pressed again since the last time you checked.", + "sensors.remoteButtonBottomLeft": "Remote bottom-left button.", + "sensors.remoteButtonBottomRight": "Remote bottom-right button.", + "sensors.remoteButtonCenter": "Remote beacon (center) button.", + "sensors.remoteButtonTopLeft": "Remote top-left button.", + "sensors.remoteButtonTopRight": "Remote top-right button." +} \ No newline at end of file diff --git a/libs/infrared-sensor/_locales/infrared-sensor-strings.json b/libs/infrared-sensor/_locales/infrared-sensor-strings.json new file mode 100644 index 00000000..ac335a2d --- /dev/null +++ b/libs/infrared-sensor/_locales/infrared-sensor-strings.json @@ -0,0 +1,24 @@ +{ + "InfraredSensorEvent.ObjectDetected|block": "object detected", + "InfraredSensorEvent.ObjectNear|block": "object near", + "sensors.InfraredSensor.onEvent|block": "on `icons.infraredSensor` %sensor|%event", + "sensors.InfraredSensor.proximity|block": "`icons.infraredSensor` %infrared|proximity", + "sensors.InfraredSensor.remoteCommand|block": "`icons.infraredSensor` %infrared|remote command", + "sensors.InfraredSensor.waitUntil|block": "wait until `icons.infraredSensor` %sensor| %event", + "sensors.RemoteInfraredBeaconButton.isPressed|block": "`icons.infraredSensor` %button|is pressed", + "sensors.RemoteInfraredBeaconButton.onEvent|block": "on `icons.infraredSensor` %button|%event", + "sensors.RemoteInfraredBeaconButton.wasPressed|block": "`icons.infraredSensor` %button|was pressed", + "sensors.infraredSensor1|block": "1", + "sensors.infraredSensor2|block": "2", + "sensors.infraredSensor3|block": "3", + "sensors.infraredSensor4|block": "4", + "sensors.remoteButtonBottomLeft|block": "bottom-left", + "sensors.remoteButtonBottomRight|block": "bottom-right", + "sensors.remoteButtonCenter|block": "center", + "sensors.remoteButtonTopLeft|block": "top-left", + "sensors.remoteButtonTopRight|block": "top-right", + "sensors|block": "sensors", + "{id:category}Sensors": "Sensors", + "{id:group}Infrared Sensor": "Infrared Sensor", + "{id:group}Remote Infrared Beacon": "Remote Infrared Beacon" +} \ No newline at end of file diff --git a/libs/core/ir.ts b/libs/infrared-sensor/ir.ts similarity index 97% rename from libs/core/ir.ts rename to libs/infrared-sensor/ir.ts index 31af128a..bf7f3b69 100644 --- a/libs/core/ir.ts +++ b/libs/infrared-sensor/ir.ts @@ -199,10 +199,8 @@ namespace sensors { //% blockNamespace=sensors //% weight=100 blockGap=8 //% group="Infrared Sensor" - on(event: InfraredSensorEvent, handler: () => void) { + onEvent(event: InfraredSensorEvent, handler: () => void) { control.onEvent(this._id, InfraredSensorEvent.ObjectNear, handler); - if ( this.proximity() == InfraredSensorEvent.ObjectNear) - control.runInBackground(handler); } /** @@ -230,7 +228,7 @@ namespace sensors { //% blockNamespace=sensors //% weight=65 blockGap=8 //% group="Infrared Sensor" - proximity() { + proximity(): number { this._setMode(IrSensorMode.Proximity) return this.getNumber(NumberFormat.UInt8LE, 0) } @@ -246,7 +244,7 @@ namespace sensors { //% blockNamespace=sensors //% weight=65 blockGap=8 //% group="Infrared Sensor" - remoteCommand() { + remoteCommand(): number { this._setMode(IrSensorMode.RemoteControl) return this.getNumber(NumberFormat.UInt8LE, this.channel) } diff --git a/libs/infrared-sensor/pxt.json b/libs/infrared-sensor/pxt.json new file mode 100644 index 00000000..2c41e8c1 --- /dev/null +++ b/libs/infrared-sensor/pxt.json @@ -0,0 +1,15 @@ +{ + "name": "infrared-sensor", + "description": "Infrared Sensor support", + "files": [ + "README.md", + "ir.ts" + ], + "testFiles": [ + "test.ts" + ], + "public": true, + "dependencies": { + "core": "file:../core" + } +} \ No newline at end of file diff --git a/libs/infrared-sensor/test.ts b/libs/infrared-sensor/test.ts new file mode 100644 index 00000000..e69de29b diff --git a/libs/ultrasonic-sensor/README.md b/libs/ultrasonic-sensor/README.md index 18721304..914c6342 100644 --- a/libs/ultrasonic-sensor/README.md +++ b/libs/ultrasonic-sensor/README.md @@ -1,3 +1,3 @@ -# Touch sensor +# Ultrasonic sensor -The library to interact with the Touch Sensor. \ No newline at end of file +The library to interact with the Ultrasonic Sensor. \ No newline at end of file diff --git a/pxtarget.json b/pxtarget.json index cabbb4e5..1f16ae36 100644 --- a/pxtarget.json +++ b/pxtarget.json @@ -13,6 +13,7 @@ "libs/color-sensor", "libs/touch-sensor", "libs/ultrasonic-sensor", + "libs/infrared-sensor", "libs/ev3" ], "simulator": { From 305a650125cb262b19581d6089bd250ebc087f5b Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Thu, 30 Nov 2017 09:59:28 -0800 Subject: [PATCH 6/7] using threshold detector --- libs/infrared-sensor/ir.ts | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/libs/infrared-sensor/ir.ts b/libs/infrared-sensor/ir.ts index bf7f3b69..c2788be4 100644 --- a/libs/infrared-sensor/ir.ts +++ b/libs/infrared-sensor/ir.ts @@ -134,16 +134,16 @@ namespace sensors { this.button.onEvent(ev, body); } } - + //% fixedInstances export class InfraredSensor extends internal.UartSensor { private channel: IrRemoteChannel; - private proximityThreshold: number; + private proximityThreshold: sensors.internal.ThresholdDetector; constructor(port: number) { super(port) this.channel = IrRemoteChannel.Ch0 - this.proximityThreshold = 10; + this.proximityThreshold = new sensors.internal.ThresholdDetector(this._id, 0, 100, 10, 90); irButton(0) // make sure buttons array is initalized // and set the mode, as otherwise button events won't work @@ -154,10 +154,7 @@ namespace sensors { if (this.mode == IrSensorMode.RemoteControl) return mapButton(this.getNumber(NumberFormat.UInt8LE, this.channel)); else if (this.mode == IrSensorMode.Proximity) { - const d = this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff; - return d < this.proximityThreshold ? InfraredSensorEvent.ObjectNear - : d > this.proximityThreshold + 5 ? InfraredSensorEvent.ObjectDetected - : 0; + return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff; } return 0 } @@ -168,9 +165,8 @@ namespace sensors { let v = !!(curr & (1 << i)) buttons[i]._update(v) } - } else { - if (curr) - control.raiseEvent(this._id, curr); + } else if (this.mode == IrSensorMode.Proximity) { + this.proximityThreshold.setLevel(curr); } } @@ -181,7 +177,7 @@ namespace sensors { setRemoteChannel(c: IrRemoteChannel) { c = Math.clamp(0, 3, c | 0) this.channel = c - this._setMode(IrSensorMode.RemoteControl) + this.setMode(IrSensorMode.RemoteControl) } setMode(m: IrSensorMode) { @@ -200,7 +196,7 @@ namespace sensors { //% weight=100 blockGap=8 //% group="Infrared Sensor" onEvent(event: InfraredSensorEvent, handler: () => void) { - control.onEvent(this._id, InfraredSensorEvent.ObjectNear, handler); + control.onEvent(this._id, event, handler); } /** @@ -216,7 +212,7 @@ namespace sensors { waitUntil(event: InfraredSensorEvent) { control.waitForEvent(this._id, event); } - + /** * Get the promixity measured by the infrared sensor, from ``0`` (close) to ``100`` (far) * @param ir the infrared sensor @@ -274,7 +270,7 @@ namespace sensors { */ //% whenUsed block="center" weight=95 fixedInstance export const remoteButtonCenter = irButton(IrRemoteButton.CenterBeacon) - + /** * Remote top-left button. */ From a92edcffeeba476aff519bc916861f9b440230af Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Thu, 30 Nov 2017 10:05:00 -0800 Subject: [PATCH 7/7] adding waitUntil to buttons --- libs/core/_locales/core-jsdoc-strings.json | 2 ++ libs/core/_locales/core-strings.json | 1 + libs/core/buttons.ts | 14 ++++++++++++++ .../_locales/touch-sensor-jsdoc-strings.json | 1 + .../_locales/touch-sensor-strings.json | 1 + libs/touch-sensor/touch.ts | 15 +++++++++++++++ 6 files changed, 34 insertions(+) diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 7d672f03..e0872308 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -18,6 +18,8 @@ "brick.Button.isPressed": "Check if button is currently pressed or not.", "brick.Button.onEvent": "Do something when a button or sensor is clicked, up or down.", "brick.Button.onEvent|param|body": "code to run when the event is raised", + "brick.Button.waitUntil": "Waits until the event is raised", + "brick.Button.waitUntil|param|ev": "the event to wait for", "brick.Button.wasPressed": "See if the button was pressed again since the last time you checked.", "brick._imagePicker": "An image", "brick._imagePicker|param|image": "the image", diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index 3da8c2f7..531eeb01 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -19,6 +19,7 @@ "Output.D|block": "D", "brick.Button.isPressed|block": "`icons.brickButtons` %button|is pressed", "brick.Button.onEvent|block": "on `icons.brickButtons` %button|%event", + "brick.Button.waitUntil|block": "wait until `icons.brickButtons` %button|%event", "brick.Button.wasPressed|block": "`icons.brickButtons` %button|was pressed", "brick._imagePicker|block": "%image", "brick.buttonDown|block": "down", diff --git a/libs/core/buttons.ts b/libs/core/buttons.ts index 348fdc13..de1e4cfa 100644 --- a/libs/core/buttons.ts +++ b/libs/core/buttons.ts @@ -127,6 +127,20 @@ namespace brick { onEvent(ev: ButtonEvent, body: () => void) { control.onEvent(this._id, ev, body) } + + /** + * Waits until the event is raised + * @param ev the event to wait for + */ + //% help=input/button/wait-until + //% blockId=buttonWaitUntil block="wait until `icons.brickButtons` %button|%event" + //% parts="brick" + //% blockNamespace=brick + //% weight=98 blockGap=8 + //% group="Buttons" + waitUntil(ev: ButtonEvent) { + control.waitForEvent(this._id, ev); + } } } diff --git a/libs/touch-sensor/_locales/touch-sensor-jsdoc-strings.json b/libs/touch-sensor/_locales/touch-sensor-jsdoc-strings.json index f9ab05fa..71f6c8bf 100644 --- a/libs/touch-sensor/_locales/touch-sensor-jsdoc-strings.json +++ b/libs/touch-sensor/_locales/touch-sensor-jsdoc-strings.json @@ -3,5 +3,6 @@ "sensors.TouchSensor.isPressed": "Check if touch sensor is touched.", "sensors.TouchSensor.onEvent": "Do something when a touch sensor is touched...", "sensors.TouchSensor.onEvent|param|body": "code to run when the event is raised", + "sensors.TouchSensor.waitUntil": "Wait until the touch sensor is touched", "sensors.TouchSensor.wasPressed": "Check if touch sensor is touched since it was last checked." } \ No newline at end of file diff --git a/libs/touch-sensor/_locales/touch-sensor-strings.json b/libs/touch-sensor/_locales/touch-sensor-strings.json index 34887021..fbda688f 100644 --- a/libs/touch-sensor/_locales/touch-sensor-strings.json +++ b/libs/touch-sensor/_locales/touch-sensor-strings.json @@ -4,6 +4,7 @@ "TouchSensorEvent.Released|block": "released", "sensors.TouchSensor.isPressed|block": "`icons.touchSensor` %sensor|is pressed", "sensors.TouchSensor.onEvent|block": "on `icons.touchSensor` %sensor|%event", + "sensors.TouchSensor.waitUntil|block": "wait until `icons.touchSensor` %sensor|%event", "sensors.TouchSensor.wasPressed|block": "`icons.touchSensor` %sensor|was pressed", "sensors.touchSensor1|block": "1", "sensors.touchSensor2|block": "2", diff --git a/libs/touch-sensor/touch.ts b/libs/touch-sensor/touch.ts index bf1a0858..645d69ba 100644 --- a/libs/touch-sensor/touch.ts +++ b/libs/touch-sensor/touch.ts @@ -51,6 +51,21 @@ namespace sensors { this.button.onEvent(ev, body) } + /** + * Wait until the touch sensor is touched + * @param sensor the touch sensor that needs to be clicked or used + * @param event the kind of button gesture that needs to be detected + */ + //% help=input/touch-sensor/wait-until + //% blockId=touchWaitUntil block="wait until `icons.touchSensor` %sensor|%event" + //% parts="touch" + //% blockNamespace=sensors + //% weight=98 blockGap=8 + //% group="Touch Sensor" + waitUntil(ev: TouchSensorEvent) { + this.button.waitUntil(ev); + } + /** * Check if touch sensor is touched. * @param sensor the port to query the request