pxt-ev3/libs/core/touch.ts

79 lines
2.3 KiB
TypeScript
Raw Normal View History

2017-10-25 00:58:47 +02:00
// keep TouchSensorEvent in sync with ButtonEvent
/**
* Touch sensor interactions
*/
const enum TouchSensorEvent {
//% block="touched"
Touched = 4,
//% block="bumped"
Bumped = 1,
//% block="released"
Released = 3,
}
2017-07-10 12:37:14 +02:00
namespace input {
//% fixedInstances
2017-07-10 12:37:14 +02:00
export class TouchSensor extends internal.AnalogSensor {
2017-10-25 00:58:47 +02:00
private button: Button;
2017-07-10 12:37:14 +02:00
2017-10-24 20:58:52 +02:00
constructor(port: number) {
super(port)
2017-10-25 00:58:47 +02:00
this.button = new Button();
2017-07-10 12:37:14 +02:00
}
_query() {
return this._readPin6() > 2500 ? 1 : 0
}
_update(prev: number, curr: number) {
this.button.update(curr > 0)
}
2017-07-10 13:47:00 +02:00
_deviceType() {
return DAL.DEVICE_TYPE_TOUCH
2017-07-10 13:47:00 +02:00
}
2017-07-10 12:37:14 +02:00
2017-10-25 00:58:47 +02:00
/**
* Check if touch sensor is touched.
* @param sensor the port to query the request
*/
//% help=input/touch/is-touched
//% block="%sensor|is touched"
//% blockId=touchIsTouched
//% parts="touch"
//% blockNamespace=input
//% weight=81 blockGap=8
//% group="Touch Sensor"
2017-10-25 00:58:47 +02:00
isTouched() {
return this.button.isPressed();
}
2017-10-24 20:58:52 +02:00
2017-10-25 00:58:47 +02:00
/**
* Do something when a 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
* @param body code to run when the event is raised
*/
//% help=input/touch/on-event
//% blockId=touchEvent block="on %sensor|%event"
//% parts="touch"
//% blockNamespace=input
//% weight=99 blockGap=8
//% group="Touch Sensor"
2017-10-25 00:58:47 +02:00
onEvent(ev: TouchSensorEvent, body: () => void) {
this.button.onEvent(<ButtonEvent><number>ev, body)
}
}
2017-10-24 20:58:52 +02:00
2017-10-25 00:58:47 +02:00
//% whenUsed block="touch sensor 1" weight=95 fixedInstance
export const touchSensor1: TouchSensor = new TouchSensor(1)
2017-10-24 20:58:52 +02:00
//% whenUsed block="touch sensor 2" weight=95 fixedInstance
2017-10-25 00:58:47 +02:00
export const touchSensor2: TouchSensor = new TouchSensor(2)
2017-10-24 20:58:52 +02:00
//% whenUsed block="touch sensor 3" weight=95 fixedInstance
2017-10-25 00:58:47 +02:00
export const touchSensor3: TouchSensor = new TouchSensor(3)
2017-10-24 20:58:52 +02:00
//% whenUsed block="touch sensor 4" weight=95 fixedInstance
2017-10-25 00:58:47 +02:00
export const touchSensor4: TouchSensor = new TouchSensor(4)
2017-07-10 12:37:14 +02:00
}