21
21
22
22
* Adafruit CircuitPython firmware for the supported boards:
23
23
https://github.com/adafruit/circuitpython/releases
24
-
25
- * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Display_Text
26
24
"""
27
25
28
26
import board
@@ -58,17 +56,18 @@ class SimpleTextDisplay:
58
56
VIOLET = (255 , 0 , 255 )
59
57
SKY = (0 , 180 , 255 )
60
58
61
- def __init__ ( # pylint: disable=too-many-arguments
59
+ def __init__ (
62
60
self ,
63
61
title = None ,
64
62
title_color = (255 , 255 , 255 ),
65
- title_scale = 1 ,
66
- title_length = 80 ,
67
- text_scale = 1 ,
63
+ title_scale : int = 1 ,
64
+ title_length : int = 0 , # Ignored - will be removed in a future version
65
+ text_scale : int = 1 ,
68
66
font = None ,
69
67
colors = None ,
70
68
display = None ,
71
69
):
70
+ # pylint: disable=too-many-arguments, unused-argument
72
71
"""Display lines of text on a display using displayio. Lines of text are created in order as
73
72
shown in the example below. If you skip a number, the line will be shown blank on the
74
73
display, e.g. if you include ``[0]`` and ``[2]``, the second line on the display will be
@@ -78,39 +77,36 @@ def __init__( # pylint: disable=too-many-arguments
78
77
must include the data call in the loop by using ``.text =``. For example, if setup is saved
79
78
as ``temperature_data = simple_text_display()`` then ``temperature_data[0].text =
80
79
microcontroller.cpu.temperature`` must be inside the ``while True:`` loop for the
81
- temperature data displayed to update as the values change. You must call `` show()` ` at the
80
+ temperature data displayed to update as the values change. You must call `show()` at the
82
81
end of the list for anything to display. See example below for usage.
83
82
84
- :param str title: The title displayed above the data. Set ``title="Title text"`` to provide
85
- a title. Defaults to None.
86
- :param title_color: The color of the title. Not necessary if no title is provided. Defaults
87
- to white (255, 255, 255).
83
+ :param None, str title: The title displayed above the data. Set ``title="Title text"`` to
84
+ provide a title. Defaults to ` None` .
85
+ :param None,Tuple(int,int,int) title_color: The color of the title. Not necessary if no
86
+ title is provided. Defaults to white (255, 255, 255).
88
87
:param int title_scale: Scale the size of the title. Not necessary if no title is provided.
89
- Defaults to 1.
90
- :param int title_length: The maximum number of characters allowed in the title. Only
91
- necessary if the title is longer than the default 80 characters.
92
- Defaults to 80.
88
+ Defaults to 1.
89
+ :param int title_length: DEPRECATED/IGNORED - This will be removed in a future version.
93
90
:param int text_scale: Scale the size of the data lines. Scales the title as well.
94
- Defaults to 1.
95
- :param str font: The font to use to display the title and data. Defaults to
96
- ``terminalio.FONT``.
97
- :param colors: A list of colors for the lines of data on the display. If you provide a
98
- single color, all lines will be that color. Otherwise it will cycle through
99
- the list you provide if the list is less than the number of lines displayed.
100
- Default colors are used if ``colors`` is not set. For example, if creating
101
- two lines of data, ``colors=((255, 255, 255), (255, 0, 0))`` would set the
102
- first line white and the second line red, and if you created four lines of
103
- data with the same setup, it would alternate white and red. You can also use
104
- the colors built into the library. For example, if you import the library
105
- as ``from adafruit_simple_text_display import SimpleTextDisplay``, you can
106
- indicate the colors as follows:
107
- ``colors=(SimpleTextDisplay.WHITE, SimpleTextDisplay.RED)``.
108
- :param display: The display object. Defaults to assuming a built-in display. To use with an
109
- external display, instantiate the display object and provide it here.
110
- Defaults to ``board.DISPLAY``.
91
+ Defaults to 1.
92
+ :param ~fontio.BuiltinFont,~adafruit_bitmap_font.bdf.BDF,~adafruit_bitmap_font.pcf.PCF font:
93
+ The font to use to display the title and data. Defaults to `terminalio.FONT`.
94
+ :param None,Tuple(Tuple(int,int,int),...) colors: A list of colors for the lines of data
95
+ on the display. If you provide a single color, all lines will be that color. Otherwise
96
+ it will cycle through the list you provide if the list is less than the number of lines
97
+ displayed. Default colors are used if ``colors`` is not set. For example, if creating
98
+ two lines of data, ``colors=((255, 255, 255), (255, 0, 0))`` would set the first line
99
+ white and the second line red, and if you created four lines of data with the same
100
+ setup, it would alternate white and red. You can also use the colors built into the
101
+ library. For example, if you import the library as
102
+ ``from adafruit_simple_text_display import SimpleTextDisplay``, you can indicate the
103
+ colors as follows: ``colors=(SimpleTextDisplay.WHITE, SimpleTextDisplay.RED)``.
104
+ :param None,~displayio.Display display: The display object. Defaults to assuming a built-in
105
+ display. To use with an external display, instantiate the display object and provide it
106
+ here. Defaults to ``board.DISPLAY``.
111
107
112
108
This example displays two lines with temperature data in C and F on the display.
113
- Remember to call `` show()` ` after the list to update the display.
109
+ Remember to call `show()` after the list to update the display.
114
110
115
111
.. code-block:: python
116
112
@@ -145,57 +141,55 @@ def __init__( # pylint: disable=too-many-arguments
145
141
)
146
142
147
143
self ._colors = colors
148
- self ._label = label
149
144
if display is None :
150
145
display = board .DISPLAY
151
146
self ._display = display
152
- self ._font = terminalio .FONT
153
- if font :
154
- self ._font = font
147
+ self ._font = font if font else terminalio .FONT
148
+ self ._text_scale = text_scale
155
149
156
- self .text_group = displayio .Group (scale = text_scale )
150
+ self .text_group = displayio .Group ()
157
151
158
152
if title :
159
- # Fail gracefully if title is longer than title_length characters. Defaults to 80.
160
- if len (title ) > title_length :
161
- raise ValueError (
162
- "Title character count must be less than or equal to title_length."
163
- " Default is 80."
164
- )
165
-
166
- title = label .Label (
153
+ title_label = label .Label (
167
154
self ._font ,
168
155
text = title ,
169
156
color = title_color ,
170
157
scale = title_scale ,
158
+ anchor_point = (0 , 0 ),
159
+ anchored_position = (0 , 0 ),
171
160
)
172
- title .x = 0
173
- title .y = 8
174
- self ._y = title .y + 18
161
+ self ._next_y = title_label .bounding_box [3 ] * title_scale
175
162
176
- self .text_group .append (title )
163
+ self .text_group .append (title_label )
177
164
else :
178
- self ._y = 3
165
+ self ._next_y = 0
179
166
180
167
self ._lines = []
181
- for num in range ( 1 ):
182
- self ._lines .append (self .add_text_line (color = colors [num % len ( colors ) ]))
168
+ # Add first line
169
+ self ._lines .append (self .add_text_line (color = colors [0 ]))
183
170
184
171
def __getitem__ (self , item ):
185
172
"""Fetch the Nth text line Group"""
186
173
if len (self ._lines ) - 1 < item :
187
- for _ in range (item - ( len (self ._lines ) - 1 ) ):
174
+ for i in range (len (self ._lines ), item + 1 ):
188
175
self ._lines .append (
189
- self .add_text_line (color = self ._colors [item % len (self ._colors )])
176
+ self .add_text_line (color = self ._colors [i % len (self ._colors )])
190
177
)
191
178
return self ._lines [item ]
192
179
193
180
def add_text_line (self , color = (255 , 255 , 255 )):
194
181
"""Adds a line on the display of the specified color and returns the label object."""
195
- text_label = self ._label .Label (self ._font , text = "" , color = color )
196
- text_label .x = 0
197
- text_label .y = self ._y
198
- self ._y = text_label .y + 13
182
+
183
+ text_label = label .Label (
184
+ self ._font ,
185
+ text = "Myj" , # Dummy value to allow bounding_box to calculate
186
+ color = color ,
187
+ scale = self ._text_scale ,
188
+ anchor_point = (0 , 0 ),
189
+ anchored_position = (0 , self ._next_y ),
190
+ )
191
+ self ._next_y += text_label .bounding_box [3 ] * text_label .scale
192
+ text_label .text = "" # Erase the dummy value after using bounding_box
199
193
self .text_group .append (text_label )
200
194
201
195
return text_label
0 commit comments