Skip to content

Commit

Permalink
class inherits TileGrid properties; update simpletest
Browse files Browse the repository at this point in the history
  • Loading branch information
CedarGroveStudios committed Jan 27, 2024
1 parent 66ca81f commit eb64d90
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 47 deletions.
77 changes: 36 additions & 41 deletions cedargrove_waveviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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,
)

Expand All @@ -135,7 +130,7 @@ def _plot_grid(self):
self._bmp,
0,
self._y_offset,
self._size[0],
self._width,
self._y_offset,
2,
)
10 changes: 4 additions & 6 deletions examples/waveviz_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit eb64d90

Please sign in to comment.