-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlogging.py
More file actions
244 lines (213 loc) · 7.2 KB
/
logging.py
File metadata and controls
244 lines (213 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT
"""
`logging`
================================================================================
CircuitPython logging data graph
* Author: Jose D. Montoya
"""
try:
from typing import Union, Optional
from circuitpython_uplot.plot import Plot
except ImportError:
pass
from bitmaptools import draw_line, fill_region
from ulab import numpy as np
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/CircuitPython_uplot.git"
class Logging:
"""
Class to log data
"""
def __init__(
self,
plot: Plot,
x: Union[list, np.linspace, np.ndarray],
y: Union[list, np.linspace, np.ndarray],
rangex: list,
rangey: list,
line_color: int = 0xFFFFFF,
ticksx: Union[np.array, list] = np.array([0, 10, 30, 50, 70, 90]),
ticksy: Union[np.array, list] = np.array([0, 10, 30, 50, 70, 90]),
tick_pos: bool = False,
fill: bool = False,
limits: Optional[list] = None,
limits_color: int = 0xFF0000,
) -> None:
"""
:param Plot plot: Plot object for the log to be drawn
:param list|ulab.numpy.linspace|ulab.numpy.ndarray x: x points coordinates
:param list|ulab.numpy.linspace|ulab.numpy.ndarray y: y points coordinates
:param list|None rangex: x range limits. Defaults to None
:param list|None rangey: y range limits. Defaults to None
:param int|None line_color: line color. Defaults to None
:param np.array|list ticksx: X axis ticks values
:param np.array|list ticksy: Y axis ticks values
:param bool tick_pos: indicates ticks position. True for below the axes.
Defaults to ``False``
:param bool fill: enable the filling of the plot. Defaults to ``False``
"""
self.points = []
self.ticksx = np.array(ticksx)
self.ticksy = np.array(ticksy)
self._fill = fill
if tick_pos:
self._tickposx = plot._tickheightx
self._tickposy = plot._tickheighty
else:
self._tickposx = 0
self._tickposy = 0
plot._plot_palette[plot._index_colorused] = line_color
self._line_index = plot._index_colorused
self.xmin = rangex[0]
self.xmax = rangex[1]
self.ymin = rangey[0]
self.ymax = rangey[1]
if limits is not None:
self._limits = np.array(
plot.transform(
self.ymin,
self.ymax,
plot._newymin,
plot._newymax,
np.array(limits),
),
dtype=np.int16,
)
plot._plot_palette[9] = limits_color
else:
self._limits = None
self.draw_points(plot, x, y)
def _draw_ticks(self, plot) -> None:
"""
Draw ticks in the plot area
"""
self._ticksxnorm = np.array(
plot.transform(
self.xmin, self.xmax, plot._newxmin, plot._newxmax, self.ticksx
),
dtype=np.int16,
)
self._ticksynorm = np.array(
plot.transform(
self.ymin, self.ymax, plot._newymin, plot._newymax, self.ticksy
),
dtype=np.int16,
)
for i, tick in enumerate(self._ticksxnorm):
draw_line(
plot._plotbitmap,
tick,
plot._newymin + self._tickposx,
tick,
plot._newymin - plot._tickheightx + self._tickposx,
2,
)
if plot._showtext:
plot.show_text(f"{self.ticksx[i]:.0f}", tick, plot._newymin, (0.5, 0.0))
for i, tick in enumerate(self._ticksynorm):
draw_line(
plot._plotbitmap,
plot._newxmin - self._tickposy,
tick,
plot._newxmin + plot._tickheighty - self._tickposy,
tick,
2,
)
if plot._showtext:
plot.show_text(f"{self.ticksy[i]:.0f}", plot._newxmin, tick, (1.0, 0.5))
@staticmethod
def clear_plot(plot) -> None:
"""
Clears the plot area
"""
fill_region(
plot._plotbitmap,
plot._newxmin + plot._tickheightx + 1,
plot._newymax + 1,
plot._newxmax - 1,
plot._newymin - plot._tickheighty,
0,
)
def draw_points(self, plot: Plot, x: list, y: list) -> None:
"""
Draws points in the plot
:param Plot plot: plot object provided
:param list x: list of x values
:param list y: list of y values
:param bool fill: parameter to fill the plot graphic. Defaults to False
:return: None
"""
self.clear_plot(plot)
if self._limits:
self._draw_limit_lines(plot)
self.draw_new_lines(plot, x, y)
if plot._showticks:
if plot._loggingfirst:
self._draw_ticks(plot)
plot._loggingfirst = False
plot._showticks = False
if plot._tickgrid:
plot._draw_gridx(self._ticksxnorm)
plot._draw_gridy(self._ticksynorm)
def draw_new_lines(self, plot: Plot, x: list, y: list) -> None:
"""
Draw the plot lines
:param Plot plot: plot object provided
:param list x: list of x values
:param list y: list of y values
:param bool fill: parameter to fill the plot graphic. Defaults to False
:return: None
"""
x = np.array(x)
y = np.array(y)
xnorm = np.array(
plot.transform(self.xmin, self.xmax, plot._newxmin, plot._newxmax, x),
dtype=np.int16,
)
ynorm = np.array(
plot.transform(self.ymin, self.ymax, plot._newymin, plot._newymax, y),
dtype=np.int16,
)
if len(x) == 1:
plot._plotbitmap[xnorm[0], ynorm[0]] = 1
else:
for index, _ in enumerate(xnorm):
if index + 1 >= len(xnorm):
break
draw_line(
plot._plotbitmap,
xnorm[index],
ynorm[index],
xnorm[index + 1],
ynorm[index + 1],
self._line_index,
)
if self._fill:
for index, _ in enumerate(xnorm):
draw_line(
plot._plotbitmap,
xnorm[index],
ynorm[index],
xnorm[index],
plot._newymin,
self._line_index,
)
def _draw_limit_lines(self, plot: Plot) -> None:
draw_line(
plot._plotbitmap,
plot._newxmin + 1,
self._limits[0],
plot._newxmax - 1,
self._limits[0],
9,
)
draw_line(
plot._plotbitmap,
plot._newxmin + 1,
self._limits[1],
plot._newxmax - 1,
self._limits[1],
9,
)