2017-10-25 00:58:47 +02:00
|
|
|
// keep TouchSensorEvent in sync with ButtonEvent
|
|
|
|
|
2017-10-28 18:13:02 +02:00
|
|
|
namespace sensors {
|
2017-10-03 08:28:44 +02:00
|
|
|
|
|
|
|
//% fixedInstances
|
2017-07-10 12:37:14 +02:00
|
|
|
export class TouchSensor extends internal.AnalogSensor {
|
2017-10-28 18:13:02 +02:00
|
|
|
private button: brick.Button;
|
2017-07-10 12:37:14 +02:00
|
|
|
|
2017-10-24 20:58:52 +02:00
|
|
|
constructor(port: number) {
|
|
|
|
super(port)
|
2017-10-28 18:13:02 +02:00
|
|
|
this.button = new brick.Button();
|
2017-07-10 12:37:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_query() {
|
|
|
|
return this._readPin6() > 2500 ? 1 : 0
|
|
|
|
}
|
|
|
|
|
|
|
|
_update(prev: number, curr: number) {
|
2017-10-27 06:10:37 +02:00
|
|
|
this.button._update(curr > 0)
|
2017-07-10 12:37:14 +02:00
|
|
|
}
|
2017-07-10 13:47:00 +02:00
|
|
|
|
|
|
|
_deviceType() {
|
2017-07-11 16:18:59 +02:00
|
|
|
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
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2018-02-10 03:17:12 +01:00
|
|
|
//% help=sensors/touch-sensor/on-event
|
2018-02-26 17:33:50 +01:00
|
|
|
//% blockId=touchEvent block="on **touch** %this|%event"
|
2017-10-25 00:58:47 +02:00
|
|
|
//% parts="touch"
|
2017-10-28 18:13:02 +02:00
|
|
|
//% blockNamespace=sensors
|
2018-02-26 17:33:50 +01:00
|
|
|
//% this.fieldEditor="ports"
|
2018-02-02 01:21:08 +01:00
|
|
|
//% weight=99 blockGap=12
|
2017-10-25 01:52:13 +02:00
|
|
|
//% group="Touch Sensor"
|
2018-01-31 17:28:00 +01:00
|
|
|
onEvent(ev: ButtonEvent, body: () => void) {
|
|
|
|
this.button.onEvent(ev, body)
|
2017-10-25 00:58:47 +02:00
|
|
|
}
|
2017-11-16 21:58:37 +01:00
|
|
|
|
2017-11-30 19:05:00 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2018-02-10 03:17:12 +01:00
|
|
|
//% help=sensors/touch-sensor/pause-until
|
2018-02-26 17:33:50 +01:00
|
|
|
//% blockId=touchWaitUntil block="pause until **touch** %this|%event"
|
2017-11-30 19:05:00 +01:00
|
|
|
//% parts="touch"
|
|
|
|
//% blockNamespace=sensors
|
2018-02-26 17:33:50 +01:00
|
|
|
//% this.fieldEditor="ports"
|
2018-02-02 01:21:08 +01:00
|
|
|
//% weight=98 blockGap=12
|
2017-11-30 19:05:00 +01:00
|
|
|
//% group="Touch Sensor"
|
2018-01-31 17:28:00 +01:00
|
|
|
pauseUntil(ev: ButtonEvent) {
|
2017-12-12 19:46:56 +01:00
|
|
|
this.button.pauseUntil(<ButtonEvent><number>ev);
|
2017-11-30 19:05:00 +01:00
|
|
|
}
|
|
|
|
|
2017-11-16 21:58:37 +01:00
|
|
|
/**
|
|
|
|
* Check if touch sensor is touched.
|
|
|
|
* @param sensor the port to query the request
|
|
|
|
*/
|
2018-02-10 03:17:12 +01:00
|
|
|
//% help=sensors/touch-sensor/is-pressed
|
2018-02-26 17:33:50 +01:00
|
|
|
//% block="**touch** %this|is pressed"
|
2017-11-29 01:11:15 +01:00
|
|
|
//% blockId=touchIsPressed
|
2017-11-16 21:58:37 +01:00
|
|
|
//% parts="touch"
|
|
|
|
//% blockNamespace=sensors
|
2018-02-26 17:33:50 +01:00
|
|
|
//% this.fieldEditor="ports"
|
2018-02-02 01:21:08 +01:00
|
|
|
//% weight=81 blockGap=8
|
2017-11-16 21:58:37 +01:00
|
|
|
//% group="Touch Sensor"
|
2017-11-29 01:11:15 +01:00
|
|
|
isPressed() {
|
2017-11-16 21:58:37 +01:00
|
|
|
return this.button.isPressed();
|
|
|
|
}
|
2017-11-29 01:11:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if touch sensor is touched since it was last checked.
|
|
|
|
* @param sensor the port to query the request
|
|
|
|
*/
|
2018-02-10 03:17:12 +01:00
|
|
|
//% help=sensors/touch-sensor/was-pressed
|
2018-02-26 17:33:50 +01:00
|
|
|
//% block="**touch** %this|was pressed"
|
2017-11-29 01:11:15 +01:00
|
|
|
//% blockId=touchWasPressed
|
|
|
|
//% parts="touch"
|
|
|
|
//% blockNamespace=sensors
|
2018-02-26 17:33:50 +01:00
|
|
|
//% this.fieldEditor="ports"
|
2018-02-02 01:21:08 +01:00
|
|
|
//% weight=81
|
2017-11-29 01:11:15 +01:00
|
|
|
//% group="Touch Sensor"
|
|
|
|
wasPressed() {
|
|
|
|
return this.button.wasPressed();
|
|
|
|
}
|
2017-10-25 00:58:47 +02:00
|
|
|
}
|
2017-10-24 20:58:52 +02:00
|
|
|
|
2018-02-26 17:33:50 +01:00
|
|
|
//% whenUsed block="1" weight=95 fixedInstance jres=icons.port1
|
2018-01-06 04:14:55 +01:00
|
|
|
export const touch1: TouchSensor = new TouchSensor(1)
|
2018-02-26 17:33:50 +01:00
|
|
|
//% whenUsed block="2" weight=95 fixedInstance jres=icons.port2
|
2018-01-06 04:14:55 +01:00
|
|
|
export const touch2: TouchSensor = new TouchSensor(2)
|
2018-02-26 17:33:50 +01:00
|
|
|
//% whenUsed block="3" weight=95 fixedInstance jres=icons.port3
|
2018-01-06 04:14:55 +01:00
|
|
|
export const touch3: TouchSensor = new TouchSensor(3)
|
2018-02-26 17:33:50 +01:00
|
|
|
//% whenUsed block="4" weight=95 fixedInstance jres=icons.port4
|
2018-01-06 04:14:55 +01:00
|
|
|
export const touch4: TouchSensor = new TouchSensor(4)
|
2017-07-10 12:37:14 +02:00
|
|
|
}
|