Skip to content

Commit

Permalink
improve memory usage and simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
CedarGroveStudios committed Jan 23, 2024
1 parent b1c03bc commit 66ca81f
Showing 1 changed file with 28 additions and 64 deletions.
92 changes: 28 additions & 64 deletions cedargrove_waveviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
https://circuitpython.org/downloads
"""

from array import array
import displayio
import bitmaptools

Expand Down Expand Up @@ -56,6 +57,7 @@ def __init__(
self._origin = origin
self._size = size
self._scale = scale
self._y_offset = self._size[1] // 2

self._palette = displayio.Palette(3)
self._palette[1] = plot_color
Expand Down Expand Up @@ -93,85 +95,47 @@ def _plot_wave(self):
determined from the extracted sample values."""
samples = len(self._wave_table) # Samples in wave table

# Detect maximum value of extracted values and calculate scale factor
max_sample_value = 0
# Create and fill the polygon arrays
x_points = array("h", [])
y_points = array("h", [])
for x in range(self._size[0]):
x_points.append(x)
table_idx = int(x * (samples / self._size[0]))
max_sample_value = max(
abs(min(max_sample_value, self._wave_table[table_idx])),
abs(max(max_sample_value, self._wave_table[table_idx])),
)
scale_y = self._size[1] / max_sample_value / 2
y_points.append(self._wave_table[table_idx])
# Update the final point
y_points[-1] = self._wave_table[-1]

self._prev_point = (0, 0) # (display x index, wave_table index)
# 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]):
y_points[y] = self._y_offset + int(y_points[y] * scale_y)

for x in range(0, self._size[0]):
table_idx = int(x * (samples / self._size[0]))
self._next_point = (x, table_idx)

bitmaptools.draw_line(
self._bmp,
self._prev_point[0],
(self._size[1] // 2)
+ (-int(self._wave_table[self._prev_point[1]] * scale_y)),
self._next_point[0],
(self._size[1] // 2)
+ (-int(self._wave_table[self._next_point[1]] * scale_y)),
1,
)

self._prev_point = self._next_point

# Always plot the final point
bitmaptools.draw_line(
# Draw the values as an open polygon
bitmaptools.draw_polygon(
self._bmp,
self._prev_point[0],
(self._size[1] // 2)
+ (-int(self._wave_table[self._prev_point[1]] * scale_y)),
self._next_point[0],
(self._size[1] // 2) + (-int(self._wave_table[-1] * scale_y)),
x_points,
y_points,
1,
False,
)

def _plot_grid(self):
"""Plot the grid lines as a bitmap."""
bitmaptools.draw_line(
self._bmp,
0,
0,
self._size[0] - 1,
0,
2,
)
bitmaptools.draw_line(
self._bmp,
self._size[0] - 1,
0,
self._size[0] - 1,
self._size[1] - 1,
2,
)
bitmaptools.draw_line(
"""Plot the grid lines."""
# Draw the outer box
bitmaptools.draw_polygon(
self._bmp,
self._size[0] - 1,
self._size[1] - 1,
0,
self._size[1] - 1,
2,
)
bitmaptools.draw_line(
self._bmp,
0,
self._size[1] - 1,
0,
0,
array("h", [0, self._size[0] - 1, self._size[0] - 1, 0]),
array("h", [0, 0, self._size[1] - 1, self._size[1] - 1]),
2,
)

# Draw x-axis line
bitmaptools.draw_line(
self._bmp,
0,
self._size[1] // 2,
self._y_offset,
self._size[0],
self._size[1] // 2,
self._y_offset,
2,
)

0 comments on commit 66ca81f

Please sign in to comment.