From a48e24eb994c15b9ef3eb462365d866bbcfdf2dd Mon Sep 17 00:00:00 2001 From: James Carr Date: Fri, 23 Jul 2021 00:04:34 +0100 Subject: [PATCH 1/2] Updates to documentation and parameter types. --- adafruit_simple_text_display.py | 62 +++++++++++++++------------------ docs/conf.py | 4 +++ 2 files changed, 32 insertions(+), 34 deletions(-) diff --git a/adafruit_simple_text_display.py b/adafruit_simple_text_display.py index 202b2a5..f21b605 100644 --- a/adafruit_simple_text_display.py +++ b/adafruit_simple_text_display.py @@ -21,8 +21,6 @@ * Adafruit CircuitPython firmware for the supported boards: https://github.com/adafruit/circuitpython/releases - - * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Display_Text """ import board @@ -62,9 +60,9 @@ def __init__( # pylint: disable=too-many-arguments self, title=None, title_color=(255, 255, 255), - title_scale=1, - title_length=80, - text_scale=1, + title_scale: int = 1, + title_length: int = 80, + text_scale: int = 1, font=None, colors=None, display=None, @@ -78,39 +76,37 @@ def __init__( # pylint: disable=too-many-arguments must include the data call in the loop by using ``.text =``. For example, if setup is saved as ``temperature_data = simple_text_display()`` then ``temperature_data[0].text = microcontroller.cpu.temperature`` must be inside the ``while True:`` loop for the - temperature data displayed to update as the values change. You must call ``show()`` at the + temperature data displayed to update as the values change. You must call `show()` at the end of the list for anything to display. See example below for usage. - :param str title: The title displayed above the data. Set ``title="Title text"`` to provide - a title. Defaults to None. - :param title_color: The color of the title. Not necessary if no title is provided. Defaults - to white (255, 255, 255). + :param None,str title: The title displayed above the data. Set ``title="Title text"`` to + provide a title. Defaults to `None`. + :param None,Tuple(int,int,int) title_color: The color of the title. Not necessary if no + title is provided. Defaults to white (255, 255, 255). :param int title_scale: Scale the size of the title. Not necessary if no title is provided. - Defaults to 1. + Defaults to 1. :param int title_length: The maximum number of characters allowed in the title. Only - necessary if the title is longer than the default 80 characters. - Defaults to 80. + necessary if the title is longer than the default 80 characters. Defaults to 80. :param int text_scale: Scale the size of the data lines. Scales the title as well. - Defaults to 1. - :param str font: The font to use to display the title and data. Defaults to - ``terminalio.FONT``. - :param colors: A list of colors for the lines of data on the display. If you provide a - single color, all lines will be that color. Otherwise it will cycle through - the list you provide if the list is less than the number of lines displayed. - Default colors are used if ``colors`` is not set. For example, if creating - two lines of data, ``colors=((255, 255, 255), (255, 0, 0))`` would set the - first line white and the second line red, and if you created four lines of - data with the same setup, it would alternate white and red. You can also use - the colors built into the library. For example, if you import the library - as ``from adafruit_simple_text_display import SimpleTextDisplay``, you can - indicate the colors as follows: - ``colors=(SimpleDisplayText.WHITE, SimpleDisplayText.RED)``. - :param display: The display object. Defaults to assuming a built-in display. To use with an - external display, instantiate the display object and provide it here. - Defaults to ``board.DISPLAY``. + Defaults to 1. + :param ~fontio.BuiltinFont,~adafruit_bitmap_font.bdf.BDF,~adafruit_bitmap_font.pcf.PCF font: + The font to use to display the title and data. Defaults to `terminalio.FONT`. + :param None,Tuple(Tuple(int,int,int),...) colors: A list of colors for the lines of data + on the display. If you provide a single color, all lines will be that color. Otherwise + it will cycle through the list you provide if the list is less than the number of lines + displayed. Default colors are used if ``colors`` is not set. For example, if creating + two lines of data, ``colors=((255, 255, 255), (255, 0, 0))`` would set the first line + white and the second line red, and if you created four lines of data with the same + setup, it would alternate white and red. You can also use the colors built into the + library. For example, if you import the library as + ``from adafruit_simple_text_display import SimpleTextDisplay``, you can indicate the + colors as follows: ``colors=(SimpleDisplayText.WHITE, SimpleDisplayText.RED)``. + :param None,~displayio.Display display: The display object. Defaults to assuming a built-in + display. To use with an external display, instantiate the display object and provide it + here. Defaults to ``board.DISPLAY``. This example displays two lines with temperature data in C and F on the display. - Remember to call ``show()`` after the list to update the display. + Remember to call `show()` after the list to update the display. .. code-block:: python @@ -149,9 +145,7 @@ def __init__( # pylint: disable=too-many-arguments if display is None: display = board.DISPLAY self._display = display - self._font = terminalio.FONT - if font: - self._font = font + self._font = font if font else terminalio.FONT self.text_group = displayio.Group(scale=text_scale) diff --git a/docs/conf.py b/docs/conf.py index 7601ef1..bef1abc 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -31,6 +31,10 @@ intersphinx_mapping = { "python": ("https://docs.python.org/3.4", None), "CircuitPython": ("https://circuitpython.readthedocs.io/en/latest/", None), + "Adafruit_CircuitPython_Bitmap_Font": ( + "https://circuitpython.readthedocs.io/projects/bitmap-font/en/latest/", + None, + ), } # Show the docstring from both the class and its __init__() method. From 5113e337c6c365383ba165cbf9d4eb1bcb0f4348 Mon Sep 17 00:00:00 2001 From: James Carr Date: Fri, 23 Jul 2021 18:53:46 +0100 Subject: [PATCH 2/2] Add dynamic row heights based on font used. (Closes #3) Deprecate 'title_length', ignore it for now. It is no longer needed with the update to Adafruit_CircuitPython_Display_Text removing 'max_glyphs'. Refactor the scaling so that the 'text_scale' is not a multiple of the 'title_scale'. Correctly add multiple rows with the color-cycling effect. Previously all new rows had the same color. --- adafruit_simple_text_display.py | 55 ++++++++++++++++----------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/adafruit_simple_text_display.py b/adafruit_simple_text_display.py index f21b605..e5f07d8 100644 --- a/adafruit_simple_text_display.py +++ b/adafruit_simple_text_display.py @@ -56,17 +56,18 @@ class SimpleTextDisplay: VIOLET = (255, 0, 255) SKY = (0, 180, 255) - def __init__( # pylint: disable=too-many-arguments + def __init__( self, title=None, title_color=(255, 255, 255), title_scale: int = 1, - title_length: int = 80, + title_length: int = 0, # Ignored - will be removed in a future version text_scale: int = 1, font=None, colors=None, display=None, ): + # pylint: disable=too-many-arguments, unused-argument """Display lines of text on a display using displayio. Lines of text are created in order as shown in the example below. If you skip a number, the line will be shown blank on the display, e.g. if you include ``[0]`` and ``[2]``, the second line on the display will be @@ -85,8 +86,7 @@ def __init__( # pylint: disable=too-many-arguments title is provided. Defaults to white (255, 255, 255). :param int title_scale: Scale the size of the title. Not necessary if no title is provided. Defaults to 1. - :param int title_length: The maximum number of characters allowed in the title. Only - necessary if the title is longer than the default 80 characters. Defaults to 80. + :param int title_length: DEPRECATED/IGNORED - This will be removed in a future version. :param int text_scale: Scale the size of the data lines. Scales the title as well. Defaults to 1. :param ~fontio.BuiltinFont,~adafruit_bitmap_font.bdf.BDF,~adafruit_bitmap_font.pcf.PCF font: @@ -141,56 +141,55 @@ def __init__( # pylint: disable=too-many-arguments ) self._colors = colors - self._label = label if display is None: display = board.DISPLAY self._display = display self._font = font if font else terminalio.FONT + self._text_scale = text_scale - self.text_group = displayio.Group(scale=text_scale) + self.text_group = displayio.Group() if title: - # Fail gracefully if title is longer than title_length characters. Defaults to 80. - if len(title) > title_length: - raise ValueError( - "Title character count must be less than or equal to title_length." - " Default is 80." - ) - - title = label.Label( + title_label = label.Label( self._font, text=title, - max_glyphs=title_length, color=title_color, scale=title_scale, + anchor_point=(0, 0), + anchored_position=(0, 0), ) - title.x = 0 - title.y = 8 - self._y = title.y + 18 + self._next_y = title_label.bounding_box[3] * title_scale - self.text_group.append(title) + self.text_group.append(title_label) else: - self._y = 3 + self._next_y = 0 self._lines = [] - for num in range(1): - self._lines.append(self.add_text_line(color=colors[num % len(colors)])) + # Add first line + self._lines.append(self.add_text_line(color=colors[0])) def __getitem__(self, item): """Fetch the Nth text line Group""" if len(self._lines) - 1 < item: - for _ in range(item - (len(self._lines) - 1)): + for i in range(len(self._lines), item + 1): self._lines.append( - self.add_text_line(color=self._colors[item % len(self._colors)]) + self.add_text_line(color=self._colors[i % len(self._colors)]) ) return self._lines[item] def add_text_line(self, color=(255, 255, 255)): """Adds a line on the display of the specified color and returns the label object.""" - text_label = self._label.Label(self._font, text="", color=color) - text_label.x = 0 - text_label.y = self._y - self._y = text_label.y + 13 + + text_label = label.Label( + self._font, + text="Myj", # Dummy value to allow bounding_box to calculate + color=color, + scale=self._text_scale, + anchor_point=(0, 0), + anchored_position=(0, self._next_y), + ) + self._next_y += text_label.bounding_box[3] * text_label.scale + text_label.text = "" # Erase the dummy value after using bounding_box self.text_group.append(text_label) return text_label