Ga naar inhoud

leddisplay

LedDisplay #

Een klasse die het LED scherm van de micro:bit voorstelt. De LED linksboven op de micro:bit is de LED op rij 1, kolom 1 De LED rechtsbeneden op de micro:bit is de LED op rij 5, kolom 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)

Maakt display met alle leds uit

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

Geeft aan of dat een LED op de gegeven rij en kolom aan of uit is

Parameters:

  • row (int) –

    de rij van de LED (geldige waarden zijn 1 tot en met 5)

  • column (int) –

    de kolom van de LED (geldige waarden zijn 1 tot en met 5)

Returns:

  • bool

    True wanneer de LED aan staat, False als de LED uit staat

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)

Zet een LED op de gegeven rij en kolom aan of uit

Parameters:

  • row (int) –

    de rij van de LED (geldige waarden zijn 1 tot en met 5)

  • column (int) –

    de kolom van de LED (geldige waarden zijn 1 tot en met 5)

  • on (bool, default: True ) –

    indien True word de LED aan gezet, indien False uit

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

Maakt een LedDisplay van een string.

Example:

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

Welke tekens gebruikt worden voor een LED die 'aan' of 'uit' is kan je zelf kiezen met de 'on' en 'of' parameters. De gegeven string moet exact 25 'on' en 'off' waarden bevatten, voor elke LED 1.

Parameters:

  • string (str) –

    de string die het LED scherm voorstelt

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

    de letter die een LED voorstelt die 'aan' is ('#' indien niet ingevuld)

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

    de letter die een LED voorstelt die 'uit' is ('.' indien niet ingevuld)

Returns:

  • LedDisplay

    Een LedDisplay die de gegeven string image voorstelt

Raises:

  • ValueError

    Indien de gegeven string niet exact 25 on/off waarden bevat

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