From eb64d90af37098287255139eae9361111aeac98b Mon Sep 17 00:00:00 2001 From: Cedar Grove Maker Studios Date: Sat, 27 Jan 2024 12:00:15 -0800 Subject: [PATCH] class inherits TileGrid properties; update simpletest --- cedargrove_waveviz.py | 77 ++++++++++++++++------------------ examples/waveviz_simpletest.py | 10 ++--- 2 files changed, 40 insertions(+), 47 deletions(-) diff --git a/cedargrove_waveviz.py b/cedargrove_waveviz.py index 72486cd..7d15bed 100644 --- a/cedargrove_waveviz.py +++ b/cedargrove_waveviz.py @@ -4,17 +4,19 @@ """ `cedargrove_waveviz` =============================================================================== -A CircuitPython class to create a positionable ``displayio.Group`` object -widget from a ``synthio.ReadableBuffer`` wave table. The class also makes -the underlying bitmap object available. +A CircuitPython class to create a positionable ``displayio.TileGrid`` object +from a ``synthio.ReadableBuffer`` wave table. The class inherits all +properties of a ``TileGrid`` object including bitmap, pixel_shader, width, +height, tile_width, tile_height, x, y. + https://github.com/CedarGroveStudios/CircuitPython_WaveViz +https://docs.circuitpython.org/en/latest/shared-bindings/displayio/#displayio.TileGrid * Author(s): JG for Cedar Grove Maker Studios Implementation Notes -------------------- **Software and Dependencies:** -* ulab for CircuitPython * Adafruit CircuitPython firmware for the supported boards: https://circuitpython.org/downloads """ @@ -24,40 +26,44 @@ import bitmaptools -class WaveViz(displayio.Group): +# pylint: disable=too-few-public-methods +class WaveViz(displayio.TileGrid): """ - The WaveViz class creates a displayio.Group from a composite - ``synthio`` waveform table. The group is created from size and color parameters. + The WaveViz class creates a positionable ``displayio.TileGrid`` object + from a ``synthio.ReadableBuffer`` wave table. The class inherits all + properties of a ``TileGrid`` object including bitmap, pixel_shader, width, + height, tile_width, tile_height, x, y. :param synthio.ReadableBuffer wave_table: The synthio waveform object of type 'h' (signed 16-bit). No default. - :param tuple origin: The group's origin coordinate integer tuple value (x, y). The - origin is the top left corner of the resultant group. No default. - :param tuple size: The group size integer tuple value (width, height) in pixels. - No default. + :param int x: The tile grid's x-axis coordinate value. No default. + :param int y: The tile grid's y-axis coordinate value. No default. + :param int width: The tile grid's width in pixels. No default. + :param int height: The tile grid's height in pixels. No default. :param integer plot_color: The waveform trace color. Defaults to 0x00FF00 (green). :param integer grid_color: The perimeter grid color. Defaults to 0x808080 (gray). :param integer back_color: The grid background color. Defaults to None (transparent). - :param integer scale: The group scale factor. Defaults to 1. """ # pylint: disable=too-many-arguments def __init__( self, wave_table, - origin, - size, + x, + y, + width, + height, plot_color=0x00FF00, grid_color=0x808080, back_color=None, - scale=1, ): """Instantiate the tile generator class.""" self._wave_table = wave_table - self._origin = origin - self._size = size - self._scale = scale - self._y_offset = self._size[1] // 2 + self._x = x + self._y = y + self._width = width + self._height = height + self._y_offset = self._height // 2 self._palette = displayio.Palette(3) self._palette[1] = plot_color @@ -69,25 +75,14 @@ def __init__( self._palette[0] = back_color # Instantiate the target bitmap - self._bmp = displayio.Bitmap(self._size[0], self._size[1], len(self._palette)) + self._bmp = displayio.Bitmap(self._width, self._height, len(self._palette)) self._bmp.fill(0) - # self becomes a displayio.Group; plot grid and wave table - super().__init__(scale=self._scale, x=self._origin[0], y=self._origin[1]) + # Plot grid and wave table self._plot_grid() # Plot the grid self._plot_wave() # Plot the wave table - self._tile_grid = displayio.TileGrid(self._bmp, pixel_shader=self._palette) - self.append(self._tile_grid) - - @property - def bitmap(self): - """The resultant bitmap object.""" - return self._bmp - - @property - def palette(self): - """The resultant displayio.Palette object.""" - return self._palette + # Bitmap becomes a displayio.TileGrid object + super().__init__(self._bmp, pixel_shader=self._palette, x=self._x, y=self._y) def _plot_wave(self): """Plot the wave_table as a bitmap. Extract samples from the wave @@ -98,17 +93,17 @@ def _plot_wave(self): # Create and fill the polygon arrays x_points = array("h", []) y_points = array("h", []) - for x in range(self._size[0]): + for x in range(self._width): x_points.append(x) - table_idx = int(x * (samples / self._size[0])) + table_idx = int(x * (samples / self._width)) y_points.append(self._wave_table[table_idx]) # Update the final point y_points[-1] = self._wave_table[-1] # Calculate the y-axis scale factor and adjust y values max_sample_value = max(y_points) - scale_y = self._size[1] / max_sample_value / 2 - for y in range(self._size[0]): + scale_y = self._height / max_sample_value / 2 + for y in range(self._width): y_points[y] = self._y_offset + int(y_points[y] * scale_y) # Draw the values as an open polygon @@ -125,8 +120,8 @@ def _plot_grid(self): # Draw the outer box bitmaptools.draw_polygon( self._bmp, - array("h", [0, self._size[0] - 1, self._size[0] - 1, 0]), - array("h", [0, 0, self._size[1] - 1, self._size[1] - 1]), + array("h", [0, self._width - 1, self._width - 1, 0]), + array("h", [0, 0, self._height - 1, self._height - 1]), 2, ) @@ -135,7 +130,7 @@ def _plot_grid(self): self._bmp, 0, self._y_offset, - self._size[0], + self._width, self._y_offset, 2, ) diff --git a/examples/waveviz_simpletest.py b/examples/waveviz_simpletest.py index 6ea116b..bd72618 100644 --- a/examples/waveviz_simpletest.py +++ b/examples/waveviz_simpletest.py @@ -7,10 +7,6 @@ from cedargrove_wavebuilder import WaveBuilder, WaveShape from cedargrove_waveviz import WaveViz -# Define size and offset for display plot window -PLOT_SIZE = (300, 240) # The plot window (width, height) in pixels -PLOT_OFFSET = (0, 0) # Left x-axis origin point (x, y) - # Define wave table parameters WAVE_TABLE_LENGTH = 512 # The wave table length in samples SAMPLE_MAXIMUM = 32700 # The maximum value of a sample @@ -49,11 +45,13 @@ ) # Display a small version on the bottom layer -splash.append(WaveViz(wave.wave_table, (20, 80), (25, 25), back_color=0x0000A0)) +splash.append( + WaveViz(wave.wave_table, x=20, y=80, width=25, height=25, back_color=0x0000A0) +) # Display a full-sized version on the top layer splash.append( - WaveViz(wave.wave_table, PLOT_OFFSET, PLOT_SIZE, back_color=None, scale=1) + WaveViz(wave.wave_table, x=0, y=0, width=300, height=240, back_color=None) ) while True: