Skip to content

Commit 198a22c

Browse files
committed
update clue plotter
1 parent 6ac07e7 commit 198a22c

File tree

2 files changed

+243
-192
lines changed

2 files changed

+243
-192
lines changed

CLUE_Sensor_Plotter/code.py

Lines changed: 87 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -43,44 +43,56 @@
4343
import board
4444

4545
from plotter import Plotter
46-
from plot_source import PlotSource, TemperaturePlotSource, PressurePlotSource, \
47-
HumidityPlotSource, ColorPlotSource, ProximityPlotSource, \
48-
IlluminatedColorPlotSource, VolumePlotSource, \
49-
AccelerometerPlotSource, GyroPlotSource, \
50-
MagnetometerPlotSource, PinPlotSource
46+
from plot_source import (
47+
TemperaturePlotSource,
48+
PressurePlotSource,
49+
HumidityPlotSource,
50+
ColorPlotSource,
51+
ProximityPlotSource,
52+
IlluminatedColorPlotSource,
53+
VolumePlotSource,
54+
AccelerometerPlotSource,
55+
GyroPlotSource,
56+
MagnetometerPlotSource,
57+
PinPlotSource,
58+
)
5159
from adafruit_clue import clue
5260

53-
5461
debug = 1
5562

56-
5763
# A list of all the data sources for plotting
58-
sources = [TemperaturePlotSource(clue, mode="Celsius"),
59-
TemperaturePlotSource(clue, mode="Fahrenheit"),
60-
PressurePlotSource(clue, mode="Metric"),
61-
PressurePlotSource(clue, mode="Imperial"),
62-
HumidityPlotSource(clue),
63-
ColorPlotSource(clue),
64-
ProximityPlotSource(clue),
65-
IlluminatedColorPlotSource(clue, mode="Red"),
66-
IlluminatedColorPlotSource(clue, mode="Green"),
67-
IlluminatedColorPlotSource(clue, mode="Blue"),
68-
IlluminatedColorPlotSource(clue, mode="Clear"),
69-
VolumePlotSource(clue),
70-
AccelerometerPlotSource(clue),
71-
GyroPlotSource(clue),
72-
MagnetometerPlotSource(clue),
73-
PinPlotSource([board.P0, board.P1, board.P2])
74-
]
64+
# NOTE: Due to memory contraints, the total number of data sources
65+
# is limited. Can try adding more until a memory limit is hit. At that
66+
# point, decide what to keep and what to toss. Can comment/uncomment lines
67+
# below as desired.
68+
sources = [
69+
TemperaturePlotSource(clue, mode="Celsius"),
70+
# TemperaturePlotSource(clue, mode="Fahrenheit"),
71+
PressurePlotSource(clue, mode="Metric"),
72+
# PressurePlotSource(clue, mode="Imperial"),
73+
HumidityPlotSource(clue),
74+
ColorPlotSource(clue),
75+
ProximityPlotSource(clue),
76+
# IlluminatedColorPlotSource(clue, mode="Red"),
77+
# IlluminatedColorPlotSource(clue, mode="Green"),
78+
# IlluminatedColorPlotSource(clue, mode="Blue"),
79+
# IlluminatedColorPlotSource(clue, mode="Clear"),
80+
# VolumePlotSource(clue),
81+
AccelerometerPlotSource(clue),
82+
# GyroPlotSource(clue),
83+
# MagnetometerPlotSource(clue),
84+
# PinPlotSource([board.P0, board.P1, board.P2])
85+
]
7586
# The first source to select when plotting starts
7687
current_source_idx = 0
7788

7889
# The various plotting styles - scroll is currently a jump scroll
79-
stylemodes = (("lines", "scroll"), # draws lines between points
80-
("lines", "wrap"),
81-
("dots", "scroll"), # just points - slightly quicker
82-
("dots", "wrap")
83-
)
90+
stylemodes = (
91+
("lines", "scroll"), # draws lines between points
92+
("lines", "wrap"),
93+
("dots", "scroll"), # just points - slightly quicker
94+
("dots", "wrap"),
95+
)
8496
current_sm_idx = 0
8597

8698

@@ -94,7 +106,7 @@ def d_print(level, *args, **kwargs):
94106

95107
def select_colors(plttr, src, def_palette):
96108
"""Choose the colours based on the particular PlotSource
97-
or forcing use of default palette."""
109+
or forcing use of default palette."""
98110
# otherwise use defaults
99111
channel_colidx = []
100112
palette = plttr.get_colors()
@@ -109,7 +121,7 @@ def select_colors(plttr, src, def_palette):
109121

110122
def ready_plot_source(plttr, srcs, def_palette, index=0):
111123
"""Select the plot source by index from srcs list and then setup the
112-
plot parameters by retrieving meta-data from the PlotSource object."""
124+
plot parameters by retrieving meta-data from the PlotSource object."""
113125
src = srcs[index]
114126
# Put the description of the source on screen at the top
115127
source_name = str(src)
@@ -132,12 +144,12 @@ def ready_plot_source(plttr, srcs, def_palette, index=0):
132144

133145
def wait_release(func, menu):
134146
"""Calls func repeatedly waiting for it to return a false value
135-
and goes through menu list as time passes.
147+
and goes through menu list as time passes.
136148
137-
The menu is a list of menu entries where each entry is a
138-
two element list of time passed in seconds and text to display
139-
for that period.
140-
The entries must be in ascending time order."""
149+
The menu is a list of menu entries where each entry is a
150+
two element list of time passed in seconds and text to display
151+
for that period.
152+
The entries must be in ascending time order."""
141153

142154
start_t_ns = time.monotonic_ns()
143155
menu_option = None
@@ -162,7 +174,7 @@ def wait_release(func, menu):
162174

163175
def popup_text(plttr, text, duration=1.0):
164176
"""Place some text on the screen using info property of Plotter object
165-
for duration seconds."""
177+
for duration seconds."""
166178
plttr.info = text
167179
time.sleep(duration)
168180
plttr.info = None
@@ -175,13 +187,15 @@ def popup_text(plttr, text, duration=1.0):
175187
# displayio has some static limits on text - pre-calculate the maximum
176188
# length of all of the different PlotSource objects
177189
max_title_len = max(len(initial_title), max([len(str(so)) for so in sources]))
178-
plotter = Plotter(board.DISPLAY,
179-
style=stylemodes[current_sm_idx][0],
180-
mode=stylemodes[current_sm_idx][1],
181-
title=initial_title,
182-
max_title_len=max_title_len,
183-
mu_output=mu_plotter_output,
184-
debug=debug)
190+
plotter = Plotter(
191+
board.DISPLAY,
192+
style=stylemodes[current_sm_idx][0],
193+
mode=stylemodes[current_sm_idx][1],
194+
title=initial_title,
195+
max_title_len=max_title_len,
196+
mu_output=mu_plotter_output,
197+
debug=debug,
198+
)
185199

186200
# If set to true this forces use of colour blindness friendly colours
187201
use_def_pal = False
@@ -190,21 +204,28 @@ def popup_text(plttr, text, duration=1.0):
190204

191205
plotter.display_on()
192206
# Using left and right here in case the CLUE is cased hiding A/B labels
193-
popup_text(plotter,
194-
"\n".join(["Button Guide",
195-
"Left: next source",
196-
" 2secs: palette",
197-
" 4s: Mu plot",
198-
" 6s: range lock",
199-
"Right: style change"]), duration=10)
207+
popup_text(
208+
plotter,
209+
"\n".join(
210+
[
211+
"Button Guide",
212+
"Left: next source",
213+
" 2secs: palette",
214+
" 4s: Mu plot",
215+
" 6s: range lock",
216+
"Right: style change",
217+
]
218+
),
219+
duration=10,
220+
)
200221

201222
count = 0
202223

203224
while True:
204225
# Set the source and start items
205-
(source, channels) = ready_plot_source(plotter, sources,
206-
use_def_pal,
207-
current_source_idx)
226+
(source, channels) = ready_plot_source(
227+
plotter, sources, use_def_pal, current_source_idx
228+
)
208229

209230
while True:
210231
# Read data from sensor or voltage from pad
@@ -213,25 +234,22 @@ def popup_text(plttr, text, duration=1.0):
213234
# Check for left (A) and right (B) buttons
214235
if clue.button_a:
215236
# Wait for button release with time-based menu
216-
opt, _ = wait_release(lambda: clue.button_a,
217-
[(2, "Next\nsource"),
218-
(4,
219-
("Source" if use_def_pal else "Default")
220-
+ "\npalette"),
221-
(6,
222-
"Mu output "
223-
+ ("off" if mu_plotter_output else "on")),
224-
(8,
225-
"Range lock\n" + ("off" if range_lock else "on"))
226-
])
237+
opt, _ = wait_release(
238+
lambda: clue.button_a,
239+
[
240+
(2, "Next\nsource"),
241+
(4, ("Source" if use_def_pal else "Default") + "\npalette"),
242+
(6, "Mu output " + ("off" if mu_plotter_output else "on")),
243+
(8, "Range lock\n" + ("off" if range_lock else "on")),
244+
],
245+
)
227246
if opt == 0: # change plot source
228247
current_source_idx = (current_source_idx + 1) % len(sources)
229248
break # to leave inner while and select the new source
230249

231250
elif opt == 1: # toggle palette
232251
use_def_pal = not use_def_pal
233-
plotter.channel_colidx = select_colors(plotter, source,
234-
use_def_pal)
252+
plotter.channel_colidx = select_colors(plotter, source, use_def_pal)
235253

236254
elif opt == 2: # toggle Mu output
237255
mu_plotter_output = not mu_plotter_output
@@ -244,8 +262,7 @@ def popup_text(plttr, text, duration=1.0):
244262
if clue.button_b: # change plot style and mode
245263
current_sm_idx = (current_sm_idx + 1) % len(stylemodes)
246264
(new_style, new_mode) = stylemodes[current_sm_idx]
247-
wait_release(lambda: clue.button_b,
248-
[(2, new_style + "\n" + new_mode)])
265+
wait_release(lambda: clue.button_b, [(2, new_style + "\n" + new_mode)])
249266
d_print(1, "Graph change", new_style, new_mode)
250267
plotter.change_stylemode(new_style, new_mode)
251268

@@ -256,7 +273,7 @@ def popup_text(plttr, text, duration=1.0):
256273
plotter.data_add(all_data)
257274

258275
# An occasional print of free heap
259-
if debug >=3 and count % 15 == 0:
276+
if debug >= 3 and count % 15 == 0:
260277
gc.collect() # must collect() first to measure free memory
261278
print("Free memory:", gc.mem_free())
262279

0 commit comments

Comments
 (0)