Skip to content

Commit 88dc811

Browse files
committed
adding cartesian data_logging
1 parent baf6294 commit 88dc811

File tree

6 files changed

+140
-10
lines changed

6 files changed

+140
-10
lines changed

circuitpython_uplot/ucartesian.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
from circuitpython_uplot.uplot import Uplot
1919
except ImportError:
2020
pass
21-
22-
from bitmaptools import draw_line
21+
from bitmaptools import draw_line, fill_region
2322
from ulab import numpy as np
2423
from vectorio import Polygon
2524

@@ -44,6 +43,7 @@ def __init__(
4443
line_color: Optional[Union[int, None]] = None,
4544
fill: bool = False,
4645
nudge: bool = True,
46+
logging: bool = False,
4747
) -> None:
4848
"""
4949
@@ -55,9 +55,11 @@ def __init__(
5555
:param int|None line_color: line color. Defaults to None
5656
:param bool fill: Show the filling. Defaults to `False`
5757
:param bool nudge: moves the graph a little for better displaying. Defaults to `True`
58+
:param bool logging: used to change the logic of the cartesian to work as a logger
5859
5960
"""
60-
points = []
61+
self.points = []
62+
6163
if line_color is not None:
6264
plot._plot_palette[plot._index_colorused] = line_color
6365

@@ -93,21 +95,31 @@ def __init__(
9395
)
9496

9597
if fill:
96-
points.append((xnorm[0], plot._newymin))
98+
self.points.append((xnorm[0], plot._newymin))
9799
for index, item in enumerate(xnorm):
98-
points.append((item, ynorm[index]))
99-
points.append((xnorm[-1], plot._newymin))
100-
points.append((xnorm[0], plot._newymin))
100+
self.points.append((item, ynorm[index]))
101+
self.points.append((xnorm[-1], plot._newymin))
102+
self.points.append((xnorm[0], plot._newymin))
101103
plot.append(
102104
Polygon(
103105
pixel_shader=plot._plot_palette,
104-
points=points,
106+
points=self.points,
105107
x=0,
106108
y=0,
107109
color_index=plot._index_colorused,
108110
)
109111
)
110112
else:
113+
if logging:
114+
fill_region(
115+
plot._plotbitmap,
116+
plot._newxmin + plot._tickheightx + 1,
117+
plot._newymax + 1,
118+
plot._newxmax - 1,
119+
plot._newymin - plot._tickheighty,
120+
0,
121+
)
122+
111123
for index, _ in enumerate(xnorm):
112124
if index + 1 >= len(xnorm):
113125
break
@@ -121,8 +133,14 @@ def __init__(
121133
)
122134
if plot._showticks:
123135
if plot._cartesianfirst:
136+
if logging:
137+
x = np.linspace(xmin, xmax, 100)
138+
y = np.linspace(ymin, ymax, 100)
124139
plot._draw_ticks(x, y)
125140
plot._cartesianfirst = False
126141
plot._showticks = False
127142

128-
plot._index_colorused = plot._index_colorused + 1
143+
if logging:
144+
plot._index_colorused = plot._index_colorused
145+
else:
146+
plot._index_colorused = plot._index_colorused + 1

circuitpython_uplot/uplot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ def _draw_ticks(self, x: int, y: int) -> None:
351351

352352
def tick_params(
353353
self,
354+
show_ticks=True,
354355
tickx_height: int = 8,
355356
ticky_height: int = 8,
356357
tickcolor: int = 0xFFFFFF,
@@ -370,7 +371,7 @@ def tick_params(
370371
371372
"""
372373

373-
self._showticks = True
374+
self._showticks = show_ticks
374375
self._tickheightx = tickx_height
375376
self._tickheighty = ticky_height
376377
self._plot_palette[2] = tickcolor

docs/examples.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,13 @@ Sparkline animation example
149149
.. literalinclude:: ../examples/uplot_sparkline.py
150150
:caption: examples/uplot_sparkline.py
151151
:linenos:
152+
153+
Cartesian Animation Example
154+
---------------------------
155+
156+
Casrtesian animation example
157+
158+
.. literalinclude:: ../examples/uplot_ucartesian_loggin_data.py
159+
:caption: examples/uplot_ucartesian_loggin_data.py
160+
:linenos:
161+
.. image:: ../docs/uplot_ex17.jpg

docs/uplot_cartesian.gif

217 KB
Loading

docs/uplot_cartesian.gif.license

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SPDX-FileCopyrightText: 2023 Jose David M.
2+
3+
SPDX-License-Identifier: MIT
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Casainho, Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
from random import choice
7+
import displayio
8+
import terminalio
9+
import board
10+
from adafruit_display_text import label
11+
from circuitpython_uplot.uplot import Uplot, color
12+
from circuitpython_uplot.ucartesian import ucartesian
13+
14+
# Setting up the display
15+
display = board.DISPLAY
16+
17+
plot = Uplot(0, 0, display.width, display.height)
18+
g = displayio.Group()
19+
20+
# Set a white background
21+
# Create the display object - the third color is red (0xff0000)
22+
DISPLAY_WIDTH = 200
23+
DISPLAY_HEIGHT = 200
24+
BLACK = 0x000000
25+
WHITE = 0xFFFFFF
26+
FOREGROUND_COLOR = BLACK
27+
BACKGROUND_COLOR = WHITE
28+
29+
background_bitmap = displayio.Bitmap(DISPLAY_WIDTH, DISPLAY_HEIGHT, 1)
30+
# Map colors in a palette
31+
palette = displayio.Palette(1)
32+
palette[0] = BACKGROUND_COLOR
33+
# Create a Tilegrid with the background and put in the displayio group
34+
t = displayio.TileGrid(background_bitmap, pixel_shader=palette)
35+
g.append(t)
36+
37+
text_temperature = label.Label(terminalio.FONT, color=FOREGROUND_COLOR, scale=3)
38+
text_temperature.anchor_point = 0.0, 0.0
39+
text_temperature.anchored_position = 25, 0
40+
g.append(text_temperature)
41+
42+
text_humidity = label.Label(terminalio.FONT, color=FOREGROUND_COLOR, scale=3)
43+
text_humidity.anchor_point = 0.0, 0.0
44+
text_humidity.anchored_position = 130, 0
45+
g.append(text_humidity)
46+
47+
48+
plot_1 = Uplot(
49+
0, 50, 200, 60, padding=1, show_box=True, box_color=BLACK, background_color=WHITE
50+
)
51+
52+
plot_2 = Uplot(
53+
0, 180, 200, 60, padding=1, show_box=True, box_color=BLACK, background_color=WHITE
54+
)
55+
plot_1.tick_params(
56+
tickx_height=4, ticky_height=4, show_ticks=True, tickcolor=color.BLACK
57+
)
58+
plot_2.tick_params(
59+
tickx_height=4, ticky_height=4, show_ticks=True, tickcolor=color.BLACK
60+
)
61+
62+
63+
temperatures = [26, 25, 24, 23, 28]
64+
humidity = [66, 67, 71, 79]
65+
x = list(range(0, 144, 1))
66+
temp_y = [choice(temperatures) for _ in x]
67+
humidity_y = [choice(humidity) for _ in x]
68+
69+
g.append(plot_1)
70+
g.append(plot_2)
71+
72+
display.show(g)
73+
display.refresh()
74+
75+
for i, element in enumerate(x):
76+
ucartesian(
77+
plot_1,
78+
x[0:i],
79+
temp_y[0:i],
80+
rangex=[0, 143],
81+
rangey=[0, 40],
82+
fill=False,
83+
line_color=BLACK,
84+
logging=True,
85+
)
86+
ucartesian(
87+
plot_2,
88+
x[0:i],
89+
humidity_y[0:i],
90+
rangex=[0, 143],
91+
rangey=[0, 100],
92+
fill=False,
93+
line_color=BLACK,
94+
logging=True,
95+
)
96+
text_temperature.text = f"{temp_y[i]}"
97+
text_humidity.text = f"{int(round(humidity_y[i], 0))}%"
98+
time.sleep(0.1)

0 commit comments

Comments
 (0)