Skip to content
This repository was archived by the owner on Dec 19, 2024. It is now read-only.

Commit e21a381

Browse files
committed
adding logging
1 parent 0806f9f commit e21a381

File tree

8 files changed

+274
-60
lines changed

8 files changed

+274
-60
lines changed

.pylintrc

Lines changed: 1 addition & 1 deletion
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,protected-access,consider-using-generator,too-few-public-methods
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,consider-using-generator,too-few-public-methods,unnecessary-list-index-lookup
1818
enable=
1919

2020
[REPORTS]

docs/api.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ MicroPython UPLOT Library
1616

1717
.. automodule:: micropython_uplot.bar
1818
:members:
19+
20+
.. automodule:: micropython_uplot.logging
21+
:members:

examples/cartesian_linetypes.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,3 @@
3434
Cartesian(plot, x, y2, line_color=(0, 255, 0), line_style="-.-")
3535
p = Cartesian(plot, x, y3, line_color=(0, 255, 255), line_style="- -")
3636
display.show()
37-
import time
38-
39-
time.sleep(1)
40-
# plot._savingppm("line_types.ppm")
41-
p.update(plot)
42-
display.show()

examples/logging_animation.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import random
7+
import gc
8+
import math
9+
from machine import Pin, SPI
10+
from ili9486 import ILI9486
11+
from micropython_uplot.plot import PLOT
12+
from micropython_uplot.utils import linspace
13+
from micropython_uplot.logging import Logging
14+
15+
# Pin definition
16+
pdc = Pin(8, Pin.OUT, value=0)
17+
prst = Pin(15, Pin.OUT, value=1)
18+
pcs = Pin(9, Pin.OUT, value=1)
19+
spi = SPI(1, sck=Pin(10), mosi=Pin(11), miso=Pin(12), baudrate=30_000_000)
20+
gc.collect()
21+
display = ILI9486(spi, pcs, pdc, prst)
22+
23+
my_plot = PLOT(display, 5, 5, 300, 250, padding=25, box_color=(255, 255, 255))
24+
my_plot.tick_params(
25+
tickx_height=4,
26+
ticky_height=4,
27+
show_ticks=True,
28+
tickcolor=(255, 125, 125),
29+
showtext=True,
30+
)
31+
32+
# Creating the x and y data
33+
x = [
34+
10,
35+
20,
36+
30,
37+
40,
38+
50,
39+
60,
40+
70,
41+
80,
42+
90,
43+
100,
44+
110,
45+
120,
46+
130,
47+
140,
48+
150,
49+
160,
50+
170,
51+
180,
52+
190,
53+
]
54+
y = [26, 22, 24, 30, 28, 35, 26, 25, 24, 23, 20, 27, 26, 33, 24, 23, 19, 27, 26]
55+
56+
# Creating the random numbers
57+
random_numbers = [19, 22, 35, 33, 24, 26, 28, 37]
58+
59+
60+
dist = 1
61+
62+
# Creating the loggraph
63+
my_loggraph = Logging(
64+
my_plot,
65+
x[0:dist],
66+
y[0:dist],
67+
rangex=[0, 210],
68+
rangey=[0, 110],
69+
line_color=(0, 0, 255),
70+
ticksx=[25, 50, 75, 100, 125, 150, 175, 200],
71+
ticksy=[25, 50, 75, 100],
72+
)
73+
74+
display.show()
75+
76+
# Showing the loggraph
77+
for _ in range(20):
78+
if dist > len(x):
79+
y.pop(0)
80+
y.append(random.choice(random_numbers))
81+
82+
my_loggraph.draw_points(my_plot, x[0:dist], y[0:dist])
83+
display.show()
84+
dist += 1
85+
time.sleep(0.5)

examples/logging_simpletest.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import gc
7+
import math
8+
from machine import Pin, SPI
9+
from ili9486 import ILI9486
10+
from micropython_uplot.plot import PLOT
11+
from micropython_uplot.utils import linspace
12+
from micropython_uplot.logging import Logging
13+
14+
# Pin definition
15+
pdc = Pin(8, Pin.OUT, value=0)
16+
prst = Pin(15, Pin.OUT, value=1)
17+
pcs = Pin(9, Pin.OUT, value=1)
18+
spi = SPI(1, sck=Pin(10), mosi=Pin(11), miso=Pin(12), baudrate=30_000_000)
19+
gc.collect()
20+
display = ILI9486(spi, pcs, pdc, prst)
21+
22+
plot = PLOT(display, 5, 5, 300, 250, padding=1, box_color=(255, 255, 255))
23+
24+
x = list(linspace(-4, 4, 25))
25+
constant = 1.0 / math.sqrt(2 * math.pi)
26+
y = [constant * math.exp((-(_**2)) / 2.0) for _ in x]
27+
# Drawing the graph
28+
my_log = Logging(plot, x, y, rangex=[-4, 4], rangey=[0, 1], line_color=(255, 255, 0))
29+
30+
display.show()
31+
time.sleep(1)
32+
33+
my_log.update(plot)
34+
display.show()

micropython_uplot/cartesian.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,6 @@ def _plot_line(self, plot, index, xnorm, ynorm):
174174
self._line_color,
175175
)
176176

177-
def update(self, plot):
178-
"""
179-
Update the plot with new data
180-
"""
181-
plot._display.fill(0)
182-
183177

184178
class LineStyle:
185179
"""

micropython_uplot/logging.py

Lines changed: 148 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,31 @@ def __init__(
3939
rangex: Optional[list] = None,
4040
rangey: Optional[list] = None,
4141
line_color: tuple = (0, 255, 0),
42-
ticksx: list = None,
43-
ticksy: list = None,
42+
ticksx: list = (0, 10, 30, 50, 70, 90),
43+
ticksy: list = (0, 10, 30, 50, 70, 90),
44+
tick_pos: bool = False,
45+
fill: bool = False,
4446
) -> None:
4547
"""
4648
4749
:param Plot plot: Plot object for the scatter to be drawn
4850
:param list x: x points coordinates
4951
:param list y: y points coordinates
50-
:param list|None rangex: x range limits. Defaults to None
52+
:param list|None rangex: x range limits. Defaults to Nonem
5153
:param list|None rangey: y range limits. Defaults to None
5254
:param int|None line_color: line color. Defaults to None
5355
:param list ticksx: X axis ticks values
5456
:param list ticksy: Y axis ticks values
57+
:param bool tick_pos: indicates ticks position. True for below the axes.
58+
Defaults to ``False``
59+
:param bool fill: enable the filling of the plot. Defaults to ``False``
5560
5661
"""
5762
self.points = []
58-
self.ticksx = ticksx
59-
self.ticksy = ticksy
63+
self.ticksx = tuple(ticksx)
64+
self.ticksy = tuple(ticksy)
65+
66+
self._pointer_index = plot._pointer_index
6067

6168
self._line_color = set_color(
6269
plot._display,
@@ -67,26 +74,72 @@ def __init__(
6774
)
6875
plot._pointer_index += 1
6976

70-
max_x = max(x)
71-
min_x = min(x)
72-
max_y = max(y)
73-
min_y = min(y)
77+
if tick_pos:
78+
self._tickposx = plot._tickheightx
79+
self._tickposy = plot._tickheighty
80+
else:
81+
self._tickposx = 0
82+
self._tickposy = 0
7483

75-
if rangex is None:
76-
self.xmin = min_x - (abs(max_x - min_x) / 10)
77-
self.xmax = max_x + (abs(max_x - min_x) / 10)
84+
self.xmin = rangex[0]
85+
self.xmax = rangex[1]
86+
self.ymin = rangey[0]
87+
self.ymax = rangey[1]
7888

79-
else:
80-
self.xmin = min(rangex)
81-
self.xmax = max(rangex)
89+
self.draw_points(plot, x, y, fill)
90+
if plot._showticks:
91+
if plot._loggingfirst:
92+
plot._loggingfirst = False
93+
self._draw_ticks(plot)
94+
plot._showticks = False
8295

83-
if rangey is None:
84-
self.ymin = min_y - (abs(max_y - min_y) / 10)
85-
self.ymax = max_y + (abs(max_y - min_y) / 10)
86-
else:
87-
self.ymin = min(rangey)
88-
self.ymax = max(rangey)
96+
def _plot_line(self, plot, index, xnorm, ynorm):
97+
plot._display.line(
98+
xnorm[index],
99+
ynorm[index],
100+
xnorm[index + 1],
101+
ynorm[index + 1],
102+
self._line_color,
103+
)
89104

105+
def draw_points(self, plot: PLOT, x: list, y: list, fill: bool = False) -> None:
106+
"""
107+
Draws points in the plot
108+
:param Plot plot: plot object provided
109+
:param list x: list of x values
110+
:param list y: list of y values
111+
:param bool fill: parameter to fill the plot graphic. Defaults to False
112+
:return: None
113+
"""
114+
self.clear_plot(plot)
115+
# if self._limits:
116+
# self._draw_limit_lines(plot)
117+
self.draw_new_lines(plot, x, y, fill)
118+
119+
@staticmethod
120+
def clear_plot(plot) -> None:
121+
"""
122+
Clears the plot area
123+
"""
124+
125+
plot._display.rect(
126+
plot._newxmin + 1 + plot._tickheightx,
127+
plot._newymax + 1,
128+
plot._buff_width - 2 - 2 * plot.padding - plot._tickheightx,
129+
plot._buff_height - 2 - 2 * plot.padding - plot._tickheighty,
130+
plot._background_color,
131+
True,
132+
)
133+
134+
def draw_new_lines(self, plot: PLOT, x: list, y: list, fill: bool = False) -> None:
135+
"""
136+
Draw the plot lines
137+
:param Plot plot: plot object provided
138+
:param list x: list of x values
139+
:param list y: list of y values
140+
:param bool fill: parameter to fill the plot graphic. Defaults to False
141+
:return: None
142+
"""
90143
xnorm = tuple(
91144
[
92145
int(
@@ -108,31 +161,82 @@ def __init__(
108161
]
109162
)
110163

111-
for index, _ in enumerate(xnorm):
112-
if index + 1 >= len(xnorm):
113-
break
114-
if y[index] >= self.ymax:
115-
continue
164+
if len(x) == 1:
165+
plot._display.pixel(xnorm[0], ynorm[0], self._line_color)
166+
else:
167+
for index, _ in enumerate(xnorm):
168+
if index + 1 >= len(xnorm):
169+
break
170+
if y[index] >= self.ymax:
171+
continue
172+
173+
self._plot_line(plot, index, xnorm, ynorm)
174+
175+
if fill:
176+
for index, _ in enumerate(xnorm):
177+
plot._display.line(
178+
xnorm[index],
179+
ynorm[index],
180+
xnorm[index],
181+
plot._newymin,
182+
self._line_color,
183+
)
116184

117-
self._draw_plotline(plot, index, xnorm, ynorm)
185+
def _draw_ticks(self, plot) -> None:
186+
"""
187+
Draw ticks in the plot area
118188
119-
if plot._showticks:
120-
if plot._cartesianfirst:
121-
plot._draw_ticks(x, y, self.ticksx, self.ticksy)
122-
plot._cartesianfirst = False
123-
plot._showticks = False
189+
"""
124190

125-
def _plot_line(self, plot, index, xnorm, ynorm):
126-
plot._display.line(
127-
xnorm[index],
128-
ynorm[index],
129-
xnorm[index + 1],
130-
ynorm[index + 1],
131-
self._line_color,
191+
ticksxnorm = tuple(
192+
[
193+
int(
194+
plot.transform(
195+
self.xmin, self.xmax, plot._newxmin, plot._newxmax, _
196+
)
197+
)
198+
for _ in self.ticksx
199+
]
200+
)
201+
ticksynorm = tuple(
202+
[
203+
int(
204+
plot.transform(
205+
self.ymin, self.ymax, plot._newymin, plot._newymax, _
206+
)
207+
)
208+
for _ in self.ticksy
209+
]
132210
)
133211

134-
def update(self, plot):
135-
"""
136-
Update the plot with new data
137-
"""
138-
plot._display.fill(0)
212+
for i, tick in enumerate(ticksxnorm):
213+
plot._display.line(
214+
tick,
215+
plot._newymin,
216+
tick,
217+
plot._newymin - plot._tickheightx,
218+
plot._tickcolor,
219+
)
220+
if plot._showtext:
221+
plot.show_text(
222+
"{:.{}f}".format(self.ticksx[i], plot._decimal_points),
223+
tick,
224+
plot._newymin,
225+
ax="x",
226+
)
227+
228+
for i, tick in enumerate(ticksynorm):
229+
plot._display.line(
230+
plot._newxmin,
231+
tick,
232+
plot._newxmin + plot._tickheighty,
233+
tick,
234+
plot._tickcolor,
235+
)
236+
if plot._showtext:
237+
plot.show_text(
238+
"{:.{}f}".format(self.ticksy[i], plot._decimal_points),
239+
plot._newxmin,
240+
tick,
241+
ax="y",
242+
)

0 commit comments

Comments
 (0)