Skip to content

leddisplay

LedDisplay #

A class representing the LED screen of the micro:bit. The LED on the top left of the micro:bit is the LED on row 1, column 1 The bottom right LED on the micro:bit is the LED in row 5, column 5

Source code in src/kaspersmicrobit/services/leddisplay.py
 10
 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
102
103
104
105
106
107
108
109
110
class LedDisplay:
    """
    A class representing the LED screen of the micro:bit.
    The LED on the top left of the micro:bit is the LED on row 1, column 1
    The bottom right LED on the micro:bit is the LED in row 5, column 5
    """
    def __init__(self, _bytes: bytearray = None):
        """
        Create a display with all LEDs off
        """
        self._display = _bytes if _bytes else bytearray(5)

    def led(self, row: int, column: int) -> bool:
        """
        Indicates whether an LED on the given row and column is on or off

        Args:
            row int: the row of the LED (valid values are 1 to 5)
            column int: the column of the LED (valid values are 1 to 5)

        Returns:
            True when the LED is on, False when the LED is off
        """
        return ((self._display[row-1] >> (5 - column)) & 1) == 1

    def set_led(self, row: int, column: int, on: bool = True):
        """
        Turn an LED on or off on the given row and column

        Args:
            row int: the row of the LED (valid values are 1 to 5)
            column int: the column of the LED (valid values are 1 to 5)
            on bool: if True the LED is turned on, if False it is turned off
        """
        if on:
            self._display[row-1] |= 1 << (5 - column)
        else:
            self._display[row-1] &= ~(1 << (5 - column))

    def to_bytes(self) -> bytearray:
        return self._display

    @staticmethod
    def from_bytes(value: ByteData):
        return LedDisplay(bytearray(value))

    @staticmethod
    def image(string: str, on: str = '#', off: str = '.') -> 'LedDisplay':
        """
        Creates an LedDisplay given a string.

        Example:

        ```python
        HEART: LedDisplay = LedDisplay.image('''
            . # . # .
            # # # # #
            # # # # #
            . # # # .
            . . # . .
        ''')
        ```

        You can choose which characters are used for an LED that is 'on' or 'off' with the 'on' and 'off'
        parameters. The given string must contain exactly 25 'on' and 'off' values, 1 for each LED.

        Args:
            string: the string representing the LED screen
            on: the letter representing an LED that is 'on' ('#' if left blank)
            off: the letter representing an LED that is 'off' ('.' if left blank)

        Returns:
            An LedDisplay representing the given string image

        Raises:
            ValueError: If the given string does not contain exactly 25 on/off values
        """
        image: List[bool] = []
        for s in string:
            if s == on:
                image.append(True)
            elif s == off:
                image.append(False)

        if len(image) != 25:
            raise ValueError("Image should contain 25 LEDs")

        leds = LedDisplay()
        for row in range(1, 6):
            for column in range(1, 6):
                leds.set_led(row, column, image[(row-1)*5+column-1])

        return leds

    def __str__(self):
        string = "\n"
        for row in range(1, 6):
            for column in range(1, 6):
                string += ' #' if self.led(row, column) else ' .'
            string += '\n'
        return string

__init__ #

__init__(_bytes: bytearray = None)

Create a display with all LEDs off

Source code in src/kaspersmicrobit/services/leddisplay.py
16
17
18
19
20
def __init__(self, _bytes: bytearray = None):
    """
    Create a display with all LEDs off
    """
    self._display = _bytes if _bytes else bytearray(5)

led #

led(row: int, column: int) -> bool

Indicates whether an LED on the given row and column is on or off

Parameters:

  • row (int) –

    the row of the LED (valid values are 1 to 5)

  • column (int) –

    the column of the LED (valid values are 1 to 5)

Returns:

  • bool

    True when the LED is on, False when the LED is off

Source code in src/kaspersmicrobit/services/leddisplay.py
22
23
24
25
26
27
28
29
30
31
32
33
def led(self, row: int, column: int) -> bool:
    """
    Indicates whether an LED on the given row and column is on or off

    Args:
        row int: the row of the LED (valid values are 1 to 5)
        column int: the column of the LED (valid values are 1 to 5)

    Returns:
        True when the LED is on, False when the LED is off
    """
    return ((self._display[row-1] >> (5 - column)) & 1) == 1

set_led #

set_led(row: int, column: int, on: bool = True)

Turn an LED on or off on the given row and column

Parameters:

  • row (int) –

    the row of the LED (valid values are 1 to 5)

  • column (int) –

    the column of the LED (valid values are 1 to 5)

  • on (bool, default: True ) –

    if True the LED is turned on, if False it is turned off

Source code in src/kaspersmicrobit/services/leddisplay.py
35
36
37
38
39
40
41
42
43
44
45
46
47
def set_led(self, row: int, column: int, on: bool = True):
    """
    Turn an LED on or off on the given row and column

    Args:
        row int: the row of the LED (valid values are 1 to 5)
        column int: the column of the LED (valid values are 1 to 5)
        on bool: if True the LED is turned on, if False it is turned off
    """
    if on:
        self._display[row-1] |= 1 << (5 - column)
    else:
        self._display[row-1] &= ~(1 << (5 - column))

image staticmethod #

image(
    string: str, on: str = "#", off: str = "."
) -> LedDisplay

Creates an LedDisplay given a string.

Example:

HEART: LedDisplay = LedDisplay.image('''
    . # . # .
    # # # # #
    # # # # #
    . # # # .
    . . # . .
''')

You can choose which characters are used for an LED that is 'on' or 'off' with the 'on' and 'off' parameters. The given string must contain exactly 25 'on' and 'off' values, 1 for each LED.

Parameters:

  • string (str) –

    the string representing the LED screen

  • on (str, default: '#' ) –

    the letter representing an LED that is 'on' ('#' if left blank)

  • off (str, default: '.' ) –

    the letter representing an LED that is 'off' ('.' if left blank)

Returns:

  • LedDisplay

    An LedDisplay representing the given string image

Raises:

  • ValueError

    If the given string does not contain exactly 25 on/off values

Source code in src/kaspersmicrobit/services/leddisplay.py
 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
@staticmethod
def image(string: str, on: str = '#', off: str = '.') -> 'LedDisplay':
    """
    Creates an LedDisplay given a string.

    Example:

    ```python
    HEART: LedDisplay = LedDisplay.image('''
        . # . # .
        # # # # #
        # # # # #
        . # # # .
        . . # . .
    ''')
    ```

    You can choose which characters are used for an LED that is 'on' or 'off' with the 'on' and 'off'
    parameters. The given string must contain exactly 25 'on' and 'off' values, 1 for each LED.

    Args:
        string: the string representing the LED screen
        on: the letter representing an LED that is 'on' ('#' if left blank)
        off: the letter representing an LED that is 'off' ('.' if left blank)

    Returns:
        An LedDisplay representing the given string image

    Raises:
        ValueError: If the given string does not contain exactly 25 on/off values
    """
    image: List[bool] = []
    for s in string:
        if s == on:
            image.append(True)
        elif s == off:
            image.append(False)

    if len(image) != 25:
        raise ValueError("Image should contain 25 LEDs")

    leds = LedDisplay()
    for row in range(1, 6):
        for column in range(1, 6):
            leds.set_led(row, column, image[(row-1)*5+column-1])

    return leds