Skip to content

Commit e77597e

Browse files
committed
adding logging
1 parent e34d825 commit e77597e

File tree

5 files changed

+241
-1
lines changed

5 files changed

+241
-1
lines changed

circuitpython_uplot/ulogging.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
7+
`ulogging`
8+
================================================================================
9+
10+
CircuitPython logging data graph
11+
12+
* Author(s): Jose D. Montoya
13+
14+
15+
"""
16+
try:
17+
from typing import Union
18+
from circuitpython_uplot.uplot import Uplot
19+
except ImportError:
20+
pass
21+
from bitmaptools import draw_line, fill_region
22+
from ulab import numpy as np
23+
24+
__version__ = "0.0.0+auto.0"
25+
__repo__ = "https://github.com/adafruit/CircuitPython_uplot.git"
26+
27+
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
30+
class ulogging:
31+
"""
32+
Class to log data
33+
"""
34+
35+
def __init__(
36+
self,
37+
plot: Uplot,
38+
x: Union[list, np.linspace, np.ndarray],
39+
y: Union[list, np.linspace, np.ndarray],
40+
rangex: list,
41+
rangey: list,
42+
line_color: int = 0xFFFFFF,
43+
ticksx: np.array = np.array([0, 10, 30, 50, 70, 90]),
44+
ticksy: np.array = np.array([0, 10, 30, 50, 70, 90]),
45+
) -> None:
46+
"""
47+
48+
:param Uplot plot: Plot object for the log to be drawn
49+
:param list|ulab.numpy.linspace|ulab.numpy.ndarray x: x points coordinates
50+
:param list|ulab.numpy.linspace|ulab.numpy.ndarray y: y points coordinates
51+
:param list|None rangex: x range limits. Defaults to None
52+
:param list|None rangey: y range limits. Defaults to None
53+
:param int|None line_color: line color. Defaults to None
54+
55+
"""
56+
self.points = []
57+
self.ticksx = np.array(ticksx)
58+
self.ticksy = np.array(ticksy)
59+
60+
plot._plot_palette[plot._index_colorused] = line_color
61+
62+
self.xmin = rangex[0]
63+
self.xmax = rangex[1]
64+
self.ymin = rangey[0]
65+
self.ymax = rangey[1]
66+
67+
x = np.array(x)
68+
y = np.array(y)
69+
70+
xnorm = np.array(
71+
plot.transform(self.xmin, self.xmax, plot._newxmin, plot._newxmax, x),
72+
dtype=np.int16,
73+
)
74+
ynorm = np.array(
75+
plot.transform(self.ymin, self.ymax, plot._newymin, plot._newymax, y),
76+
dtype=np.int16,
77+
)
78+
79+
fill_region(
80+
plot._plotbitmap,
81+
plot._newxmin + plot._tickheightx + 1,
82+
plot._newymax + 1,
83+
plot._newxmax - 1,
84+
plot._newymin - plot._tickheighty,
85+
0,
86+
)
87+
88+
if len(x) == 1:
89+
plot._plotbitmap[xnorm[0], ynorm[0]] = 1
90+
else:
91+
for index, _ in enumerate(xnorm):
92+
if index + 1 >= len(xnorm):
93+
break
94+
draw_line(
95+
plot._plotbitmap,
96+
xnorm[index],
97+
ynorm[index],
98+
xnorm[index + 1],
99+
ynorm[index + 1],
100+
plot._index_colorused,
101+
)
102+
if plot._showticks:
103+
if plot._loggingfirst:
104+
self._draw_ticks(plot)
105+
plot._loggingfirst = False
106+
plot._showticks = False
107+
108+
def _draw_ticks(self, plot) -> None:
109+
"""
110+
Draw ticks in the plot area
111+
112+
"""
113+
114+
ticksxnorm = np.array(
115+
plot.transform(
116+
self.xmin, self.xmax, plot._newxmin, plot._newxmax, self.ticksx
117+
),
118+
dtype=np.int16,
119+
)
120+
ticksynorm = np.array(
121+
plot.transform(
122+
self.ymin, self.ymax, plot._newymin, plot._newymax, self.ticksy
123+
),
124+
dtype=np.int16,
125+
)
126+
127+
for i, tick in enumerate(ticksxnorm):
128+
draw_line(
129+
plot._plotbitmap,
130+
tick,
131+
plot._newymin,
132+
tick,
133+
plot._newymin - plot._tickheightx,
134+
2,
135+
)
136+
if plot._showtext:
137+
plot.show_text(
138+
"{:.2f}".format(ticksxnorm[i]), tick, plot._newymin, (0.5, 0.0)
139+
)
140+
for i, tick in enumerate(ticksynorm):
141+
draw_line(
142+
plot._plotbitmap,
143+
plot._newxmin,
144+
tick,
145+
plot._newxmin + plot._tickheighty,
146+
tick,
147+
2,
148+
)
149+
if plot._showtext:
150+
plot.show_text(
151+
"{:.2f}".format(ticksynorm[i]), plot._newxmin, tick, (1.0, 0.5)
152+
)

circuitpython_uplot/uplot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def __init__(
108108
self._newymax = padding
109109

110110
self._cartesianfirst = True
111+
self._loggingfirst = True
111112

112113
self._showtext = False
113114

docs/api.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@
2121

2222
.. automodule:: circuitpython_uplot.umap
2323
:members:
24+
25+
.. automodule:: circuitpython_uplot.ulogging
26+
:members:

docs/examples.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,18 @@ Sparkline animation example
153153
Cartesian Animation Example
154154
---------------------------
155155

156-
Casrtesian animation example
156+
Cartesian animation example
157157

158158
.. literalinclude:: ../examples/uplot_ucartesian_loggin_data.py
159159
:caption: examples/uplot_ucartesian_loggin_data.py
160160
:linenos:
161161
.. image:: ../docs/uplot_cartesian.gif
162+
163+
Logging Example
164+
---------------------------
165+
166+
Logging example
167+
168+
.. literalinclude:: ../examples/uplot_ulogging.py
169+
:caption: examples/uplot_ulogging.py
170+
:linenos:

examples/uplot_ulogging.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# SPDX-FileCopyrightText: Copyright (c) Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import displayio
6+
import terminalio
7+
import board
8+
from adafruit_display_text import label
9+
from circuitpython_uplot.uplot import Uplot, color
10+
from circuitpython_uplot.ulogging import ulogging
11+
12+
# Setting up the display
13+
display = board.DISPLAY
14+
15+
plot = Uplot(0, 0, display.width, display.height)
16+
g = displayio.Group()
17+
18+
DISPLAY_WIDTH = 200
19+
DISPLAY_HEIGHT = 200
20+
FOREGROUND_COLOR = color.BLACK
21+
BACKGROUND_COLOR = color.WHITE
22+
23+
background_bitmap = displayio.Bitmap(DISPLAY_WIDTH, DISPLAY_HEIGHT, 1)
24+
# Map colors in a palette
25+
palette = displayio.Palette(1)
26+
palette[0] = BACKGROUND_COLOR
27+
# Create a Tilegrid with the background and put in the displayio group
28+
t = displayio.TileGrid(background_bitmap, pixel_shader=palette)
29+
g.append(t)
30+
31+
text_temperature = label.Label(terminalio.FONT, color=FOREGROUND_COLOR, scale=3)
32+
text_temperature.anchor_point = 0.0, 0.0
33+
text_temperature.anchored_position = 25, 0
34+
g.append(text_temperature)
35+
36+
text_humidity = label.Label(terminalio.FONT, color=FOREGROUND_COLOR, scale=3)
37+
text_humidity.anchor_point = 0.0, 0.0
38+
text_humidity.anchored_position = 130, 0
39+
g.append(text_humidity)
40+
41+
plot_1 = Uplot(
42+
0,
43+
50,
44+
200,
45+
60,
46+
padding=1,
47+
show_box=True,
48+
box_color=color.BLACK,
49+
background_color=color.WHITE,
50+
)
51+
52+
plot_1.tick_params(
53+
tickx_height=4, ticky_height=4, show_ticks=True, tickcolor=color.BLACK
54+
)
55+
56+
x = [10, 20, 30, 40, 50]
57+
temp_y = [26, 25, 24, 23, 28]
58+
59+
g.append(plot_1)
60+
61+
display.show(g)
62+
display.refresh()
63+
64+
dist = 3
65+
66+
ulogging(
67+
plot_1,
68+
x[0:dist],
69+
temp_y[0:dist],
70+
rangex=[0, 200],
71+
rangey=[0, 100],
72+
line_color=color.BLACK,
73+
ticksx=[10, 50, 80, 100],
74+
ticksy=[15, 30, 45, 60],
75+
)

0 commit comments

Comments
 (0)