Skip to content

Commit 8630ac8

Browse files
committed
logging_values
1 parent 89e1398 commit 8630ac8

File tree

5 files changed

+173
-50
lines changed

5 files changed

+173
-50
lines changed

.pylintrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ unsafe-load-any-extension=no
1414

1515
[MESSAGES CONTROL]
1616
confidence=
17-
disable=raw-checker-failed,bad-inline-option,locally-disabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,import-error,pointless-string-statement,unspecified-encoding
17+
disable=raw-checker-failed,bad-inline-option,locally-disabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,import-error,pointless-string-statement,unspecified-encoding,protected-access,unnecessary-list-index-lookup
1818
enable=
1919

2020
[REPORTS]
@@ -80,7 +80,7 @@ class-rgx=[A-Z_][a-zA-Z0-9_]+$
8080
const-rgx=(([A-Z_][A-Z0-9_]*)|(__.*__))$
8181
docstring-min-length=-1
8282
function-rgx=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$
83-
good-names=r,g,b,w,i,j,k,n,x,y,z,ex,ok,Run,_,cs,TVOC,ubar,rx,ry,x0,y0,y1,x1
83+
good-names=r,g,b,w,i,j,k,n,x,y,z,ex,ok,Run,_,cs,TVOC,ubar,rx,ry,x0,y0,y1,x1,ulogging
8484
include-naming-hint=no
8585
inlinevar-rgx=[A-Za-z_][A-Za-z0-9_]*$
8686
method-rgx=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$

circuitpython_uplot/ulogging.py

Lines changed: 75 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
__repo__ = "https://github.com/adafruit/CircuitPython_uplot.git"
2626

2727

28-
# pylint: disable=too-many-arguments, invalid-name, no-self-use, too-few-public-methods
29-
# pylint: disable=too-many-locals, too-many-branches, protected-access, unnecessary-list-index-lookup
3028
class ulogging:
3129
"""
3230
Class to log data
@@ -77,51 +75,7 @@ def __init__(
7775
self.ymin = rangey[0]
7876
self.ymax = rangey[1]
7977

80-
x = np.array(x)
81-
y = np.array(y)
82-
83-
xnorm = np.array(
84-
plot.transform(self.xmin, self.xmax, plot._newxmin, plot._newxmax, x),
85-
dtype=np.int16,
86-
)
87-
ynorm = np.array(
88-
plot.transform(self.ymin, self.ymax, plot._newymin, plot._newymax, y),
89-
dtype=np.int16,
90-
)
91-
92-
fill_region(
93-
plot._plotbitmap,
94-
plot._newxmin + plot._tickheightx + 1,
95-
plot._newymax + 1,
96-
plot._newxmax - 1,
97-
plot._newymin - plot._tickheighty,
98-
0,
99-
)
100-
101-
if len(x) == 1:
102-
plot._plotbitmap[xnorm[0], ynorm[0]] = 1
103-
else:
104-
for index, _ in enumerate(xnorm):
105-
if index + 1 >= len(xnorm):
106-
break
107-
draw_line(
108-
plot._plotbitmap,
109-
xnorm[index],
110-
ynorm[index],
111-
xnorm[index + 1],
112-
ynorm[index + 1],
113-
plot._index_colorused,
114-
)
115-
if fill:
116-
for index, _ in enumerate(xnorm):
117-
draw_line(
118-
plot._plotbitmap,
119-
xnorm[index],
120-
ynorm[index],
121-
xnorm[index],
122-
plot._newymin,
123-
plot._index_colorused,
124-
)
78+
self.draw_points(plot, x, y, fill)
12579

12680
if plot._showticks:
12781
if plot._loggingfirst:
@@ -174,3 +128,77 @@ def _draw_ticks(self, plot) -> None:
174128
plot.show_text(
175129
"{:.2f}".format(ticksynorm[i]), plot._newxmin, tick, (1.0, 0.5)
176130
)
131+
132+
@staticmethod
133+
def clear_plot(plot) -> None:
134+
"""
135+
Clears the plot area
136+
"""
137+
138+
fill_region(
139+
plot._plotbitmap,
140+
plot._newxmin + plot._tickheightx + 1,
141+
plot._newymax + 1,
142+
plot._newxmax - 1,
143+
plot._newymin - plot._tickheighty,
144+
0,
145+
)
146+
147+
def draw_points(self, plot: Uplot, x: list, y: list, fill: bool = False) -> None:
148+
"""
149+
Draws points in the plot
150+
:param Uplot plot: plot object provided
151+
:param list x: list of x values
152+
:param list y: list of y values
153+
:param bool fill: parameter to fill the plot graphic. Defaults to False
154+
:return: None
155+
"""
156+
self.clear_plot(plot)
157+
158+
self.draw_new_lines(plot, x, y, fill)
159+
160+
def draw_new_lines(self, plot: Uplot, x: list, y: list, fill: bool = False) -> None:
161+
"""
162+
Draw the plot lines
163+
:param Uplot plot: plot object provided
164+
:param list x: list of x values
165+
:param list y: list of y values
166+
:param bool fill: parameter to fill the plot graphic. Defaults to False
167+
:return: None
168+
"""
169+
x = np.array(x)
170+
y = np.array(y)
171+
172+
xnorm = np.array(
173+
plot.transform(self.xmin, self.xmax, plot._newxmin, plot._newxmax, x),
174+
dtype=np.int16,
175+
)
176+
ynorm = np.array(
177+
plot.transform(self.ymin, self.ymax, plot._newymin, plot._newymax, y),
178+
dtype=np.int16,
179+
)
180+
181+
if len(x) == 1:
182+
plot._plotbitmap[xnorm[0], ynorm[0]] = 1
183+
else:
184+
for index, _ in enumerate(xnorm):
185+
if index + 1 >= len(xnorm):
186+
break
187+
draw_line(
188+
plot._plotbitmap,
189+
xnorm[index],
190+
ynorm[index],
191+
xnorm[index + 1],
192+
ynorm[index + 1],
193+
plot._index_colorused,
194+
)
195+
if fill:
196+
for index, _ in enumerate(xnorm):
197+
draw_line(
198+
plot._plotbitmap,
199+
xnorm[index],
200+
ynorm[index],
201+
xnorm[index],
202+
plot._newymin,
203+
plot._index_colorused,
204+
)

docs/examples.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,17 @@ Logging fill example
230230
:lines: 5-
231231
.. image:: ../docs/uplot_ex22.jpg
232232

233+
234+
Logging Changing Values Example
235+
---------------------------------------
236+
237+
This example shows how to redraw new_values in the same plot
238+
239+
.. literalinclude:: ../examples/uplot_logging_changing_values.py
240+
:caption: examples/uplot_logging_changing_values.py
241+
:lines: 5-
242+
243+
233244
SVG Images examples
234245
---------------------------
235246

docs/quick_start.rst

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,22 @@ There are some parameters that you can customize:
469469
470470
plot = Uplot(0, 0, display.width, display.height)
471471
472-
ulogging(plot, x, y, rangex=[0, 200], rangey=[0, 100], ticksx=[10, 50, 80, 100], ticksy=[15, 30, 45, 60],)
472+
x = [10, 20, 30, 40, 50]
473+
temp_y = [10, 15, 35, 10, 25]
474+
475+
my_log = ulogging(plot, x, y, rangex=[0, 200], rangey=[0, 100], ticksx=[10, 50, 80, 100], ticksy=[15, 30, 45, 60],)
476+
477+
478+
if you want to redraw new data in the same plot, you could do something like this:
479+
480+
.. code-block:: python
481+
482+
483+
x_new = [10, 20, 30, 40, 50]
484+
y_new = [26, 50, 26, 50, 26]
485+
486+
my_log.draw_points(plot_1, x_new, y_new)
487+
473488
474489
===============
475490
SVG
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# SPDX-FileCopyrightText: Copyright (c) Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import displayio
6+
import board
7+
import time
8+
from circuitpython_uplot.uplot import Uplot, color
9+
from circuitpython_uplot.ulogging import ulogging
10+
11+
# Setting up the display
12+
display = board.DISPLAY
13+
14+
plot = Uplot(0, 0, display.width, display.height)
15+
g = displayio.Group()
16+
17+
DISPLAY_WIDTH = 200
18+
DISPLAY_HEIGHT = 200
19+
FOREGROUND_COLOR = color.BLACK
20+
BACKGROUND_COLOR = color.WHITE
21+
22+
background_bitmap = displayio.Bitmap(DISPLAY_WIDTH, DISPLAY_HEIGHT, 1)
23+
# Map colors in a palette
24+
palette = displayio.Palette(1)
25+
palette[0] = BACKGROUND_COLOR
26+
# Create a Tilegrid with the background and put in the displayio group
27+
t = displayio.TileGrid(background_bitmap, pixel_shader=palette)
28+
g.append(t)
29+
30+
31+
plot_1 = Uplot(
32+
0,
33+
50,
34+
200,
35+
60,
36+
padding=1,
37+
show_box=True,
38+
box_color=color.BLACK,
39+
background_color=color.WHITE,
40+
)
41+
42+
plot_1.tick_params(
43+
tickx_height=4, ticky_height=4, show_ticks=True, tickcolor=color.BLACK
44+
)
45+
46+
x = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
47+
temp_y = [26, 25, 24, 23, 28, 24, 54, 76, 34, 23]
48+
49+
g.append(plot_1)
50+
51+
display.show(g)
52+
display.refresh()
53+
54+
my_log = ulogging(
55+
plot_1,
56+
x,
57+
temp_y,
58+
rangex=[0, 200],
59+
rangey=[0, 100],
60+
line_color=color.BLACK,
61+
ticksx=[10, 50, 80, 100],
62+
ticksy=[15, 30, 45, 60],
63+
)
64+
65+
66+
while True:
67+
for i in range(len(x)):
68+
my_log.draw_points(plot_1, x[0:i], temp_y[0:i])
69+
time.sleep(1)

0 commit comments

Comments
 (0)