display.py 5.3 KB
Newer Older
Gerd's avatar
Gerd committed
1
2
3
import sys_display
import color

4
5
6
7
8
9
10
# font enumeration
FONT8 = 0
FONT12 = 1
FONT16 = 2
FONT20 = 3
FONT24 = 4

11

Gerd's avatar
Gerd committed
12
13
14
15
16
17
class Display:
    """
    The display class provides methods to allow the lcd display
    in card10 to be used in a safe way. All draw methods return
    the display object so that it is possible to chain calls.
    It is recommended to use a context manager as following:
Rahix's avatar
Rahix committed
18

Gerd's avatar
Gerd committed
19
    .. code-block:: python
Rahix's avatar
Rahix committed
20

Gerd's avatar
Gerd committed
21
22
23
24
25
        import display
        with display.open() as disp:
            disp.clear().update()
    """

Rahix's avatar
Rahix committed
26
27
28
29
30
31
    def __init__(self):
        """
        Opens the display. Will fail the display can't be locked
        """
        sys_display.open()

Gerd's avatar
Gerd committed
32
33
34
35
36
37
38
39
40
41
42
43
44
    def __enter__(self):
        return self

    def __exit__(self, _et, _ev, _t):
        self.close()

    @classmethod
    def open(cls):
        """
        Opens the display. Will fail the display can't be locked
        """
        return cls()

Rahix's avatar
Rahix committed
45
46
    @staticmethod
    def close():
Gerd's avatar
Gerd committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
        """
        Closes and unlocks the display. To be able to use it again,
        it is necessary to open and lock it again with Display.open()
        """
        sys_display.close()

    def update(self):
        """
        Updates the display based on the changes previously made by
        various draw functions
        """
        sys_display.update()

    def clear(self, col=None):
        """
        Clears the display using the color provided, or the default
        color black

        :param col: Clearing color (expects RGB triple)
        """
        col = col or color.BLACK
        sys_display.clear(col)
        return self

71
    def print(self, text, *, fg=None, bg=None, posx=0, posy=0, font=FONT20):
Gerd's avatar
Gerd committed
72
        """
Gerd's avatar
Gerd committed
73
        Prints a string on the display.
Rahix's avatar
Rahix committed
74

Gerd's avatar
Gerd committed
75
76
        :param text: Text to print
        :param fg: Foreground color (expects RGB triple)
77
        :param bg: Background color (expects RGB triple) or None for transparent background
Gerd's avatar
Gerd committed
78
79
        :param posx: X-Position of the first character, 0 <= posx <= 159
        :param posy: Y-Position of the first character, 0 <= posy <= 79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
        :param font: 0 <= font <= 4 (currently) selects a font

        Avaiable Fonts:

         - :py:data:`display.FONT8`
         - :py:data:`display.FONT12`
         - :py:data:`display.FONT16`
         - :py:data:`display.FONT20`
         - :py:data:`display.FONT24`

         **Example:**

        .. code-block:: python

            with display.open() as d:
                d.clear()
                d.print('Hello Earth!', font=display.FONT24)
                d.update()
Rahix's avatar
Rahix committed
98
99
100
101

        .. versionchanged:: 1.11

            Added transparent background printing.
Gerd's avatar
Gerd committed
102
103
        """
        fg = fg or color.WHITE
104
        bg = bg or fg
Gerd's avatar
Gerd committed
105

106
        sys_display.print_adv(text, posx, posy, fg, bg, font)
Gerd's avatar
Gerd committed
107
108
        return self

109
110
111
112
    def pixel(self, x, y, *, col=None):
        """
        Draws a pixel on the display

Gerd's avatar
Gerd committed
113
114
        :param x: X coordinate, 0<= x <= 159
        :param y: Y coordinate, 0<= y <= 79
115
116
117
118
119
120
121
122
        :param col: color of the pixel (expects RGB tripple)
        """

        col = col or color.WHITE

        sys_display.pixel(x, y, col)
        return self

123
124
125
126
    def backlight(self, brightness):
        """
        Set display backlight brightness

127
        :param brightness: backlight brightness 0 <= brightness <= 100
128
129
130
131
132
        """

        sys_display.backlight(brightness)
        return self

Gerd's avatar
Gerd committed
133
134
135
136
    def line(self, xs, ys, xe, ye, *, col=None, dotted=False, size=1):
        """
        Draws a line on the display.

Gerd's avatar
Gerd committed
137
138
139
140
        :param xs: X start coordinate, 0 <= xs <= 159
        :param ys: Y start coordinate, 0 <= ys <= 79
        :param xe: X end coordinate, 0 <= xe <= 159
        :param ye: Y end coordinate, 0 <= ye <= 79
Gerd's avatar
Gerd committed
141
142
        :param col: color of the line (expects RGB triple)
        :param dotted: whether the line should be dotted or not
Rahix's avatar
Rahix committed
143
144
           (questionable implementation: draws every other pixel white, draws
           white squares at higher pixel sizes)
Gerd's avatar
Gerd committed
145
146
147
148
149
150
151
152
153
154
155
156
        :param size: size of the individual pixels, ranges from 1 to 8
        """

        col = col or color.WHITE

        sys_display.line(xs, ys, xe, ye, col, dotted, size)
        return self

    def rect(self, xs, ys, xe, ye, *, col=None, filled=True, size=1):
        """
        Draws a rectangle on the display.

Gerd's avatar
Gerd committed
157
158
159
160
        :param xs: X start coordinate, 0 <= xs <= 159
        :param ys: Y start coordinate, 0 <= ys <= 79
        :param xe: X end coordinate, 0 <= xe <= 159
        :param ye: Y end coordinate, 0 <= ye <= 79
Gerd's avatar
Gerd committed
161
162
163
164
165
166
167
168
169
170
171
172
173
        :param col: color of the outline and fill (expects RGB triple)
        :param filled: whether the rectangle should be filled or not
        :param size: size of the individual pixels, ranges from 1 to 8
        """

        col = col or color.WHITE

        sys_display.rect(xs, ys, xe, ye, col, filled, size)
        return self

    def circ(self, x, y, rad, *, col=None, filled=True, size=1):
        """
        Draws a circle on the display.
Rahix's avatar
Rahix committed
174

Gerd's avatar
Gerd committed
175
176
        :param x: center x coordinate, 0 <= x <= 159
        :param y: center y coordinate, 0 <= y <= 79
Gerd's avatar
Gerd committed
177
178
179
180
181
182
183
184
185
186
187
        :param rad: radius
        :param col: color of the outline and fill (expects RGB triple)
        :param filled: whether the rectangle should be filled or not
        :param size: size of the individual pixels, ranges from 1 to 8
        """

        col = col or color.WHITE

        sys_display.circ(x, y, rad, col, filled, size)
        return self

188

Gerd's avatar
Gerd committed
189
open = Display.open
Rahix's avatar
Rahix committed
190
close = Display.close