Merge pull request #59 from Microsoft/touch

adding touch button examples
This commit is contained in:
Peli de Halleux 2017-11-28 16:35:48 -08:00 committed by GitHub
commit b9be74bad8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 180 additions and 53 deletions

View File

@ -4,5 +4,12 @@ Here are some fun programs for your @boardname@!
## Fun stuff
Coming soon.
```codecard
[
{
"name": "Happy unhappy",
"description": "Keep your brick entertained and happy",
"url":"/examples/happy-unhappy",
"cardType": "example"
}]
```

View File

@ -0,0 +1,12 @@
# Happy unhappy
Use a touch sensor to make the brick happy.
```blocks
sensors.touchSensor1.onEvent(TouchSensorEvent.Pressed, function () {
brick.showImage(images.expressionsBigSmile)
})
sensors.touchSensor1.onEvent(TouchSensorEvent.Released, function () {
brick.showImage(images.expressionsSick)
})
```

View File

@ -1,3 +1,7 @@
# Reference
TODO
## See Also
[touch sensor](/reference/sensors/touch-sensor)

View File

@ -14,7 +14,6 @@
"MMap.setNumber": "Write a number in specified format in the buffer.",
"MMap.slice": "Read a range of bytes into a buffer.",
"MMap.write": "Perform write(2) on the underlaying file",
"TouchSensorEvent": "Touch sensor interactions",
"brick.Button": "Generic button class, for device buttons and sensors.",
"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.",
@ -27,8 +26,8 @@
"brick.buttonLeft": "Left button on the EV3 Brick.",
"brick.buttonRight": "Right button on the EV3 Brick.",
"brick.buttonUp": "Up button on the EV3 Brick.",
"brick.pattern": "Pattern block.",
"brick.pattern|param|pattern": "the lights pattern to use. eg: LightsPattern.Green",
"brick.lightPattern": "Pattern block.",
"brick.lightPattern|param|pattern": "the lights pattern to use. eg: LightsPattern.Green",
"brick.print": "Show text on the screen.",
"brick.print|param|text": "the text to print on the screen, eg: \"Hello world\"",
"brick.print|param|x": "the starting position's x coordinate, eg: 0",
@ -86,9 +85,6 @@
"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.TouchSensor.isTouched": "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.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",

View File

@ -27,9 +27,6 @@
"Output.B|block": "B",
"Output.C|block": "C",
"Output.D|block": "D",
"TouchSensorEvent.Bumped|block": "bumped",
"TouchSensorEvent.Pressed|block": "pressed",
"TouchSensorEvent.Released|block": "released",
"UltrasonicSensorEvent.ObjectDetected|block": "object detected",
"UltrasonicSensorEvent.ObjectNear|block": "object near",
"brick.Button.isPressed|block": "`icons.brickButtons` %button|is pressed",
@ -41,7 +38,7 @@
"brick.buttonLeft|block": "left",
"brick.buttonRight|block": "right",
"brick.buttonUp|block": "up",
"brick.pattern|block": "%pattern",
"brick.lightPattern|block": "%pattern",
"brick.print|block": "`icons.brickDisplay` print %text| at x: %x| y: %y",
"brick.setPixel|block": "`icons.brickDisplay` set pixel %on| at x: %x| y: %y",
"brick.setStatusLight|block": "set `icons.brickButtons` to %pattern=led_pattern",
@ -82,8 +79,6 @@
"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.TouchSensor.isTouched|block": "`icons.touchSensor` %sensor|is touched",
"sensors.TouchSensor.onEvent|block": "on `icons.touchSensor` %sensor|%event",
"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",
@ -104,10 +99,6 @@
"sensors.remoteButtonCenter|block": "center",
"sensors.remoteButtonTopLeft|block": "top-left",
"sensors.remoteButtonTopRight|block": "top-right",
"sensors.touchSensor1|block": "1",
"sensors.touchSensor2|block": "2",
"sensors.touchSensor3|block": "3",
"sensors.touchSensor4|block": "4",
"sensors.ultrasonic1|block": "1",
"sensors.ultrasonic2|block": "2",
"sensors.ultrasonic3|block": "3",
@ -132,6 +123,5 @@
"{id:group}Motors": "Motors",
"{id:group}Remote Infrared Beacon": "Remote Infrared Beacon",
"{id:group}Screen": "Screen",
"{id:group}Touch Sensor": "Touch Sensor",
"{id:group}Ultrasonic Sensor": "Ultrasonic Sensor"
}

View File

@ -4,34 +4,34 @@
*/
const enum LightsPattern {
//% block=Off enumval=0
//% blockIdentity=output.pattern
//% blockIdentity=brick.lightPattern
Off = 0,
//% block=Green enumval=1
//% blockIdentity=output.pattern
//% blockIdentity=brick.lightPattern
Green = 1,
//% block=Red enumval=2
//% blockIdentity=output.pattern
//% blockIdentity=brick.lightPattern
Red = 2,
//% block=Orange enumval=3
//% blockIdentity=output.pattern
//% blockIdentity=brick.lightPattern
Orange = 3,
//% block="Flashing Green" enumval=4
//% blockIdentity=output.pattern
//% blockIdentity=brick.lightPattern
GreenFlash = 4,
//% block="Flashing Red" enumval=5
//% blockIdentity=output.pattern
//% blockIdentity=brick.lightPattern
RedFlash = 5,
//% block="Flashing Orange" enumval=6
//% blockIdentity=output.pattern
//% blockIdentity=brick.lightPattern
OrangeFlash = 6,
//% block="Pulsing Green" enumval=7
//% blockIdentity=output.pattern
//% blockIdentity=brick.lightPattern
GreenPulse = 7,
//% block="Pulsing Red" enumval=8
//% blockIdentity=output.pattern
//% blockIdentity=brick.lightPattern
RedPulse = 8,
//% block="Pulsing Orange" enumval=9
//% blockIdentity=output.pattern
//% blockIdentity=brick.lightPattern
OrangePulse = 9,
}
@ -69,13 +69,14 @@ namespace brick {
if (this._isPressed == curr) return
this._isPressed = curr
if (curr) {
this._wasPressed = true;
this.downTime = control.millis()
control.raiseEvent(this._id, ButtonEvent.Down)
} else {
control.raiseEvent(this._id, ButtonEvent.Up)
let delta = control.millis() - this.downTime
control.raiseEvent(this._id, ButtonEvent.Click)
//control.raiseEvent(this._id, delta > 500 ? ButtonEvent.LongClick : ButtonEvent.Click)
const delta = control.millis() - this.downTime;
if (delta < 500)
control.raiseEvent(this._id, ButtonEvent.Click)
}
}
@ -252,7 +253,7 @@ namespace brick {
//% blockId=led_pattern block="%pattern"
//% shim=TD_ID colorSecondary="#6e9a36" group="Light"
//% blockHidden=true useEnumVal=1 pattern.fieldOptions.decompileLiterals=1
export function pattern(pattern: LightsPattern): number {
export function lightPattern(pattern: LightsPattern): number {
return pattern;
}
}

View File

@ -10,7 +10,7 @@ namespace control {
this._id = id
}
getId() {
id() {
return this._id;
}
}

View File

@ -132,26 +132,26 @@ namespace sensors.internal {
}
export class Sensor extends control.Component {
protected port: number // this is 0-based
protected _port: number // this is 0-based
constructor(port_: number) {
super()
if (!(1 <= port_ && port_ <= DAL.NUM_INPUTS))
control.panic(120)
this.port = port_ - 1
this._port = port_ - 1
init()
sensorInfos[this.port].sensors.push(this)
sensorInfos[this._port].sensors.push(this)
}
_activated() { }
// 1-based
getPort() {
return this.port + 1
port() {
return this._port + 1
}
isActive() {
return sensorInfos[this.port].sensor == this
return sensorInfos[this._port].sensor == this
}
_query() {
@ -173,7 +173,7 @@ namespace sensors.internal {
_readPin6() {
if (!this.isActive()) return 0
return analogMM.getNumber(NumberFormat.Int16LE, AnalogOff.InPin6 + 2 * this.port)
return analogMM.getNumber(NumberFormat.Int16LE, AnalogOff.InPin6 + 2 * this._port)
}
}
@ -202,18 +202,18 @@ namespace sensors.internal {
if (!this.isActive()) return
if (this.realmode != this.mode) {
this.realmode = v
setUartMode(this.port, v)
setUartMode(this._port, v)
}
}
getBytes(): Buffer {
return getUartBytes(this.isActive() ? this.port : -1)
return getUartBytes(this.isActive() ? this._port : -1)
}
getNumber(fmt: NumberFormat, off: number) {
if (!this.isActive())
return 0
return getUartNumber(fmt, off, this.port)
return getUartNumber(fmt, off, this._port)
}
}

View File

@ -22,7 +22,6 @@
"color.ts",
"gyro.ts",
"ultrasonic.ts",
"touch.ts",
"shims.d.ts",
"enums.d.ts",
"dal.d.ts",

View File

@ -143,7 +143,8 @@ namespace brick {
if (!image) return;
image.draw(0, 0, Draw.Normal);
delay = Math.max(0, delay);
loops.pause(delay);
if (delay > 0)
loops.pause(delay);
}
/**

View File

@ -8,7 +8,8 @@
"dependencies": {
"base": "file:../base",
"core": "file:../core",
"music": "file:../music"
"music": "file:../music",
"touch-sensor": "file:../touch-sensor"
},
"public": true
}

View File

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

View File

@ -0,0 +1,7 @@
{
"TouchSensorEvent": "Touch sensor interactions",
"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.wasPressed": "Check if touch sensor is touched since it was last checked."
}

View File

@ -0,0 +1,14 @@
{
"TouchSensorEvent.Bumped|block": "bumped",
"TouchSensorEvent.Pressed|block": "pressed",
"TouchSensorEvent.Released|block": "released",
"sensors.TouchSensor.isPressed|block": "`icons.touchSensor` %sensor|is pressed",
"sensors.TouchSensor.onEvent|block": "on `icons.touchSensor` %sensor|%event",
"sensors.TouchSensor.wasPressed|block": "`icons.touchSensor` %sensor|was pressed",
"sensors.touchSensor1|block": "1",
"sensors.touchSensor2|block": "2",
"sensors.touchSensor3|block": "3",
"sensors.touchSensor4|block": "4",
"{id:category}Sensors": "Sensors",
"{id:group}Touch Sensor": "Touch Sensor"
}

View File

@ -0,0 +1,15 @@
# Touch Sensor
```cards
sensors.touchSensor1.onEvent(TouchSensorEvent.Pressed, function () {
brick.showImage(images.expressionsBigSmile)
})
sensors.touchSensor1.isPressed();
sensors.touchSensor1.wasPressed();
```
## See Also
[on Event](/reference/sensors/touch-sensor/on-event),
[is pressed](reference/sensors/touch-sensor/is-pressed),
[was pressed](reference/sensors/touch-sensor/was-pressed)

View File

@ -0,0 +1,11 @@
# is Pressed
```blocks
loops.forever(function () {
if (sensors.touchSensor1.isPressed()) {
brick.setStatusLight(LightsPattern.Green)
} else {
brick.setStatusLight(LightsPattern.Orange)
}
})
```

View File

@ -0,0 +1,16 @@
# On Event
```sig
sensors.touchSensor1.onEvent(TouchSensorEvent.Released, function () { })
```
# Parameters
## Examples
```blocks
sensors.touchSensor1.onEvent(TouchSensorEvent.Released, function () {
brick.showImage(images.expressionsSick)
})
```

View File

@ -0,0 +1,11 @@
# was Pressed
```blocks
loops.forever(function () {
if (sensors.touchSensor1.wasPressed()) {
brick.setStatusLight(LightsPattern.Green)
} else {
brick.setStatusLight(LightsPattern.Orange)
}
})
```

View File

@ -0,0 +1,15 @@
{
"name": "touch-sensor",
"description": "Touch Sensor support",
"files": [
"README.md",
"touch.ts"
],
"testFiles": [
"test.ts"
],
"public": true,
"dependencies": {
"core": "file:../core"
}
}

View File

@ -0,0 +1,8 @@
sensors.touchSensor1.onEvent(TouchSensorEvent.Pressed, function () {
})
sensors.touchSensor2.onEvent(TouchSensorEvent.Bumped, function () {
})
sensors.touchSensor3.onEvent(TouchSensorEvent.Released, function () {
})
sensors.touchSensor4.isPressed();
sensors.touchSensor4.wasPressed();

View File

@ -41,7 +41,7 @@ namespace sensors {
* @param event the kind of button gesture that needs to be detected
* @param body code to run when the event is raised
*/
//% help=input/touch/on-event
//% help=input/touch-sensor/on-event
//% blockId=touchEvent block="on `icons.touchSensor` %sensor|%event"
//% parts="touch"
//% blockNamespace=sensors
@ -55,16 +55,31 @@ namespace sensors {
* Check if touch sensor is touched.
* @param sensor the port to query the request
*/
//% help=input/touch/is-touched
//% block="`icons.touchSensor` %sensor|is touched"
//% blockId=touchIsTouched
//% help=input/touch-sensor/is-pressed
//% block="`icons.touchSensor` %sensor|is pressed"
//% blockId=touchIsPressed
//% parts="touch"
//% blockNamespace=sensors
//% weight=81 blockGap=8
//% group="Touch Sensor"
isTouched() {
isPressed() {
return this.button.isPressed();
}
/**
* Check if touch sensor is touched since it was last checked.
* @param sensor the port to query the request
*/
//% help=input/touch-sensor/was-pressed
//% block="`icons.touchSensor` %sensor|was pressed"
//% blockId=touchWasPressed
//% parts="touch"
//% blockNamespace=sensors
//% weight=81 blockGap=8
//% group="Touch Sensor"
wasPressed() {
return this.button.wasPressed();
}
}
//% whenUsed block="1" weight=95 fixedInstance

View File

@ -10,6 +10,7 @@
"libs/base",
"libs/core",
"libs/music",
"libs/touch-sensor",
"libs/ev3"
],
"simulator": {