Merge pull request #59 from Microsoft/touch
adding touch button examples
This commit is contained in:
commit
b9be74bad8
@ -4,5 +4,12 @@ Here are some fun programs for your @boardname@!
|
|||||||
|
|
||||||
## Fun stuff
|
## Fun stuff
|
||||||
|
|
||||||
|
```codecard
|
||||||
Coming soon.
|
[
|
||||||
|
{
|
||||||
|
"name": "Happy unhappy",
|
||||||
|
"description": "Keep your brick entertained and happy",
|
||||||
|
"url":"/examples/happy-unhappy",
|
||||||
|
"cardType": "example"
|
||||||
|
}]
|
||||||
|
```
|
12
docs/examples/happy-unhappy.md
Normal file
12
docs/examples/happy-unhappy.md
Normal 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)
|
||||||
|
})
|
||||||
|
```
|
@ -1,3 +1,7 @@
|
|||||||
# Reference
|
# Reference
|
||||||
|
|
||||||
TODO
|
|
||||||
|
|
||||||
|
## See Also
|
||||||
|
|
||||||
|
[touch sensor](/reference/sensors/touch-sensor)
|
@ -14,7 +14,6 @@
|
|||||||
"MMap.setNumber": "Write a number in specified format in the buffer.",
|
"MMap.setNumber": "Write a number in specified format in the buffer.",
|
||||||
"MMap.slice": "Read a range of bytes into a buffer.",
|
"MMap.slice": "Read a range of bytes into a buffer.",
|
||||||
"MMap.write": "Perform write(2) on the underlaying file",
|
"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": "Generic button class, for device buttons and sensors.",
|
||||||
"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.",
|
||||||
@ -27,8 +26,8 @@
|
|||||||
"brick.buttonLeft": "Left button on the EV3 Brick.",
|
"brick.buttonLeft": "Left button on the EV3 Brick.",
|
||||||
"brick.buttonRight": "Right button on the EV3 Brick.",
|
"brick.buttonRight": "Right button on the EV3 Brick.",
|
||||||
"brick.buttonUp": "Up button on the EV3 Brick.",
|
"brick.buttonUp": "Up button on the EV3 Brick.",
|
||||||
"brick.pattern": "Pattern block.",
|
"brick.lightPattern": "Pattern block.",
|
||||||
"brick.pattern|param|pattern": "the lights pattern to use. eg: LightsPattern.Green",
|
"brick.lightPattern|param|pattern": "the lights pattern to use. eg: LightsPattern.Green",
|
||||||
"brick.print": "Show text on the screen.",
|
"brick.print": "Show text on the screen.",
|
||||||
"brick.print|param|text": "the text to print on the screen, eg: \"Hello world\"",
|
"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",
|
"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": "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",
|
||||||
"sensors.RemoteInfraredBeaconButton.wasPressed": "See if the remote button was pressed again since the last time you checked.",
|
"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.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": "Registers code to run when the given color is close",
|
||||||
"sensors.UltraSonicSensor.on|param|handler": "the code to run when detected",
|
"sensors.UltraSonicSensor.on|param|handler": "the code to run when detected",
|
||||||
|
@ -27,9 +27,6 @@
|
|||||||
"Output.B|block": "B",
|
"Output.B|block": "B",
|
||||||
"Output.C|block": "C",
|
"Output.C|block": "C",
|
||||||
"Output.D|block": "D",
|
"Output.D|block": "D",
|
||||||
"TouchSensorEvent.Bumped|block": "bumped",
|
|
||||||
"TouchSensorEvent.Pressed|block": "pressed",
|
|
||||||
"TouchSensorEvent.Released|block": "released",
|
|
||||||
"UltrasonicSensorEvent.ObjectDetected|block": "object detected",
|
"UltrasonicSensorEvent.ObjectDetected|block": "object detected",
|
||||||
"UltrasonicSensorEvent.ObjectNear|block": "object near",
|
"UltrasonicSensorEvent.ObjectNear|block": "object near",
|
||||||
"brick.Button.isPressed|block": "`icons.brickButtons` %button|is pressed",
|
"brick.Button.isPressed|block": "`icons.brickButtons` %button|is pressed",
|
||||||
@ -41,7 +38,7 @@
|
|||||||
"brick.buttonLeft|block": "left",
|
"brick.buttonLeft|block": "left",
|
||||||
"brick.buttonRight|block": "right",
|
"brick.buttonRight|block": "right",
|
||||||
"brick.buttonUp|block": "up",
|
"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.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.setPixel|block": "`icons.brickDisplay` set pixel %on| at x: %x| y: %y",
|
||||||
"brick.setStatusLight|block": "set `icons.brickButtons` to %pattern=led_pattern",
|
"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.isPressed|block": "`icons.infraredSensor` %button|is pressed",
|
||||||
"sensors.RemoteInfraredBeaconButton.onEvent|block": "on `icons.infraredSensor` %button|%event",
|
"sensors.RemoteInfraredBeaconButton.onEvent|block": "on `icons.infraredSensor` %button|%event",
|
||||||
"sensors.RemoteInfraredBeaconButton.wasPressed|block": "`icons.infraredSensor` %button|was pressed",
|
"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.distance|block": "`icons.ultrasonicSensor` %sensor|distance",
|
||||||
"sensors.UltraSonicSensor.on|block": "on `icons.ultrasonicSensor` %sensor|%event",
|
"sensors.UltraSonicSensor.on|block": "on `icons.ultrasonicSensor` %sensor|%event",
|
||||||
"sensors.UltraSonicSensor.wait|block": "wait `icons.ultrasonicSensor` %sensor|for %event",
|
"sensors.UltraSonicSensor.wait|block": "wait `icons.ultrasonicSensor` %sensor|for %event",
|
||||||
@ -104,10 +99,6 @@
|
|||||||
"sensors.remoteButtonCenter|block": "center",
|
"sensors.remoteButtonCenter|block": "center",
|
||||||
"sensors.remoteButtonTopLeft|block": "top-left",
|
"sensors.remoteButtonTopLeft|block": "top-left",
|
||||||
"sensors.remoteButtonTopRight|block": "top-right",
|
"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.ultrasonic1|block": "1",
|
||||||
"sensors.ultrasonic2|block": "2",
|
"sensors.ultrasonic2|block": "2",
|
||||||
"sensors.ultrasonic3|block": "3",
|
"sensors.ultrasonic3|block": "3",
|
||||||
@ -132,6 +123,5 @@
|
|||||||
"{id:group}Motors": "Motors",
|
"{id:group}Motors": "Motors",
|
||||||
"{id:group}Remote Infrared Beacon": "Remote Infrared Beacon",
|
"{id:group}Remote Infrared Beacon": "Remote Infrared Beacon",
|
||||||
"{id:group}Screen": "Screen",
|
"{id:group}Screen": "Screen",
|
||||||
"{id:group}Touch Sensor": "Touch Sensor",
|
|
||||||
"{id:group}Ultrasonic Sensor": "Ultrasonic Sensor"
|
"{id:group}Ultrasonic Sensor": "Ultrasonic Sensor"
|
||||||
}
|
}
|
@ -4,34 +4,34 @@
|
|||||||
*/
|
*/
|
||||||
const enum LightsPattern {
|
const enum LightsPattern {
|
||||||
//% block=Off enumval=0
|
//% block=Off enumval=0
|
||||||
//% blockIdentity=output.pattern
|
//% blockIdentity=brick.lightPattern
|
||||||
Off = 0,
|
Off = 0,
|
||||||
//% block=Green enumval=1
|
//% block=Green enumval=1
|
||||||
//% blockIdentity=output.pattern
|
//% blockIdentity=brick.lightPattern
|
||||||
Green = 1,
|
Green = 1,
|
||||||
//% block=Red enumval=2
|
//% block=Red enumval=2
|
||||||
//% blockIdentity=output.pattern
|
//% blockIdentity=brick.lightPattern
|
||||||
Red = 2,
|
Red = 2,
|
||||||
//% block=Orange enumval=3
|
//% block=Orange enumval=3
|
||||||
//% blockIdentity=output.pattern
|
//% blockIdentity=brick.lightPattern
|
||||||
Orange = 3,
|
Orange = 3,
|
||||||
//% block="Flashing Green" enumval=4
|
//% block="Flashing Green" enumval=4
|
||||||
//% blockIdentity=output.pattern
|
//% blockIdentity=brick.lightPattern
|
||||||
GreenFlash = 4,
|
GreenFlash = 4,
|
||||||
//% block="Flashing Red" enumval=5
|
//% block="Flashing Red" enumval=5
|
||||||
//% blockIdentity=output.pattern
|
//% blockIdentity=brick.lightPattern
|
||||||
RedFlash = 5,
|
RedFlash = 5,
|
||||||
//% block="Flashing Orange" enumval=6
|
//% block="Flashing Orange" enumval=6
|
||||||
//% blockIdentity=output.pattern
|
//% blockIdentity=brick.lightPattern
|
||||||
OrangeFlash = 6,
|
OrangeFlash = 6,
|
||||||
//% block="Pulsing Green" enumval=7
|
//% block="Pulsing Green" enumval=7
|
||||||
//% blockIdentity=output.pattern
|
//% blockIdentity=brick.lightPattern
|
||||||
GreenPulse = 7,
|
GreenPulse = 7,
|
||||||
//% block="Pulsing Red" enumval=8
|
//% block="Pulsing Red" enumval=8
|
||||||
//% blockIdentity=output.pattern
|
//% blockIdentity=brick.lightPattern
|
||||||
RedPulse = 8,
|
RedPulse = 8,
|
||||||
//% block="Pulsing Orange" enumval=9
|
//% block="Pulsing Orange" enumval=9
|
||||||
//% blockIdentity=output.pattern
|
//% blockIdentity=brick.lightPattern
|
||||||
OrangePulse = 9,
|
OrangePulse = 9,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,13 +69,14 @@ namespace brick {
|
|||||||
if (this._isPressed == curr) return
|
if (this._isPressed == curr) return
|
||||||
this._isPressed = curr
|
this._isPressed = curr
|
||||||
if (curr) {
|
if (curr) {
|
||||||
|
this._wasPressed = true;
|
||||||
this.downTime = control.millis()
|
this.downTime = control.millis()
|
||||||
control.raiseEvent(this._id, ButtonEvent.Down)
|
control.raiseEvent(this._id, ButtonEvent.Down)
|
||||||
} else {
|
} else {
|
||||||
control.raiseEvent(this._id, ButtonEvent.Up)
|
control.raiseEvent(this._id, ButtonEvent.Up)
|
||||||
let delta = control.millis() - this.downTime
|
const delta = control.millis() - this.downTime;
|
||||||
|
if (delta < 500)
|
||||||
control.raiseEvent(this._id, ButtonEvent.Click)
|
control.raiseEvent(this._id, ButtonEvent.Click)
|
||||||
//control.raiseEvent(this._id, delta > 500 ? ButtonEvent.LongClick : ButtonEvent.Click)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,7 +253,7 @@ namespace brick {
|
|||||||
//% blockId=led_pattern block="%pattern"
|
//% blockId=led_pattern block="%pattern"
|
||||||
//% shim=TD_ID colorSecondary="#6e9a36" group="Light"
|
//% shim=TD_ID colorSecondary="#6e9a36" group="Light"
|
||||||
//% blockHidden=true useEnumVal=1 pattern.fieldOptions.decompileLiterals=1
|
//% blockHidden=true useEnumVal=1 pattern.fieldOptions.decompileLiterals=1
|
||||||
export function pattern(pattern: LightsPattern): number {
|
export function lightPattern(pattern: LightsPattern): number {
|
||||||
return pattern;
|
return pattern;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ namespace control {
|
|||||||
this._id = id
|
this._id = id
|
||||||
}
|
}
|
||||||
|
|
||||||
getId() {
|
id() {
|
||||||
return this._id;
|
return this._id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -132,26 +132,26 @@ namespace sensors.internal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class Sensor extends control.Component {
|
export class Sensor extends control.Component {
|
||||||
protected port: number // this is 0-based
|
protected _port: number // this is 0-based
|
||||||
|
|
||||||
constructor(port_: number) {
|
constructor(port_: number) {
|
||||||
super()
|
super()
|
||||||
if (!(1 <= port_ && port_ <= DAL.NUM_INPUTS))
|
if (!(1 <= port_ && port_ <= DAL.NUM_INPUTS))
|
||||||
control.panic(120)
|
control.panic(120)
|
||||||
this.port = port_ - 1
|
this._port = port_ - 1
|
||||||
init()
|
init()
|
||||||
sensorInfos[this.port].sensors.push(this)
|
sensorInfos[this._port].sensors.push(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
_activated() { }
|
_activated() { }
|
||||||
|
|
||||||
// 1-based
|
// 1-based
|
||||||
getPort() {
|
port() {
|
||||||
return this.port + 1
|
return this._port + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
isActive() {
|
isActive() {
|
||||||
return sensorInfos[this.port].sensor == this
|
return sensorInfos[this._port].sensor == this
|
||||||
}
|
}
|
||||||
|
|
||||||
_query() {
|
_query() {
|
||||||
@ -173,7 +173,7 @@ namespace sensors.internal {
|
|||||||
|
|
||||||
_readPin6() {
|
_readPin6() {
|
||||||
if (!this.isActive()) return 0
|
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.isActive()) return
|
||||||
if (this.realmode != this.mode) {
|
if (this.realmode != this.mode) {
|
||||||
this.realmode = v
|
this.realmode = v
|
||||||
setUartMode(this.port, v)
|
setUartMode(this._port, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getBytes(): Buffer {
|
getBytes(): Buffer {
|
||||||
return getUartBytes(this.isActive() ? this.port : -1)
|
return getUartBytes(this.isActive() ? this._port : -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
getNumber(fmt: NumberFormat, off: number) {
|
getNumber(fmt: NumberFormat, off: number) {
|
||||||
if (!this.isActive())
|
if (!this.isActive())
|
||||||
return 0
|
return 0
|
||||||
return getUartNumber(fmt, off, this.port)
|
return getUartNumber(fmt, off, this._port)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,7 +22,6 @@
|
|||||||
"color.ts",
|
"color.ts",
|
||||||
"gyro.ts",
|
"gyro.ts",
|
||||||
"ultrasonic.ts",
|
"ultrasonic.ts",
|
||||||
"touch.ts",
|
|
||||||
"shims.d.ts",
|
"shims.d.ts",
|
||||||
"enums.d.ts",
|
"enums.d.ts",
|
||||||
"dal.d.ts",
|
"dal.d.ts",
|
||||||
|
@ -143,6 +143,7 @@ namespace brick {
|
|||||||
if (!image) return;
|
if (!image) return;
|
||||||
image.draw(0, 0, Draw.Normal);
|
image.draw(0, 0, Draw.Normal);
|
||||||
delay = Math.max(0, delay);
|
delay = Math.max(0, delay);
|
||||||
|
if (delay > 0)
|
||||||
loops.pause(delay);
|
loops.pause(delay);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,7 +8,8 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"base": "file:../base",
|
"base": "file:../base",
|
||||||
"core": "file:../core",
|
"core": "file:../core",
|
||||||
"music": "file:../music"
|
"music": "file:../music",
|
||||||
|
"touch-sensor": "file:../touch-sensor"
|
||||||
},
|
},
|
||||||
"public": true
|
"public": true
|
||||||
}
|
}
|
||||||
|
3
libs/touch-sensor/README.md
Normal file
3
libs/touch-sensor/README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Touch sensor
|
||||||
|
|
||||||
|
The library to interact with the Touch Sensor.
|
@ -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."
|
||||||
|
}
|
14
libs/touch-sensor/_locales/touch-sensor-strings.json
Normal file
14
libs/touch-sensor/_locales/touch-sensor-strings.json
Normal 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"
|
||||||
|
}
|
15
libs/touch-sensor/docs/reference/sensors/touch-sensor.md
Normal file
15
libs/touch-sensor/docs/reference/sensors/touch-sensor.md
Normal 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)
|
@ -0,0 +1,11 @@
|
|||||||
|
# is Pressed
|
||||||
|
|
||||||
|
```blocks
|
||||||
|
loops.forever(function () {
|
||||||
|
if (sensors.touchSensor1.isPressed()) {
|
||||||
|
brick.setStatusLight(LightsPattern.Green)
|
||||||
|
} else {
|
||||||
|
brick.setStatusLight(LightsPattern.Orange)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
@ -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)
|
||||||
|
})
|
||||||
|
```
|
@ -0,0 +1,11 @@
|
|||||||
|
# was Pressed
|
||||||
|
|
||||||
|
```blocks
|
||||||
|
loops.forever(function () {
|
||||||
|
if (sensors.touchSensor1.wasPressed()) {
|
||||||
|
brick.setStatusLight(LightsPattern.Green)
|
||||||
|
} else {
|
||||||
|
brick.setStatusLight(LightsPattern.Orange)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
15
libs/touch-sensor/pxt.json
Normal file
15
libs/touch-sensor/pxt.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
8
libs/touch-sensor/test.ts
Normal file
8
libs/touch-sensor/test.ts
Normal 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();
|
@ -41,7 +41,7 @@ namespace sensors {
|
|||||||
* @param event the kind of button gesture that needs to be detected
|
* @param event the kind of button gesture that needs to be detected
|
||||||
* @param body code to run when the event is raised
|
* @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"
|
//% blockId=touchEvent block="on `icons.touchSensor` %sensor|%event"
|
||||||
//% parts="touch"
|
//% parts="touch"
|
||||||
//% blockNamespace=sensors
|
//% blockNamespace=sensors
|
||||||
@ -55,16 +55,31 @@ namespace sensors {
|
|||||||
* 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
|
||||||
*/
|
*/
|
||||||
//% help=input/touch/is-touched
|
//% help=input/touch-sensor/is-pressed
|
||||||
//% block="`icons.touchSensor` %sensor|is touched"
|
//% block="`icons.touchSensor` %sensor|is pressed"
|
||||||
//% blockId=touchIsTouched
|
//% blockId=touchIsPressed
|
||||||
//% parts="touch"
|
//% parts="touch"
|
||||||
//% blockNamespace=sensors
|
//% blockNamespace=sensors
|
||||||
//% weight=81 blockGap=8
|
//% weight=81 blockGap=8
|
||||||
//% group="Touch Sensor"
|
//% group="Touch Sensor"
|
||||||
isTouched() {
|
isPressed() {
|
||||||
return this.button.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
|
//% whenUsed block="1" weight=95 fixedInstance
|
@ -10,6 +10,7 @@
|
|||||||
"libs/base",
|
"libs/base",
|
||||||
"libs/core",
|
"libs/core",
|
||||||
"libs/music",
|
"libs/music",
|
||||||
|
"libs/touch-sensor",
|
||||||
"libs/ev3"
|
"libs/ev3"
|
||||||
],
|
],
|
||||||
"simulator": {
|
"simulator": {
|
||||||
|
Loading…
Reference in New Issue
Block a user