Skip to content

Commit

Permalink
adding logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jposada202020 committed Mar 9, 2023
1 parent e34d825 commit e77597e
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 1 deletion.
152 changes: 152 additions & 0 deletions circuitpython_uplot/ulogging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT

"""
`ulogging`
================================================================================
CircuitPython logging data graph
* Author(s): Jose D. Montoya
"""
try:
from typing import Union
from circuitpython_uplot.uplot import Uplot
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"


# pylint: disable=too-many-arguments, invalid-name, no-self-use, too-few-public-methods
# pylint: disable=too-many-locals, too-many-branches, protected-access
class ulogging:
"""
Class to log data
"""

def __init__(
self,
plot: Uplot,
x: Union[list, np.linspace, np.ndarray],
y: Union[list, np.linspace, np.ndarray],
rangex: list,
rangey: list,
line_color: int = 0xFFFFFF,
ticksx: np.array = np.array([0, 10, 30, 50, 70, 90]),
ticksy: np.array = np.array([0, 10, 30, 50, 70, 90]),
) -> None:
"""
:param Uplot 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
"""
self.points = []
self.ticksx = np.array(ticksx)
self.ticksy = np.array(ticksy)

plot._plot_palette[plot._index_colorused] = line_color

self.xmin = rangex[0]
self.xmax = rangex[1]
self.ymin = rangey[0]
self.ymax = rangey[1]

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

fill_region(
plot._plotbitmap,
plot._newxmin + plot._tickheightx + 1,
plot._newymax + 1,
plot._newxmax - 1,
plot._newymin - plot._tickheighty,
0,
)

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],
plot._index_colorused,
)
if plot._showticks:
if plot._loggingfirst:
self._draw_ticks(plot)
plot._loggingfirst = False
plot._showticks = False

def _draw_ticks(self, plot) -> None:
"""
Draw ticks in the plot area
"""

ticksxnorm = np.array(
plot.transform(
self.xmin, self.xmax, plot._newxmin, plot._newxmax, self.ticksx
),
dtype=np.int16,
)
ticksynorm = np.array(
plot.transform(
self.ymin, self.ymax, plot._newymin, plot._newymax, self.ticksy
),
dtype=np.int16,
)

for i, tick in enumerate(ticksxnorm):
draw_line(
plot._plotbitmap,
tick,
plot._newymin,
tick,
plot._newymin - plot._tickheightx,
2,
)
if plot._showtext:
plot.show_text(
"{:.2f}".format(ticksxnorm[i]), tick, plot._newymin, (0.5, 0.0)
)
for i, tick in enumerate(ticksynorm):
draw_line(
plot._plotbitmap,
plot._newxmin,
tick,
plot._newxmin + plot._tickheighty,
tick,
2,
)
if plot._showtext:
plot.show_text(
"{:.2f}".format(ticksynorm[i]), plot._newxmin, tick, (1.0, 0.5)
)
1 change: 1 addition & 0 deletions circuitpython_uplot/uplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def __init__(
self._newymax = padding

self._cartesianfirst = True
self._loggingfirst = True

self._showtext = False

Expand Down
3 changes: 3 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@

.. automodule:: circuitpython_uplot.umap
:members:

.. automodule:: circuitpython_uplot.ulogging
:members:
11 changes: 10 additions & 1 deletion docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,18 @@ Sparkline animation example
Cartesian Animation Example
---------------------------

Casrtesian animation example
Cartesian animation example

.. literalinclude:: ../examples/uplot_ucartesian_loggin_data.py
:caption: examples/uplot_ucartesian_loggin_data.py
:linenos:
.. image:: ../docs/uplot_cartesian.gif

Logging Example
---------------------------

Logging example

.. literalinclude:: ../examples/uplot_ulogging.py
:caption: examples/uplot_ulogging.py
:linenos:
75 changes: 75 additions & 0 deletions examples/uplot_ulogging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# SPDX-FileCopyrightText: Copyright (c) Jose D. Montoya
#
# SPDX-License-Identifier: MIT

import displayio
import terminalio
import board
from adafruit_display_text import label
from circuitpython_uplot.uplot import Uplot, color
from circuitpython_uplot.ulogging import ulogging

# Setting up the display
display = board.DISPLAY

plot = Uplot(0, 0, display.width, display.height)
g = displayio.Group()

DISPLAY_WIDTH = 200
DISPLAY_HEIGHT = 200
FOREGROUND_COLOR = color.BLACK
BACKGROUND_COLOR = color.WHITE

background_bitmap = displayio.Bitmap(DISPLAY_WIDTH, DISPLAY_HEIGHT, 1)
# Map colors in a palette
palette = displayio.Palette(1)
palette[0] = BACKGROUND_COLOR
# Create a Tilegrid with the background and put in the displayio group
t = displayio.TileGrid(background_bitmap, pixel_shader=palette)
g.append(t)

text_temperature = label.Label(terminalio.FONT, color=FOREGROUND_COLOR, scale=3)
text_temperature.anchor_point = 0.0, 0.0
text_temperature.anchored_position = 25, 0
g.append(text_temperature)

text_humidity = label.Label(terminalio.FONT, color=FOREGROUND_COLOR, scale=3)
text_humidity.anchor_point = 0.0, 0.0
text_humidity.anchored_position = 130, 0
g.append(text_humidity)

plot_1 = Uplot(
0,
50,
200,
60,
padding=1,
show_box=True,
box_color=color.BLACK,
background_color=color.WHITE,
)

plot_1.tick_params(
tickx_height=4, ticky_height=4, show_ticks=True, tickcolor=color.BLACK
)

x = [10, 20, 30, 40, 50]
temp_y = [26, 25, 24, 23, 28]

g.append(plot_1)

display.show(g)
display.refresh()

dist = 3

ulogging(
plot_1,
x[0:dist],
temp_y[0:dist],
rangex=[0, 200],
rangey=[0, 100],
line_color=color.BLACK,
ticksx=[10, 50, 80, 100],
ticksy=[15, 30, 45, 60],
)

0 comments on commit e77597e

Please sign in to comment.