diff --git a/docs/examples/gameofLife.md b/docs/examples/gameofLife.md index 0da12a30..93a6b049 100644 --- a/docs/examples/gameofLife.md +++ b/docs/examples/gameofLife.md @@ -22,13 +22,13 @@ Here's a program that simulates cell life in the LED matrix. Use button ``A`` fo let lifeChart: Image = null //Use button A for the next iteration of game of life -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { gameOfLife(); show(); }) //Use button B for reseting to random initial seed state -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { reset(); show(); }) diff --git a/docs/projects/coin-flipper.md b/docs/projects/coin-flipper.md index f82a7480..5a9e1f6d 100644 --- a/docs/projects/coin-flipper.md +++ b/docs/projects/coin-flipper.md @@ -11,7 +11,7 @@ Let's create a coin flipping program to simulate a real coin toss. We'll use ico Get an ``||input:on button A pressed||`` block from the ``||input:Input||`` drawer in the toolbox. We'll put our coin flipping code in here. ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { }) ``` @@ -22,7 +22,7 @@ Grab an ``||logic:if else||`` block and set it inside ``||input:on button A pres The ``||Math:pick random true or false||`` returns a random ``true`` or ``false`` value which we use to determine a ``heads`` or ``tails`` result for a coin toss. ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { if (Math.randomBoolean()) { } else { } @@ -34,7 +34,7 @@ input.onButtonPressed(Button.A, () => { Now, put a ``||basic:show icon||`` block inside both the ``||logic:if||`` and the ``||logic:else||``. Pick images to mean ``heads`` and ``tails``. ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { if (Math.randomBoolean()) { basic.showIcon(IconNames.Skull) } else { @@ -52,7 +52,7 @@ Press button **A** in the simulator to try the coin toss code. You can animate the coin toss to add the feeling of suspense. Place different ``||basic:show icon||`` blocks before the ``||logic:if||`` to show that the coin is flipping. ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { basic.showIcon(IconNames.Diamond) basic.showIcon(IconNames.SmallDiamond) basic.showIcon(IconNames.Diamond) diff --git a/docs/projects/love-meter.md b/docs/projects/love-meter.md index 37fbc01b..145df8ba 100644 --- a/docs/projects/love-meter.md +++ b/docs/projects/love-meter.md @@ -11,7 +11,7 @@ Make a love meter, how sweet! The @boardname@ is feeling the love, then sometime Let's build a **LOVE METER** machine. Place an ``||input:on pin pressed||`` block to run code when pin **0** is pressed. Use ``P0`` from the list of pin inputs. ```blocks -input.onPinPressed(TouchPin.P0, () => { +input.onPinTouched(TouchPin.P0, Button.Click, () => { }); ``` @@ -20,7 +20,7 @@ input.onPinPressed(TouchPin.P0, () => { Using ``||basic:show number||`` and ``||Math:pick random||`` blocks, show a random number from `0` to `100` when pin **0** is pressed. ```blocks -input.onPinPressed(TouchPin.P0, () => { +input.onPinTouched(TouchPin.P0, Button.Click, () => { basic.showNumber(Math.randomRange(0, 100)); }); ``` @@ -34,7 +34,7 @@ Show ``"LOVE METER"`` on the screen when the @boardname@ starts. ```blocks basic.showString("LOVE METER"); -input.onPinPressed(TouchPin.P0, () => { +input.onPinTouched(TouchPin.P0, Button.Click, () => { basic.showNumber(Math.randomRange(0, 100)); }); ``` diff --git a/docs/projects/mini-chat.md b/docs/projects/mini-chat.md index 4695e152..9195baab 100644 --- a/docs/projects/mini-chat.md +++ b/docs/projects/mini-chat.md @@ -12,7 +12,7 @@ Use ``||input:on button pressed||`` to send a text message over radio with ``||r Every @boardname@ nearby will receive this message. ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { radio.sendString("Yo"); }); ``` @@ -41,7 +41,7 @@ radio.onReceivedString(function (receivedString) { Press button **A** on the simulator, you will notice that a second @boardname@ appears (if your screen is too small, the simulator might decide not to show it). Try pressing **A** again and notice that the "Yo" message gets displayed on the other @boardname@. ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { radio.sendString("Yo"); }); radio.onReceivedString(function (receivedString) { diff --git a/docs/projects/smiley-buttons.md b/docs/projects/smiley-buttons.md index 271560f6..1cacea48 100644 --- a/docs/projects/smiley-buttons.md +++ b/docs/projects/smiley-buttons.md @@ -12,7 +12,7 @@ Code the buttons on the @boardname@ to show that it's happy or sad. Place a ``||input:on button pressed||`` block to run code when button **A** is pressed. ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { }); ``` @@ -21,7 +21,7 @@ input.onButtonPressed(Button.A, () => { Place a ``||basic:show leds||`` block inside ``||input:on button pressed||`` to display a smiley on the screen. Press the **A** button in the simulator to see the smiley. ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { basic.showLeds(` # # . # # # # . # # @@ -37,7 +37,7 @@ input.onButtonPressed(Button.A, () => { Add ``||input:on button pressed||`` and ``||basic:show leds||`` blocks to display a frowny when button **B** is pressed. ```blocks -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { basic.showLeds(` # # . # # # # . # # @@ -53,7 +53,7 @@ input.onButtonPressed(Button.B, () => { Add a secret mode that happens when **A** and **B** are pressed together. For this case, add multiple ``||basic:show leds||`` blocks to create an animation. ```blocks -input.onButtonPressed(Button.AB, () => { +input.onButtonEvent(Button.AB, ButtonEvent.Click, () => { basic.showLeds(` . . . . . # . # . . diff --git a/docs/projects/snap-the-dot.md b/docs/projects/snap-the-dot.md index 5d3a0f55..d38ae2b1 100644 --- a/docs/projects/snap-the-dot.md +++ b/docs/projects/snap-the-dot.md @@ -58,8 +58,8 @@ When **A** is pressed, we test if the sprite is in the center or not. Use a ``||input:on button pressed||`` block to handle the **A** button. Put in a ``||logic:if||`` block and test if ``||game:x||`` is equal to `2`. ```blocks -let sprite = game.createSprite(2, 2) -input.onButtonPressed(Button.A, function () { +let sprite: game.LedSprite = null +input.onButtonEvent(Button.A, ButtonEvent.Click, function () { if (sprite.get(LedSpriteProperty.X) == 2) { } else { } @@ -76,8 +76,8 @@ basic.forever(function () { Finally, pull out an ``||game:add score||`` and a ``||game:game over||`` block to handle both success (sprite in the center) and failure (sprite not in the center). ```blocks -let sprite = game.createSprite(2, 2) -input.onButtonPressed(Button.A, function () { +let sprite: game.LedSprite = null +input.onButtonEvent(Button.A, ButtonEvent.Click, function () { if (sprite.get(LedSpriteProperty.X) == 2) { game.addScore(1) } else { diff --git a/docs/reference/basic/forever.md b/docs/reference/basic/forever.md index c7f9bfb6..4babcb0a 100644 --- a/docs/reference/basic/forever.md +++ b/docs/reference/basic/forever.md @@ -43,7 +43,7 @@ let num = 0 basic.forever(() => { basic.showNumber(num) }) -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { num = num + 1 }) ``` @@ -59,7 +59,7 @@ Try this on your @boardname@: basic.forever(() => { basic.showNumber(6789) }) -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { basic.showNumber(2) }) ``` diff --git a/docs/reference/bluetooth/stop-advertising.md b/docs/reference/bluetooth/stop-advertising.md index 2753a273..464507cc 100644 --- a/docs/reference/bluetooth/stop-advertising.md +++ b/docs/reference/bluetooth/stop-advertising.md @@ -24,7 +24,7 @@ bluetooth.stopAdvertising(); ## Example: stop advertising on button pressed ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { bluetooth.stopAdvertising(); }) ``` diff --git a/docs/reference/bluetooth/uart-write-line.md b/docs/reference/bluetooth/uart-write-line.md index 5a24714d..5387ef12 100644 --- a/docs/reference/bluetooth/uart-write-line.md +++ b/docs/reference/bluetooth/uart-write-line.md @@ -27,7 +27,7 @@ bluetooth.onBluetoothDisconnected(() => { basic.showString("D"); connected = 0; }); -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { if (connected == 1) { bluetooth.uartWriteLine("HELLO"); } diff --git a/docs/reference/bluetooth/uart-write-string.md b/docs/reference/bluetooth/uart-write-string.md index 695ac943..46d4415e 100644 --- a/docs/reference/bluetooth/uart-write-string.md +++ b/docs/reference/bluetooth/uart-write-string.md @@ -27,7 +27,7 @@ bluetooth.onBluetoothDisconnected(() => { basic.showString("D"); connected = 0; }); -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { if (connected == 1) { bluetooth.uartWriteString("HELLO"); } diff --git a/docs/reference/control/in-background.md b/docs/reference/control/in-background.md index 7ac747c5..93b4dd57 100644 --- a/docs/reference/control/in-background.md +++ b/docs/reference/control/in-background.md @@ -29,7 +29,7 @@ control.inBackground(() => { basic.pause(100) } }) -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { num++; }) ``` @@ -42,7 +42,7 @@ let num = 0 basic.forever(() => { basic.showNumber(num) }) -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { num++; }) ``` diff --git a/docs/reference/control/reset.md b/docs/reference/control/reset.md index 8131c07a..27dbf208 100644 --- a/docs/reference/control/reset.md +++ b/docs/reference/control/reset.md @@ -24,11 +24,11 @@ When you get tired of counting, press button `B` to reset the ```blocks let item = 0; basic.showNumber(item); -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { item = item + 1; basic.showNumber(item); }); -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { control.reset(); }); ``` diff --git a/docs/reference/event-handler.md b/docs/reference/event-handler.md index 0027a7a1..65fcdde3 100644 --- a/docs/reference/event-handler.md +++ b/docs/reference/event-handler.md @@ -9,7 +9,7 @@ An event handler is code that is associated with a particular event, such as "bu Functions named "on " create an association between an event and the event handler code. For example, the following code registers the event handler (the code between the `do` and `end` keywords) with the event of a press of button A: ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { basic.showString("hello", 150) }) ``` @@ -21,7 +21,7 @@ After this code executes, then whenever button A is pressed in the future, the s Once you have registered an event handler for an event, like above, that event handler is active for the rest of the program execution. If you want to stop the string "hello" from printing each time button A is pressed then you need to arrange for the following code to execute: ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { }) ``` @@ -32,10 +32,10 @@ The above code associated an event handler that does nothing with the event of a The above example also illustrates that there is only one event handler for each event. What is the result of the following code? ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { basic.showString("hello", 150) }) -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { basic.showString("goodbye", 150) }) ``` @@ -43,7 +43,7 @@ input.onButtonPressed(Button.A, () => { The answer is that whenever button A is pressed, the string "goodbye" will be printed. If you want both the strings "hello" and "goodbye" to be printed, you need to write the code like this: ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { basic.showString("hello", 150) basic.showString("goodbye", 150) }) diff --git a/docs/reference/game/add-score.md b/docs/reference/game/add-score.md index 70b5e2f4..ead1649a 100644 --- a/docs/reference/game/add-score.md +++ b/docs/reference/game/add-score.md @@ -16,11 +16,11 @@ Press button ``A`` as much as possible to increase the score. Press ``B`` to display the score and reset the score. ```blocks -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { basic.showNumber(game.score()) game.setScore(0) }) -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { game.addScore(1) }) ``` diff --git a/docs/reference/game/change-score-by.md b/docs/reference/game/change-score-by.md index a3cb236e..3ad51817 100644 --- a/docs/reference/game/change-score-by.md +++ b/docs/reference/game/change-score-by.md @@ -16,7 +16,7 @@ Press button ``A`` as much as possible. At the end of 10 seconds, the program will show your score. ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { game.addScore(1) }) game.startCountdown(10000) diff --git a/docs/reference/game/clear.md b/docs/reference/game/clear.md index 93b89c15..fcecfa2f 100644 --- a/docs/reference/game/clear.md +++ b/docs/reference/game/clear.md @@ -27,7 +27,7 @@ let img = images.createImage(` . . . . . `) img.showImage(0) -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { img.clear() img.showImage(0) }) diff --git a/docs/reference/game/game-over.md b/docs/reference/game/game-over.md index 2e50fea5..15f01fcd 100644 --- a/docs/reference/game/game-over.md +++ b/docs/reference/game/game-over.md @@ -14,10 +14,10 @@ If you press button `B`, it shows an animation and ends the game. ```blocks basic.showString("PICK A BUTTON"); -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { basic.showString("YOU WIN!"); }); -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { game.gameOver(); }); ``` diff --git a/docs/reference/game/if-on-edge-bounce.md b/docs/reference/game/if-on-edge-bounce.md index 841eaf5a..9972be49 100644 --- a/docs/reference/game/if-on-edge-bounce.md +++ b/docs/reference/game/if-on-edge-bounce.md @@ -20,7 +20,7 @@ degrees -- exactly the opposite direction. ```blocks let ball = game.createSprite(4, 2); basic.showNumber(ball.get(LedSpriteProperty.Direction)); -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { ball.ifOnEdgeBounce(); basic.showNumber(ball.get(LedSpriteProperty.Direction)); }); diff --git a/docs/reference/game/is-paused.md b/docs/reference/game/is-paused.md index 7f497d5d..ff5a7f46 100644 --- a/docs/reference/game/is-paused.md +++ b/docs/reference/game/is-paused.md @@ -15,7 +15,7 @@ game.isPaused() Resume the game if it's paused and button **B** is pressed. ```blocks -input.onButtonPressed(Button.B, function () { +input.onButtonEvent(Button.B, ButtonEvent.Click, function () { if (game.isPaused()) { game.resume() } diff --git a/docs/reference/game/is-running.md b/docs/reference/game/is-running.md index 40ee9565..2ceba3f3 100644 --- a/docs/reference/game/is-running.md +++ b/docs/reference/game/is-running.md @@ -15,7 +15,7 @@ game.isRunning() If the game is currently running, end the game if button **B** is pressed. ```blocks -input.onButtonPressed(Button.B, function () { +input.onButtonEvent(Button.B, ButtonEvent.Click, function () { if (game.isRunning()) { game.gameOver() } diff --git a/docs/reference/game/score.md b/docs/reference/game/score.md index b2fe712a..2d7dcd47 100644 --- a/docs/reference/game/score.md +++ b/docs/reference/game/score.md @@ -13,7 +13,7 @@ This program adds one point to your score every time you press button second) and shows your score. ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { game.addScore(1); basic.pause(500); basic.showNumber(game.score()); diff --git a/docs/reference/game/set-score.md b/docs/reference/game/set-score.md index e6cda6e6..b51e3a1a 100644 --- a/docs/reference/game/set-score.md +++ b/docs/reference/game/set-score.md @@ -16,11 +16,11 @@ Press button ``A`` as much as possible to increase the score. Press ``B`` to display the score and reset the score. ```blocks -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { basic.showNumber(game.score()) game.setScore(0) }) -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { game.addScore(1) }) ``` diff --git a/docs/reference/game/start-countdown.md b/docs/reference/game/start-countdown.md index 89ca2a77..d5cc0178 100644 --- a/docs/reference/game/start-countdown.md +++ b/docs/reference/game/start-countdown.md @@ -17,7 +17,7 @@ Press button ``A`` as much as possible. At the end of 10 seconds, the program will show your score. ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { game.addScore(1) }) game.startCountdown(10000) diff --git a/docs/reference/images/arrow-image.md b/docs/reference/images/arrow-image.md index 98b8c36c..519afa7f 100644 --- a/docs/reference/images/arrow-image.md +++ b/docs/reference/images/arrow-image.md @@ -29,10 +29,10 @@ Display a left arrow when button A is pressed or a right arrow when button B is let arrowLeft = images.arrowImage(ArrowNames.West) let arrowRight = images.arrowImage(ArrowNames.East) -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { arrowLeft.showImage(0); }); -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { arrowRight.showImage(0); }); ``` diff --git a/docs/reference/images/create-big-image.md b/docs/reference/images/create-big-image.md index c5c75db1..5fd3ced8 100644 --- a/docs/reference/images/create-big-image.md +++ b/docs/reference/images/create-big-image.md @@ -36,10 +36,10 @@ let arrows = images.createBigImage(` . . # . . . # # # . . . # . . . . # . . `); -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { arrows.showImage(0); }); -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { arrows.showImage(5); }); ``` diff --git a/docs/reference/images/create-image.md b/docs/reference/images/create-image.md index d4ab2563..ce44163d 100644 --- a/docs/reference/images/create-image.md +++ b/docs/reference/images/create-image.md @@ -25,7 +25,7 @@ arrow and show it on the LED screen. If you press button `B`, the program will show a picture of the arrow upside-down. ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { images.createImage(` . . # . . . # # # . @@ -34,7 +34,7 @@ input.onButtonPressed(Button.A, () => { . . # . . `).showImage(0); }); -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { images.createImage(` . . # . . . . # . . diff --git a/docs/reference/images/icon-image.md b/docs/reference/images/icon-image.md index d8ae4333..b6d48f51 100644 --- a/docs/reference/images/icon-image.md +++ b/docs/reference/images/icon-image.md @@ -20,10 +20,10 @@ Show a happy face when button A is pressed or a sad face when button B is presse let iamHappy = images.iconImage(IconNames.Happy) let iamSad = images.iconImage(IconNames.Sad) -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { iamHappy.showImage(0); }); -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { iamSad.showImage(0); }); ``` diff --git a/docs/reference/images/show-image.md b/docs/reference/images/show-image.md index 3867e500..ae8dd6cf 100644 --- a/docs/reference/images/show-image.md +++ b/docs/reference/images/show-image.md @@ -31,10 +31,10 @@ let arrows = images.createBigImage(` . . # . . . # # # . . . # . . . . # . . `); -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { arrows.showImage(0); }); -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { arrows.showImage(5); }); ``` diff --git a/docs/reference/input.md b/docs/reference/input.md index 80920799..16567982 100644 --- a/docs/reference/input.md +++ b/docs/reference/input.md @@ -3,13 +3,13 @@ Events and data from sensors ```cards -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { }); input.onGesture(Gesture.Shake, () => { }); -input.onPinPressed(TouchPin.P0, () => { +input.onPinTouched(TouchPin.P0, Button.Click, () => { }); input.buttonIsPressed(Button.A); diff --git a/docs/reference/input/calibrate-compass.md b/docs/reference/input/calibrate-compass.md index 6096a82b..71e86dcc 100644 --- a/docs/reference/input/calibrate-compass.md +++ b/docs/reference/input/calibrate-compass.md @@ -23,7 +23,7 @@ confuse the @boardname@. This example runs the calibration when the user presses **A+B** buttons. ```blocks -input.onButtonPressed(Button.AB, () => { +input.onButtonEvent(Button.AB, ButtonEvent.Click, () => { input.calibrateCompass(); }) ``` diff --git a/docs/reference/input/compass-heading.md b/docs/reference/input/compass-heading.md index 5696c29e..62cbfe54 100644 --- a/docs/reference/input/compass-heading.md +++ b/docs/reference/input/compass-heading.md @@ -70,7 +70,7 @@ confuse the @boardname@. Keep the calibration handy by running it when the user pressed **A+B**. ```block -input.onButtonPressed(Button.AB, () => { +input.onButtonEvent(Button.AB, ButtonEvent.Click, () => { input.calibrateCompass(); }) ``` diff --git a/docs/reference/input/light-level.md b/docs/reference/input/light-level.md index 558aab08..52360fb5 100644 --- a/docs/reference/input/light-level.md +++ b/docs/reference/input/light-level.md @@ -29,7 +29,7 @@ program shows the light level on the [LED screen](/device/screen). ```blocks -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { let level = input.lightLevel() basic.showNumber(level) }) diff --git a/docs/reference/input/on-button-pressed.md b/docs/reference/input/on-button-pressed.md index c17d7049..13bae578 100644 --- a/docs/reference/input/on-button-pressed.md +++ b/docs/reference/input/on-button-pressed.md @@ -9,7 +9,7 @@ on the @boardname@. * For `A` and `B` together: This handler works when `A` and `B` are both pushed down, then one of them is released within 1.5 seconds of pushing down the second button. ```sig -input.onButtonPressed(Button.A, () => {}) +input.onButtonEvent(Button.A, ButtonEvent.Click, () => {}) ``` Find out how buttons provide input to the @boardname@ in this video: @@ -24,7 +24,7 @@ Each time you press the button, the [LED screen](/device/screen) shows the `coun ```blocks let count = 0 basic.showNumber(count) -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { count++; basic.showNumber(count); }) @@ -35,7 +35,7 @@ input.onButtonPressed(Button.A, () => { This example shows a number from 1 to 6 when you press the `B` button. ```blocks -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { let dice = Math.randomRange(0, 5) + 1 basic.showNumber(dice) }) diff --git a/docs/reference/input/on-pin-pressed.md b/docs/reference/input/on-pin-pressed.md index 7d384c6e..3ada6ba4 100644 --- a/docs/reference/input/on-pin-pressed.md +++ b/docs/reference/input/on-pin-pressed.md @@ -14,7 +14,7 @@ through your body and back into the @boardname@. This is called **completing a circuit**. It's like you're a big wire! ```sig -input.onPinPressed(TouchPin.P0, () => { +input.onPinTouched(TouchPin.P0, Button.Click, () => { }) ``` @@ -43,7 +43,7 @@ Every time you press the pin, the program shows the number of times on the scree ```blocks let count = 0 basic.showNumber(count) -input.onPinPressed(TouchPin.P0, () => { +input.onPinTouched(TouchPin.P0, Button.Click, () => { count = count + 1 basic.showNumber(count) }) diff --git a/docs/reference/input/on-pin-released.md b/docs/reference/input/on-pin-released.md index 724a3282..c70e590c 100644 --- a/docs/reference/input/on-pin-released.md +++ b/docs/reference/input/on-pin-released.md @@ -13,7 +13,7 @@ through your body and back into the @boardname@. This is called **completing a circuit**. It's like you're a big wire! ```sig -input.onPinReleased(TouchPin.P0, () => { +input.onPinTouched(TouchPin.P0, ButtonEvent.Click, () => { }) ``` @@ -36,7 +36,7 @@ Every time you release the pin, the program shows the number of times on the scr ```blocks let count = 0 basic.showNumber(count, 100) -input.onPinReleased(TouchPin.P0, () => { +input.onPinTouched(TouchPin.P0, ButtonEvent.Click, () => { count = count + 1 basic.showNumber(count, 100) }) diff --git a/docs/reference/input/running-time.md b/docs/reference/input/running-time.md index 099fb640..6cc79d57 100644 --- a/docs/reference/input/running-time.md +++ b/docs/reference/input/running-time.md @@ -18,7 +18,7 @@ program finds the number of milliseconds since the program started and shows it on the [LED screen](/device/screen). ```blocks -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { let now = input.runningTime() basic.showNumber(now) }) diff --git a/docs/reference/led/enable.md b/docs/reference/led/enable.md index 9123e2fb..4e02db14 100644 --- a/docs/reference/led/enable.md +++ b/docs/reference/led/enable.md @@ -15,7 +15,7 @@ led.enable(false); This program turns off the screen when pressing button ``B`` ```blocks -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { led.enable(false) }); ``` diff --git a/docs/reference/led/stop-animation.md b/docs/reference/led/stop-animation.md index 050d0a2f..1cd9159f 100644 --- a/docs/reference/led/stop-animation.md +++ b/docs/reference/led/stop-animation.md @@ -13,7 +13,7 @@ This program sets up the ``stop animation`` part of the program, and then shows a string that you can stop with button ``B``. ```blocks -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { led.stopAnimation(); }); basic.showString("STOP ME! STOP ME! PLEASE, WON'T SOMEBODY STOP ME?"); diff --git a/docs/reference/music/on-event.md b/docs/reference/music/on-event.md index 4c019542..b5655d7b 100644 --- a/docs/reference/music/on-event.md +++ b/docs/reference/music/on-event.md @@ -43,7 +43,7 @@ music.onEvent(MusicEvent.BackgroundMelodyResumed, () => { music.onEvent(MusicEvent.BackgroundMelodyRepeated, () => { serial.writeLine("background repeated") }) -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { music.beginMelody(music.builtInMelody(Melodies.BaDing), MelodyOptions.Once) }) music.setTempo(100) diff --git a/docs/reference/music/set-play-tone.md b/docs/reference/music/set-play-tone.md index 81c168cd..8d8f5347 100644 --- a/docs/reference/music/set-play-tone.md +++ b/docs/reference/music/set-play-tone.md @@ -17,7 +17,7 @@ This example send the frequency and duration over radio and plays it on the remote @boardname@. ```typescript -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { music.playTone(440, 120) led.toggle(0, 0) }) @@ -26,7 +26,7 @@ radio.onReceivedNumber(function (receivedNumber) { const duration = receivedNumber & 0xffff; music.playTone(freq, duration); }) -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { music.setPlayTone((frequency: number, duration: number) => { radio.sendNumber((frequency << 16) | (duration & 0xffff)); }) diff --git a/docs/reference/pins/digital-read-pin.md b/docs/reference/pins/digital-read-pin.md index f3338b87..9b0d6955 100644 --- a/docs/reference/pins/digital-read-pin.md +++ b/docs/reference/pins/digital-read-pin.md @@ -48,7 +48,7 @@ keeper @boardname@, you can press button `B` on the remote to buzz and make the score bigger on the other @boardname@. ```blocks -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { pins.digitalWritePin(DigitalPin.P1, 1); basic.pause(500); pins.digitalWritePin(DigitalPin.P1, 0); diff --git a/docs/reference/pins/digital-write-pin.md b/docs/reference/pins/digital-write-pin.md index 9b03fec0..2cc7a861 100644 --- a/docs/reference/pins/digital-write-pin.md +++ b/docs/reference/pins/digital-write-pin.md @@ -46,7 +46,7 @@ will use ``digital write pin`` to make the other @boardname@ buzz and make the score bigger. ```blocks -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { pins.digitalWritePin(DigitalPin.P1, 1); basic.pause(500); pins.digitalWritePin(DigitalPin.P1, 0); diff --git a/docs/reference/radio/receive-string.md b/docs/reference/radio/receive-string.md index 3c6ae814..c5ecbb2c 100644 --- a/docs/reference/radio/receive-string.md +++ b/docs/reference/radio/receive-string.md @@ -34,7 +34,7 @@ If you load this program onto two or more @boardname@s, you can send a code word The other @boardname@s will receive the code word and then show it. ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { radio.sendString("Codeword: TRIMARAN") basic.showString("SENT"); }) @@ -59,10 +59,10 @@ This program will also receive your friend's mood. ```blocks let data: string = ""; -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { radio.sendString("H"); }); -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { radio.sendString("S"); }); radio.onDataReceived(() => { diff --git a/docs/reference/radio/send-number.md b/docs/reference/radio/send-number.md index 15303c6f..a396efab 100644 --- a/docs/reference/radio/send-number.md +++ b/docs/reference/radio/send-number.md @@ -27,7 +27,7 @@ in the `x` direction (left and right) to other @boardname@s. This kind of program might be useful in a model car or model rocket. ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { radio.sendNumber(input.acceleration(Dimension.X)) }) ``` diff --git a/docs/reference/radio/send-string.md b/docs/reference/radio/send-string.md index 07211194..75e295e1 100644 --- a/docs/reference/radio/send-string.md +++ b/docs/reference/radio/send-string.md @@ -26,7 +26,7 @@ code word from one of them to the others by pressing button `A`. The other @boardname@s will receive the code word and then show it. ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { radio.sendString("Codeword: TRIMARAN") basic.showString("SENT"); }) diff --git a/docs/reference/radio/send-value.md b/docs/reference/radio/send-value.md index b3737c1b..bbcf4fd5 100644 --- a/docs/reference/radio/send-value.md +++ b/docs/reference/radio/send-value.md @@ -29,7 +29,7 @@ or model rocket. ```blocks radio.setGroup(99) -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { radio.sendValue("acc", input.acceleration(Dimension.X)) }) ``` diff --git a/docs/reference/radio/write-received-packet-to-serial.md b/docs/reference/radio/write-received-packet-to-serial.md index 26fc44b6..7d7ca22a 100644 --- a/docs/reference/radio/write-received-packet-to-serial.md +++ b/docs/reference/radio/write-received-packet-to-serial.md @@ -30,7 +30,7 @@ the second @boardname@), this program sends temperature data to the serial port. ```blocks -input.onButtonPressed(Button.A, function () { +input.onButtonEvent(Button.A, ButtonEvent.Click, function () { radio.sendNumber(input.temperature()) radio.sendValue("temp", input.temperature()) radio.sendString("It's warm now") diff --git a/docs/reference/radio/write-value-to-serial.md b/docs/reference/radio/write-value-to-serial.md index d3feb48e..c3b0815e 100644 --- a/docs/reference/radio/write-value-to-serial.md +++ b/docs/reference/radio/write-value-to-serial.md @@ -29,7 +29,7 @@ the second @boardname@), this program sends temperature data to serial. ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { radio.sendNumber(input.temperature()); }); radio.onDataReceived(() => { diff --git a/docs/reference/serial/redirect.md b/docs/reference/serial/redirect.md index 4563c646..8a688b84 100644 --- a/docs/reference/serial/redirect.md +++ b/docs/reference/serial/redirect.md @@ -32,7 +32,7 @@ serial port to use the pins. The new configuration uses pin ``P1`` to transmit a ``P2`` to receive. The baud rate is set to `9600`. ```blocks -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { serial.redirect(SerialPin.P1, SerialPin.P2, BaudRate.BaudRate9600); }); ``` diff --git a/docs/types/buffer/using-buffers.md b/docs/types/buffer/using-buffers.md index 352d2fd6..5e112b7e 100644 --- a/docs/types/buffer/using-buffers.md +++ b/docs/types/buffer/using-buffers.md @@ -57,7 +57,7 @@ You could simply save the light measurements in an array like this: ```blocks let darkness: number[] = [] -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { for (let i = 0; i < 60 * 4; i++) { darkness.push(input.lightLevel()) basic.pause(60000) @@ -77,7 +77,7 @@ The code in blocks for recording the light level is modified to make our file da ```typescript-ignore let darkness = pins.createBuffer(60 * 4); -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { for (let i = 0; i < 60 * 4; i++) { darkness.setNumber(NumberFormat.UInt8LE, i, input.lightLevel()) basic.pause(60000) @@ -90,7 +90,7 @@ Later, we can upload the file to the laptop computer by pressing the **B** butto ```typescript-ignore let dataReady = false; let darkness = pins.createBuffer(60 * 4); -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { for (let i = 0; i < 60 * 4; i++) { darkness.setNumber(NumberFormat.UInt8LE, i, input.lightLevel()) basic.pause(60000) @@ -98,7 +98,7 @@ input.onButtonPressed(Button.A, () => { dataReady = true; }) -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { if (dataReady) { serial.writeLine("Transferring file: DARKNESS, Length: " + darkness.length + " bytes..."); serial.writeBuffer(darkness) diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index d43084a7..3b79d239 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -378,6 +378,7 @@ "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|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|param|button": "the button to query the request, eg: Button.A", "input.calibrate": "Obsolete, use input.calibrateCompass instead.", @@ -388,6 +389,10 @@ "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|param|dimension": "the x, y, or z dimension, eg: Dimension.X", + "input.onButtonEvent": "Do something when a button (A, B or both A+B) is pushed down and released again.", + "input.onButtonEvent|param|body": "code to run when event is raised", + "input.onButtonEvent|param|button": "the button", + "input.onButtonEvent|param|eventType": "event Type", "input.onButtonPressed": "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.onButtonPressed|param|button": "the button that needs to be pressed", @@ -404,6 +409,9 @@ "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.onPinTouched": "Do something when a pin is touched and released again (while also touching the GND pin).", + "input.onPinTouched|param|body": "the code to run when event is fired on pin", + "input.onPinTouched|param|name": "the pin, eg: TouchPin.P0", "input.onScreenDown": "Attaches code to run when the screen is facing down.", "input.onScreenDown|param|body": "TODO", "input.onScreenUp": "Attaches code to run when the screen is facing up.", diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index 801407b1..9283219b 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -45,6 +45,10 @@ "BeatFraction.Whole|block": "1", "Buffer|block": "Buffer", "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.Green|block": "green", "Colors.Indigo|block": "indigo", @@ -302,16 +306,19 @@ "images.iconImage|block": "icon image %i", "images|block": "images", "input.acceleration|block": "acceleration (mg)|%NAME", + "input.buttonEventValueId|block": "%id", "input.buttonIsPressed|block": "button|%NAME|is pressed", "input.calibrateCompass|block": "calibrate compass", "input.compassHeading|block": "compass heading (°)", "input.isGesture|block": "is %gesture gesture", "input.lightLevel|block": "light level", "input.magneticForce|block": "magnetic force (µT)|%NAME", + "input.onButtonEvent|block": "on button %NAME| is %eventType=control_button_event_value_id", "input.onButtonPressed|block": "on button|%NAME|pressed", "input.onGesture|block": "on |%NAME", "input.onPinPressed|block": "on pin %name|pressed", "input.onPinReleased|block": "on pin %NAME|released", + "input.onPinTouched|block": "on pin %name|is %eventType=control_button_event_value_id", "input.pinIsPressed|block": "pin %NAME|is pressed", "input.rotation|block": "rotation (°)|%NAME", "input.runningTimeMicros|block": "running time (micros)", diff --git a/libs/core/blocks-test/input.blocks b/libs/core/blocks-test/input.blocks index dce61b60..a35685af 100644 --- a/libs/core/blocks-test/input.blocks +++ b/libs/core/blocks-test/input.blocks @@ -123,10 +123,7 @@ Gesture.SixG - + TouchPin.P2 - - TouchPin.P2 - \ No newline at end of file diff --git a/libs/core/blocks-test/test.blocks b/libs/core/blocks-test/test.blocks index d1e1be1e..aef7be03 100644 --- a/libs/core/blocks-test/test.blocks +++ b/libs/core/blocks-test/test.blocks @@ -117,7 +117,7 @@ - + TouchPin.P2 @@ -350,9 +350,6 @@ 0 - - TouchPin.P2 - @@ -484,14 +481,6 @@ - - TouchPin.P2 - - - AcceleratorRange.EightG - - - diff --git a/libs/core/enums.d.ts b/libs/core/enums.d.ts index 34620484..50246c6e 100644 --- a/libs/core/enums.d.ts +++ b/libs/core/enums.d.ts @@ -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 { //% block=x X = 0, diff --git a/libs/core/game.ts b/libs/core/game.ts index 41038ce7..6da731d0 100644 --- a/libs/core/game.ts +++ b/libs/core/game.ts @@ -323,9 +323,9 @@ namespace game { } function unplugEvents(): void { - input.onButtonPressed(Button.A, () => { }); - input.onButtonPressed(Button.B, () => { }); - input.onButtonPressed(Button.AB, () => { + input.onButtonEvent(Button.A, ButtonEvent.Click, () => { }); + input.onButtonEvent(Button.B, ButtonEvent.Click, () => { }); + input.onButtonEvent(Button.AB, ButtonEvent.Click, () => { control.reset(); }); } diff --git a/libs/core/input.cpp b/libs/core/input.cpp index d268df2a..dec3964c 100644 --- a/libs/core/input.cpp +++ b/libs/core/input.cpp @@ -7,6 +7,21 @@ enum class Button { 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 { //% block=x X = 0, @@ -161,14 +176,29 @@ enum class MesDpadButtonInfo { //% color=#B4009E weight=99 icon="\uf192" namespace input { + /** + * Do something when a button (A, B or both A+B) is pushed down and released again. + * @param button the button + * @param body code to run when event is raised + * @param eventType event Type + */ + //% help=input/on-button-event weight=85 blockGap=16 + //% blockId=device_button_event block="on button %NAME| is %eventType=control_button_event_value_id" + //% parts="buttonpair" + void onButtonEvent(Button button, int eventType, Action body) { + registerWithDal((int)button, eventType, body); + } + + // Deprecated /** * 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 body code to run when event is raised */ //% help=input/on-button-pressed weight=85 blockGap=16 - //% blockId=device_button_event block="on button|%NAME|pressed" + //% blockId=device_button_pressed block="on button|%NAME|pressed" //% parts="buttonpair" + //% blockHidden=true void onButtonPressed(Button button, Action body) { registerWithDal((int)button, MICROBIT_BUTTON_EVT_CLICK, body); } @@ -206,13 +236,31 @@ namespace input { return uBit.accelerometer.getGesture() == gi; } - /** + /** + * Do something when a pin is touched and released again (while also touching the GND pin). + * @param name the pin, eg: TouchPin.P0 + * @param body the code to run when event is fired on pin + */ + //% help=input/on-pin-touch weight=83 blockGap=32 + //% blockId=device_pin_touch block="on pin %name|is %eventType=control_button_event_value_id" + void onPinTouched(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 + /** * 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 body the code to run when the pin is pressed */ //% help=input/on-pin-pressed weight=83 blockGap=32 - //% blockId=device_pin_event block="on pin %name|pressed" + //% blockId=device_pin_input block="on pin %name|pressed" + //% blockHidden=true void onPinPressed(TouchPin name, Action body) { auto pin = getPin((int)name); if (!pin) return; @@ -222,6 +270,7 @@ namespace input { 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 @@ -229,7 +278,7 @@ namespace input { */ //% help=input/on-pin-released weight=6 blockGap=16 //% blockId=device_pin_released block="on pin %NAME|released" - //% advanced=true + //% blockHidden=true void onPinReleased(TouchPin name, Action body) { auto pin = getPin((int)name); if (!pin) return; diff --git a/libs/core/input.ts b/libs/core/input.ts index c6d0a487..33c768b3 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -3,6 +3,15 @@ */ //% color=#C90072 weight=99 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. * @param body TODO diff --git a/libs/core/shims.d.ts b/libs/core/shims.d.ts index 9d21058b..68e1eb64 100644 --- a/libs/core/shims.d.ts +++ b/libs/core/shims.d.ts @@ -222,14 +222,26 @@ declare namespace basic { //% color=#B4009E weight=99 icon="\uf192" declare namespace input { + /** + * Do something when a button (A, B or both A+B) is pushed down and released again. + * @param button the button + * @param body code to run when event is raised + * @param eventType event Type + */ + //% help=input/on-button-event weight=85 blockGap=16 + //% blockId=device_button_event block="on button %NAME| is %eventType=control_button_event_value_id" + //% parts="buttonpair" shim=input::onButtonEvent + function onButtonEvent(button: Button, eventType: int32, body: () => void): void; + /** * 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 body code to run when event is raised */ //% help=input/on-button-pressed weight=85 blockGap=16 - //% blockId=device_button_event block="on button|%NAME|pressed" - //% parts="buttonpair" shim=input::onButtonPressed + //% blockId=device_button_pressed block="on button|%NAME|pressed" + //% parts="buttonpair" + //% blockHidden=true shim=input::onButtonPressed function onButtonPressed(button: Button, body: () => void): void; /** @@ -253,13 +265,23 @@ declare namespace input { //% gesture.fieldEditor="gestures" gesture.fieldOptions.columns=4 shim=input::isGesture function isGesture(gesture: Gesture): boolean; + /** + * Do something when a pin is touched and released again (while also touching the GND pin). + * @param name the pin, eg: TouchPin.P0 + * @param body the code to run when event is fired on pin + */ + //% help=input/on-pin-touch weight=83 blockGap=32 + //% blockId=device_pin_touch block="on pin %name|is %eventType=control_button_event_value_id" shim=input::onPinTouched + function onPinTouched(name: TouchPin, eventType: int32, body: () => void): void; + /** * 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 body the code to run when the pin is pressed */ //% help=input/on-pin-pressed weight=83 blockGap=32 - //% blockId=device_pin_event block="on pin %name|pressed" shim=input::onPinPressed + //% blockId=device_pin_input block="on pin %name|pressed" + //% blockHidden=true shim=input::onPinPressed function onPinPressed(name: TouchPin, body: () => void): void; /** @@ -269,7 +291,7 @@ declare namespace input { */ //% help=input/on-pin-released weight=6 blockGap=16 //% blockId=device_pin_released block="on pin %NAME|released" - //% advanced=true shim=input::onPinReleased + //% blockHidden=true shim=input::onPinReleased function onPinReleased(name: TouchPin, body: () => void): void; /** diff --git a/libs/radio-broadcast/docs/reference/radio/on-received-message.md b/libs/radio-broadcast/docs/reference/radio/on-received-message.md index 8946d0fd..fc6cbc0c 100644 --- a/libs/radio-broadcast/docs/reference/radio/on-received-message.md +++ b/libs/radio-broadcast/docs/reference/radio/on-received-message.md @@ -22,10 +22,10 @@ enum RadioMessage { heart, skull } -input.onButtonPressed(Button.A, function () { +input.onButtonEvent(Button.A, ButtonEvent.Click, function () { radio.sendMessage(RadioMessage.heart) }) -input.onButtonPressed(Button.B, function () { +input.onButtonEvent(Button.B, ButtonEvent.Click, function () { radio.sendMessage(RadioMessage.skull) }) radio.onReceivedMessage(RadioMessage.heart, function () { diff --git a/libs/radio-broadcast/docs/reference/radio/send-message.md b/libs/radio-broadcast/docs/reference/radio/send-message.md index e8dd9619..01058f11 100644 --- a/libs/radio-broadcast/docs/reference/radio/send-message.md +++ b/libs/radio-broadcast/docs/reference/radio/send-message.md @@ -20,10 +20,10 @@ enum RadioMessage { heart, skull } -input.onButtonPressed(Button.A, function () { +input.onButtonEvent(Button.A, ButtonEvent.Click, function () { radio.sendMessage(RadioMessage.heart) }) -input.onButtonPressed(Button.B, function () { +input.onButtonEvent(Button.B, ButtonEvent.Click, function () { radio.sendMessage(RadioMessage.skull) }) radio.onReceivedMessage(RadioMessage.heart, function () { diff --git a/sim/state/buttonpair.ts b/sim/state/buttonpair.ts index 419b8d39..3840359c 100644 --- a/sim/state/buttonpair.ts +++ b/sim/state/buttonpair.ts @@ -1,13 +1,23 @@ 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 { let b = board().buttonPairState; if (button == b.props.ID_BUTTON_AB && !b.usesButtonAB) { b.usesButtonAB = true; runtime.queueDisplayUpdate(); } - pxtcore.registerWithDal(button, DAL.MICROBIT_BUTTON_EVT_CLICK, handler); + pxtcore.registerWithDal(button, ButtonEvent.Click, handler); } - + export function buttonIsPressed(button: number): boolean { let b = board().buttonPairState; if (button == b.abBtn.id && !b.usesButtonAB) { diff --git a/sim/state/edgeconnector.ts b/sim/state/edgeconnector.ts index e32d6c4d..dc20100e 100644 --- a/sim/state/edgeconnector.ts +++ b/sim/state/edgeconnector.ts @@ -1,12 +1,22 @@ namespace pxsim.input { + export function onPinTouched(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) { let pin = getPin(pinId); if (!pin) return; pin.isTouched(); runtime.queueDisplayUpdate(); - pxtcore.registerWithDal(pin.id, DAL.MICROBIT_BUTTON_EVT_CLICK, handler); + pxtcore.registerWithDal(pin.id, ButtonEvent.Click, handler); } + // Deprecated export function onPinReleased(pinId: number, handler: RefAction) { let pin = getPin(pinId); if (!pin) return; diff --git a/tests/hat-game.ts b/tests/hat-game.ts index 751113f2..6fbb74bf 100644 --- a/tests/hat-game.ts +++ b/tests/hat-game.ts @@ -7,7 +7,7 @@ let level: number let swapSpeed: number initializeGame() -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { if (ballRevealing) { index = index + 1 if (index > 2) { @@ -16,7 +16,7 @@ input.onButtonPressed(Button.A, () => { basic.showString(cupSelect[index], 150) } }) -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { if (ballRevealing) { ballRevealing = false if (correctBall == index) { diff --git a/tests/meteorite.ts b/tests/meteorite.ts index 297da4b4..a0ecd0d8 100644 --- a/tests/meteorite.ts +++ b/tests/meteorite.ts @@ -18,7 +18,7 @@ counter = 0 pause = 700 led.plot(oneX, oneY) led.plot(twoX, twoY) -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { if (oneX > 0) { led.unplot(oneX, oneY) led.unplot(twoX, twoY) @@ -28,7 +28,7 @@ input.onButtonPressed(Button.A, () => { led.plot(twoX, twoY) } }) -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { if (twoX < 4) { led.unplot(oneX, oneY) led.unplot(twoX, twoY) diff --git a/tests/pac-man-runaway.ts b/tests/pac-man-runaway.ts index 196150c2..4c7c400b 100644 --- a/tests/pac-man-runaway.ts +++ b/tests/pac-man-runaway.ts @@ -107,12 +107,12 @@ basic.forever(() => { basic.pause(500) } }) -input.onButtonPressed(Button.A, () => { +input.onButtonEvent(Button.A, ButtonEvent.Click, () => { let temp = Math.abs(person.dirX) * (-1) person.dirX = Math.abs(person.dirY) * (-1) person.dirY = temp }) -input.onButtonPressed(Button.B, () => { +input.onButtonEvent(Button.B, ButtonEvent.Click, () => { let temp1 = Math.abs(person.dirX) person.dirX = Math.abs(person.dirY) person.dirY = temp1 diff --git a/tests/wg-canoe-polo-timer.ts b/tests/wg-canoe-polo-timer.ts index 3960be0b..7edd3e1e 100644 --- a/tests/wg-canoe-polo-timer.ts +++ b/tests/wg-canoe-polo-timer.ts @@ -24,13 +24,13 @@ playOneGame(gameTime) showFinalScores(scoreA, scoreB) function startIOMonitor() { - input.onButtonPressed(Button.A, () => { + input.onButtonEvent(Button.A, ButtonEvent.Click, () => { AWasPressed = true }) - input.onButtonPressed(Button.B, () => { + input.onButtonEvent(Button.B, ButtonEvent.Click, () => { BWasPressed = true }) - input.onButtonPressed(Button.AB, () => { + input.onButtonEvent(Button.AB, ButtonEvent.Click, () => { ABWasPressed = true AWasPressed = false BWasPressed = false diff --git a/tests/wg-dr-who.ts b/tests/wg-dr-who.ts index 204b59f2..1699b33c 100644 --- a/tests/wg-dr-who.ts +++ b/tests/wg-dr-who.ts @@ -501,13 +501,13 @@ function convertPenaltyTimeToScore(penaltyTime: number): number { } function startIOMonitor() { - input.onButtonPressed(Button.A, () => { + input.onButtonEvent(Button.A, ButtonEvent.Click, () => { AWasPressed = true }) - input.onButtonPressed(Button.B, () => { + input.onButtonEvent(Button.B, ButtonEvent.Click, () => { BWasPressed = true }) - input.onButtonPressed(Button.AB, () => { + input.onButtonEvent(Button.AB, ButtonEvent.Click, () => { ABWasPressed = true }) input.onShake(() => { diff --git a/tests/wg-operation.ts b/tests/wg-operation.ts index c43532ab..75eae609 100644 --- a/tests/wg-operation.ts +++ b/tests/wg-operation.ts @@ -377,11 +377,11 @@ function beepNTimesFor(times: number, duration: number) { function startIOMonitor() { aWasPressed = false - input.onButtonPressed(Button.A, () => { + input.onButtonEvent(Button.A, ButtonEvent.Click, () => { aWasPressed = true }) bWasPressed = false - input.onButtonPressed(Button.B, () => { + input.onButtonEvent(Button.B, ButtonEvent.Click, () => { bWasPressed = true }) wasTweezers = false diff --git a/tests/wg-user-confidance.ts b/tests/wg-user-confidance.ts index d03df848..c60e6f93 100644 --- a/tests/wg-user-confidance.ts +++ b/tests/wg-user-confidance.ts @@ -476,10 +476,10 @@ function testTiltZ() { } function startIOMonitor() { - input.onButtonPressed(Button.A, () => { + input.onButtonEvent(Button.A, ButtonEvent.Click, () => { AWasPressed = true }) - input.onButtonPressed(Button.B, () => { + input.onButtonEvent(Button.B, ButtonEvent.Click, () => { BWasPressed = true }) input.onShake(() => {