78
78
79
79
class _ClueSimpleTextDisplay :
80
80
"""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 ):
83
83
import displayio
84
84
import terminalio
85
85
from adafruit_display_text import label
@@ -88,31 +88,39 @@ def __init__(self, title="CLUE Sensor Data", title_color=0xFFFFFF, title_scale=1
88
88
colors = ((255 , 0 , 255 ), (0 , 255 , 0 ), (255 , 0 , 0 ), (0 , 255 , 255 ), (255 , 255 , 0 ),
89
89
(0 , 0 , 255 ), (255 , 0 , 180 ), (0 , 180 , 255 ), (255 , 180 , 0 ), (180 , 0 , 255 ))
90
90
91
+ self ._colors = colors
91
92
self ._label = label
92
93
self ._display = board .DISPLAY
93
94
self ._font = terminalio .FONT
94
95
if font :
95
96
self ._font = font
96
97
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 )
100
99
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." )
106
104
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
109
114
110
115
self ._lines = []
111
- for num in range (num_lines ):
116
+ for num in range (1 ):
112
117
self ._lines .append (self .add_text_line (color = colors [num % len (colors )]))
113
118
114
119
def __getitem__ (self , item ):
115
120
"""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 )]))
116
124
return self ._lines [item ]
117
125
118
126
def add_text_line (self , color = 0xFFFFFF ):
@@ -614,7 +622,8 @@ def pixel(self):
614
622
615
623
from adafruit_clue import clue
616
624
617
- clue.pixel.fill((255, 0, 255))
625
+ while True:
626
+ clue.pixel.fill((255, 0, 255))
618
627
"""
619
628
return self ._pixel
620
629
@@ -792,27 +801,34 @@ def loud_sound(self, sound_threshold=200):
792
801
return self .sound_level > sound_threshold
793
802
794
803
@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.
798
811
799
812
Setup occurs before the loop. For data to be dynamically updated on the display, you must
800
813
include the data call in the loop by using ``.text =``. For example, if setup is saved as
801
814
``clue_data = display_clue_data()`` then ``clue_data[0].text = clue.proximity`` must be
802
815
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.
809
825
:param int text_scale: Scale the size of the data lines. Scales the title as well.
810
- Defaults to 1.
826
+ Defaults to 1.
811
827
:param str font: The font to use to display the title and data. Defaults to built in
812
828
``terminalio.FONT``.
813
829
: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.
816
832
Default colors are used if ``colors`` is not set. For example, if creating
817
833
two lines of data, ``colors=((255, 255, 255), (255, 0, 0))`` would set the
818
834
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
822
838
:alt: Display Clue Data demo
823
839
824
840
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.
825
842
826
843
.. code-block:: python
827
844
828
845
from adafruit_clue import clue
829
846
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)
832
848
833
849
while True:
834
850
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
837
853
clue_data.show()
838
854
"""
839
855
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 )
842
857
843
858
844
859
clue = Clue () # pylint: disable=invalid-name
0 commit comments