Ga naar inhoud

uart

UartService #

Deze klasse bevat methodes om bytes of strings naar de micro:bit te verzenden of te ontvangen

Zie ook: https://lancaster-university.github.io/microbit-docs/ble/uart-service/

Source code in src/kaspersmicrobit/services/uart.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
class UartService:
    """
    This class contains methods to send or receive bytes or strings to the micro:bit

    See Also: https://lancaster-university.github.io/microbit-docs/ble/uart-service/
    """
    def __init__(self, device: BluetoothDevice):
        self._device = device

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

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

    def receive(self, callback: Callable[[ByteData], None]):
        """
        You can call this method if you want to be notified when bytes are sent from the micro:bit
        via the uart service

        Args:
            callback (Callable[[ByteData], None]): a function that will be called with the received bytes

        Raises:
            errors.BluetoothServiceNotFound: When the uart service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the uart service is running but there was no way
                to activate the notifications of uart data (normally does not occur)
        """
        self._device.notify(Service.UART, Characteristic.TX_CHARACTERISTIC, lambda sender, data: callback(data))

    def receive_string(self, callback: Callable[[str], None]):
        """
        You can call this method if you want to be notified when a string is sent from the micro:bit
        via the uart service

        Args:
            callback (Callable[[str], None]): a function that will be called with the received string

        Raises:
            errors.BluetoothServiceNotFound: When the uart service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the uart service is running but there was no way
                to activate the notifications of uart data (normally does not occur)
        """
        self.receive(UartService.to_string(callback))

    def send(self, data: ByteData):
        """
        Send bytes via the uart service to the micro:bit

        Args:
            data (ByteData): the bytes that are sent

        Raises:
            errors.BluetoothServiceNotFound: When the uart service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the uart service is running but there was no way
                to send data via the UART service (normally does not occur)
        """
        for i in range(0, len(data), PDU_BYTE_LIMIT):
            self._device.write(Service.UART, Characteristic.RX_CHARACTERISTIC, data[i:i + PDU_BYTE_LIMIT])

    def send_string(self, string: str):
        """
        Send a string via the uart service to the micro:bit

        Args:
            string (str): the string to be sent

        Raises:
            errors.BluetoothServiceNotFound: When the uart service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the uart service is running but there was no way
                to send data via the UART service (normally does not occur)
        """
        self.send(UartService.from_string(string))

    @staticmethod
    def from_string(string: str) -> bytes:
        return string.encode("utf-8")

    @staticmethod
    def to_string(callback):
        return lambda data: callback(str(data, "utf-8"))

is_available #

is_available() -> bool

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

Returns:

  • bool

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

Source code in src/kaspersmicrobit/services/uart.py
22
23
24
25
26
27
28
29
def is_available(self) -> bool:
    """
    Checks whether the UART Bluetooth service is found on the connected micro:bit.

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

receive #

receive(callback: Callable[[ByteData], None])

Deze methode kan je oproepen wanneer je verwittigd wil wanneer er bytes worden verstuurd vanuit de micro:bit via de uart service

Parameters:

  • callback (Callable[[ByteData], None]) –

    een functie wordt opgeroepen met de ontvangen bytes

Raises:

Source code in src/kaspersmicrobit/services/uart.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def receive(self, callback: Callable[[ByteData], None]):
    """
    You can call this method if you want to be notified when bytes are sent from the micro:bit
    via the uart service

    Args:
        callback (Callable[[ByteData], None]): a function that will be called with the received bytes

    Raises:
        errors.BluetoothServiceNotFound: When the uart service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the uart service is running but there was no way
            to activate the notifications of uart data (normally does not occur)
    """
    self._device.notify(Service.UART, Characteristic.TX_CHARACTERISTIC, lambda sender, data: callback(data))

receive_string #

receive_string(callback: Callable[[str], None])

Deze methode kan je oproepen wanneer je verwittigd wil wanneer er een string wordt verstuurd vanuit de micro:bit via de uart service

Parameters:

  • callback (Callable[[str], None]) –

    een functie wordt opgeroepen met de ontvangen string

Raises:

Source code in src/kaspersmicrobit/services/uart.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def receive_string(self, callback: Callable[[str], None]):
    """
    You can call this method if you want to be notified when a string is sent from the micro:bit
    via the uart service

    Args:
        callback (Callable[[str], None]): a function that will be called with the received string

    Raises:
        errors.BluetoothServiceNotFound: When the uart service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the uart service is running but there was no way
            to activate the notifications of uart data (normally does not occur)
    """
    self.receive(UartService.to_string(callback))

send #

send(data: ByteData)

Verzend bytes via de uart service naar de micro:bit

Parameters:

  • data (ByteData) –

    de bytes die verzonden worden

Raises:

Source code in src/kaspersmicrobit/services/uart.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def send(self, data: ByteData):
    """
    Send bytes via the uart service to the micro:bit

    Args:
        data (ByteData): the bytes that are sent

    Raises:
        errors.BluetoothServiceNotFound: When the uart service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the uart service is running but there was no way
            to send data via the UART service (normally does not occur)
    """
    for i in range(0, len(data), PDU_BYTE_LIMIT):
        self._device.write(Service.UART, Characteristic.RX_CHARACTERISTIC, data[i:i + PDU_BYTE_LIMIT])

send_string #

send_string(string: str)

Verzend een string via de uart service naar de micro:bit

Parameters:

  • string (str) –

    de string die verzonden wordt

Raises:

Source code in src/kaspersmicrobit/services/uart.py
76
77
78
79
80
81
82
83
84
85
86
87
88
def send_string(self, string: str):
    """
    Send a string via the uart service to the micro:bit

    Args:
        string (str): the string to be sent

    Raises:
        errors.BluetoothServiceNotFound: When the uart service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the uart service is running but there was no way
            to send data via the UART service (normally does not occur)
    """
    self.send(UartService.from_string(string))