109 lines
3.4 KiB
TypeScript
Raw Normal View History

2017-10-24 15:58:47 -07:00
// keep TouchSensorEvent in sync with ButtonEvent
/**
* Touch sensor interactions
*/
const enum TouchSensorEvent {
2017-10-24 21:55:37 -07:00
//% block="pressed"
Pressed = 4,
2017-10-24 15:58:47 -07:00
//% block="bumped"
Bumped = 1,
//% block="released"
Released = 3,
}
2017-10-28 09:13:02 -07:00
namespace sensors {
//% fixedInstances
2017-07-10 11:37:14 +01:00
export class TouchSensor extends internal.AnalogSensor {
2017-10-28 09:13:02 -07:00
private button: brick.Button;
2017-07-10 11:37:14 +01:00
2017-10-24 19:58:52 +01:00
constructor(port: number) {
super(port)
2017-10-28 09:13:02 -07:00
this.button = new brick.Button();
2017-07-10 11:37:14 +01:00
}
_query() {
return this._readPin6() > 2500 ? 1 : 0
}
_update(prev: number, curr: number) {
2017-10-26 21:10:37 -07:00
this.button._update(curr > 0)
2017-07-10 11:37:14 +01:00
}
2017-07-10 12:47:00 +01:00
_deviceType() {
return DAL.DEVICE_TYPE_TOUCH
2017-07-10 12:47:00 +01:00
}
2017-07-10 11:37:14 +01:00
2017-10-24 15:58:47 -07: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-sensor/on-event
//% blockId=touchEvent block="on %sensor|%event"
2017-10-24 15:58:47 -07:00
//% parts="touch"
2017-10-28 09:13:02 -07:00
//% blockNamespace=sensors
2017-10-24 15:58:47 -07:00
//% weight=99 blockGap=8
//% group="Touch Sensor"
2017-10-24 15:58:47 -07:00
onEvent(ev: TouchSensorEvent, body: () => void) {
this.button.onEvent(<ButtonEvent><number>ev, body)
}
2017-11-16 12:58:37 -08:00
2017-11-30 10:05:00 -08: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
*/
2017-12-12 10:49:45 -08:00
//% help=input/touch-sensor/pause-until
//% blockId=touchWaitUntil block="pause until %sensor|%event"
2017-11-30 10:05:00 -08:00
//% parts="touch"
//% blockNamespace=sensors
//% weight=98 blockGap=8
//% group="Touch Sensor"
pauseUntil(ev: TouchSensorEvent) {
this.button.pauseUntil(<ButtonEvent><number>ev);
2017-11-30 10:05:00 -08:00
}
2017-11-16 12:58:37 -08:00
/**
* Check if touch sensor is touched.
* @param sensor the port to query the request
*/
//% help=input/touch-sensor/is-pressed
//% block="%sensor|is pressed"
//% blockId=touchIsPressed
2017-11-16 12:58:37 -08:00
//% parts="touch"
//% blockNamespace=sensors
//% weight=81 blockGap=8
//% group="Touch Sensor"
isPressed() {
2017-11-16 12:58:37 -08:00
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="%sensor|was pressed"
//% blockId=touchWasPressed
//% parts="touch"
//% blockNamespace=sensors
//% weight=81 blockGap=8
//% group="Touch Sensor"
wasPressed() {
return this.button.wasPressed();
}
2017-10-24 15:58:47 -07:00
}
2017-10-24 19:58:52 +01:00
//% whenUsed block="touch 1" weight=95 fixedInstance jres=icons.port1
2017-10-24 15:58:47 -07:00
export const touchSensor1: TouchSensor = new TouchSensor(1)
//% whenUsed block="touch 2" weight=95 fixedInstance jres=icons.port2
2017-10-24 15:58:47 -07:00
export const touchSensor2: TouchSensor = new TouchSensor(2)
//% whenUsed block="touch 3" weight=95 fixedInstance jres=icons.port3
2017-10-24 15:58:47 -07:00
export const touchSensor3: TouchSensor = new TouchSensor(3)
//% whenUsed block="touch 4" weight=95 fixedInstance jres=icons.port4
2017-10-24 15:58:47 -07:00
export const touchSensor4: TouchSensor = new TouchSensor(4)
2017-07-10 11:37:14 +01:00
}