From 282134f5dc515d5f451147deaea16b3c595cb40e Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Thu, 26 Oct 2017 20:51:13 -0700 Subject: [PATCH] refactoring IR --- libs/core/_locales/core-jsdoc-strings.json | 9 ++- libs/core/_locales/core-strings.json | 9 ++- libs/core/ir.ts | 81 +++++++++++++++------- libs/core/ns.ts | 2 +- libs/core/ultrasonic.ts | 28 ++++---- 5 files changed, 87 insertions(+), 42 deletions(-) diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 79da497f..40c39a29 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -32,15 +32,18 @@ "input.ColorSensor.reflectedLight": "Get current reflected light value from the color sensor.", "input.GyroSensor.angle": "Get the current angle from the gyroscope.", "input.GyroSensor.rate": "Get the current rotation rate from the gyroscope.", + "input.InfraredSensor.onObjectNear": "Registers code to run when an object is getting near", + "input.InfraredSensor.onObjectNear|param|distance": "the proximity, eg: 10", + "input.InfraredSensor.onObjectNear|param|handler": "the code to run when detected", "input.InfraredSensor.proximity": "Get the promixity measured by the infrared sensor, from ``0`` (close) to ``100`` (far)", "input.InfraredSensor.remoteCommand": "Get the remote commandreceived the infrared sensor.", "input.TouchSensor.isTouched": "Check if touch sensor is touched.", "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.UltraSonicSensor.onObjectNear": "Registers code to run when the given color is close", + "input.UltraSonicSensor.onObjectNear|param|distance": "the distance in centimeters when an object is close, eg: 10", + "input.UltraSonicSensor.onObjectNear|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 15244f77..89226773 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -25,6 +25,8 @@ "Output.B|block": "B", "Output.C|block": "C", "Output.D|block": "D", + "PromixityEvent.ObjectDetected|block": "object detected", + "PromixityEvent.ObjectNear|block": "object near", "TouchSensorEvent.Bumped|block": "bumped", "TouchSensorEvent.Pressed|block": "pressed", "TouchSensorEvent.Released|block": "released", @@ -39,12 +41,13 @@ "input.ColorSensor.reflectedLight|block": "%color| reflected light", "input.GyroSensor.angle|block": "%sensor|angle", "input.GyroSensor.rate|block": "%sensor|rotation rate", + "input.InfraredSensor.onObjectNear|block": "on %sensor|object within %distance", "input.InfraredSensor.proximity|block": "%infrared|proximity", "input.InfraredSensor.remoteCommand|block": "%infrared|remote command", "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.UltraSonicSensor.onObjectNear|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", @@ -58,6 +61,10 @@ "input.gyro2|block": "gyro sensor 2", "input.gyro3|block": "gyro sensor 3", "input.gyro4|block": "gyro sensor 4", + "input.infraredSensor1|block": "infrared sensor 1", + "input.infraredSensor2|block": "infrared sensor 2", + "input.infraredSensor3|block": "infrared sensor 3", + "input.infraredSensor4|block": "infrared sensor 4", "input.remoteBottomLeft|block": "remote bottom-left", "input.remoteBottomRight|block": "remote bottom-right", "input.remoteCenter|block": "remote center", diff --git a/libs/core/ir.ts b/libs/core/ir.ts index 5e2849fa..ff5fb7bd 100644 --- a/libs/core/ir.ts +++ b/libs/core/ir.ts @@ -54,10 +54,10 @@ namespace input { } // make sure sensors are up - create(infrared1) - create(infrared2) - create(infrared3) - create(infrared4) + create(infraredSensor1) + create(infraredSensor2) + create(infraredSensor3) + create(infraredSensor4) } let num = -1 @@ -68,30 +68,43 @@ namespace input { num = Math.clamp(0, buttons.length - 1, num) return buttons[num] } - + //% fixedInstances export class InfraredSensor extends internal.UartSensor { - private channel: IrRemoteChannel + private channel: IrRemoteChannel; + private proximityThreshold: number; constructor(port: number) { super(port) this.channel = IrRemoteChannel.Ch0 + this.proximityThreshold = 10; irButton(0) // make sure buttons array is initalized // and set the mode, as otherwise button events won't work - this.mode = IrSensorMode.RemoteControl + this.mode = IrSensorMode.RemoteControl; } _query() { if (this.mode == IrSensorMode.RemoteControl) - return mapButton(this.getNumber(NumberFormat.UInt8LE, this.channel)) + 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 ? PromixityEvent.ObjectNear + : d > this.proximityThreshold + 5 ? PromixityEvent.ObjectDetected + : 0; + } return 0 } _update(prev: number, curr: number) { - for (let i = 0; i < buttons.length; ++i) { - let v = !!(curr & (1 << i)) - buttons[i].update(v) + if (this.mode == IrSensorMode.RemoteControl) { + for (let i = 0; i < buttons.length; ++i) { + let v = !!(curr & (1 << i)) + buttons[i].update(v) + } + } else { + if (curr) + control.raiseEvent(this._id, curr); } } @@ -102,13 +115,33 @@ namespace input { setRemoteChannel(c: IrRemoteChannel) { c = Math.clamp(0, 3, c | 0) this.channel = c - this.setMode(IrSensorMode.RemoteControl) + this._setMode(IrSensorMode.RemoteControl) } - setMode(m: IrSensorMode) { + _setMode(m: IrSensorMode) { this._setMode(m) } + /** + * Registers code to run when an object is getting near + * @param distance the proximity, eg: 10 + * @param handler the code to run when detected + */ + //% help=input/infrared/on-object-near + //% block="on %sensor|object within %distance" + //% blockId=infraredOnObjectNear + //% parts="infraredsensor" + //% blockNamespace=input + //% weight=100 blockGap=8 + //% distance.min=1 distance.max=95 + //% group="Infrared Sensor" + onObjectNear(distance: number, handler: () => void) { + this.proximityThreshold = Math.max(1, Math.min(95, distance)); + control.onEvent(this._id, PromixityEvent.ObjectNear, handler); + if (this.proximity() == PromixityEvent.ObjectNear) + control.runInBackground(handler); + } + /** * Get the promixity measured by the infrared sensor, from ``0`` (close) to ``100`` (far) * @param ir the infrared sensor @@ -121,7 +154,7 @@ namespace input { //% weight=65 blockGap=8 //% group="Infrared Sensor" proximity() { - this.setMode(IrSensorMode.Proximity) + this._setMode(IrSensorMode.Proximity) return this.getNumber(NumberFormat.UInt8LE, 0) } @@ -137,28 +170,28 @@ namespace input { //% weight=65 blockGap=8 //% group="Infrared Sensor" remoteCommand() { - this.setMode(IrSensorMode.RemoteControl) + this._setMode(IrSensorMode.RemoteControl) return this.getNumber(NumberFormat.UInt8LE, this.channel) } // TODO getDirectionAndDistance() { - this.setMode(IrSensorMode.Seek) + this._setMode(IrSensorMode.Seek) return this.getNumber(NumberFormat.UInt16LE, this.channel * 2) } } - //% fixedInstance whenUsed - export const infrared1: InfraredSensor = new InfraredSensor(1) + //% fixedInstance whenUsed block="infrared sensor 1" + export const infraredSensor1: InfraredSensor = new InfraredSensor(1) - //% fixedInstance whenUsed - export const infrared2: InfraredSensor = new InfraredSensor(2) + //% fixedInstance whenUsed block="infrared sensor 2" + export const infraredSensor2: InfraredSensor = new InfraredSensor(2) - //% fixedInstance whenUsed - export const infrared3: InfraredSensor = new InfraredSensor(3) + //% fixedInstance whenUsed block="infrared sensor 3" + export const infraredSensor3: InfraredSensor = new InfraredSensor(3) - //% fixedInstance whenUsed - export const infrared4: InfraredSensor = new InfraredSensor(4) + //% fixedInstance whenUsed block="infrared sensor 4" + export const infraredSensor4: InfraredSensor = new InfraredSensor(4) /** * Remote top-left button. diff --git a/libs/core/ns.ts b/libs/core/ns.ts index 14238015..7d1eea57 100644 --- a/libs/core/ns.ts +++ b/libs/core/ns.ts @@ -1,5 +1,5 @@ //% color="#B4009E" weight=98 icon="\uf192" -//% groups='["Touch Sensor", "Color Sensor", "Ultrasonic Sensor", "Infrared Sensor", "Remote", "Brick", "Gyro Sensor"]' +//% groups='["Brick", "Touch Sensor", "Color Sensor", "Ultrasonic Sensor", "Infrared Sensor", "Remote", "Gyro Sensor"]' namespace input { } diff --git a/libs/core/ultrasonic.ts b/libs/core/ultrasonic.ts index 8cdc6981..008be83d 100644 --- a/libs/core/ultrasonic.ts +++ b/libs/core/ultrasonic.ts @@ -1,5 +1,7 @@ -enum UltrasonicEvent { - ObjectClose = 1, +const enum PromixityEvent { + //% block="object near" + ObjectNear = 1, + //% block="object detected" ObjectDetected = 2 } @@ -7,11 +9,11 @@ namespace input { //% fixedInstances export class UltraSonicSensor extends internal.UartSensor { - private threshold: number; + private promixityThreshold: number; constructor(port: number) { super(port) - this.threshold = 10; + this.promixityThreshold = 10; } _deviceType() { @@ -20,8 +22,8 @@ namespace input { _query(): number { const d = this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff; - return d < this.threshold ? UltrasonicEvent.ObjectClose - : d > this.threshold + 5 ? UltrasonicEvent.ObjectDetected + return d < this.promixityThreshold ? PromixityEvent.ObjectNear + : d > this.promixityThreshold + 5 ? PromixityEvent.ObjectDetected : 0; } @@ -35,18 +37,18 @@ namespace input { * @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 + //% help=input/ultrasonic/on-object-near //% block="on %sensor|object within %distance|cm" //% blockId=ultrasonicOnObjectClose - //% parts="ultrasonicsensor" + //% parts="infraredsensor" //% 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) + //% distance.min=1 distance.max=250 + onObjectNear(distance: number, handler: () => void) { + this.promixityThreshold = Math.max(1, Math.min(95, distance)); + control.onEvent(this._id, PromixityEvent.ObjectNear, handler); + if (this.distance() == PromixityEvent.ObjectNear) control.runInBackground(handler); }