Skip to content

Commit 2a8c959

Browse files
add automatic scale property; update docs
1 parent dd5f861 commit 2a8c959

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

cedargrove_waveviz.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
class WaveViz(displayio.TileGrid):
3131
"""
3232
The WaveViz class creates a positionable ``displayio.TileGrid`` object
33-
from a ``synthio.ReadableBuffer`` wave table. The class inherits all
34-
properties of a ``TileGrid`` object including bitmap, pixel_shader, width,
33+
from a ``synthio.ReadableBuffer`` wave table. The class inherits the
34+
properties of a ``TileGrid`` object of bitmap, pixel_shader, width,
3535
height, x, y.
3636
3737
:param synthio.ReadableBuffer wave_table: The synthio waveform object of type 'h'
@@ -43,6 +43,8 @@ class WaveViz(displayio.TileGrid):
4343
:param integer plot_color: The waveform trace color. Defaults to 0x00FF00 (green).
4444
:param integer grid_color: The perimeter grid color. Defaults to 0x808080 (gray).
4545
:param integer back_color: The grid background color. Defaults to None (transparent).
46+
:param bool auto_scale: Automatically adjust resultant plot to the wave table's
47+
full-scale value. Defaults to True (auto scale enabled).
4648
"""
4749

4850
# pylint: disable=too-many-arguments
@@ -56,6 +58,7 @@ def __init__(
5658
plot_color=0x00FF00,
5759
grid_color=0x808080,
5860
back_color=None,
61+
auto_scale=True,
5962
):
6063
"""Instantiate the tile generator class."""
6164
self._wave_table = wave_table
@@ -64,6 +67,9 @@ def __init__(
6467
self._width = width
6568
self._height = height
6669
self._y_offset = self._height // 2
70+
self._auto_scale = auto_scale
71+
self._max_sample_value = 32767 # Maximum signed 16-bit value
72+
self._scale_y = 0 # Define for later use
6773

6874
self._palette = displayio.Palette(3)
6975
self._palette[1] = plot_color
@@ -79,15 +85,19 @@ def __init__(
7985
self._bmp.fill(0)
8086

8187
# Plot grid and wave table
82-
self._plot_grid() # Plot the grid
83-
self._plot_wave() # Plot the wave table
88+
self._update_plot()
8489
# Bitmap becomes a displayio.TileGrid object
8590
super().__init__(self._bmp, pixel_shader=self._palette, x=self._x, y=self._y)
8691

8792
@property
88-
def max_result(self):
89-
"""The full-scale value of the plotted image."""
90-
return self._max_sample_value
93+
def wave_table(self):
94+
"""The synthio waveform array object."""
95+
return self._wave_table
96+
97+
@wave_table.setter
98+
def wave_table(self, new_wave_table):
99+
self._wave_table = new_wave_table
100+
self._update_plot()
91101

92102
@property
93103
def width(self):
@@ -100,14 +110,24 @@ def height(self):
100110
return self._height
101111

102112
@property
103-
def wave_table(self):
104-
"""The synthio waveform array object."""
105-
return self._wave_table
113+
def auto_scale(self):
114+
"""Automatically adjust resultant plot to the wave table's
115+
full-scale value."""
116+
return self._auto_scale
106117

107-
@wave_table.setter
108-
def wave_table(self, new_wave_table):
109-
self._wave_table = new_wave_table
110-
# Instantiate the target bitmap
118+
@auto_scale.setter
119+
def auto_scale(self, new_auto_scale):
120+
self._auto_scale = new_auto_scale
121+
self._update_plot()
122+
123+
@property
124+
def max_result(self):
125+
"""The full-scale value of the plotted image."""
126+
return self._max_sample_value
127+
128+
def _update_plot(self):
129+
"""Clears the bitmap and plots the grid and waveform."""
130+
# Clear the target bitmap
111131
self._bmp.fill(0)
112132

113133
# Plot grid and wave table
@@ -134,7 +154,10 @@ def _plot_wave(self):
134154
# Calculate the y-axis scale factor and adjust y values
135155
self._max_sample_value = max(max(y_points), abs(min(y_points)))
136156
if self._max_sample_value != 0:
137-
self._scale_y = self._height / self._max_sample_value / 2
157+
if self._auto_scale:
158+
self._scale_y = self._height / self._max_sample_value / 2
159+
else:
160+
self._scale_y = self._height / 32767 / 2
138161
else:
139162
self._scale_y = 1
140163
for y in range(self._width):
1.62 KB
Binary file not shown.

media/waveviz_api_page1a.png

233 KB
Loading

0 commit comments

Comments
 (0)