Ga naar inhoud

buttons

ButtonCallback module-attribute #

ButtonCallback = Callable[[str], None]

Een functie met 1 argument (de knop "A" of "B")

ButtonState #

Bases: IntEnum

Alle mogelijke toestanden van een knop:

  • RELEASE: losgelaten
  • PRESS: ingedrukt
  • LONG_PRESS: minstens 2 seconden lang ingedrukt
Source code in src/kaspersmicrobit/services/buttons.py
16
17
18
19
20
21
22
23
24
25
26
class ButtonState(IntEnum):
    """
    All possible states of a button:

    - RELEASE: released
    - PRESS: pressed
    - LONG_PRESS: pressed for at least 2 seconds
    """
    RELEASE = 0
    PRESS = 1
    PRESS_LONG = 2

ButtonService #

Deze klasse bevat de functies die je kan aanspreken in verband met de A en B knoppen van de micro:bit

Dit zijn alle mogelijkheden aangeboden door de bluetooth button service

Zie ook: 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
class 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/
    """

    def __init__(self, device: BluetoothDevice):
        self._device = device

    @staticmethod
    def _create_button_callback(button: str, press, long_press, up):
        def button_callback(sender, data):
            if ButtonState.RELEASE == data[0] and up:
                up(button)
            elif ButtonState.PRESS == data[0] and press:
                press(button)
            elif ButtonState.PRESS_LONG == data[0] and long_press:
                long_press(button)
            else:
                pass
        return button_callback

    def is_available(self) -> bool:
        """
        Checks whether the Bluetooth service button is found on the connected micro:bit.

        Returns:
            true if the button service was found, false if not.
        """
        return self._device.is_service_available(Service.BUTTON)

    def on_button_a(self, 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.

        Args:
            press (ButtonCallback): a function called when the A button is pressed
            long_press (ButtonCallback): a function that is called when pressed for a long time (at least 2 seconds).
                the A button is pressed
            release (ButtonCallback): a function called when the A button is released

        Raises:
            errors.BluetoothServiceNotFound: When the button service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the button service is running but there was no way to get the
                activate notifications for button A (normally does not occur)
        """
        self._device.notify(Service.BUTTON, Characteristic.BUTTON_A,
                            ButtonService._create_button_callback('A', press, long_press, release))

    def on_button_b(self, 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.

        Args:
            press (ButtonCallback): a function called when the B button is pressed
            long_press (ButtonCallback): a function that is called when pressed for a long time (at least 2 seconds).
                the B button is pressed
            release (ButtonCallback): a function called when the B button is released

        Raises:
            errors.BluetoothServiceNotFound: When the button service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the button service is running but there was no way to get the
                activate notifications for button B (normally does not occur)
        """
        self._device.notify(Service.BUTTON, Characteristic.BUTTON_B,
                            ButtonService._create_button_callback('B', press, long_press, release))

    def read_button_a(self) -> ButtonState:
        """
        Returns the state of the A button

        Returns:
            The state of the A button (RELEASE, PRESS or LONG_PRESS)

        Raises:
            errors.BluetoothServiceNotFound: When the button service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the button service is running but there was no way to
                read the state of button A (normally does not occur)
        """
        return ButtonState(self._device.read(Service.BUTTON, Characteristic.BUTTON_A)[0])

    def read_button_b(self) -> ButtonState:
        """
        Returns the state of the B button

        Returns(ButtonState):
            The state of the B button (RELEASE, PRESS or LONG_PRESS)

        Raises:
            errors.BluetoothServiceNotFound: When the button service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the button service is running but there was no way to
                read the state of button B (normally does not occur)
        """
        return ButtonState(self._device.read(Service.BUTTON, Characteristic.BUTTON_B)[0])

is_available #

is_available() -> bool

Kijkt na of de button bluetooth service gevonden wordt op de geconnecteerde micro:bit.

Returns:

  • bool

    true als de button service gevonden werd, false indien niet.

Source code in src/kaspersmicrobit/services/buttons.py
54
55
56
57
58
59
60
61
def is_available(self) -> bool:
    """
    Checks whether the Bluetooth service button is found on the connected micro:bit.

    Returns:
        true if the button service was found, false if not.
    """
    return self._device.is_service_available(Service.BUTTON)

on_button_a #

on_button_a(
    press: ButtonCallback = None,
    long_press: ButtonCallback = None,
    release: ButtonCallback = None,
)

Deze functie kan je oproepen wanneer je verwittigd wil worden wanneer de A knop van je micro:bit ingedrukt (press), lang ingedrukt (long_press) of losgelaten (release)

De functies die je kan meegeven als argument zijn functies met 1 string parameter. Deze functies zullen worden opgeroepen met de naam van de knop als argument.

Parameters:

  • press (ButtonCallback, default: None ) –

    een functie die wordt opgeroepen wanneer er op de A knop gedrukt wordt

  • long_press (ButtonCallback, default: None ) –

    een functie die wordt opgeroepen wanneer er lang (minstens 2 seconden) op de A knop gedrukt wordt

  • release (ButtonCallback, default: None ) –

    een functie die wordt opgeroepen wanneer de A knop wordt losgelaten

Raises:

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
def on_button_a(self, 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.

    Args:
        press (ButtonCallback): a function called when the A button is pressed
        long_press (ButtonCallback): a function that is called when pressed for a long time (at least 2 seconds).
            the A button is pressed
        release (ButtonCallback): a function called when the A button is released

    Raises:
        errors.BluetoothServiceNotFound: When the button service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the button service is running but there was no way to get the
            activate notifications for button A (normally does not occur)
    """
    self._device.notify(Service.BUTTON, Characteristic.BUTTON_A,
                        ButtonService._create_button_callback('A', press, long_press, release))

on_button_b #

on_button_b(
    press: ButtonCallback = None,
    long_press: ButtonCallback = None,
    release: ButtonCallback = None,
)

Deze functie kan je oproepen wanneer je verwittigd wil worden wanneer de B knop van je micro:bit ingedrukt (press), lang ingedrukt (long_press) of losgelaten (release)

De functies die je kan meegeven als argument zijn functies met 1 string parameter. Deze functies zullen worden opgeroepen met de naam van de knop als argument.

Parameters:

  • press (ButtonCallback, default: None ) –

    een functie die wordt opgeroepen wanneer er op de B knop gedrukt wordt

  • long_press (ButtonCallback, default: None ) –

    een functie die wordt opgeroepen wanneer er lang (minstens 2 seconden) op de B knop gedrukt wordt

  • release (ButtonCallback, default: None ) –

    een functie die wordt opgeroepen wanneer de B knop wordt losgelaten

Raises:

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
def on_button_b(self, 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.

    Args:
        press (ButtonCallback): a function called when the B button is pressed
        long_press (ButtonCallback): a function that is called when pressed for a long time (at least 2 seconds).
            the B button is pressed
        release (ButtonCallback): a function called when the B button is released

    Raises:
        errors.BluetoothServiceNotFound: When the button service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the button service is running but there was no way to get the
            activate notifications for button B (normally does not occur)
    """
    self._device.notify(Service.BUTTON, Characteristic.BUTTON_B,
                        ButtonService._create_button_callback('B', press, long_press, release))

read_button_a #

read_button_a() -> ButtonState

Geef de toestand van de A knop

Returns:

  • ButtonState

    De toestand van de A knop (RELEASE, PRESS of LONG_PRESS)

Raises:

Source code in src/kaspersmicrobit/services/buttons.py
109
110
111
112
113
114
115
116
117
118
119
120
121
def read_button_a(self) -> ButtonState:
    """
    Returns the state of the A button

    Returns:
        The state of the A button (RELEASE, PRESS or LONG_PRESS)

    Raises:
        errors.BluetoothServiceNotFound: When the button service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the button service is running but there was no way to
            read the state of button A (normally does not occur)
    """
    return ButtonState(self._device.read(Service.BUTTON, Characteristic.BUTTON_A)[0])

read_button_b #

read_button_b() -> ButtonState

Geef de toestand van de B knop

Returns (ButtonState): De toestand van de B knop (RELEASE, PRESS of LONG_PRESS)

Raises:

Source code in src/kaspersmicrobit/services/buttons.py
123
124
125
126
127
128
129
130
131
132
133
134
135
def read_button_b(self) -> ButtonState:
    """
    Returns the state of the B button

    Returns(ButtonState):
        The state of the B button (RELEASE, PRESS or LONG_PRESS)

    Raises:
        errors.BluetoothServiceNotFound: When the button service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the button service is running but there was no way to
            read the state of button B (normally does not occur)
    """
    return ButtonState(self._device.read(Service.BUTTON, Characteristic.BUTTON_B)[0])