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

Commit 0806f9f

Browse files
committed
adding cartesian linetypes
1 parent 522fb5e commit 0806f9f

File tree

6 files changed

+200
-7
lines changed

6 files changed

+200
-7
lines changed

docs/examples.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ Cartesian Simple Test
1919
:lines: 5-
2020
.. image:: ../docs/cartesian_simpletest.jpg
2121

22+
Cartesian Line Types
23+
----------------------------
24+
25+
Cartesian Line Types examples
26+
27+
.. literalinclude:: ../examples/cartesian_linetypes.py
28+
:caption: examples/cartesian_linetypes.py
29+
:lines: 5-
30+
.. image:: ../docs/line_types.jpg
31+
2232

2333
Cartesian Fill
2434
----------------------------

docs/line_types.jpg

39.1 KB
Loading

examples/cartesian_linetypes.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import gc
6+
import math
7+
from machine import Pin, SPI
8+
from ili9486 import ILI9486
9+
from micropython_uplot.plot import PLOT
10+
from micropython_uplot.utils import linspace
11+
from micropython_uplot.cartesian import Cartesian
12+
13+
# Pin definition
14+
pdc = Pin(8, Pin.OUT, value=0)
15+
prst = Pin(15, Pin.OUT, value=1)
16+
pcs = Pin(9, Pin.OUT, value=1)
17+
spi = SPI(1, sck=Pin(10), mosi=Pin(11), miso=Pin(12), baudrate=30_000_000)
18+
gc.collect()
19+
display = ILI9486(spi, pcs, pdc, prst)
20+
21+
plot = PLOT(display, 5, 5, 300, 250, padding=1, box_color=(255, 255, 255))
22+
plot.tick_params(
23+
tickx_height=12, ticky_height=12, tickcolor=(100, 100, 100), tickgrid=True
24+
)
25+
26+
# Creating some points to graph
27+
x = list(linspace(-4, 4, 50))
28+
constant = 1.0 / math.sqrt(2 * math.pi)
29+
y = [constant * math.exp((-(_**2)) / 2.0) for _ in x]
30+
y2 = [constant / 2 * math.exp((-(_**2)) / 1.0) for _ in x]
31+
y3 = [10 + (2 + constant / 1.1 * math.exp((-(_**2)) / 0.5 + 2)) for _ in x]
32+
# Drawing the graph
33+
Cartesian(plot, x, y, line_color=(255, 255, 0), line_style=".")
34+
Cartesian(plot, x, y2, line_color=(0, 255, 0), line_style="-.-")
35+
p = Cartesian(plot, x, y3, line_color=(0, 255, 255), line_style="- -")
36+
display.show()
37+
import time
38+
39+
time.sleep(1)
40+
# plot._savingppm("line_types.ppm")
41+
p.update(plot)
42+
display.show()

micropython_uplot/cartesian.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ def __init__(
8787
if self._line_type not in ["-", ".", "- -", "-.-"]:
8888
raise ValueError("line_style must be a valid option")
8989

90-
if line_style is None:
91-
self._line_type = "-"
92-
9390
max_x = max(x)
9491
min_x = min(x)
9592
max_y = max(y)
@@ -161,9 +158,9 @@ def _draw_plotline(self, plot, index, xnorm, ynorm):
161158
if index % 3 == 0:
162159
self._plot_line(plot, index, xnorm, ynorm)
163160
else:
164-
plot._plotbitmap[xnorm[index], ynorm[index]] = plot._index_colorused
161+
plot._display.pixel(xnorm[index], ynorm[index], self._line_color)
165162
elif self._line_type == ".":
166-
plot._plotbitmap[xnorm[index], ynorm[index]] = plot._index_colorused
163+
plot._display.pixel(xnorm[index], ynorm[index], self._line_color)
167164
elif self._line_type == "- -":
168165
if index % 2 == 0:
169166
self._plot_line(plot, index, xnorm, ynorm)
@@ -177,6 +174,12 @@ def _plot_line(self, plot, index, xnorm, ynorm):
177174
self._line_color,
178175
)
179176

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

181184
class LineStyle:
182185
"""

micropython_uplot/logging.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
7+
`logging`
8+
================================================================================
9+
10+
MicroPython logging utility
11+
12+
* Author: Jose D. Montoya
13+
14+
15+
"""
16+
try:
17+
from typing import Optional
18+
from micropython_uplot.plot import PLOT
19+
except ImportError:
20+
pass
21+
22+
from micropython_uplot.colors import set_color
23+
24+
25+
__version__ = "0.0.0+auto.0"
26+
__repo__ = "https://github.com/adafruit/MicroPython_UPLOT.git"
27+
28+
29+
class Logging:
30+
"""
31+
Class to log data
32+
"""
33+
34+
def __init__(
35+
self,
36+
plot: PLOT,
37+
x: list,
38+
y: list,
39+
rangex: Optional[list] = None,
40+
rangey: Optional[list] = None,
41+
line_color: tuple = (0, 255, 0),
42+
ticksx: list = None,
43+
ticksy: list = None,
44+
) -> None:
45+
"""
46+
47+
:param Plot plot: Plot object for the scatter to be drawn
48+
:param list x: x points coordinates
49+
:param list y: y points coordinates
50+
:param list|None rangex: x range limits. Defaults to None
51+
:param list|None rangey: y range limits. Defaults to None
52+
:param int|None line_color: line color. Defaults to None
53+
:param list ticksx: X axis ticks values
54+
:param list ticksy: Y axis ticks values
55+
56+
"""
57+
self.points = []
58+
self.ticksx = ticksx
59+
self.ticksy = ticksy
60+
61+
self._line_color = set_color(
62+
plot._display,
63+
self._pointer_index,
64+
line_color[0],
65+
line_color[1],
66+
line_color[2],
67+
)
68+
plot._pointer_index += 1
69+
70+
max_x = max(x)
71+
min_x = min(x)
72+
max_y = max(y)
73+
min_y = min(y)
74+
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)
78+
79+
else:
80+
self.xmin = min(rangex)
81+
self.xmax = max(rangex)
82+
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)
89+
90+
xnorm = tuple(
91+
[
92+
int(
93+
plot.transform(
94+
self.xmin, self.xmax, plot._newxmin, plot._newxmax, _
95+
)
96+
)
97+
for _ in x
98+
]
99+
)
100+
ynorm = tuple(
101+
[
102+
int(
103+
plot.transform(
104+
self.ymin, self.ymax, plot._newymin, plot._newymax, _
105+
)
106+
)
107+
for _ in y
108+
]
109+
)
110+
111+
for index, _ in enumerate(xnorm):
112+
if index + 1 >= len(xnorm):
113+
break
114+
if y[index] >= self.ymax:
115+
continue
116+
117+
self._draw_plotline(plot, index, xnorm, ynorm)
118+
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
124+
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,
132+
)
133+
134+
def update(self, plot):
135+
"""
136+
Update the plot with new data
137+
"""
138+
plot._display.fill(0)

micropython_uplot/plot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,8 @@ def _savingppm(self, filename: str = "picture.ppm", width=480, height=320):
484484
self._color2,
485485
self._color1,
486486
(0, 255, 0),
487-
(255, 0, 0),
488-
(255, 0, 0),
487+
(0, 255, 255),
488+
(99, 0, 99),
489489
(0, 94, 153),
490490
(0, 167, 109),
491491
]

0 commit comments

Comments
 (0)