simplified IR API

This commit is contained in:
Peli de Halleux 2017-10-30 21:39:50 -07:00
parent 16a025f3a0
commit 329a1e15ea
4 changed files with 72 additions and 34 deletions

View File

@ -68,10 +68,11 @@
"sensors.ColorSensor.reflectedLight": "Get current reflected light value from the color sensor.", "sensors.ColorSensor.reflectedLight": "Get current reflected light value from the color sensor.",
"sensors.GyroSensor.angle": "Get the current angle from the gyroscope.", "sensors.GyroSensor.angle": "Get the current angle from the gyroscope.",
"sensors.GyroSensor.rate": "Get the current rotation rate from the gyroscope.", "sensors.GyroSensor.rate": "Get the current rotation rate from the gyroscope.",
"sensors.InfraredSensor.onObjectNear": "Registers code to run when an object is getting near.", "sensors.InfraredSensor.on": "Registers code to run when an object is getting near.",
"sensors.InfraredSensor.onObjectNear|param|handler": "the code to run when detected", "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.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.remoteCommand": "Get the remote commandreceived the infrared sensor.",
"sensors.InfraredSensor.wait": "Waits for the event to occur",
"sensors.RemoteInfraredBeaconButton.isPressed": "Check if a remote button is currently pressed or not.", "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": "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.onEvent|param|body": "code to run when the event is raised",
@ -80,8 +81,9 @@
"sensors.TouchSensor.onEvent": "Do something when a 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.onEvent|param|body": "code to run when the event is raised",
"sensors.UltraSonicSensor.distance": "Gets the distance from the sonar in millimeters", "sensors.UltraSonicSensor.distance": "Gets the distance from the sonar in millimeters",
"sensors.UltraSonicSensor.onObjectNear": "Registers code to run when the given color is close", "sensors.UltraSonicSensor.on": "Registers code to run when the given color is close",
"sensors.UltraSonicSensor.onObjectNear|param|handler": "the code to run when detected", "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.remoteButtonBottomLeft": "Remote bottom-left button.",
"sensors.remoteButtonBottomRight": "Remote bottom-right button.", "sensors.remoteButtonBottomRight": "Remote bottom-right button.",
"sensors.remoteButtonCenter": "Remote beacon (center) button.", "sensors.remoteButtonCenter": "Remote beacon (center) button.",

View File

@ -10,6 +10,8 @@
"ColorSensorColor.Red|block": "red", "ColorSensorColor.Red|block": "red",
"ColorSensorColor.White|block": "white", "ColorSensorColor.White|block": "white",
"ColorSensorColor.Yellow|block": "yellow", "ColorSensorColor.Yellow|block": "yellow",
"InfraredSensorEvent.ObjectDetected|block": "object detected",
"InfraredSensorEvent.ObjectNear|block": "object near",
"LightsPattern.GreenFlash|block": "Flashing Green", "LightsPattern.GreenFlash|block": "Flashing Green",
"LightsPattern.GreenPulse|block": "Pulsing Green", "LightsPattern.GreenPulse|block": "Pulsing Green",
"LightsPattern.Green|block": "Green", "LightsPattern.Green|block": "Green",
@ -25,11 +27,11 @@
"Output.B|block": "B", "Output.B|block": "B",
"Output.C|block": "C", "Output.C|block": "C",
"Output.D|block": "D", "Output.D|block": "D",
"PromixityEvent.ObjectDetected|block": "object detected",
"PromixityEvent.ObjectNear|block": "object near",
"TouchSensorEvent.Bumped|block": "bumped", "TouchSensorEvent.Bumped|block": "bumped",
"TouchSensorEvent.Pressed|block": "pressed", "TouchSensorEvent.Pressed|block": "pressed",
"TouchSensorEvent.Released|block": "released", "TouchSensorEvent.Released|block": "released",
"UltrasonicSensorEvent.ObjectDetected|block": "object detected",
"UltrasonicSensorEvent.ObjectNear|block": "object near",
"brick.Button.isPressed|block": "%button|is pressed", "brick.Button.isPressed|block": "%button|is pressed",
"brick.Button.onEvent|block": "on %button|%event", "brick.Button.onEvent|block": "on %button|%event",
"brick.Button.wasPressed|block": "%button|was pressed", "brick.Button.wasPressed|block": "%button|was pressed",
@ -71,16 +73,18 @@
"sensors.ColorSensor.reflectedLight|block": "%color| reflected light", "sensors.ColorSensor.reflectedLight|block": "%color| reflected light",
"sensors.GyroSensor.angle|block": "%sensor|angle", "sensors.GyroSensor.angle|block": "%sensor|angle",
"sensors.GyroSensor.rate|block": "%sensor|rotation rate", "sensors.GyroSensor.rate|block": "%sensor|rotation rate",
"sensors.InfraredSensor.onObjectNear|block": "on %sensor|object near", "sensors.InfraredSensor.on|block": "on %sensor|%event",
"sensors.InfraredSensor.proximity|block": "%infrared|proximity", "sensors.InfraredSensor.proximity|block": "%infrared|proximity",
"sensors.InfraredSensor.remoteCommand|block": "%infrared|remote command", "sensors.InfraredSensor.remoteCommand|block": "%infrared|remote command",
"sensors.InfraredSensor.wait|block": "wait %sensor|for %event",
"sensors.RemoteInfraredBeaconButton.isPressed|block": "%button|is pressed", "sensors.RemoteInfraredBeaconButton.isPressed|block": "%button|is pressed",
"sensors.RemoteInfraredBeaconButton.onEvent|block": "on %button|%event", "sensors.RemoteInfraredBeaconButton.onEvent|block": "on %button|%event",
"sensors.RemoteInfraredBeaconButton.wasPressed|block": "%button|was pressed", "sensors.RemoteInfraredBeaconButton.wasPressed|block": "%button|was pressed",
"sensors.TouchSensor.isTouched|block": "%sensor|is touched", "sensors.TouchSensor.isTouched|block": "%sensor|is touched",
"sensors.TouchSensor.onEvent|block": "on %sensor|%event", "sensors.TouchSensor.onEvent|block": "on %sensor|%event",
"sensors.UltraSonicSensor.distance|block": "%sensor|distance", "sensors.UltraSonicSensor.distance|block": "%sensor|distance",
"sensors.UltraSonicSensor.onObjectNear|block": "on %sensor|object near", "sensors.UltraSonicSensor.on|block": "on %sensor|%event",
"sensors.UltraSonicSensor.wait|block": "wait %sensor|for %event",
"sensors.color1|block": "color sensor 1", "sensors.color1|block": "color sensor 1",
"sensors.color2|block": "color sensor 2", "sensors.color2|block": "color sensor 2",
"sensors.color3|block": "color sensor 3", "sensors.color3|block": "color sensor 3",

View File

@ -21,6 +21,13 @@ const enum IrRemoteButton {
BottomRight = 0x10, BottomRight = 0x10,
} }
const enum InfraredSensorEvent {
//% block="object near"
ObjectNear = 1,
//% block="object detected"
ObjectDetected = 2
}
namespace sensors { namespace sensors {
function mapButton(v: number) { function mapButton(v: number) {
switch (v) { switch (v) {
@ -148,8 +155,8 @@ namespace sensors {
return mapButton(this.getNumber(NumberFormat.UInt8LE, this.channel)); return mapButton(this.getNumber(NumberFormat.UInt8LE, this.channel));
else if (this.mode == IrSensorMode.Proximity) { else if (this.mode == IrSensorMode.Proximity) {
const d = this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff; const d = this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff;
return d < this.proximityThreshold ? PromixityEvent.ObjectNear return d < this.proximityThreshold ? UltrasonicSensorEvent.ObjectNear
: d > this.proximityThreshold + 5 ? PromixityEvent.ObjectDetected : d > this.proximityThreshold + 5 ? UltrasonicSensorEvent.ObjectDetected
: 0; : 0;
} }
return 0 return 0
@ -185,21 +192,31 @@ namespace sensors {
* Registers code to run when an object is getting near. * Registers code to run when an object is getting near.
* @param handler the code to run when detected * @param handler the code to run when detected
*/ */
//% help=input/infrared/on-object-near //% help=input/infrared/on
//% block="on %sensor|object near" //% block="on %sensor|%event"
//% blockId=infraredOnObjectNear //% blockId=infraredOn
//% parts="infraredsensor" //% parts="infraredsensor"
//% blockNamespace=sensors //% blockNamespace=sensors
//% weight=100 blockGap=8 //% weight=100 blockGap=8
//% group="Infrared Sensor" //% group="Infrared Sensor"
onObjectNear(handler: () => void) { on(event: InfraredSensorEvent, handler: () => void) {
control.onEvent(this._id, PromixityEvent.ObjectNear, handler); control.onEvent(this._id, InfraredSensorEvent.ObjectNear, handler);
if (this.proximity() == PromixityEvent.ObjectNear) if ( this.proximity() == InfraredSensorEvent.ObjectNear)
control.runInBackground(handler); control.runInBackground(handler);
} }
setObjectNearThreshold(distance: number) { /**
this.proximityThreshold = Math.max(1, Math.min(95, distance)); * Waits for the event to occur
*/
//% help=input/ultrasonic/wait
//% block="wait %sensor|for %event"
//% blockId=ultrasonicWait
//% parts="infraredsensor"
//% blockNamespace=sensors
//% weight=99 blockGap=8
//% group="Ultrasonic Sensor"
wait(event: InfraredSensorEvent) {
// TODO
} }
/** /**

View File

@ -1,4 +1,4 @@
const enum PromixityEvent { const enum UltrasonicSensorEvent {
//% block="object near" //% block="object near"
ObjectNear = 1, ObjectNear = 1,
//% block="object detected" //% block="object detected"
@ -10,10 +10,12 @@ namespace sensors {
//% fixedInstances //% fixedInstances
export class UltraSonicSensor extends internal.UartSensor { export class UltraSonicSensor extends internal.UartSensor {
private promixityThreshold: number; private promixityThreshold: number;
private movementThreshold: number;
constructor(port: number) { constructor(port: number) {
super(port) super(port)
this.promixityThreshold = 10; this.promixityThreshold = 10;
this.movementThreshold = 1;
} }
_deviceType() { _deviceType() {
@ -21,36 +23,49 @@ namespace sensors {
} }
_query(): number { _query(): number {
const d = this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff; return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff;
return d < this.promixityThreshold ? PromixityEvent.ObjectNear
: d > this.promixityThreshold + 5 ? PromixityEvent.ObjectDetected
: 0;
} }
_update(prev: number, curr: number) { _update(prev: number, curr: number) {
if (curr) // is there an object near?
control.raiseEvent(this._id, curr); if (prev >= this.promixityThreshold && curr < this.promixityThreshold)
control.raiseEvent(this._id, UltrasonicSensorEvent.ObjectNear); // TODO proper HI-LO sensor
// did something change?
if (Math.abs(prev - curr) > this.movementThreshold)
control.raiseEvent(this._id, UltrasonicSensorEvent.ObjectDetected); // TODO debouncing
} }
/** /**
* Registers code to run when the given color is close * Registers code to run when the given color is close
* @param handler the code to run when detected * @param handler the code to run when detected
*/ */
//% help=input/ultrasonic/on-object-near //% help=input/ultrasonic/on
//% block="on %sensor|object near" //% block="on %sensor|%event"
//% blockId=ultrasonicOnObjectClose //% blockId=ultrasonicOn
//% parts="infraredsensor" //% parts="infraredsensor"
//% blockNamespace=sensors //% blockNamespace=sensors
//% weight=100 blockGap=8 //% weight=100 blockGap=8
//% group="Ultrasonic Sensor" //% group="Ultrasonic Sensor"
onObjectNear(handler: () => void) { on(event: UltrasonicSensorEvent, handler: () => void) {
control.onEvent(this._id, PromixityEvent.ObjectNear, handler); control.onEvent(this._id, event, handler);
if (this.distance() == PromixityEvent.ObjectNear) if (event == UltrasonicSensorEvent.ObjectNear
&& this.distance() < this.promixityThreshold)
control.runInBackground(handler); control.runInBackground(handler);
} }
setObjectNearThreshold(distance: number) { /**
this.promixityThreshold = Math.max(1, Math.min(250, distance)); * Waits for the event to occur
*/
//% help=input/ultrasonic/wait
//% block="wait %sensor|for %event"
//% blockId=ultrasonicWait
//% parts="infraredsensor"
//% blockNamespace=sensors
//% weight=99 blockGap=8
//% group="Ultrasonic Sensor"
wait(event: UltrasonicSensorEvent) {
// TODO
} }
/** /**
@ -65,7 +80,7 @@ namespace sensors {
//% weight=65 blockGap=8 //% weight=65 blockGap=8
//% group="Ultrasonic Sensor" //% group="Ultrasonic Sensor"
distance() { distance() {
// it supposedly also has an inch mode, but we stick to mm // it supposedly also has an inch mode, but we stick to cm
this._setMode(0) this._setMode(0)
return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff; return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff;
} }