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

Commit

Permalink
adding logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jposada202020 committed Aug 18, 2023
1 parent 0806f9f commit e21a381
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ unsafe-load-any-extension=no

[MESSAGES CONTROL]
confidence=
disable=raw-checker-failed,bad-inline-option,locally-disabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,import-error,pointless-string-statement,unspecified-encoding,protected-access,consider-using-generator,too-few-public-methods
disable=raw-checker-failed,bad-inline-option,locally-disabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,import-error,pointless-string-statement,unspecified-encoding,protected-access,consider-using-generator,too-few-public-methods,unnecessary-list-index-lookup
enable=

[REPORTS]
Expand Down
3 changes: 3 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ MicroPython UPLOT Library

.. automodule:: micropython_uplot.bar
:members:

.. automodule:: micropython_uplot.logging
:members:
6 changes: 0 additions & 6 deletions examples/cartesian_linetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,3 @@
Cartesian(plot, x, y2, line_color=(0, 255, 0), line_style="-.-")
p = Cartesian(plot, x, y3, line_color=(0, 255, 255), line_style="- -")
display.show()
import time

time.sleep(1)
# plot._savingppm("line_types.ppm")
p.update(plot)
display.show()
85 changes: 85 additions & 0 deletions examples/logging_animation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT

import time
import random
import gc
import math
from machine import Pin, SPI
from ili9486 import ILI9486
from micropython_uplot.plot import PLOT
from micropython_uplot.utils import linspace
from micropython_uplot.logging import Logging

# 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)

my_plot = PLOT(display, 5, 5, 300, 250, padding=25, box_color=(255, 255, 255))
my_plot.tick_params(
tickx_height=4,
ticky_height=4,
show_ticks=True,
tickcolor=(255, 125, 125),
showtext=True,
)

# Creating the x and y data
x = [
10,
20,
30,
40,
50,
60,
70,
80,
90,
100,
110,
120,
130,
140,
150,
160,
170,
180,
190,
]
y = [26, 22, 24, 30, 28, 35, 26, 25, 24, 23, 20, 27, 26, 33, 24, 23, 19, 27, 26]

# Creating the random numbers
random_numbers = [19, 22, 35, 33, 24, 26, 28, 37]


dist = 1

# Creating the loggraph
my_loggraph = Logging(
my_plot,
x[0:dist],
y[0:dist],
rangex=[0, 210],
rangey=[0, 110],
line_color=(0, 0, 255),
ticksx=[25, 50, 75, 100, 125, 150, 175, 200],
ticksy=[25, 50, 75, 100],
)

display.show()

# Showing the loggraph
for _ in range(20):
if dist > len(x):
y.pop(0)
y.append(random.choice(random_numbers))

my_loggraph.draw_points(my_plot, x[0:dist], y[0:dist])
display.show()
dist += 1
time.sleep(0.5)
34 changes: 34 additions & 0 deletions examples/logging_simpletest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT

import time
import gc
import math
from machine import Pin, SPI
from ili9486 import ILI9486
from micropython_uplot.plot import PLOT
from micropython_uplot.utils import linspace
from micropython_uplot.logging import Logging

# 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))

x = list(linspace(-4, 4, 25))
constant = 1.0 / math.sqrt(2 * math.pi)
y = [constant * math.exp((-(_**2)) / 2.0) for _ in x]
# Drawing the graph
my_log = Logging(plot, x, y, rangex=[-4, 4], rangey=[0, 1], line_color=(255, 255, 0))

display.show()
time.sleep(1)

my_log.update(plot)
display.show()
6 changes: 0 additions & 6 deletions micropython_uplot/cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,6 @@ def _plot_line(self, plot, index, xnorm, ynorm):
self._line_color,
)

def update(self, plot):
"""
Update the plot with new data
"""
plot._display.fill(0)


class LineStyle:
"""
Expand Down
192 changes: 148 additions & 44 deletions micropython_uplot/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,31 @@ def __init__(
rangex: Optional[list] = None,
rangey: Optional[list] = None,
line_color: tuple = (0, 255, 0),
ticksx: list = None,
ticksy: list = None,
ticksx: list = (0, 10, 30, 50, 70, 90),
ticksy: list = (0, 10, 30, 50, 70, 90),
tick_pos: bool = False,
fill: bool = False,
) -> None:
"""
:param Plot plot: Plot object for the scatter to be drawn
:param list x: x points coordinates
:param list y: y points coordinates
:param list|None rangex: x range limits. Defaults to None
:param list|None rangex: x range limits. Defaults to Nonem
:param list|None rangey: y range limits. Defaults to None
:param int|None line_color: line color. Defaults to None
:param list ticksx: X axis ticks values
:param 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 = ticksx
self.ticksy = ticksy
self.ticksx = tuple(ticksx)
self.ticksy = tuple(ticksy)

self._pointer_index = plot._pointer_index

self._line_color = set_color(
plot._display,
Expand All @@ -67,26 +74,72 @@ def __init__(
)
plot._pointer_index += 1

max_x = max(x)
min_x = min(x)
max_y = max(y)
min_y = min(y)
if tick_pos:
self._tickposx = plot._tickheightx
self._tickposy = plot._tickheighty
else:
self._tickposx = 0
self._tickposy = 0

if rangex is None:
self.xmin = min_x - (abs(max_x - min_x) / 10)
self.xmax = max_x + (abs(max_x - min_x) / 10)
self.xmin = rangex[0]
self.xmax = rangex[1]
self.ymin = rangey[0]
self.ymax = rangey[1]

else:
self.xmin = min(rangex)
self.xmax = max(rangex)
self.draw_points(plot, x, y, fill)
if plot._showticks:
if plot._loggingfirst:
plot._loggingfirst = False
self._draw_ticks(plot)
plot._showticks = False

if rangey is None:
self.ymin = min_y - (abs(max_y - min_y) / 10)
self.ymax = max_y + (abs(max_y - min_y) / 10)
else:
self.ymin = min(rangey)
self.ymax = max(rangey)
def _plot_line(self, plot, index, xnorm, ynorm):
plot._display.line(
xnorm[index],
ynorm[index],
xnorm[index + 1],
ynorm[index + 1],
self._line_color,
)

def draw_points(self, plot: PLOT, x: list, y: list, fill: bool = False) -> 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, fill)

@staticmethod
def clear_plot(plot) -> None:
"""
Clears the plot area
"""

plot._display.rect(
plot._newxmin + 1 + plot._tickheightx,
plot._newymax + 1,
plot._buff_width - 2 - 2 * plot.padding - plot._tickheightx,
plot._buff_height - 2 - 2 * plot.padding - plot._tickheighty,
plot._background_color,
True,
)

def draw_new_lines(self, plot: PLOT, x: list, y: list, fill: bool = False) -> 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
"""
xnorm = tuple(
[
int(
Expand All @@ -108,31 +161,82 @@ def __init__(
]
)

for index, _ in enumerate(xnorm):
if index + 1 >= len(xnorm):
break
if y[index] >= self.ymax:
continue
if len(x) == 1:
plot._display.pixel(xnorm[0], ynorm[0], self._line_color)
else:
for index, _ in enumerate(xnorm):
if index + 1 >= len(xnorm):
break
if y[index] >= self.ymax:
continue

self._plot_line(plot, index, xnorm, ynorm)

if fill:
for index, _ in enumerate(xnorm):
plot._display.line(
xnorm[index],
ynorm[index],
xnorm[index],
plot._newymin,
self._line_color,
)

self._draw_plotline(plot, index, xnorm, ynorm)
def _draw_ticks(self, plot) -> None:
"""
Draw ticks in the plot area
if plot._showticks:
if plot._cartesianfirst:
plot._draw_ticks(x, y, self.ticksx, self.ticksy)
plot._cartesianfirst = False
plot._showticks = False
"""

def _plot_line(self, plot, index, xnorm, ynorm):
plot._display.line(
xnorm[index],
ynorm[index],
xnorm[index + 1],
ynorm[index + 1],
self._line_color,
ticksxnorm = tuple(
[
int(
plot.transform(
self.xmin, self.xmax, plot._newxmin, plot._newxmax, _
)
)
for _ in self.ticksx
]
)
ticksynorm = tuple(
[
int(
plot.transform(
self.ymin, self.ymax, plot._newymin, plot._newymax, _
)
)
for _ in self.ticksy
]
)

def update(self, plot):
"""
Update the plot with new data
"""
plot._display.fill(0)
for i, tick in enumerate(ticksxnorm):
plot._display.line(
tick,
plot._newymin,
tick,
plot._newymin - plot._tickheightx,
plot._tickcolor,
)
if plot._showtext:
plot.show_text(
"{:.{}f}".format(self.ticksx[i], plot._decimal_points),
tick,
plot._newymin,
ax="x",
)

for i, tick in enumerate(ticksynorm):
plot._display.line(
plot._newxmin,
tick,
plot._newxmin + plot._tickheighty,
tick,
plot._tickcolor,
)
if plot._showtext:
plot.show_text(
"{:.{}f}".format(self.ticksy[i], plot._decimal_points),
plot._newxmin,
tick,
ax="y",
)
Loading

0 comments on commit e21a381

Please sign in to comment.