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

Commit 4d01f5d

Browse files
committed
adding map
1 parent 0a881dc commit 4d01f5d

File tree

6 files changed

+187
-5
lines changed

6 files changed

+187
-5
lines changed

docs/api.rst

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

2020
.. automodule:: micropython_uplot.logging
2121
:members:
22+
23+
.. automodule:: micropython_uplot.map
24+
:members:

docs/examples.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,13 @@ Logging Animation
120120
:caption: examples/logging_animation.py
121121
:lines: 5-
122122
.. image:: ../docs/logging_animation.jpg
123+
124+
Map Simpletest
125+
----------------------------
126+
127+
Map Simpletest
128+
129+
.. literalinclude:: ../examples/map_simpletest.py
130+
:caption: examples/map_simpletest.py
131+
:lines: 5-
132+
.. image:: ../docs/map_simpletest.jpg

docs/map_simpletest.jpg

18.2 KB
Loading

examples/map_simpletest.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import gc
6+
from math import pi, exp, sqrt
7+
from random import choice
8+
from machine import Pin, SPI
9+
import array
10+
from ili9486 import ILI9486
11+
from micropython_uplot.plot import PLOT
12+
from micropython_uplot.utils import linspace
13+
from micropython_uplot.map import Map
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+
plot = PLOT(display, 5, 5, 300, 250, padding=1, box_color=(255, 255, 255))
24+
25+
# Setting some date to plot
26+
x = linspace(-4, 4, 100)
27+
y = tuple([(2.0 / sqrt(2 * pi) * exp((-(value**2)) / 4.0)) for value in x])
28+
ymax = max(y)
29+
30+
y1 = []
31+
32+
rows, cols = (10, 10)
33+
indice = 0
34+
35+
for i in range(rows):
36+
col = []
37+
for j in range(cols):
38+
col.append(y[indice])
39+
indice += 1
40+
y1.append(col)
41+
42+
43+
# Plotting and showing the plot
44+
Map(plot, y1, ymax, [rows, cols], (255, 0, 68), (68, 0, 255))
45+
# Plotting and showing the plot
46+
display.show()

micropython_uplot/map.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# SPDX-FileCopyrightText: 2023 Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
6+
"""
7+
8+
`map`
9+
================================================================================
10+
11+
MicroPython color map graph
12+
13+
* Author: Jose D. Montoya
14+
15+
16+
"""
17+
try:
18+
from micropython_uplot.plot import PLOT
19+
except ImportError:
20+
pass
21+
22+
from math import floor
23+
from micropython_uplot.colors import set_color
24+
25+
__version__ = "0.0.0+auto.0"
26+
__repo__ = "https://github.com/adafruit/MicrotPython_UPLOT.git"
27+
28+
29+
class Map:
30+
"""
31+
Main class to display different graphics
32+
"""
33+
34+
def __init__(
35+
self,
36+
plot: PLOT,
37+
data_points: list,
38+
data_points_max: float,
39+
matrix_shape: list,
40+
initial_color: tuple,
41+
final_color: tuple,
42+
) -> None:
43+
"""
44+
Due to IL9436 Display driver limitations number of map colors is limited to 10
45+
:param PLOT plot: Plot object for the scatter to be drawn
46+
:param list data_points: data points to create the color map
47+
:param float data_points_max: data points max value
48+
:param list matrix_shape: data_points matrix shape
49+
:param tuple initial_color: initial color to create the color map
50+
:param tuple final_color: final color to create the color map
51+
52+
"""
53+
54+
self._numbins = 10
55+
56+
self._pointer_index = 3
57+
self._step = data_points_max / self._numbins
58+
59+
width = plot._newxmax - plot._newxmin
60+
height = plot._newymin - plot._newymax
61+
xdist = width // matrix_shape[0]
62+
ydist = height // matrix_shape[1]
63+
64+
for i in range(1, 11):
65+
color = color_fade(initial_color, final_color, i / 10)
66+
print(color)
67+
set_color(
68+
plot._display,
69+
self._pointer_index + i,
70+
color[0],
71+
color[1],
72+
color[2],
73+
)
74+
75+
deltax = plot._newxmin + plot.padding
76+
deltay = plot._newymax + plot.padding
77+
78+
for i, row in enumerate(data_points):
79+
for j, col in enumerate(row):
80+
if floor(col / self._step) > 9:
81+
color = 9
82+
else:
83+
color = floor(col / self._step)
84+
plot._display.rect(
85+
deltax,
86+
deltay,
87+
xdist,
88+
ydist,
89+
self._pointer_index + color + 1,
90+
True,
91+
)
92+
deltax = deltax + xdist
93+
deltax = plot._newxmin + plot.padding
94+
deltay = deltay + ydist
95+
96+
97+
def color_fade(start_color: int, end_color: int, fraction: float):
98+
"""Linear extrapolation of a color between two RGB colors (tuple or 24-bit integer).
99+
100+
:param start_color: starting color
101+
:param end_color: ending color
102+
:param fraction: Floating point number ranging from 0 to 1 indicating what
103+
fraction of interpolation between start_color and end_color.
104+
105+
"""
106+
107+
if fraction >= 1:
108+
return end_color
109+
if fraction <= 0:
110+
return start_color
111+
112+
faded_color = [0, 0, 0]
113+
for i in range(3):
114+
faded_color[i] = start_color[i] - int(
115+
(start_color[i] - end_color[i]) * fraction
116+
)
117+
return faded_color

micropython_uplot/plot.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -483,11 +483,17 @@ def _savingppm(self, filename: str = "picture.ppm", width=480, height=320):
483483
self._color0,
484484
self._color2,
485485
self._color1,
486-
(0, 255, 0),
487-
(0, 255, 255),
488-
(99, 0, 99),
489-
(0, 94, 153),
490-
(0, 167, 109),
486+
self._color0,
487+
(237, 0, 86),
488+
(218, 0, 105),
489+
(199, 0, 124),
490+
(181, 0, 142),
491+
(162, 0, 161),
492+
(143, 0, 180),
493+
(125, 0, 198),
494+
(106, 0, 217),
495+
(87, 0, 236),
496+
(68, 0, 255),
491497
]
492498

493499
# Header values for the file

0 commit comments

Comments
 (0)