Skip to content

Font-based row height #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 53 additions & 60 deletions adafruit_simple_text_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,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=1,
title_length=80,
text_scale=1,
title_scale: int = 1,
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
Expand All @@ -78,39 +77,36 @@ 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.
: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.
Defaults to 1.
: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 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

Expand Down Expand Up @@ -145,58 +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 = terminalio.FONT
if font:
self._font = font
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
Expand Down
4 changes: 4 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down