Ga naar inhoud

led

LedService #

Met de functies in deze klasse kan je leds aan of uit zetten, of een korte tekst laten scrollen op het scherm

Source code in src/kaspersmicrobit/services/led.py
 11
 12
 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
 97
 98
 99
100
101
class LedService:
    """
    Using the functions in this class you can turn LEDs on or off, or scroll a short text on the screen
    """
    def __init__(self, device: BluetoothDevice):
        self._device = device

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

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

    def show(self, led_display: LedDisplay):
        """
        Turn on the LEDs on the micro:bit as indicated in the LEDs parameter

        Args:
            led_display: the on/off state of the LEDs

        Raises:
            errors.BluetoothServiceNotFound: When the LED service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the led service is active but there was no way
                to write the LED display (normally does not occur)
        """
        self._device.write(Service.LED, Characteristic.LED_MATRIX_STATE, led_display.to_bytes())

    def read(self) -> LedDisplay:
        """
        Read the on/off values from the micro:bit LED display

        Returns:
            the on/off state of the LEDs

        Raises:
            errors.BluetoothServiceNotFound: When the LED service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the led service is active but there was no way
                to read the LED display (normally not present)
        """
        return LedDisplay.from_bytes(self._device.read(Service.LED, Characteristic.LED_MATRIX_STATE))

    def show_text(self, text: str):
        """
        Let the given text scroll by on the LED screen of the micro:bit. You can control the speed of scrolling
        through the scrolling delay methods.

        Args:
            text: The text to be displayed (maximum 20 characters)

        Raises:
            ValueError: if text contains more than 20 characters
            errors.BluetoothServiceNotFound: When the LED service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the led service is active but there was no way
                to write the LED text (normally does not occur)
        """
        octets = text.encode("utf-8")
        if len(octets) > 20:
            raise ValueError('Text too long, maximum 20 characters allowed')

        self._device.write(Service.LED, Characteristic.LED_TEXT, octets)

    def set_scrolling_delay(self, delay_in_millis: int):
        """
        Adjust how quickly text scrolls by on the LED screen.

        Args:
            delay_in_millis: the time it takes for 1 letter to pass across the screen in milliseconds

        Raises:
            errors.BluetoothServiceNotFound: When the LED service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the led service is active but there was no way
                to write the scrolling delay (normally does not occur)
        """
        self._device.write(Service.LED, Characteristic.SCROLLING_DELAY, delay_in_millis.to_bytes(2, 'little'))

    def get_scrolling_delay(self) -> int:
        """
        Return how quickly a text scrolls past the screen

        Returns:
             the time it takes for 1 letter to pass across the screen in milliseconds

        Raises:
            errors.BluetoothServiceNotFound: When the LED service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the led service is active but there was no way
                to read the scrolling delay (normally does not occur)
        """
        return int.from_bytes(self._device.read(Service.LED, Characteristic.SCROLLING_DELAY)[0:2], 'little')

is_available #

is_available() -> bool

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

Returns:

  • bool

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

Source code in src/kaspersmicrobit/services/led.py
18
19
20
21
22
23
24
25
def is_available(self) -> bool:
    """
    Checks whether the LED Bluetooth service is found on the connected micro:bit.

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

show #

show(led_display: LedDisplay)

Zet de leds op de micro:bit aan zoals de leds parameter aangeeft

Parameters:

  • led_display (LedDisplay) –

    de aan/uit staat van de leds

Raises:

Source code in src/kaspersmicrobit/services/led.py
27
28
29
30
31
32
33
34
35
36
37
38
39
def show(self, led_display: LedDisplay):
    """
    Turn on the LEDs on the micro:bit as indicated in the LEDs parameter

    Args:
        led_display: the on/off state of the LEDs

    Raises:
        errors.BluetoothServiceNotFound: When the LED service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the led service is active but there was no way
            to write the LED display (normally does not occur)
    """
    self._device.write(Service.LED, Characteristic.LED_MATRIX_STATE, led_display.to_bytes())

read #

read() -> LedDisplay

Lees de aan/uit waarden van de micro:bit led display

Returns:

Raises:

Source code in src/kaspersmicrobit/services/led.py
41
42
43
44
45
46
47
48
49
50
51
52
53
def read(self) -> LedDisplay:
    """
    Read the on/off values from the micro:bit LED display

    Returns:
        the on/off state of the LEDs

    Raises:
        errors.BluetoothServiceNotFound: When the LED service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the led service is active but there was no way
            to read the LED display (normally not present)
    """
    return LedDisplay.from_bytes(self._device.read(Service.LED, Characteristic.LED_MATRIX_STATE))

show_text #

show_text(text: str)

Laat de gegeven tekst voorbij scrollen op het led scherm van de micro:bit. De snelheid van het scrollen kan je instellen via de scrolling delay.

Parameters:

  • text (str) –

    De te tonen tekst (maximum 20 tekens)

Raises:

  • ValueError

    indien text meer dan 20 tekens bevat

  • BluetoothServiceNotFound

    Wanneer de led service niet actief is op de micro:bit

  • BluetoothCharacteristicNotFound

    Wanneer de led service actief is, maar er geen manier was om de led text te schrijven (komt normaal gezien niet voor)

Source code in src/kaspersmicrobit/services/led.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def show_text(self, text: str):
    """
    Let the given text scroll by on the LED screen of the micro:bit. You can control the speed of scrolling
    through the scrolling delay methods.

    Args:
        text: The text to be displayed (maximum 20 characters)

    Raises:
        ValueError: if text contains more than 20 characters
        errors.BluetoothServiceNotFound: When the LED service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the led service is active but there was no way
            to write the LED text (normally does not occur)
    """
    octets = text.encode("utf-8")
    if len(octets) > 20:
        raise ValueError('Text too long, maximum 20 characters allowed')

    self._device.write(Service.LED, Characteristic.LED_TEXT, octets)

set_scrolling_delay #

set_scrolling_delay(delay_in_millis: int)

Stel in hoe snel een tekst voorbijrolt op het led scherm.

Parameters:

  • delay_in_millis (int) –

    de tijd die 1 letter er over doet om over het scherm voorbij te komen in milliseconden

Raises:

Source code in src/kaspersmicrobit/services/led.py
75
76
77
78
79
80
81
82
83
84
85
86
87
def set_scrolling_delay(self, delay_in_millis: int):
    """
    Adjust how quickly text scrolls by on the LED screen.

    Args:
        delay_in_millis: the time it takes for 1 letter to pass across the screen in milliseconds

    Raises:
        errors.BluetoothServiceNotFound: When the LED service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the led service is active but there was no way
            to write the scrolling delay (normally does not occur)
    """
    self._device.write(Service.LED, Characteristic.SCROLLING_DELAY, delay_in_millis.to_bytes(2, 'little'))

get_scrolling_delay #

get_scrolling_delay() -> int

Lees de hoe snel een tekst voorbijscrolt over het scherm

Returns:

  • int

    de tijd die 1 letter er over doet om over het scherm voorbij te komen in milliseconden

Raises:

Source code in src/kaspersmicrobit/services/led.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def get_scrolling_delay(self) -> int:
    """
    Return how quickly a text scrolls past the screen

    Returns:
         the time it takes for 1 letter to pass across the screen in milliseconds

    Raises:
        errors.BluetoothServiceNotFound: When the LED service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the led service is active but there was no way
            to read the scrolling delay (normally does not occur)
    """
    return int.from_bytes(self._device.read(Service.LED, Characteristic.SCROLLING_DELAY)[0:2], 'little')