diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 489af20e..8ef02a1d 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -9,6 +9,7 @@ "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", "control": "Program controls and events.", "control.allocateNotifyEvent": "Allocates the next user notification event", "control.deviceFirmwareVersion": "Determine the version of system software currently running.", @@ -30,6 +31,9 @@ "input.GyroSensor.rate": "Get the current rotation rate from the gyroscope.", "input.IrSensor.distance": "Get the distance measured by the infrared sensor.", "input.IrSensor.remoteCommand": "Get the remote commandreceived the infrared sensor.", + "input.TouchSensor.isTouched": "Check if touch sensor is touched.", + "input.TouchSensor.onEvent": "Do something when a touch sensor is touched...", + "input.TouchSensor.onEvent|param|body": "code to run when the event is raised", "input.UltraSonicSensor.distance": "Gets the distance from the sonar in millimeters", "input.buttonDown": "Down button.", "input.buttonEnter": "Enter button.", diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index 332a978b..5c734f49 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -1,7 +1,6 @@ { "ButtonEvent.Click|block": "click", "ButtonEvent.Down|block": "down", - "ButtonEvent.LongClick|block": "long click", "ButtonEvent.Up|block": "up", "LightsPattern.GreenFlash|block": "Flashing Green", "LightsPattern.GreenPulse|block": "Pulsing Green", @@ -13,6 +12,9 @@ "LightsPattern.RedFlash|block": "Flashing Red", "LightsPattern.RedPulse|block": "Pulsing Red", "LightsPattern.Red|block": "Red", + "TouchSensorEvent.Bumped|block": "bumped", + "TouchSensorEvent.Released|block": "released", + "TouchSensorEvent.Touched|block": "touched", "control.raiseEvent|block": "raise event|from %src|with value %value", "control|block": "control", "input.Button.isPressed|block": "%button|is pressed", @@ -25,6 +27,8 @@ "input.GyroSensor.rate|block": "%sensor|rotation rate", "input.IrSensor.distance|block": "%infrared|distance", "input.IrSensor.remoteCommand|block": "%infrared|remote command", + "input.TouchSensor.isTouched|block": "%sensor|is touched", + "input.TouchSensor.onEvent|block": "on %sensor|%event", "input.UltraSonicSensor.distance|block": "%sensor|distance", "input.buttonDown|block": "button down", "input.buttonEnter|block": "button enter", @@ -77,5 +81,6 @@ "{id:group}Lights": "Lights", "{id:group}Motors": "Motors", "{id:group}Screen": "Screen", + "{id:group}Touch sensor": "Touch sensor", "{id:group}Ultrasonic sensor": "Ultrasonic sensor" } \ No newline at end of file diff --git a/libs/core/buttons.ts b/libs/core/buttons.ts index 1ef7d54c..fc61a7b4 100644 --- a/libs/core/buttons.ts +++ b/libs/core/buttons.ts @@ -41,8 +41,6 @@ const enum LightsPattern { const enum ButtonEvent { //% block="click" Click = 1, - //% block="long click" - LongClick = 2, //% block="up" Up = 3, //% block="down" @@ -76,7 +74,8 @@ namespace input { } else { control.raiseEvent(this._id, ButtonEvent.Up) let delta = control.millis() - this.downTime - control.raiseEvent(this._id, delta > 500 ? ButtonEvent.LongClick : ButtonEvent.Click) + control.raiseEvent(this._id, ButtonEvent.Click) + //control.raiseEvent(this._id, delta > 500 ? ButtonEvent.LongClick : ButtonEvent.Click) } } @@ -131,7 +130,7 @@ namespace input { //% button.fieldEditor="gridpicker" //% button.fieldOptions.width=220 //% button.fieldOptions.columns=3 - //% weight=99 + //% weight=99 blockGap=8 //% group="Buttons" onEvent(ev: ButtonEvent, body: () => void) { control.onEvent(this._id, ev, body) diff --git a/libs/core/touch.ts b/libs/core/touch.ts index c75ac0b9..f7ac7c4c 100644 --- a/libs/core/touch.ts +++ b/libs/core/touch.ts @@ -1,12 +1,26 @@ +// keep TouchSensorEvent in sync with ButtonEvent + +/** + * Touch sensor interactions + */ +const enum TouchSensorEvent { + //% block="touched" + Touched = 4, + //% block="bumped" + Bumped = 1, + //% block="released" + Released = 3, +} + namespace input { //% fixedInstances export class TouchSensor extends internal.AnalogSensor { - button: Button; + private button: Button; constructor(port: number) { super(port) - this.button = new Button() + this.button = new Button(); } _query() { @@ -20,26 +34,45 @@ namespace input { _deviceType() { return DAL.DEVICE_TYPE_TOUCH } + + /** + * 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" + isTouched() { + return this.button.isPressed(); + } + + /** + * 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" + onEvent(ev: TouchSensorEvent, body: () => void) { + this.button.onEvent(ev, body) + } } - //% whenUsed - export const touchSensorImpl1: TouchSensor = new TouchSensor(1) - //% whenUsed - export const touchSensorImpl2: TouchSensor = new TouchSensor(2) - //% whenUsed - export const touchSensorImpl3: TouchSensor = new TouchSensor(3) - //% whenUsed - export const touchSensorImpl4: TouchSensor = new TouchSensor(4) - //% whenUsed block="touch sensor 1" weight=95 fixedInstance - export const touchSensor1: Button = touchSensorImpl1.button - + export const touchSensor1: TouchSensor = new TouchSensor(1) //% whenUsed block="touch sensor 2" weight=95 fixedInstance - export const touchSensor2: Button = touchSensorImpl2.button - + export const touchSensor2: TouchSensor = new TouchSensor(2) //% whenUsed block="touch sensor 3" weight=95 fixedInstance - export const touchSensor3: Button = touchSensorImpl3.button - + export const touchSensor3: TouchSensor = new TouchSensor(3) //% whenUsed block="touch sensor 4" weight=95 fixedInstance - export const touchSensor4: Button = touchSensorImpl4.button + export const touchSensor4: TouchSensor = new TouchSensor(4) }