buttons
ButtonCallback
module-attribute
#
ButtonCallback = Callable[[str], None]
A function with 1 argument (te button "A" or "B")
ButtonState #
Bases: IntEnum
All possible states of a button:
- RELEASE: released
- PRESS: pressed
- LONG_PRESS: pressed for at least 2 seconds
Source code in src/kaspersmicrobit/services/buttons.py
16 17 18 19 20 21 22 23 24 25 26 |
|
ButtonService #
This class contains the functions that you can access related to the A and B buttons of the micro:bit
These are all options offered by the Bluetooth button service
See Also: https://lancaster-university.github.io/microbit-docs/ble/button-service/
Source code in src/kaspersmicrobit/services/buttons.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
|
is_available #
is_available() -> bool
Checks whether the Bluetooth service button is found on the connected micro:bit.
Returns:
-
bool
–true if the button service was found, false if not.
Source code in src/kaspersmicrobit/services/buttons.py
54 55 56 57 58 59 60 61 |
|
on_button_a #
on_button_a(
press: ButtonCallback = None,
long_press: ButtonCallback = None,
release: ButtonCallback = None,
)
You can call this function if you want to be notified when the A button of your micro:bit is pressed (press), long pressed (long_press) or released (release)
A function that you can pass as an argument is a function with 1 string parameter. This function will be called with the button name as argument.
Parameters:
-
press
(ButtonCallback
, default:None
) –a function called when the A button is pressed
-
long_press
(ButtonCallback
, default:None
) –a function that is called when pressed for a long time (at least 2 seconds). the A button is pressed
-
release
(ButtonCallback
, default:None
) –a function called when the A button is released
Raises:
-
BluetoothServiceNotFound
–When the button service is not active on the micro:bit
-
BluetoothCharacteristicNotFound
–When the button service is running but there was no way to get the activate notifications for button A (normally does not occur)
Source code in src/kaspersmicrobit/services/buttons.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
on_button_b #
on_button_b(
press: ButtonCallback = None,
long_press: ButtonCallback = None,
release: ButtonCallback = None,
)
You can call this function if you want to be notified when the B button of your micro:bit is pressed (press), long pressed (long_press) or released (release)
A function that you can pass as an argument is a function with 1 string parameter. This function will be called with the button name as argument.
Parameters:
-
press
(ButtonCallback
, default:None
) –a function called when the B button is pressed
-
long_press
(ButtonCallback
, default:None
) –a function that is called when pressed for a long time (at least 2 seconds). the B button is pressed
-
release
(ButtonCallback
, default:None
) –a function called when the B button is released
Raises:
-
BluetoothServiceNotFound
–When the button service is not active on the micro:bit
-
BluetoothCharacteristicNotFound
–When the button service is running but there was no way to get the activate notifications for button B (normally does not occur)
Source code in src/kaspersmicrobit/services/buttons.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
read_button_a #
read_button_a() -> ButtonState
Returns the state of the A button
Returns:
-
ButtonState
–The state of the A button (RELEASE, PRESS or LONG_PRESS)
Raises:
-
BluetoothServiceNotFound
–When the button service is not active on the micro:bit
-
BluetoothCharacteristicNotFound
–When the button service is running but there was no way to read the state of button A (normally does not occur)
Source code in src/kaspersmicrobit/services/buttons.py
109 110 111 112 113 114 115 116 117 118 119 120 121 |
|
read_button_b #
read_button_b() -> ButtonState
Returns the state of the B button
Returns(ButtonState): The state of the B button (RELEASE, PRESS or LONG_PRESS)
Raises:
-
BluetoothServiceNotFound
–When the button service is not active on the micro:bit
-
BluetoothCharacteristicNotFound
–When the button service is running but there was no way to read the state of button B (normally does not occur)
Source code in src/kaspersmicrobit/services/buttons.py
123 124 125 126 127 128 129 130 131 132 133 134 135 |
|