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

Commit

Permalink
adding map
Browse files Browse the repository at this point in the history
  • Loading branch information
jposada202020 committed Aug 19, 2023
1 parent 0a881dc commit 4d01f5d
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 5 deletions.
3 changes: 3 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ MicroPython UPLOT Library

.. automodule:: micropython_uplot.logging
:members:

.. automodule:: micropython_uplot.map
:members:
10 changes: 10 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,13 @@ Logging Animation
:caption: examples/logging_animation.py
:lines: 5-
.. image:: ../docs/logging_animation.jpg

Map Simpletest
----------------------------

Map Simpletest

.. literalinclude:: ../examples/map_simpletest.py
:caption: examples/map_simpletest.py
:lines: 5-
.. image:: ../docs/map_simpletest.jpg
Binary file added docs/map_simpletest.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions examples/map_simpletest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT

import gc
from math import pi, exp, sqrt
from random import choice
from machine import Pin, SPI
import array
from ili9486 import ILI9486
from micropython_uplot.plot import PLOT
from micropython_uplot.utils import linspace
from micropython_uplot.map import Map

# Pin definition
pdc = Pin(8, Pin.OUT, value=0)
prst = Pin(15, Pin.OUT, value=1)
pcs = Pin(9, Pin.OUT, value=1)
spi = SPI(1, sck=Pin(10), mosi=Pin(11), miso=Pin(12), baudrate=30_000_000)
gc.collect()
display = ILI9486(spi, pcs, pdc, prst)

plot = PLOT(display, 5, 5, 300, 250, padding=1, box_color=(255, 255, 255))

# Setting some date to plot
x = linspace(-4, 4, 100)
y = tuple([(2.0 / sqrt(2 * pi) * exp((-(value**2)) / 4.0)) for value in x])
ymax = max(y)

y1 = []

rows, cols = (10, 10)
indice = 0

for i in range(rows):
col = []
for j in range(cols):
col.append(y[indice])
indice += 1
y1.append(col)


# Plotting and showing the plot
Map(plot, y1, ymax, [rows, cols], (255, 0, 68), (68, 0, 255))
# Plotting and showing the plot
display.show()
117 changes: 117 additions & 0 deletions micropython_uplot/map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# SPDX-FileCopyrightText: 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT


"""
`map`
================================================================================
MicroPython color map graph
* Author: Jose D. Montoya
"""
try:
from micropython_uplot.plot import PLOT
except ImportError:
pass

from math import floor
from micropython_uplot.colors import set_color

__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/MicrotPython_UPLOT.git"


class Map:
"""
Main class to display different graphics
"""

def __init__(
self,
plot: PLOT,
data_points: list,
data_points_max: float,
matrix_shape: list,
initial_color: tuple,
final_color: tuple,
) -> None:
"""
Due to IL9436 Display driver limitations number of map colors is limited to 10
:param PLOT plot: Plot object for the scatter to be drawn
:param list data_points: data points to create the color map
:param float data_points_max: data points max value
:param list matrix_shape: data_points matrix shape
:param tuple initial_color: initial color to create the color map
:param tuple final_color: final color to create the color map
"""

self._numbins = 10

self._pointer_index = 3
self._step = data_points_max / self._numbins

width = plot._newxmax - plot._newxmin
height = plot._newymin - plot._newymax
xdist = width // matrix_shape[0]
ydist = height // matrix_shape[1]

for i in range(1, 11):
color = color_fade(initial_color, final_color, i / 10)
print(color)
set_color(
plot._display,
self._pointer_index + i,
color[0],
color[1],
color[2],
)

deltax = plot._newxmin + plot.padding
deltay = plot._newymax + plot.padding

for i, row in enumerate(data_points):
for j, col in enumerate(row):
if floor(col / self._step) > 9:
color = 9
else:
color = floor(col / self._step)
plot._display.rect(
deltax,
deltay,
xdist,
ydist,
self._pointer_index + color + 1,
True,
)
deltax = deltax + xdist
deltax = plot._newxmin + plot.padding
deltay = deltay + ydist


def color_fade(start_color: int, end_color: int, fraction: float):
"""Linear extrapolation of a color between two RGB colors (tuple or 24-bit integer).
:param start_color: starting color
:param end_color: ending color
:param fraction: Floating point number ranging from 0 to 1 indicating what
fraction of interpolation between start_color and end_color.
"""

if fraction >= 1:
return end_color
if fraction <= 0:
return start_color

faded_color = [0, 0, 0]
for i in range(3):
faded_color[i] = start_color[i] - int(
(start_color[i] - end_color[i]) * fraction
)
return faded_color
16 changes: 11 additions & 5 deletions micropython_uplot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,17 @@ def _savingppm(self, filename: str = "picture.ppm", width=480, height=320):
self._color0,
self._color2,
self._color1,
(0, 255, 0),
(0, 255, 255),
(99, 0, 99),
(0, 94, 153),
(0, 167, 109),
self._color0,
(237, 0, 86),
(218, 0, 105),
(199, 0, 124),
(181, 0, 142),
(162, 0, 161),
(143, 0, 180),
(125, 0, 198),
(106, 0, 217),
(87, 0, 236),
(68, 0, 255),
]

# Header values for the file
Expand Down

0 comments on commit 4d01f5d

Please sign in to comment.