From 6b44352839d1ba21db4c31fd753aedfd8d376f75 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Thu, 26 Oct 2017 20:38:17 -0700 Subject: [PATCH] event for ultrasonic module --- libs/core/_locales/core-jsdoc-strings.json | 3 ++ libs/core/_locales/core-strings.json | 1 + libs/core/ultrasonic.ts | 42 +++++++++++++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 7c2a88f8..79da497f 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -38,6 +38,9 @@ "input.TouchSensor.onEvent": "Do something when a touch sensor is touched...", "input.TouchSensor.onEvent|param|body": "code to run when the event is raised", "input.UltraSonicSensor.distance": "Gets the distance from the sonar in millimeters", + "input.UltraSonicSensor.onObject": "Registers code to run when the given color is close", + "input.UltraSonicSensor.onObject|param|distance": "the distance in centimeters when an object is close, eg: 10", + "input.UltraSonicSensor.onObject|param|handler": "the code to run when detected", "input.buttonDown": "Down button on the EV3 Brick.", "input.buttonEnter": "Enter button on the EV3 Brick.", "input.buttonLeft": "Left button on the EV3 Brick.", diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index c6c420c7..15244f77 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -44,6 +44,7 @@ "input.TouchSensor.isTouched|block": "%sensor|is touched", "input.TouchSensor.onEvent|block": "on %sensor|%event", "input.UltraSonicSensor.distance|block": "%sensor|distance", + "input.UltraSonicSensor.onObject|block": "on %sensor|object within %distance|cm", "input.buttonDown|block": "brick button down", "input.buttonEnter|block": "brick button enter", "input.buttonLeft|block": "brick button left", diff --git a/libs/core/ultrasonic.ts b/libs/core/ultrasonic.ts index 79b63a0f..8cdc6981 100644 --- a/libs/core/ultrasonic.ts +++ b/libs/core/ultrasonic.ts @@ -1,15 +1,55 @@ +enum UltrasonicEvent { + ObjectClose = 1, + ObjectDetected = 2 +} + namespace input { //% fixedInstances export class UltraSonicSensor extends internal.UartSensor { + private threshold: number; + constructor(port: number) { super(port) + this.threshold = 10; } _deviceType() { return DAL.DEVICE_TYPE_ULTRASONIC } + _query(): number { + const d = this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff; + return d < this.threshold ? UltrasonicEvent.ObjectClose + : d > this.threshold + 5 ? UltrasonicEvent.ObjectDetected + : 0; + } + + _update(prev: number, curr: number) { + if (curr) + control.raiseEvent(this._id, curr); + } + + /** + * Registers code to run when the given color is close + * @param distance the distance in centimeters when an object is close, eg: 10 + * @param handler the code to run when detected + */ + //% help=input/ultrasonic/on-object + //% block="on %sensor|object within %distance|cm" + //% blockId=ultrasonicOnObjectClose + //% parts="ultrasonicsensor" + //% blockNamespace=input + //% weight=100 blockGap=8 + //% group="Ultrasonic Sensor" + onObject(distance: number, handler: () => void) { + this.threshold = Math.max(1, Math.min(95, distance)); + control.onEvent(this._id, UltrasonicEvent.ObjectClose, handler); + this._setMode(0) + if (this._query() == UltrasonicEvent.ObjectClose) + control.runInBackground(handler); + } + /** * Gets the distance from the sonar in millimeters * @param sensor the ultrasonic sensor port @@ -24,7 +64,7 @@ namespace input { distance() { // it supposedly also has an inch mode, but we stick to mm this._setMode(0) - return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff + return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff; } }