Merge pull request #65 from Microsoft/ultrasonicsensor

Ultrasonic/IR sensor refactoring work
This commit is contained in:
Peli de Halleux 2017-11-30 10:14:30 -08:00 committed by GitHub
commit 73b5e7dc3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 186 additions and 111 deletions

View File

@ -17,10 +17,10 @@
"sensors.ColorSensor.light|block": "`icons.colorSensor` %color|%mode", "sensors.ColorSensor.light|block": "`icons.colorSensor` %color|%mode",
"sensors.ColorSensor.onColorDetected|block": "on `icons.colorSensor` %sensor|detected color %color", "sensors.ColorSensor.onColorDetected|block": "on `icons.colorSensor` %sensor|detected color %color",
"sensors.ColorSensor.onLightChanged|block": "on `icons.colorSensor` %sensor|%mode|%condition", "sensors.ColorSensor.onLightChanged|block": "on `icons.colorSensor` %sensor|%mode|%condition",
"sensors.colorSensor1|block": "1", "sensors.color1|block": "1",
"sensors.colorSensor2|block": "2", "sensors.color2|block": "2",
"sensors.colorSensor3|block": "3", "sensors.color3|block": "3",
"sensors.colorSensor4|block": "4", "sensors.color4|block": "4",
"sensors|block": "sensors", "sensors|block": "sensors",
"{id:category}Sensors": "Sensors", "{id:category}Sensors": "Sensors",
"{id:group}Color Sensor": "Color Sensor" "{id:group}Color Sensor": "Color Sensor"

View File

@ -172,14 +172,14 @@ namespace sensors {
} }
//% whenUsed block="1" weight=95 fixedInstance //% 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 //% 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 //% 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 //% whenUsed block="4" weight=90 fixedInstance
export const colorSensor4: ColorSensor = new ColorSensor(4) export const color4: ColorSensor = new ColorSensor(4)
} }

View File

@ -1,11 +1,11 @@
# Color Sensor # Color Sensor
```cards ```cards
sensors.colorSensor1.onColorDetected(ColorSensorColor.Blue, function () { sensors.color1.onColorDetected(ColorSensorColor.Blue, function () {
}) })
sensors.colorSensor1.color(); sensors.color1.color();
sensors.colorSensor1.ambientLight(); sensors.color1.ambientLight();
sensors.colorSensor1.reflectedLight(); sensors.color1.reflectedLight();
``` ```
## See Also ## See Also

View File

@ -2,7 +2,7 @@
```blocks ```blocks
loops.forever(function () { loops.forever(function () {
if (sensors.colorSensor1.ambientLight() > 20) { if (sensors.color1.ambientLight() > 20) {
brick.setStatusLight(LightsPattern.Green) brick.setStatusLight(LightsPattern.Green)
} else { } else {
brick.setStatusLight(LightsPattern.Orange) brick.setStatusLight(LightsPattern.Orange)

View File

@ -2,7 +2,7 @@
```blocks ```blocks
loops.forever(function () { loops.forever(function () {
if (sensors.colorSensor1.color() == ColorSensorColor.Green) { if (sensors.color1.color() == ColorSensorColor.Green) {
brick.setStatusLight(LightsPattern.Green) brick.setStatusLight(LightsPattern.Green)
} else { } else {
brick.setStatusLight(LightsPattern.Orange) brick.setStatusLight(LightsPattern.Orange)

View File

@ -1,7 +1,7 @@
# On Color Detected # On Color Detected
```sig ```sig
sensors.colorSensor1.onColorDetected(ColorSensorColor.Blue, function () { }) sensors.color1.onColorDetected(ColorSensorColor.Blue, function () { })
``` ```
# Parameters # Parameters
@ -10,7 +10,7 @@ sensors.colorSensor1.onColorDetected(ColorSensorColor.Blue, function () { })
```blocks ```blocks
sensors.colorSensor1.onColorDetected(ColorSensorColor.Blue, function () { sensors.color1.onColorDetected(ColorSensorColor.Blue, function () {
brick.showImage(images.expressionsSick) brick.showImage(images.expressionsSick)
}) })
``` ```

View File

@ -2,7 +2,7 @@
```blocks ```blocks
loops.forever(function () { loops.forever(function () {
if (sensors.colorSensor1.reflectedLight() > 20) { if (sensors.color1.reflectedLight() > 20) {
brick.setStatusLight(LightsPattern.Green) brick.setStatusLight(LightsPattern.Green)
} else { } else {
brick.setStatusLight(LightsPattern.Orange) brick.setStatusLight(LightsPattern.Orange)

View File

@ -18,6 +18,8 @@
"brick.Button.isPressed": "Check if button is currently pressed or not.", "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": "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.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.Button.wasPressed": "See if the button was pressed again since the last time you checked.",
"brick._imagePicker": "An image", "brick._imagePicker": "An image",
"brick._imagePicker|param|image": "the image", "brick._imagePicker|param|image": "the image",
@ -70,24 +72,6 @@
"screen.unpackPNG": "Decompresses a 1-bit gray scale PNG image to image format.", "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.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.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.wait": "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.",
"sensors.remoteButtonTopLeft": "Remote top-left button.",
"sensors.remoteButtonTopRight": "Remote top-right button.",
"serial": "Reading and writing data over a serial connection.", "serial": "Reading and writing data over a serial connection.",
"serial.writeDmesg": "Send DMESG debug buffer over serial." "serial.writeDmesg": "Send DMESG debug buffer over serial."
} }

View File

@ -2,8 +2,6 @@
"ButtonEvent.Click|block": "click", "ButtonEvent.Click|block": "click",
"ButtonEvent.Down|block": "down", "ButtonEvent.Down|block": "down",
"ButtonEvent.Up|block": "up", "ButtonEvent.Up|block": "up",
"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",
@ -19,10 +17,9 @@
"Output.B|block": "B", "Output.B|block": "B",
"Output.C|block": "C", "Output.C|block": "C",
"Output.D|block": "D", "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.isPressed|block": "`icons.brickButtons` %button|is pressed",
"brick.Button.onEvent|block": "on `icons.brickButtons` %button|%event", "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.Button.wasPressed|block": "`icons.brickButtons` %button|was pressed",
"brick._imagePicker|block": "%image", "brick._imagePicker|block": "%image",
"brick.buttonDown|block": "down", "brick.buttonDown|block": "down",
@ -60,34 +57,10 @@
"screen|block": "screen", "screen|block": "screen",
"sensors.GyroSensor.angle|block": "`icons.gyroSensor` %sensor|angle", "sensors.GyroSensor.angle|block": "`icons.gyroSensor` %sensor|angle",
"sensors.GyroSensor.rate|block": "`icons.gyroSensor` %sensor|rotation rate", "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.wait|block": "wait `icons.infraredSensor` %sensor|for %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.gyro1|block": "1",
"sensors.gyro2|block": "2", "sensors.gyro2|block": "2",
"sensors.gyro3|block": "3", "sensors.gyro3|block": "3",
"sensors.gyro4|block": "4", "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.ultrasonic1|block": "1",
"sensors.ultrasonic2|block": "2",
"sensors.ultrasonic3|block": "3",
"sensors.ultrasonic4|block": "4",
"sensors|block": "sensors",
"serial|block": "serial", "serial|block": "serial",
"{id:category}Brick": "Brick", "{id:category}Brick": "Brick",
"{id:category}Control": "Control", "{id:category}Control": "Control",
@ -101,10 +74,7 @@
"{id:category}Serial": "Serial", "{id:category}Serial": "Serial",
"{id:group}Buttons": "Buttons", "{id:group}Buttons": "Buttons",
"{id:group}Gyro Sensor": "Gyro Sensor", "{id:group}Gyro Sensor": "Gyro Sensor",
"{id:group}Infrared Sensor": "Infrared Sensor",
"{id:group}Light": "Light", "{id:group}Light": "Light",
"{id:group}Motors": "Motors", "{id:group}Motors": "Motors",
"{id:group}Remote Infrared Beacon": "Remote Infrared Beacon", "{id:group}Screen": "Screen"
"{id:group}Screen": "Screen",
"{id:group}Ultrasonic Sensor": "Ultrasonic Sensor"
} }

View File

@ -127,6 +127,20 @@ namespace brick {
onEvent(ev: ButtonEvent, body: () => void) { onEvent(ev: ButtonEvent, body: () => void) {
control.onEvent(this._id, ev, body) 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);
}
} }
} }

View File

@ -18,9 +18,7 @@
"output.ts", "output.ts",
"core.ts", "core.ts",
"input.ts", "input.ts",
"ir.ts",
"gyro.ts", "gyro.ts",
"ultrasonic.ts",
"shims.d.ts", "shims.d.ts",
"enums.d.ts", "enums.d.ts",
"dal.d.ts", "dal.d.ts",

View File

@ -10,7 +10,9 @@
"core": "file:../core", "core": "file:../core",
"music": "file:../music", "music": "file:../music",
"color-sensor": "file:../color-sensor", "color-sensor": "file:../color-sensor",
"touch-sensor": "file:../touch-sensor" "touch-sensor": "file:../touch-sensor",
"ultrasonic-sensor": "file:../ultrasonic-sensor",
"infrared-sensor": "file:../infrared-sensor"
}, },
"public": true "public": true
} }

View File

@ -0,0 +1,3 @@
# Infrared sensor
The library to interact with the Infrared Sensor.

View File

@ -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."
}

View File

@ -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"
}

View File

@ -138,12 +138,12 @@ namespace sensors {
//% fixedInstances //% fixedInstances
export class InfraredSensor extends internal.UartSensor { export class InfraredSensor extends internal.UartSensor {
private channel: IrRemoteChannel; private channel: IrRemoteChannel;
private proximityThreshold: number; private proximityThreshold: sensors.internal.ThresholdDetector;
constructor(port: number) { constructor(port: number) {
super(port) super(port)
this.channel = IrRemoteChannel.Ch0 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 irButton(0) // make sure buttons array is initalized
// and set the mode, as otherwise button events won't work // and set the mode, as otherwise button events won't work
@ -154,10 +154,7 @@ namespace sensors {
if (this.mode == IrSensorMode.RemoteControl) 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) { else if (this.mode == IrSensorMode.Proximity) {
const d = this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff; return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff;
return d < this.proximityThreshold ? UltrasonicSensorEvent.ObjectNear
: d > this.proximityThreshold + 5 ? UltrasonicSensorEvent.ObjectDetected
: 0;
} }
return 0 return 0
} }
@ -168,9 +165,8 @@ namespace sensors {
let v = !!(curr & (1 << i)) let v = !!(curr & (1 << i))
buttons[i]._update(v) buttons[i]._update(v)
} }
} else { } else if (this.mode == IrSensorMode.Proximity) {
if (curr) this.proximityThreshold.setLevel(curr);
control.raiseEvent(this._id, curr);
} }
} }
@ -181,7 +177,7 @@ namespace sensors {
setRemoteChannel(c: IrRemoteChannel) { setRemoteChannel(c: IrRemoteChannel) {
c = Math.clamp(0, 3, c | 0) c = Math.clamp(0, 3, c | 0)
this.channel = c this.channel = c
this._setMode(IrSensorMode.RemoteControl) this.setMode(IrSensorMode.RemoteControl)
} }
setMode(m: IrSensorMode) { setMode(m: IrSensorMode) {
@ -199,24 +195,22 @@ namespace sensors {
//% blockNamespace=sensors //% blockNamespace=sensors
//% weight=100 blockGap=8 //% weight=100 blockGap=8
//% group="Infrared Sensor" //% group="Infrared Sensor"
on(event: InfraredSensorEvent, handler: () => void) { onEvent(event: InfraredSensorEvent, handler: () => void) {
control.onEvent(this._id, InfraredSensorEvent.ObjectNear, handler); control.onEvent(this._id, event, handler);
if ( this.proximity() == InfraredSensorEvent.ObjectNear)
control.runInBackground(handler);
} }
/** /**
* Waits for the event to occur * Waits for the event to occur
*/ */
//% help=input/ultrasonic/wait //% help=input/ultrasonic/wait
//% block="wait `icons.infraredSensor` %sensor|for %event" //% block="wait until `icons.infraredSensor` %sensor| %event"
//% blockId=infraredwait //% blockId=infraredwait
//% parts="infraredsensor" //% parts="infraredsensor"
//% blockNamespace=sensors //% blockNamespace=sensors
//% weight=99 blockGap=8 //% weight=99 blockGap=8
//% group="Infrared Sensor" //% group="Infrared Sensor"
wait(event: InfraredSensorEvent) { waitUntil(event: InfraredSensorEvent) {
// TODO control.waitForEvent(this._id, event);
} }
/** /**
@ -230,7 +224,7 @@ namespace sensors {
//% blockNamespace=sensors //% blockNamespace=sensors
//% weight=65 blockGap=8 //% weight=65 blockGap=8
//% group="Infrared Sensor" //% group="Infrared Sensor"
proximity() { proximity(): number {
this._setMode(IrSensorMode.Proximity) this._setMode(IrSensorMode.Proximity)
return this.getNumber(NumberFormat.UInt8LE, 0) return this.getNumber(NumberFormat.UInt8LE, 0)
} }
@ -246,7 +240,7 @@ namespace sensors {
//% blockNamespace=sensors //% blockNamespace=sensors
//% weight=65 blockGap=8 //% weight=65 blockGap=8
//% group="Infrared Sensor" //% group="Infrared Sensor"
remoteCommand() { remoteCommand(): number {
this._setMode(IrSensorMode.RemoteControl) this._setMode(IrSensorMode.RemoteControl)
return this.getNumber(NumberFormat.UInt8LE, this.channel) return this.getNumber(NumberFormat.UInt8LE, this.channel)
} }

View File

@ -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"
}
}

View File

View File

@ -3,5 +3,6 @@
"sensors.TouchSensor.isPressed": "Check if touch sensor is touched.", "sensors.TouchSensor.isPressed": "Check if touch sensor is touched.",
"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.TouchSensor.waitUntil": "Wait until the touch sensor is touched",
"sensors.TouchSensor.wasPressed": "Check if touch sensor is touched since it was last checked." "sensors.TouchSensor.wasPressed": "Check if touch sensor is touched since it was last checked."
} }

View File

@ -4,6 +4,7 @@
"TouchSensorEvent.Released|block": "released", "TouchSensorEvent.Released|block": "released",
"sensors.TouchSensor.isPressed|block": "`icons.touchSensor` %sensor|is pressed", "sensors.TouchSensor.isPressed|block": "`icons.touchSensor` %sensor|is pressed",
"sensors.TouchSensor.onEvent|block": "on `icons.touchSensor` %sensor|%event", "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.TouchSensor.wasPressed|block": "`icons.touchSensor` %sensor|was pressed",
"sensors.touchSensor1|block": "1", "sensors.touchSensor1|block": "1",
"sensors.touchSensor2|block": "2", "sensors.touchSensor2|block": "2",

View File

@ -51,6 +51,21 @@ namespace sensors {
this.button.onEvent(<ButtonEvent><number>ev, body) this.button.onEvent(<ButtonEvent><number>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(<ButtonEvent><number>ev);
}
/** /**
* Check if touch sensor is touched. * Check if touch sensor is touched.
* @param sensor the port to query the request * @param sensor the port to query the request

View File

@ -0,0 +1,3 @@
# Ultrasonic sensor
The library to interact with the Ultrasonic Sensor.

View File

@ -0,0 +1,6 @@
{
"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"
}

View File

@ -0,0 +1,14 @@
{
"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 until `icons.ultrasonicSensor` %sensor| %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"
}

View File

@ -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"
}
}

View File

View File

@ -1,20 +1,22 @@
const enum UltrasonicSensorEvent { enum UltrasonicSensorEvent {
//% block="object near"
ObjectNear = 1,
//% block="object detected" //% 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 { namespace sensors {
//% fixedInstances //% fixedInstances
export class UltraSonicSensor extends internal.UartSensor { export class UltraSonicSensor extends internal.UartSensor {
private promixityThreshold: number; private promixityThreshold: sensors.internal.ThresholdDetector;
private movementThreshold: number; private movementThreshold: number;
constructor(port: number) { constructor(port: number) {
super(port) super(port)
this.promixityThreshold = 10; this.promixityThreshold = new sensors.internal.ThresholdDetector(this.id(), 0, 255, 10, 100); // range is 0..255cm
this.movementThreshold = 1; this.movementThreshold = 1;
} }
@ -28,12 +30,11 @@ namespace sensors {
_update(prev: number, curr: number) { _update(prev: number, curr: number) {
// is there an object near? // is there an object near?
if (prev >= this.promixityThreshold && curr < this.promixityThreshold) this.promixityThreshold.setLevel(curr);
control.raiseEvent(this._id, UltrasonicSensorEvent.ObjectNear); // TODO proper HI-LO sensor
// did something change? // did something change?
if (Math.abs(prev - curr) > this.movementThreshold) if (Math.abs(prev - curr) > this.movementThreshold)
control.raiseEvent(this._id, UltrasonicSensorEvent.ObjectDetected); // TODO debouncing control.raiseEvent(this._id, UltrasonicSensorEvent.ObjectDetected);
} }
/** /**
@ -47,29 +48,26 @@ namespace sensors {
//% blockNamespace=sensors //% blockNamespace=sensors
//% weight=100 blockGap=8 //% weight=100 blockGap=8
//% group="Ultrasonic Sensor" //% group="Ultrasonic Sensor"
on(event: UltrasonicSensorEvent, handler: () => void) { onEvent(event: UltrasonicSensorEvent, handler: () => void) {
control.onEvent(this._id, event, handler); control.onEvent(this._id, event, handler);
if (event == UltrasonicSensorEvent.ObjectNear
&& this.distance() < this.promixityThreshold)
control.runInBackground(handler);
} }
/** /**
* Waits for the event to occur * Waits for the event to occur
*/ */
//% help=input/ultrasonic/wait //% help=input/ultrasonic/wait
//% block="wait `icons.ultrasonicSensor` %sensor|for %event" //% block="wait until `icons.ultrasonicSensor` %sensor| %event"
//% blockId=ultrasonicWait //% blockId=ultrasonicWait
//% parts="ultrasonicsensor" //% parts="ultrasonicsensor"
//% blockNamespace=sensors //% blockNamespace=sensors
//% weight=99 blockGap=8 //% weight=99 blockGap=8
//% group="Ultrasonic Sensor" //% group="Ultrasonic Sensor"
wait(event: UltrasonicSensorEvent) { waitUntil(event: UltrasonicSensorEvent) {
// TODO control.waitForEvent(this._id, event);
} }
/** /**
* Gets the distance from the sonar in millimeters * Gets the distance from the sonar in centimeters
* @param sensor the ultrasonic sensor port * @param sensor the ultrasonic sensor port
*/ */
//% help=input/ultrasonic/distance //% help=input/ultrasonic/distance
@ -79,10 +77,10 @@ namespace sensors {
//% blockNamespace=sensors //% blockNamespace=sensors
//% weight=65 blockGap=8 //% weight=65 blockGap=8
//% group="Ultrasonic Sensor" //% group="Ultrasonic Sensor"
distance() { distance(): number {
// it supposedly also has an inch mode, but we stick to cm // 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; // range is 0..255
} }
} }

View File

@ -12,6 +12,8 @@
"libs/music", "libs/music",
"libs/color-sensor", "libs/color-sensor",
"libs/touch-sensor", "libs/touch-sensor",
"libs/ultrasonic-sensor",
"libs/infrared-sensor",
"libs/ev3" "libs/ev3"
], ],
"simulator": { "simulator": {