2121
2222* Adafruit CircuitPython firmware for the supported boards:
2323 https://github.com/adafruit/circuitpython/releases
24-
25- * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Display_Text
2624"""
2725
2826import board
@@ -58,17 +56,18 @@ class SimpleTextDisplay:
5856 VIOLET = (255 , 0 , 255 )
5957 SKY = (0 , 180 , 255 )
6058
61- def __init__ ( # pylint: disable=too-many-arguments
59+ def __init__ (
6260 self ,
6361 title = None ,
6462 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 ,
6866 font = None ,
6967 colors = None ,
7068 display = None ,
7169 ):
70+ # pylint: disable=too-many-arguments, unused-argument
7271 """Display lines of text on a display using displayio. Lines of text are created in order as
7372 shown in the example below. If you skip a number, the line will be shown blank on the
7473 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
7877 must include the data call in the loop by using ``.text =``. For example, if setup is saved
7978 as ``temperature_data = simple_text_display()`` then ``temperature_data[0].text =
8079 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
8281 end of the list for anything to display. See example below for usage.
8382
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).
8887 :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.
9390 :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=(SimpleDisplayText.WHITE, SimpleDisplayText.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=(SimpleDisplayText.WHITE, SimpleDisplayText.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``.
111107
112108 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.
114110
115111 .. code-block:: python
116112
@@ -145,57 +141,55 @@ def __init__( # pylint: disable=too-many-arguments
145141 )
146142
147143 self ._colors = colors
148- self ._label = label
149144 if display is None :
150145 display = board .DISPLAY
151146 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
155149
156- self .text_group = displayio .Group (scale = text_scale )
150+ self .text_group = displayio .Group ()
157151
158152 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 (
167154 self ._font ,
168155 text = title ,
169156 color = title_color ,
170157 scale = title_scale ,
158+ anchor_point = (0 , 0 ),
159+ anchored_position = (0 , 0 ),
171160 )
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
175162
176- self .text_group .append (title )
163+ self .text_group .append (title_label )
177164 else :
178- self ._y = 3
165+ self ._next_y = 0
179166
180167 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 ]))
183170
184171 def __getitem__ (self , item ):
185172 """Fetch the Nth text line Group"""
186173 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 ):
188175 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 )])
190177 )
191178 return self ._lines [item ]
192179
193180 def add_text_line (self , color = (255 , 255 , 255 )):
194181 """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
199193 self .text_group .append (text_label )
200194
201195 return text_label
0 commit comments