Skip to content

Commit 17918c7

Browse files
authored
Merge pull request #6 from kattni/title-fix
Title fix
2 parents 15fdef4 + 13fca41 commit 17918c7

File tree

3 files changed

+50
-31
lines changed

3 files changed

+50
-31
lines changed

adafruit_clue.py

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878

7979
class _ClueSimpleTextDisplay:
8080
"""Easily display lines of text on CLUE display."""
81-
def __init__(self, title="CLUE Sensor Data", title_color=0xFFFFFF, title_scale=1, # pylint: disable=too-many-arguments
82-
text_scale=1, font=None, num_lines=1, colors=None):
81+
def __init__(self, title=None, title_color=0xFFFFFF, title_scale=1, # pylint: disable=too-many-arguments
82+
text_scale=1, font=None, colors=None):
8383
import displayio
8484
import terminalio
8585
from adafruit_display_text import label
@@ -88,31 +88,39 @@ def __init__(self, title="CLUE Sensor Data", title_color=0xFFFFFF, title_scale=1
8888
colors = ((255, 0, 255), (0, 255, 0), (255, 0, 0), (0, 255, 255), (255, 255, 0),
8989
(0, 0, 255), (255, 0, 180), (0, 180, 255), (255, 180, 0), (180, 0, 255))
9090

91+
self._colors = colors
9192
self._label = label
9293
self._display = board.DISPLAY
9394
self._font = terminalio.FONT
9495
if font:
9596
self._font = font
9697

97-
# Fail gracefully if title is longer than 60 characters.
98-
if len(title) > 60:
99-
raise ValueError("Title must be 60 characters or less.")
98+
self.text_group = displayio.Group(max_size=20, scale=text_scale)
10099

101-
title = label.Label(self._font, text=title, max_glyphs=60, color=title_color,
102-
scale=title_scale)
103-
title.x = 0
104-
title.y = 8
105-
self._y = title.y + 20
100+
if title:
101+
# Fail gracefully if title is longer than 60 characters.
102+
if len(title) > 60:
103+
raise ValueError("Title must be 60 characters or less.")
106104

107-
self.text_group = displayio.Group(max_size=20, scale=text_scale)
108-
self.text_group.append(title)
105+
title = label.Label(self._font, text=title, max_glyphs=60, color=title_color,
106+
scale=title_scale)
107+
title.x = 0
108+
title.y = 8
109+
self._y = title.y + 18
110+
111+
self.text_group.append(title)
112+
else:
113+
self._y = 3
109114

110115
self._lines = []
111-
for num in range(num_lines):
116+
for num in range(1):
112117
self._lines.append(self.add_text_line(color=colors[num % len(colors)]))
113118

114119
def __getitem__(self, item):
115120
"""Fetch the Nth text line Group"""
121+
if len(self._lines) - 1 < item:
122+
for _ in range(item - (len(self._lines) - 1)):
123+
self._lines.append(self.add_text_line(color=self._colors[item % len(self._colors)]))
116124
return self._lines[item]
117125

118126
def add_text_line(self, color=0xFFFFFF):
@@ -614,7 +622,8 @@ def pixel(self):
614622
615623
from adafruit_clue import clue
616624
617-
clue.pixel.fill((255, 0, 255))
625+
while True:
626+
clue.pixel.fill((255, 0, 255))
618627
"""
619628
return self._pixel
620629

@@ -792,27 +801,34 @@ def loud_sound(self, sound_threshold=200):
792801
return self.sound_level > sound_threshold
793802

794803
@staticmethod
795-
def simple_text_display(title="CLUE Sensor Data", title_color=(255, 255, 255), title_scale=1, # pylint: disable=too-many-arguments
796-
num_lines=1, text_scale=1, font=None, colors=None):
797-
"""Display lines of text on the CLUE display.
804+
def simple_text_display(title=None, title_color=(255, 255, 255), title_scale=1, # pylint: disable=too-many-arguments
805+
text_scale=1, font=None, colors=None):
806+
"""Display lines of text on the CLUE display. Lines of text are created in order as shown
807+
in the example below. If you skip a number, the line will be shown blank on the display,
808+
e.g. if you include ``[0]`` and ``[2]``, the second line on the display will be empty, and
809+
the text specified for lines 0 and 2 will be displayed on the first and third line.
810+
Remember, Python begins counting at 0, so the first line on the display is 0 in the code.
798811
799812
Setup occurs before the loop. For data to be dynamically updated on the display, you must
800813
include the data call in the loop by using ``.text =``. For example, if setup is saved as
801814
``clue_data = display_clue_data()`` then ``clue_data[0].text = clue.proximity`` must be
802815
inside the ``while True:`` loop for the proximity data displayed to update as the
803-
values change.
804-
805-
:param str title: The title displayed above the data. Defaults to "CLUE Sensor Data".
806-
:param title_color: The color of the displayed title. Defaults to white 255, 255, 255).
807-
:param int title_scale: Scale the size of the title. Defaults to 1.
808-
:param int num_lines: The number of lines of data you intend to display. Defaults to 1.
816+
values change. You must call ``show()`` at the end of the list for anything to display.
817+
See example below for usage.
818+
819+
:param str title: The title displayed above the data. Set ``title="Title text"`` to provide
820+
a title. Defaults to None.
821+
:param title_color: The color of the title. Not necessary if no title is provided. Defaults
822+
to white (255, 255, 255).
823+
:param int title_scale: Scale the size of the title. Not necessary if no title is provided.
824+
Defaults to 1.
809825
:param int text_scale: Scale the size of the data lines. Scales the title as well.
810-
Defaults to 1.
826+
Defaults to 1.
811827
:param str font: The font to use to display the title and data. Defaults to built in
812828
``terminalio.FONT``.
813829
:param colors: A list of colors for the lines of data on the display. If you provide a
814-
single color, all lines will be that color. Otherwise it will alternate the
815-
list you provide if the list is less than the number of lines displayed.
830+
single color, all lines will be that color. Otherwise it will cycle through
831+
the list you provide if the list is less than the number of lines displayed.
816832
Default colors are used if ``colors`` is not set. For example, if creating
817833
two lines of data, ``colors=((255, 255, 255), (255, 0, 0))`` would set the
818834
first line white and the second line red, and if you created four lines of
@@ -822,13 +838,13 @@ def simple_text_display(title="CLUE Sensor Data", title_color=(255, 255, 255), t
822838
:alt: Display Clue Data demo
823839
824840
This example displays three lines with acceleration, gyro and magnetic data on the display.
841+
Remember to call ``show()`` after the list to update the display.
825842
826843
.. code-block:: python
827844
828845
from adafruit_clue import clue
829846
830-
clue_data = clue.simple_text_display(title="CLUE Sensor Data!", title_scale=2,
831-
num_lines=3)
847+
clue_data = clue.simple_text_display(title="CLUE Sensor Data!", title_scale=2)
832848
833849
while True:
834850
clue_data[0].text = "Acceleration: {:.2f} {:.2f} {:.2f}".format(*clue.acceleration)
@@ -837,8 +853,7 @@ def simple_text_display(title="CLUE Sensor Data", title_color=(255, 255, 255), t
837853
clue_data.show()
838854
"""
839855
return _ClueSimpleTextDisplay(title=title, title_color=title_color, title_scale=title_scale,
840-
num_lines=num_lines, text_scale=text_scale, font=font,
841-
colors=colors)
856+
text_scale=text_scale, font=font, colors=colors)
842857

843858

844859
clue = Clue() # pylint: disable=invalid-name

docs/examples.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ Ensure your device works with this simple test.
66
.. literalinclude:: ../examples/clue_simpletest.py
77
:caption: examples/clue_simpletest.py
88
:linenos:
9+
10+
.. literalinclude:: ../examples/clue_display_sensor_data.py
11+
:caption: examples/clue_display_sensor_data.py
12+
:linenos:

examples/clue_display_sensor_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
clue.sea_level_pressure = 1020
44

5-
clue_data = clue.simple_text_display(title="CLUE Sensor Data!", title_scale=2, num_lines=15)
5+
clue_data = clue.simple_text_display(title="CLUE Sensor Data!", title_scale=2)
66

77
while True:
88
clue_data[0].text = "Acceleration: {:.2f} {:.2f} {:.2f} m/s^2".format(*clue.acceleration)

0 commit comments

Comments
 (0)