|
| 1 | +#!/usr/bin/python3 |
| 2 | +# SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: MIT |
| 5 | +""" |
| 6 | +Display a simple test pattern of 3 shapes on a single 64x32 matrix panel. |
| 7 | +
|
| 8 | +Run like this: |
| 9 | +
|
| 10 | +$ python simpletest.py |
| 11 | +
|
| 12 | +""" |
| 13 | +import adafruit_raspberry_pi5_piomatter |
| 14 | +import numpy as np |
| 15 | +import rainbowio |
| 16 | +from PIL import Image, ImageDraw |
| 17 | + |
| 18 | +width = 64 |
| 19 | +height = 32 |
| 20 | +pen_radius = 1 |
| 21 | + |
| 22 | + |
| 23 | +canvas = Image.new('RGB', (width, height), (0, 0, 0)) |
| 24 | +draw = ImageDraw.Draw(canvas) |
| 25 | + |
| 26 | +geometry = adafruit_raspberry_pi5_piomatter.Geometry(width=width, height=height, n_addr_lines=4, |
| 27 | + rotation=adafruit_raspberry_pi5_piomatter.Orientation.Normal) |
| 28 | +framebuffer = np.asarray(canvas) + 0 # Make a mutable copy |
| 29 | +matrix = adafruit_raspberry_pi5_piomatter.AdafruitMatrixBonnetRGB888Packed(framebuffer, geometry) |
| 30 | + |
| 31 | +color_index = 0 |
| 32 | + |
| 33 | +def update_matrix(): |
| 34 | + framebuffer[:] = np.asarray(canvas) |
| 35 | + matrix.show() |
| 36 | + |
| 37 | +def darken_color(hex_color, darkness_factor): |
| 38 | + # Convert hex color number to RGB |
| 39 | + r = (hex_color >> 16) & 0xFF |
| 40 | + g = (hex_color >> 8) & 0xFF |
| 41 | + b = hex_color & 0xFF |
| 42 | + |
| 43 | + # Apply darkness factor |
| 44 | + r = int(r * (1 - darkness_factor)) |
| 45 | + g = int(g * (1 - darkness_factor)) |
| 46 | + b = int(b * (1 - darkness_factor)) |
| 47 | + |
| 48 | + # Ensure values are within the valid range |
| 49 | + r = max(0, min(255, r)) |
| 50 | + g = max(0, min(255, g)) |
| 51 | + b = max(0, min(255, b)) |
| 52 | + |
| 53 | + # Convert RGB back to hex number |
| 54 | + darkened_hex_color = (r << 16) + (g << 8) + b |
| 55 | + |
| 56 | + return darkened_hex_color |
| 57 | + |
| 58 | +step_count = 4 |
| 59 | +darkness_factor = 0.5 |
| 60 | + |
| 61 | +clearing = False |
| 62 | + |
| 63 | +try: |
| 64 | + # step_down_size = pen_radius * 2 + 2 |
| 65 | + |
| 66 | + while True: |
| 67 | + for step in range(step_count): |
| 68 | + step_down_size = step * (pen_radius* 2) + (2 * step) |
| 69 | + for x in range(pen_radius + step_down_size, width - pen_radius - step_down_size - 1): |
| 70 | + color_index = (color_index + 2) % 256 |
| 71 | + color = darken_color(rainbowio.colorwheel(color_index), darkness_factor) if not clearing else 0x000000 |
| 72 | + draw.circle((x, pen_radius + step_down_size), pen_radius, color) |
| 73 | + update_matrix() |
| 74 | + for y in range(pen_radius + step_down_size, height - pen_radius - step_down_size - 1): |
| 75 | + color_index = (color_index + 2) % 256 |
| 76 | + color = darken_color(rainbowio.colorwheel(color_index), darkness_factor) if not clearing else 0x000000 |
| 77 | + draw.circle((width - pen_radius - step_down_size -1, y), pen_radius, color) |
| 78 | + update_matrix() |
| 79 | + for x in range(width - pen_radius - step_down_size - 1, pen_radius + step_down_size, -1): |
| 80 | + color_index = (color_index + 2) % 256 |
| 81 | + color = darken_color(rainbowio.colorwheel(color_index), darkness_factor) if not clearing else 0x000000 |
| 82 | + draw.circle((x, height - pen_radius - step_down_size - 1), pen_radius, color) |
| 83 | + update_matrix() |
| 84 | + for y in range(height - pen_radius - step_down_size - 1, pen_radius + ((step+1) * (pen_radius* 2) + (2 * (step+1))) -1, -1): |
| 85 | + color_index = (color_index + 2) % 256 |
| 86 | + color = darken_color(rainbowio.colorwheel(color_index), darkness_factor) if not clearing else 0x000000 |
| 87 | + draw.circle((pen_radius + step_down_size, y), pen_radius, color) |
| 88 | + update_matrix() |
| 89 | + |
| 90 | + if step != step_count-1: |
| 91 | + # connect to next iter |
| 92 | + for x in range(pen_radius + step_down_size, pen_radius + ((step+1) * (pen_radius* 2) + (2 * (step+1)))): |
| 93 | + color_index = (color_index + 2) % 256 |
| 94 | + color = darken_color(rainbowio.colorwheel(color_index), |
| 95 | + darkness_factor) if not clearing else 0x000000 |
| 96 | + draw.circle((x, pen_radius + ((step+1) * (pen_radius* 2) + (2 * (step+1)))), pen_radius, color) |
| 97 | + update_matrix() |
| 98 | + |
| 99 | + clearing = not clearing |
| 100 | + |
| 101 | +except KeyboardInterrupt: |
| 102 | + print("Exiting") |
0 commit comments