Updates for V4 (#197)

* update yotta defaults for 16kb devices

* refactor deprecated blocks

* updates for button events

* update button events

* update refference

* update docs

* update docs

* update button event blocks

* update docs

* update block id
This commit is contained in:
Juri Wolf
2022-08-10 18:36:19 +02:00
committed by GitHub
parent 32783f38ba
commit 5f7a8e5301
107 changed files with 1218 additions and 706 deletions

View File

@ -0,0 +1,62 @@
# Button event
Returns the ID of one of these button event types:
* Pressed Down (1)
* Released Up (2)
* Clicked (3)
* Long clicked (4)
* Hold (5)
This block can be used to define the event type in [on button event](input/on-button-event) and [on pin event](input/on-pin-event).
Note, that by pressing a Button multiple events can raise at the same moment:
| # | User input | Event raised |
|---|--------------------------|--------------|
| 1 | Press A down | A.Down |
| 2 | Hold A for > 1.5 seconds | A.Hold |
| 3 | release A | A.Up |
| | | A.LongClick |
| # | User input | Event raised |
|---|--------------|--------------|
| 1 | Press A down | A.Down |
| 2 | Press B down | B.Down |
| 3 | | A+B.Down |
| | Release A up | A.Up |
| | | A+B.Up |
| | | A+B.Click |
| | Release B up | B.Up |
* Every Up-Event will always be followed by either a Click- OR Long-Click-Event, depending on how long the Down-Event has been ago.
## Pressed Down
* For button `A` or `B`: This handler works when the button is pushed down.
* For `A` and `B` together: This handler works when `A` and `B` are both pushed down, at the moment the second button is pressed.
## Released Up
* For button `A` or `B`: This handler works when the button is released up.
* For `A` and `B` together: This handler works at the moment the first button is released up while `A` and `B` are both pushed down.
## Clicked
* For button `A` or `B`: This handler works when the button is pushed down and released within 1 second.
* 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.
## Long clicked
* For button `A` or `B`: This handler works when the button is pushed down and released after more than 1 second.
* For `A` and `B` together: This handler works when `A` and `B` are both pushed down, then one of them is released after more than 1.5 seconds after pushing down the second button.
## Hold
* For button `A` or `B`: This handler works when the button is pushed down and hold for 1 second.
* For `A` and `B` together: This handler works when `A` and `B` are both pushed down and hold for 1.5 seconds after pushing down the second button.
**This is an advanced API.** For more information, see the
[@boardname@ runtime messageBus documentation](https://lancaster-university.github.io/microbit-docs/ubit/messageBus/).
## See also
[on button event](input/on-button-event), [on pin event](input/on-pin-event)

View File

@ -23,7 +23,7 @@ confuse the @boardname@.
This example runs the calibration when the user presses **A+B** buttons.
```blocks
input.onButtonEvent(Button.AB, ButtonEvent.Click, () => {
input.onButtonEvent(Button.AB, input.buttonEventClick(), () => {
input.calibrateCompass();
})
```

View File

@ -70,7 +70,7 @@ confuse the @boardname@.
Keep the calibration handy by running it when the user pressed **A+B**.
```block
input.onButtonEvent(Button.AB, ButtonEvent.Click, () => {
input.onButtonEvent(Button.AB, input.buttonEventClick(), () => {
input.calibrateCompass();
})
```

View File

@ -29,7 +29,7 @@ program shows the light level
on the [LED screen](/device/screen).
```blocks
input.onButtonEvent(Button.B, ButtonEvent.Click, () => {
input.onButtonEvent(Button.B, input.buttonEventClick(), () => {
let level = input.lightLevel()
basic.showNumber(level)
})

View File

@ -0,0 +1,42 @@
# logo Is Pressed
Check if the @boardname@ logo is currently being pressed.
```sig
input.logoIsPressed()
```
## ~ reminder
![works with micro:bit V2 only image](/static/v2/v2-only.png)
This block requires the [micro:bit V2](/device/v2) hardware. If you use this block with a micro:bit v1 board, you will see the **927** error code on the screen.
## ~
The logo on the @boardname@ works just like a touch pin. You can check the whether or not the logo is currently being pressed. You use the [boolean](/types/boolean) value for the status of the logo press to make a logical decision in your program.
## Returns
* a [boolean](types/boolean) value that is `true` if the logo is pressed, `false` if the logo is not pressed.
## Example
Show an icon on the LEDs while the logo is pressed.
```blocks
basic.forever(function () {
if (input.logoIsPressed()) {
basic.showIcon(IconNames.Diamond)
} else {
basic.clearScreen()
}
})
```
## See also
[micro:bit V2](/device/v2),
[on logo event](/reference/input/on-logo-event),
[pin is pressed](/referene/inpu/pin-is-pressed),
[touch set mode](/referene/inpu/touch-set-mode)

View File

@ -1,7 +1,9 @@
# On Button Event
Start an [event handler](/reference/event-handler) (part of the program that will run when something happens, like when a button is pressed).
This handler works when button `A` or `B` is pressed, or `A` and `B` together.
This handler works when button `A` or `B` is clicked, or `A` and `B` together. You can choose another event type by using
the [button event block](/reference/input/button-event). Possible event types are `pressed down` (1), `released up` (2), `clicked` (3), `long clicked` (4) or `hold` (5).
When you are using this function in a web browser, click the buttons on the screen instead of the ones
on the @boardname@.
@ -9,7 +11,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.onButtonEvent(Button.A, ButtonEvent.Click, () => {})
input.onButtonEvent(Button.A, input.buttonEventClick(), () => {})
```
Find out how buttons provide input to the @boardname@ in this video:
@ -24,7 +26,7 @@ Each time you press the button, the [LED screen](/device/screen) shows the `coun
```blocks
let count = 0
basic.showNumber(count)
input.onButtonEvent(Button.A, ButtonEvent.Click, () => {
input.onButtonEvent(Button.A, input.buttonEventClick(), () => {
count++;
basic.showNumber(count);
})
@ -35,7 +37,7 @@ input.onButtonEvent(Button.A, ButtonEvent.Click, () => {
This example shows a number from 1 to 6 when you press the `B` button.
```blocks
input.onButtonEvent(Button.B, ButtonEvent.Click, () => {
input.onButtonEvent(Button.B, input.buttonEventClick(), () => {
let dice = randint(0, 5) + 1
basic.showNumber(dice)
})
@ -50,5 +52,5 @@ Otherwise, sometimes they would show a `0`.
## See also
[button is pressed](/reference/input/button-is-pressed), [forever](/reference/basic/forever), [random](/blocks/math)
[button is pressed](/reference/input/button-is-pressed), [forever](/reference/basic/forever), [random](/blocks/math), [button event block](/reference/input/button-event)

View File

@ -0,0 +1,38 @@
# on Logo Event
Run some code in your program when the @boardname@ logo is pressed, touched, or released.
```sig
input.onLogoEvent(TouchButtonEvent.Pressed, function () {})
```
### ~ reminder
![works with micro:bit V2 only image](/static/v2/v2-only.png)
This block requires the [micro:bit V2](/device/v2) hardware. If you use this block with a micro:bit v1 board, you will see the **927** error code on the screen.
### ~
The logo on the @boardname@ works just like a touch pin. The logo will detect your touch. You can have code inside an event that will run when the logo is pressed.
## Parameters
* **action**: the logo event to run your code for. The events are ``released``, ``pressed``, ``touched`` or ``long pressed``.
## Example
Show a message on the LEDs when the @boardname@ logo is pressed.
```blocks
input.onLogoEvent(TouchButtonEvent.Pressed, function () {
basic.showString("I was pressed!")
})
```
## See also
[micro:bit V2](/device/v2),
[logo is pressed](/reference/input/logo-is-pressed),
[on pin pressed](/reference/input/on-logo-released),
[touch set mode](/referene/inpu/touch-set-mode)

View File

@ -3,18 +3,19 @@
Start an [event handler](/reference/event-handler) (part of the
program that will run when something happens, like when a button is
pressed). This handler works when you touch pin `0`, `1`, or `2`
together with `GND`, and release it within 1 second.
together with `GND`, and release it within 1 second. You can choose another event type by using
the [button event block](/reference/input/button-event). Possible event types are `pressed down` (1), `released up` (2), `clicked` (3), `long clicked` (4) or `hold` (5).
When you are using this function in a web
browser, click the pins on the screen instead of the ones on the
@boardname@.
If you hold the `GND` pin with one hand and touch pin `0`, `1`, or `2`
If you hold the `GND` pin with one hand and touch pin `0`, `1`, `2` or `3`
with the other, a very small (safe) amount of electricity will flow
through your body and back into the @boardname@. This is called
**completing a circuit**. It's like you're a big wire!
```sig
input.onPinTouchEvent(TouchPin.P0, ButtonEvent.Click, () => {
input.onPinTouchEvent(TouchPin.P0, input.buttonEventClick(), () => {
})
```
@ -43,7 +44,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.onPinTouchEvent(TouchPin.P0, ButtonEvent.Click, () => {
input.onPinTouchEvent(TouchPin.P0, input.buttonEventClick(), () => {
count = count + 1
basic.showNumber(count)
})
@ -51,5 +52,5 @@ input.onPinTouchEvent(TouchPin.P0, ButtonEvent.Click, () => {
## See also
[@boardname@ pins](/device/pins), [pin is pressed](/reference/input/pin-is-pressed), [analog read pin](/reference/pins/analog-read-pin), [analog write pin](/reference/pins/analog-write-pin), [digital read pin](/reference/pins/digital-read-pin), [digital write pin](/reference/pins/digital-write-pin)
[@boardname@ pins](/device/pins), [pin is pressed](/reference/input/pin-is-pressed), [analog read pin](/reference/pins/analog-read-pin), [analog write pin](/reference/pins/analog-write-pin), [digital read pin](/reference/pins/digital-read-pin), [digital write pin](/reference/pins/digital-write-pin), [button event block](/reference/input/button-event)

View File

@ -0,0 +1,46 @@
# on Sound
Run some code when the microphone hears a sound.
```sig
input.onSound(DetectedSound.Loud, function () {})
```
The microphone will detect sounds that are quiet or loud. You can have the microphone detect
a sound at a certain level and run some code in and event when it hears the sound. There are
two sound ranges you can detect for: `loud` or `quiet`.
### ~ reminder
![works with micro:bit V2 only image](/static/v2/v2-only.png)
This block requires the [micro:bit V2](/device/v2) hardware. If you use this block with a micro:bit v1 board, you will see the **927** error code on the screen.
### ~
## Parameters
* **sound**: the type of sound to detect: `loud` or `quiet`.
* **handler**: the code to run when a sound is heard.
## Example
Show an icon animation when the microphone detects a sound.
```blocks
input.onSound(DetectedSound.Loud, function () {
basic.showIcon(IconNames.Square)
basic.showIcon(IconNames.SmallSquare)
basic.showIcon(IconNames.SmallDiamond)
basic.clearScreen()
})
```
# See also #seealso
[sound level](/reference/input/sound-level),
[set sound threshold](/reference/input/sound-level)
```package
microphone
```

View File

@ -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.onButtonEvent(Button.B, ButtonEvent.Click, () => {
input.onButtonEvent(Button.B, input.buttonEventClick(), () => {
let now = input.runningTime()
basic.showNumber(now)
})

View File

@ -0,0 +1,48 @@
# set Sound Threshold
Tell how loud it should be for your board to detect a loud sound.
```sig
input.setSoundThreshold(SoundThreshold.Loud, 0)
```
When the microphone hears a sound, it sets a number for how loud the sound was at that moment.
This number is the sound level and has a value from `0` (low sound) to `255` (loud sound). You can use
a sound level number as a _threshold_ (just the right amount of sound) to make the
[on sound](/reference/input/on-sound) event happen. To set a threshold, you choose the type of sound
to detect, `loud` or `quiet`, and then the sound level for that type.
### ~ reminder
![works with micro:bit V2 only image](/static/v2/v2-only.png)
This block requires the [micro:bit V2](/device/v2) hardware. If you use this block with a micro:bit v1 board, you will see the **927** error code on the screen.
### ~
## Parameters
* **sound**: the type of sound to dectect: `loud` or `quiet`.
* **threshold**: the sound level [number](/types/number) which makes a sound event happen.
## Example #example
Show an icon animation when the microphone detects a sound louder than a sound level of `200`.
```blocks
input.setSoundThreshold(SoundThreshold.Loud, 200)
input.onSound(DetectedSound.Loud, function () {
basic.showIcon(IconNames.Square)
basic.showIcon(IconNames.SmallSquare)
basic.showIcon(IconNames.SmallDiamond)
basic.clearScreen()
})
```
# See also #seealso
[on sound](/reference/input/on-sound), [sound level](/reference/input/sound-level)
```package
microphone
```

View File

@ -0,0 +1,32 @@
# sound Level
Find out what the the level of sound heard by the microphone is.
```sig
input.soundLevel()
```
## Returns
* a ``number`` between `0` (low sound) and `255` (loud sound) which tells how loud the sounds are that the microphone hears.
## Example
Show a checkerboard icon while the sound level is greater than `100`.
```blocks
basic.forever(function () {
if (input.soundLevel() > 100) {
basic.showIcon(IconNames.Chessboard)
} else {
basic.clearScreen()
}
})
```
## See also
```package
microphone
```