Input blocks revision

- add Button and Pin event types
- merge onPinPressed & onPinReleased in new onPinEvent function
- create new onButtonEvent function
This commit is contained in:
Amerlander 2020-02-20 03:24:13 +01:00
parent 9c8595afdd
commit 38cf0ec0c6
9 changed files with 120 additions and 57 deletions

View File

@ -365,6 +365,7 @@
"input": "Events and data from sensors", "input": "Events and data from sensors",
"input.acceleration": "Get the acceleration value in milli-gravitys (when the board is laying flat with the screen up, x=0, y=0 and z=-1024)", "input.acceleration": "Get the acceleration value in milli-gravitys (when the board is laying flat with the screen up, x=0, y=0 and z=-1024)",
"input.acceleration|param|dimension": "x, y, or z dimension, eg: Dimension.X", "input.acceleration|param|dimension": "x, y, or z dimension, eg: Dimension.X",
"input.buttonEventValueId": "Returns the value of a C++ runtime constant",
"input.buttonIsPressed": "Get the button state (pressed or not) for ``A`` and ``B``.", "input.buttonIsPressed": "Get the button state (pressed or not) for ``A`` and ``B``.",
"input.buttonIsPressed|param|button": "the button to query the request, eg: Button.A", "input.buttonIsPressed|param|button": "the button to query the request, eg: Button.A",
"input.calibrate": "Obsolete, use input.calibrateCompass instead.", "input.calibrate": "Obsolete, use input.calibrateCompass instead.",
@ -375,9 +376,10 @@
"input.lightLevel": "Reads the light level applied to the LED screen in a range from ``0`` (dark) to ``255`` bright.", "input.lightLevel": "Reads the light level applied to the LED screen in a range from ``0`` (dark) to ``255`` bright.",
"input.magneticForce": "Get the magnetic force value in ``micro-Teslas`` (``µT``). This function is not supported in the simulator.", "input.magneticForce": "Get the magnetic force value in ``micro-Teslas`` (``µT``). This function is not supported in the simulator.",
"input.magneticForce|param|dimension": "the x, y, or z dimension, eg: Dimension.X", "input.magneticForce|param|dimension": "the x, y, or z dimension, eg: Dimension.X",
"input.onButtonPressed": "Do something when a button (A, B or both A+B) is pushed down and released again.", "input.onButtonEvent": "Do something when a button (A, B or both A+B) is pushed down and released again.",
"input.onButtonPressed|param|body": "code to run when event is raised", "input.onButtonEvent|param|body": "code to run when event is raised",
"input.onButtonPressed|param|button": "the button that needs to be pressed", "input.onButtonEvent|param|button": "the button",
"input.onButtonEvent|param|eventType": "event Type",
"input.onGesture": "Do something when when a gesture is done (like shaking the micro:bit).", "input.onGesture": "Do something when when a gesture is done (like shaking the micro:bit).",
"input.onGesture|param|body": "code to run when gesture is raised", "input.onGesture|param|body": "code to run when gesture is raised",
"input.onGesture|param|gesture": "the type of gesture to track, eg: Gesture.Shake", "input.onGesture|param|gesture": "the type of gesture to track, eg: Gesture.Shake",
@ -385,12 +387,9 @@
"input.onLogoDown|param|body": "TODO", "input.onLogoDown|param|body": "TODO",
"input.onLogoUp": "Attaches code to run when the logo is oriented upwards and the board is vertical.", "input.onLogoUp": "Attaches code to run when the logo is oriented upwards and the board is vertical.",
"input.onLogoUp|param|body": "TODO", "input.onLogoUp|param|body": "TODO",
"input.onPinPressed": "Do something when a pin is touched and released again (while also touching the GND pin).", "input.onPinEvent": "Do something when a pin is touched and released again (while also touching the GND pin).",
"input.onPinPressed|param|body": "the code to run when the pin is pressed", "input.onPinEvent|param|body": "the code to run when the pin is pressed",
"input.onPinPressed|param|name": "the pin that needs to be pressed, eg: TouchPin.P0", "input.onPinEvent|param|name": "the pin, eg: TouchPin.P0",
"input.onPinReleased": "Do something when a pin is released.",
"input.onPinReleased|param|body": "the code to run when the pin is released",
"input.onPinReleased|param|name": "the pin that needs to be released, eg: TouchPin.P0",
"input.onScreenDown": "Attaches code to run when the screen is facing down.", "input.onScreenDown": "Attaches code to run when the screen is facing down.",
"input.onScreenDown|param|body": "TODO", "input.onScreenDown|param|body": "TODO",
"input.onScreenUp": "Attaches code to run when the screen is facing up.", "input.onScreenUp": "Attaches code to run when the screen is facing up.",
@ -496,18 +495,18 @@
"pins.P1": "Pin P1", "pins.P1": "Pin P1",
"pins.P2": "Pin P2", "pins.P2": "Pin P2",
"pins.P3": "Pin P3", "pins.P3": "Pin P3",
"pins.analogPitch": "Emit a plse-width modulation (PWM) signal to the current pitch pin. Use `analog set pitch pin` to define the pitch pin.", "pins.analogPitch": "Emit a pulse-width modulation (PWM) signal to the current pitch pin. Use `analog set pitch pin` to define the pitch pin.",
"pins.analogPitch|param|frequency": "frequency to modulate in Hz.", "pins.analogPitch|param|frequency": "frequency to modulate in Hz.",
"pins.analogPitch|param|ms": "duration of the pitch in milli seconds.", "pins.analogPitch|param|ms": "duration of the pitch in milli seconds.",
"pins.analogReadPin": "Read the connector value as analog, that is, as a value comprised between 0 and 1023.", "pins.analogReadPin": "Read the connector value as analog, that is, as a value comprised between 0 and 1023.",
"pins.analogReadPin|param|name": "pin to write to, eg: AnalogPin.P1", "pins.analogReadPin|param|name": "of pin to read from, eg: AnalogPin.P1",
"pins.analogSetPeriod": "Configure the pulse-width modulation (PWM) period of the analog output in microseconds.\nIf this pin is not configured as an analog output (using `analog write pin`), the operation has no effect.", "pins.analogSetPeriod": "Configure the pulse-width modulation (PWM) period of the analog output in microseconds.\nIf this pin is not configured as an analog output (using `analog write pin`), the operation has no effect.",
"pins.analogSetPeriod|param|micros": "period in micro seconds. eg:20000", "pins.analogSetPeriod|param|micros": "period in micro seconds. eg:20000",
"pins.analogSetPeriod|param|name": "analog pin to set period to, eg: AnalogPin.P1", "pins.analogSetPeriod|param|name": "analog pin to set period to, eg: AnalogPin.P1",
"pins.analogSetPitchPin": "Set the pin used when using analog pitch or music.", "pins.analogSetPitchPin": "Set the pin used when using analog pitch or music.",
"pins.analogSetPitchPin|param|name": "pin to modulate pitch from", "pins.analogSetPitchPin|param|name": "pin to modulate pitch from",
"pins.analogWritePin": "Set the connector value as analog. Value must be comprised between 0 and 1023.", "pins.analogWritePin": "Set the connector value as analog. Value must be comprised between 0 and 1023.",
"pins.analogWritePin|param|name": "pin name to write to, eg: AnalogPin.P1", "pins.analogWritePin|param|name": "of pin to write to, eg: AnalogPin.P1",
"pins.analogWritePin|param|value": "value to write to the pin between ``0`` and ``1023``. eg:1023,0", "pins.analogWritePin|param|value": "value to write to the pin between ``0`` and ``1023``. eg:1023,0",
"pins.createBuffer": "Create a new zero-initialized buffer.", "pins.createBuffer": "Create a new zero-initialized buffer.",
"pins.createBufferFromArray": "Create a new buffer initalized to bytes from given array.", "pins.createBufferFromArray": "Create a new buffer initalized to bytes from given array.",
@ -545,7 +544,7 @@
"pins.setEvents": "Configure the events emitted by this pin. Events can be subscribed to\nusing ``control.onEvent()``.", "pins.setEvents": "Configure the events emitted by this pin. Events can be subscribed to\nusing ``control.onEvent()``.",
"pins.setEvents|param|name": "pin to set the event mode on, eg: DigitalPin.P0", "pins.setEvents|param|name": "pin to set the event mode on, eg: DigitalPin.P0",
"pins.setEvents|param|type": "the type of events for this pin to emit, eg: PinEventType.Edge", "pins.setEvents|param|type": "the type of events for this pin to emit, eg: PinEventType.Edge",
"pins.setPull": "Configure the pull directiion of of a pin.", "pins.setPull": "Configure the pull direction of of a pin.",
"pins.setPull|param|name": "pin to set the pull mode on, eg: DigitalPin.P0", "pins.setPull|param|name": "pin to set the pull mode on, eg: DigitalPin.P0",
"pins.setPull|param|pull": "one of the mbed pull configurations, eg: PinPullMode.PullUp", "pins.setPull|param|pull": "one of the mbed pull configurations, eg: PinPullMode.PullUp",
"pins.sizeOf": "Get the size in bytes of specified number format.", "pins.sizeOf": "Get the size in bytes of specified number format.",

View File

@ -44,6 +44,10 @@
"BeatFraction.Sixteenth|block": "1/16", "BeatFraction.Sixteenth|block": "1/16",
"BeatFraction.Whole|block": "1", "BeatFraction.Whole|block": "1",
"Button.AB|block": "A+B", "Button.AB|block": "A+B",
"ButtonEvent.Click|block": "clicked",
"ButtonEvent.Down|block": "pressed down",
"ButtonEvent.LongClick|block": "long clicked",
"ButtonEvent.Up|block": "released up",
"Colors.Blue|block": "blue", "Colors.Blue|block": "blue",
"Colors.Green|block": "green", "Colors.Green|block": "green",
"Colors.Indigo|block": "indigo", "Colors.Indigo|block": "indigo",
@ -299,16 +303,16 @@
"images.iconImage|block": "icon image %i", "images.iconImage|block": "icon image %i",
"images|block": "images", "images|block": "images",
"input.acceleration|block": "acceleration (mg)|%NAME", "input.acceleration|block": "acceleration (mg)|%NAME",
"input.buttonEventValueId|block": "%id",
"input.buttonIsPressed|block": "button|%NAME|is pressed", "input.buttonIsPressed|block": "button|%NAME|is pressed",
"input.calibrateCompass|block": "calibrate compass", "input.calibrateCompass|block": "calibrate compass",
"input.compassHeading|block": "compass heading (°)", "input.compassHeading|block": "compass heading (°)",
"input.isGesture|block": "is %gesture gesture", "input.isGesture|block": "is %gesture gesture",
"input.lightLevel|block": "light level", "input.lightLevel|block": "light level",
"input.magneticForce|block": "magnetic force (µT)|%NAME", "input.magneticForce|block": "magnetic force (µT)|%NAME",
"input.onButtonPressed|block": "on button|%NAME|pressed", "input.onButtonEvent|block": "on button %NAME| is %eventType=control_button_event_value_id",
"input.onGesture|block": "on |%NAME", "input.onGesture|block": "on |%NAME",
"input.onPinPressed|block": "on pin %name|pressed", "input.onPinEvent|block": "on pin %name|is %eventType=control_button_event_value_id",
"input.onPinReleased|block": "on pin %NAME|released",
"input.pinIsPressed|block": "pin %NAME|is pressed", "input.pinIsPressed|block": "pin %NAME|is pressed",
"input.rotation|block": "rotation (°)|%NAME", "input.rotation|block": "rotation (°)|%NAME",
"input.runningTimeMicros|block": "running time (micros)", "input.runningTimeMicros|block": "running time (micros)",

16
libs/core/enums.d.ts vendored
View File

@ -39,6 +39,22 @@ declare namespace basic {
} }
declare const enum ButtonEvent {
//% blockIdentity="input.buttonEventValueId"
//% block="pressed down"
Down = 1, // MICROBIT_BUTTON_EVT_DOWN
//% blockIdentity="input.buttonEventValueId"
//% block="released up"
Up = 2, // MICROBIT_BUTTON_EVT_UP
//% blockIdentity="input.buttonEventValueId"
//% block="clicked"
Click = 3, // MICROBIT_BUTTON_EVT_CLICK
//% blockIdentity="input.buttonEventValueId"
//% block="long clicked"
LongClick = 4, // MICROBIT_BUTTON_EVT_LONG_CLICK
}
declare const enum Dimension { declare const enum Dimension {
//% block=x //% block=x
X = 0, X = 0,

View File

@ -323,9 +323,9 @@ namespace game {
} }
function unplugEvents(): void { function unplugEvents(): void {
input.onButtonPressed(Button.A, () => { }); input.onButtonEvent(Button.A, DAL.MICROBIT_BUTTON_EVT_CLICK, () => { });
input.onButtonPressed(Button.B, () => { }); input.onButtonEvent(Button.B, DAL.MICROBIT_BUTTON_EVT_CLICK, () => { });
input.onButtonPressed(Button.AB, () => { input.onButtonEvent(Button.AB, DAL.MICROBIT_BUTTON_EVT_CLICK, () => {
control.reset(); control.reset();
}); });
} }

View File

@ -7,6 +7,21 @@ enum class Button {
AB = MICROBIT_ID_BUTTON_AB, AB = MICROBIT_ID_BUTTON_AB,
}; };
enum class ButtonEvent {
//% blockIdentity="input.buttonEventValueId"
//% block="pressed down"
Down = MICROBIT_BUTTON_EVT_DOWN,
//% blockIdentity="input.buttonEventValueId"
//% block="released up"
Up = MICROBIT_BUTTON_EVT_UP,
//% blockIdentity="input.buttonEventValueId"
//% block="clicked"
Click = MICROBIT_BUTTON_EVT_CLICK,
//% blockIdentity="input.buttonEventValueId"
//% block="long clicked"
LongClick = MICROBIT_BUTTON_EVT_LONG_CLICK,
};
enum class Dimension { enum class Dimension {
//% block=x //% block=x
X = 0, X = 0,
@ -163,14 +178,20 @@ enum class MesDpadButtonInfo {
namespace input { namespace input {
/** /**
* Do something when a button (A, B or both A+B) is pushed down and released again. * Do something when a button (A, B or both A+B) is pushed down and released again.
* @param button the button that needs to be pressed * @param button the button
* @param body code to run when event is raised * @param body code to run when event is raised
* @param eventType event Type
*/ */
//% help=input/on-button-pressed weight=85 blockGap=16 //% help=input/on-button-event weight=85 blockGap=16
//% blockId=device_button_event block="on button|%NAME|pressed" //% blockId=device_button_event block="on button %NAME| is %eventType=control_button_event_value_id"
//% parts="buttonpair" //% parts="buttonpair"
void onButtonPressed(Button button, Action body) { void onButtonEvent(Button button, int eventType, Action body) {
registerWithDal((int)button, MICROBIT_BUTTON_EVT_CLICK, body); registerWithDal((int)button, eventType, body);
}
// Deprecated
void onButtonPressed(Button button, int eventType, Action body) {
registerWithDal((int)button, eventType, body);
} }
/** /**
@ -208,11 +229,21 @@ namespace input {
/** /**
* Do something when a pin is touched and released again (while also touching the GND pin). * Do something when a pin is touched and released again (while also touching the GND pin).
* @param name the pin that needs to be pressed, eg: TouchPin.P0 * @param name the pin, eg: TouchPin.P0
* @param body the code to run when the pin is pressed * @param body the code to run when the pin is pressed
*/ */
//% help=input/on-pin-pressed weight=83 blockGap=32 //% help=input/on-pin-pressed weight=83 blockGap=32
//% blockId=device_pin_event block="on pin %name|pressed" //% blockId=device_pin_event block="on pin %name|is %eventType=control_button_event_value_id"
void onPinEvent(TouchPin name, int eventType, Action body) {
auto pin = getPin((int)name);
if (!pin) return;
// Forces the PIN to switch to makey-makey style detection.
pin->isTouched();
registerWithDal((int)name, eventType, body);
}
// Deprecated
void onPinPressed(TouchPin name, Action body) { void onPinPressed(TouchPin name, Action body) {
auto pin = getPin((int)name); auto pin = getPin((int)name);
if (!pin) return; if (!pin) return;
@ -222,14 +253,7 @@ namespace input {
registerWithDal((int)name, MICROBIT_BUTTON_EVT_CLICK, body); registerWithDal((int)name, MICROBIT_BUTTON_EVT_CLICK, body);
} }
/** // Deprecated
* Do something when a pin is released.
* @param name the pin that needs to be released, eg: TouchPin.P0
* @param body the code to run when the pin is released
*/
//% help=input/on-pin-released weight=6 blockGap=16
//% blockId=device_pin_released block="on pin %NAME|released"
//% advanced=true
void onPinReleased(TouchPin name, Action body) { void onPinReleased(TouchPin name, Action body) {
auto pin = getPin((int)name); auto pin = getPin((int)name);
if (!pin) return; if (!pin) return;

View File

@ -3,6 +3,15 @@
*/ */
//% color=#C90072 weight=99 //% color=#C90072 weight=99
namespace input { namespace input {
/**
* Returns the value of a C++ runtime constant
*/
//% weight=1 weight=19 blockId="control_button_event_value_id" block="%id"
//% shim=TD_ID advanced=true
export function buttonEventValueId(id: ButtonEvent): number {
return id;
}
/** /**
* Attaches code to run when the screen is facing up. * Attaches code to run when the screen is facing up.
* @param body TODO * @param body TODO

35
libs/core/shims.d.ts vendored
View File

@ -224,13 +224,14 @@ declare namespace input {
/** /**
* Do something when a button (A, B or both A+B) is pushed down and released again. * Do something when a button (A, B or both A+B) is pushed down and released again.
* @param button the button that needs to be pressed * @param button the button
* @param body code to run when event is raised * @param body code to run when event is raised
* @param eventType event Type
*/ */
//% help=input/on-button-pressed weight=85 blockGap=16 //% help=input/on-button-event weight=85 blockGap=16
//% blockId=device_button_event block="on button|%NAME|pressed" //% blockId=device_button_event block="on button %NAME| is %eventType=control_button_event_value_id"
//% parts="buttonpair" shim=input::onButtonPressed //% parts="buttonpair" shim=input::onButtonEvent
function onButtonPressed(button: Button, body: () => void): void; function onButtonEvent(button: Button, eventType: int32, body: () => void): void;
/** /**
* Do something when when a gesture is done (like shaking the micro:bit). * Do something when when a gesture is done (like shaking the micro:bit).
@ -255,22 +256,12 @@ declare namespace input {
/** /**
* Do something when a pin is touched and released again (while also touching the GND pin). * Do something when a pin is touched and released again (while also touching the GND pin).
* @param name the pin that needs to be pressed, eg: TouchPin.P0 * @param name the pin, eg: TouchPin.P0
* @param body the code to run when the pin is pressed * @param body the code to run when the pin is pressed
*/ */
//% help=input/on-pin-pressed weight=83 blockGap=32 //% help=input/on-pin-pressed weight=83 blockGap=32
//% blockId=device_pin_event block="on pin %name|pressed" shim=input::onPinPressed //% blockId=device_pin_event block="on pin %name|is %eventType=control_button_event_value_id" shim=input::onPinEvent
function onPinPressed(name: TouchPin, body: () => void): void; function onPinEvent(name: TouchPin, eventType: int32, body: () => void): void;
/**
* Do something when a pin is released.
* @param name the pin that needs to be released, eg: TouchPin.P0
* @param body the code to run when the pin is released
*/
//% help=input/on-pin-released weight=6 blockGap=16
//% blockId=device_pin_released block="on pin %NAME|released"
//% advanced=true shim=input::onPinReleased
function onPinReleased(name: TouchPin, body: () => void): void;
/** /**
* Get the button state (pressed or not) for ``A`` and ``B``. * Get the button state (pressed or not) for ``A`` and ``B``.
@ -655,7 +646,7 @@ declare namespace pins {
/** /**
* Read the connector value as analog, that is, as a value comprised between 0 and 1023. * Read the connector value as analog, that is, as a value comprised between 0 and 1023.
* @param name pin to write to, eg: AnalogPin.P1 * @param name of pin to read from, eg: AnalogPin.P1
*/ */
//% help=pins/analog-read-pin weight=25 //% help=pins/analog-read-pin weight=25
//% blockId=device_get_analog_pin block="analog read|pin %name" blockGap="8" //% blockId=device_get_analog_pin block="analog read|pin %name" blockGap="8"
@ -665,7 +656,7 @@ declare namespace pins {
/** /**
* Set the connector value as analog. Value must be comprised between 0 and 1023. * Set the connector value as analog. Value must be comprised between 0 and 1023.
* @param name pin name to write to, eg: AnalogPin.P1 * @param name of pin to write to, eg: AnalogPin.P1
* @param value value to write to the pin between ``0`` and ``1023``. eg:1023,0 * @param value value to write to the pin between ``0`` and ``1023``. eg:1023,0
*/ */
//% help=pins/analog-write-pin weight=24 //% help=pins/analog-write-pin weight=24
@ -760,7 +751,7 @@ declare namespace pins {
function analogSetPitchPin(name: AnalogPin): void; function analogSetPitchPin(name: AnalogPin): void;
/** /**
* Emit a plse-width modulation (PWM) signal to the current pitch pin. Use `analog set pitch pin` to define the pitch pin. * Emit a pulse-width modulation (PWM) signal to the current pitch pin. Use `analog set pitch pin` to define the pitch pin.
* @param frequency frequency to modulate in Hz. * @param frequency frequency to modulate in Hz.
* @param ms duration of the pitch in milli seconds. * @param ms duration of the pitch in milli seconds.
*/ */
@ -769,7 +760,7 @@ declare namespace pins {
function analogPitch(frequency: int32, ms: int32): void; function analogPitch(frequency: int32, ms: int32): void;
/** /**
* Configure the pull directiion of of a pin. * Configure the pull direction of of a pin.
* @param name pin to set the pull mode on, eg: DigitalPin.P0 * @param name pin to set the pull mode on, eg: DigitalPin.P0
* @param pull one of the mbed pull configurations, eg: PinPullMode.PullUp * @param pull one of the mbed pull configurations, eg: PinPullMode.PullUp
*/ */

View File

@ -1,4 +1,14 @@
namespace pxsim.input { namespace pxsim.input {
export function onButtonEvent(button: number, buttonEvent: number, handler: RefAction): void {
let b = board().buttonPairState;
if (button == b.props.ID_BUTTON_AB && !b.usesButtonAB) {
b.usesButtonAB = true;
runtime.queueDisplayUpdate();
}
pxtcore.registerWithDal(button, buttonEvent, handler);
}
// Deprecated
export function onButtonPressed(button: number, handler: RefAction): void { export function onButtonPressed(button: number, handler: RefAction): void {
let b = board().buttonPairState; let b = board().buttonPairState;
if (button == b.props.ID_BUTTON_AB && !b.usesButtonAB) { if (button == b.props.ID_BUTTON_AB && !b.usesButtonAB) {

View File

@ -1,4 +1,13 @@
namespace pxsim.input { namespace pxsim.input {
export function onPinEvent(pinId: number, pinEvent: number, handler: RefAction) {
let pin = getPin(pinId);
if (!pin) return;
pin.isTouched();
runtime.queueDisplayUpdate();
pxtcore.registerWithDal(pin.id, pinEvent, handler);
}
// Deprecated
export function onPinPressed(pinId: number, handler: RefAction) { export function onPinPressed(pinId: number, handler: RefAction) {
let pin = getPin(pinId); let pin = getPin(pinId);
if (!pin) return; if (!pin) return;
@ -7,6 +16,7 @@ namespace pxsim.input {
pxtcore.registerWithDal(pin.id, DAL.MICROBIT_BUTTON_EVT_CLICK, handler); pxtcore.registerWithDal(pin.id, DAL.MICROBIT_BUTTON_EVT_CLICK, handler);
} }
// Deprecated
export function onPinReleased(pinId: number, handler: RefAction) { export function onPinReleased(pinId: number, handler: RefAction) {
let pin = getPin(pinId); let pin = getPin(pinId);
if (!pin) return; if (!pin) return;