Skip to content

display

Display utilities.

Classes:

Name Description
Display

Display class.

Functions:

Name Description
test_display

Test for the display class.

Display

Display(i2c: I2C)

Display class.

Parameters:

Name Type Description Default
i2c I2C

A machine.I2C instance.

required

Methods:

Name Description
clear_display

Clear the display.

draw_progressbar

Draw a progressbar.

filled_rect

Draw a filled rectangle on the display.

print_line

Print a line of text on the display.

rect

Draw a rectangle frame on the display.

Source code in src/aalec/display.py
15
16
17
18
19
def __init__(self, i2c: machine.I2C):
    self._display = sh1106.SH1106_I2C(
        constants.DISPLAY_WIDTH, constants.DISPLAY_HEIGHT, i2c
    )
    self._display.rotate()

clear_display

clear_display() -> None

Clear the display.

Source code in src/aalec/display.py
42
43
44
45
def clear_display(self) -> None:
    """Clear the display."""
    self._display.fill(constants.BLACK)
    self._display.show()

draw_progressbar

draw_progressbar(
    x: int, y: int, width: int, height: int, percent: int
) -> None

Draw a progressbar.

Parameters:

Name Type Description Default
x int

X coordinate of the upper left corner of the progressbar

required
y int

Y coordinate of the upper left corner of the progressbar

required
width int

Width of the progressbar in pixel. (x delta to the lower right corner.)

required
height int

Height of the progressbar in pixel. (y delta to the lower right corner.)

required
percent int

How many percent the bar is filled (grows to the right).

required
Source code in src/aalec/display.py
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
def draw_progressbar(
    self, x: int, y: int, width: int, height: int, percent: int
) -> None:
    """Draw a progressbar.

    Args:
        x (int): X coordinate of the upper left corner of the progressbar
        y (int): Y coordinate of the upper left corner of the progressbar
        width (int): Width of the progressbar in pixel. (x delta to the lower right corner.)
        height (int): Height of the progressbar in pixel. (y delta to the lower right corner.)
        percent (int): How many percent the bar is filled (grows to the right).
    """
    # Blank the space for the progressbar.
    self._display.fill_rect(x, y, width, height, constants.BLACK)
    # Draw the frame
    if height >= 3:
        self._display.rect(x, y, width, height, constants.WHITE)
        inner_width = int((width - 2) * percent / 100)
        self._display.fill_rect(
            x + 1, y + 1, inner_width, height - 2, constants.WHITE
        )
    else:
        inner_width = int(width * percent / 100)
        self._display.fill_rect(x, y, inner_width, height, constants.WHITE)
    self._display.show()

filled_rect

filled_rect(
    x: int, y: int, width: int, height: int, color: int
) -> None

Draw a filled rectangle on the display.

Parameters:

Name Type Description Default
x int

X coordinate of the upper left corner of the progressbar

required
y int

Y coordinate of the upper left corner of the progressbar

required
width int

Width of the progressbar in pixel. (x delta to the lower right corner.)

required
height int

Height of the progressbar in pixel. (y delta to the lower right corner.)

required
color int

Fill color (constants.WHITE or constants.BLACK)

required
Source code in src/aalec/display.py
60
61
62
63
64
65
66
67
68
69
70
71
def filled_rect(self, x: int, y: int, width: int, height: int, color: int) -> None:
    """Draw a filled rectangle on the display.

    Args:
        x (int): X coordinate of the upper left corner of the progressbar
        y (int): Y coordinate of the upper left corner of the progressbar
        width (int): Width of the progressbar in pixel. (x delta to the lower right corner.)
        height (int): Height of the progressbar in pixel. (y delta to the lower right corner.)
        color (int): Fill color (`constants.WHITE` or `constants.BLACK`)
    """
    self._display.fill_rect(x, y, width, height, color)
    self._display.show()

print_line

print_line(line: int, text: str) -> None

Print a line of text on the display.

A line can be at most 16 characters long. (A character has a size of 8x8 pixels.)

Parameters:

Name Type Description Default
line int

Line number. Valid values are from 1 to 5.

required
text str

The content to display.

required
Source code in src/aalec/display.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def print_line(self, line: int, text: str) -> None:
    """Print a line of text on the display.

    A line can be at most 16 characters long. (A character has a size of 8x8 pixels.)

    Args:
        line (int): Line number. Valid values are from 1 to 5.
        text (str): The content to display.
    """
    y = ((line - 1) % constants.MAX_LINE_COUNT) * constants.LINE_HEIGHT
    self._display.fill_rect(
        0,
        y,
        constants.DISPLAY_WIDTH,
        constants.LINE_HEIGHT,
        constants.BLACK,
    )
    self._display.text(text, 0, y)
    self._display.show()
    pass

rect

rect(
    x: int, y: int, width: int, height: int, color: int
) -> None

Draw a rectangle frame on the display.

Parameters:

Name Type Description Default
x int

X coordinate of the upper left corner of the progressbar

required
y int

Y coordinate of the upper left corner of the progressbar

required
width int

Width of the progressbar in pixel. (x delta to the lower right corner.)

required
height int

Height of the progressbar in pixel. (y delta to the lower right corner.)

required
color int

Frame color (constants.WHITE or constants.BLACK)

required
Source code in src/aalec/display.py
47
48
49
50
51
52
53
54
55
56
57
58
def rect(self, x: int, y: int, width: int, height: int, color: int) -> None:
    """Draw a rectangle frame on the display.

    Args:
        x (int): X coordinate of the upper left corner of the progressbar
        y (int): Y coordinate of the upper left corner of the progressbar
        width (int): Width of the progressbar in pixel. (x delta to the lower right corner.)
        height (int): Height of the progressbar in pixel. (y delta to the lower right corner.)
        color (int): Frame color (`constants.WHITE` or `constants.BLACK`)
    """
    self._display.rect(x, y, width, height, color)
    self._display.show()

test_display

test_display() -> None

Test for the display class.

Source code in src/aalec/display.py
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
def test_display() -> None:
    """Test for the display class."""
    from time import sleep

    i2c = machine.I2C(
        sda=machine.Pin(constants.PIN_SDA), scl=machine.Pin(constants.PIN_SCL)
    )
    display = Display(i2c)

    screens = [
        ["Once Once upon a", "midnight dreary,", "while I", "pondered, weak"],
        ["and weary, Over", "many a quaint", "and curious", "volume of"],
        ["forgotten lore-", "While I nodded,", "nearly napping,", "suddenly there"],
        ["came a tapping,", "As of some one", "gently rapping,", "rapping at my"],
        ["chamber door.", '"Tis some', 'visitor," I', "muttered,"],
        ['"tapping at my', "chamber door-", "Only this and", "nothing more."],
    ]
    screen_count = len(screens)

    for nr, screen in enumerate(screens):
        display.filled_rect(0, 0, 128, 59, constants.BLACK)
        for lnr, line in enumerate(screen):
            display.print_line(lnr + 1, line)
            display.draw_progressbar(
                0, 60, 100, 3, int((4 * nr + lnr + 1) * 100.0 / (4 * screen_count))
            )
            sleep(1)