-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathled_test_pattern.py
More file actions
executable file
·365 lines (311 loc) · 14.7 KB
/
led_test_pattern.py
File metadata and controls
executable file
·365 lines (311 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env python3
"""
LED Screen Test Pattern Generator for Raspberry Pi
Creates a colorful test pattern for 800x800 LED screen connected via HDMI
"""
import pygame
import sys
import math
import time
import os
from typing import Tuple
# Set environment variables for headless setup
# Try different video drivers in order of preference
video_drivers = ['kmsdrm', 'directfb', 'x11']
for driver in video_drivers:
try:
os.environ['SDL_VIDEODRIVER'] = driver
# Test if this driver works
pygame.init()
pygame.quit()
print(f"Using video driver: {driver}")
break
except:
continue
else:
# Fallback to default
os.environ['SDL_VIDEODRIVER'] = 'kmsdrm'
print("Using default video driver: kmsdrm")
# Initialize pygame with error handling
try:
pygame.init()
print("Pygame initialized successfully")
except pygame.error as e:
print(f"Pygame initialization failed: {e}")
print("Try the troubleshooting steps in README.md")
sys.exit(1)
# Screen dimensions - actual LED screen is 320x320
SCREEN_WIDTH = 320
SCREEN_HEIGHT = 320
SCREEN_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT)
# Display settings
DISPLAY_WIDTH = 800 # What the system reports
DISPLAY_HEIGHT = 800
DISPLAY_SIZE = (DISPLAY_WIDTH, DISPLAY_HEIGHT)
# Colors (RGB)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
GRAY = (128, 128, 128)
class LEDTestPattern:
def __init__(self):
# Initialize display at full resolution (800x800)
try:
self.screen = pygame.display.set_mode(DISPLAY_SIZE, pygame.FULLSCREEN)
print(f"Display initialized at {DISPLAY_SIZE}")
except pygame.error as e:
print(f"Failed to initialize display: {e}")
sys.exit(1)
# Create a surface for the LED screen content (320x320)
self.led_surface = pygame.Surface(SCREEN_SIZE)
# LED screen position on the larger display
# Try different corners to find the right one
self.led_positions = [
(0, 0), # Top-left
(DISPLAY_WIDTH - SCREEN_WIDTH, 0), # Top-right
(0, DISPLAY_HEIGHT - SCREEN_HEIGHT), # Bottom-left
(DISPLAY_WIDTH - SCREEN_WIDTH, DISPLAY_HEIGHT - SCREEN_HEIGHT), # Bottom-right
((DISPLAY_WIDTH - SCREEN_WIDTH) // 2, (DISPLAY_HEIGHT - SCREEN_HEIGHT) // 2), # Center
]
self.current_led_position = 0
self.led_x, self.led_y = self.led_positions[self.current_led_position]
# Rotation settings
self.rotation_angles = [0, 90, 180, 270]
self.current_rotation = 2 # Start with 180 degrees (upside down)
pygame.display.set_caption("LED Test Pattern - 320x320 on 800x800")
# Initialize font for text patterns
try:
self.font_large = pygame.font.Font(None, 48)
self.font_medium = pygame.font.Font(None, 32)
self.font_small = pygame.font.Font(None, 24)
except:
self.font_large = pygame.font.SysFont('Arial', 48)
self.font_medium = pygame.font.SysFont('Arial', 32)
self.font_small = pygame.font.SysFont('Arial', 24)
# Hide the cursor for cleaner LED display
pygame.mouse.set_visible(False)
self.clock = pygame.time.Clock()
self.running = True
print(f"LED screen positioned at ({self.led_x}, {self.led_y})")
print("Press 'P' to cycle through LED screen positions")
def draw_color_bars(self):
"""Draw horizontal color bars"""
bar_height = SCREEN_HEIGHT // 8
colors = [RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE, BLACK]
for i, color in enumerate(colors):
y_start = i * bar_height
pygame.draw.rect(self.led_surface, color, (0, y_start, SCREEN_WIDTH, bar_height))
def draw_grid_pattern(self):
"""Draw a grid pattern"""
grid_size = 20 # 16x16 grid for 320x320
for x in range(0, SCREEN_WIDTH, grid_size):
pygame.draw.line(self.led_surface, WHITE, (x, 0), (x, SCREEN_HEIGHT), 1)
for y in range(0, SCREEN_HEIGHT, grid_size):
pygame.draw.line(self.led_surface, WHITE, (0, y), (SCREEN_WIDTH, y), 1)
def draw_gradient_circle(self):
"""Draw a radial gradient circle"""
center_x, center_y = SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2
max_radius = min(SCREEN_WIDTH, SCREEN_HEIGHT) // 2
for radius in range(max_radius, 0, -2):
# Create gradient from red to blue
intensity = int(255 * (1 - radius / max_radius))
color = (intensity, 0, 255 - intensity)
pygame.draw.circle(self.led_surface, color, (center_x, center_y), radius)
def draw_checkerboard(self):
"""Draw a checkerboard pattern"""
square_size = 20 # Smaller squares for 320x320
for x in range(0, SCREEN_WIDTH, square_size):
for y in range(0, SCREEN_HEIGHT, square_size):
if (x // square_size + y // square_size) % 2 == 0:
pygame.draw.rect(self.led_surface, WHITE, (x, y, square_size, square_size))
else:
pygame.draw.rect(self.led_surface, BLACK, (x, y, square_size, square_size))
def draw_concentric_circles(self):
"""Draw concentric circles"""
center_x, center_y = SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2
colors = [RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA]
for i in range(6):
radius = (i + 1) * 25 # Smaller circles for 320x320
color = colors[i % len(colors)]
pygame.draw.circle(self.led_surface, color, (center_x, center_y), radius, 3)
def draw_text_orientation(self):
"""Draw text patterns to show orientation"""
# Clear background
self.led_surface.fill(BLACK)
# Top text
top_text = self.font_large.render("TOP", True, WHITE)
top_rect = top_text.get_rect(center=(SCREEN_WIDTH//2, 30))
self.led_surface.blit(top_text, top_rect)
# Bottom text
bottom_text = self.font_large.render("BOTTOM", True, WHITE)
bottom_rect = bottom_text.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT-30))
self.led_surface.blit(bottom_text, bottom_rect)
# Left text
left_text = self.font_medium.render("LEFT", True, WHITE)
left_text = pygame.transform.rotate(left_text, 90)
left_rect = left_text.get_rect(center=(30, SCREEN_HEIGHT//2))
self.led_surface.blit(left_text, left_rect)
# Right text
right_text = self.font_medium.render("RIGHT", True, WHITE)
right_text = pygame.transform.rotate(right_text, 90)
right_rect = right_text.get_rect(center=(SCREEN_WIDTH-30, SCREEN_HEIGHT//2))
self.led_surface.blit(right_text, right_rect)
# Center text with rotation info
center_text = self.font_small.render(f"ROT: {self.rotation_angles[self.current_rotation]}°", True, YELLOW)
center_rect = center_text.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2))
self.led_surface.blit(center_text, center_rect)
def draw_arrow_pattern(self):
"""Draw arrows pointing in different directions"""
self.led_surface.fill(BLACK)
# Draw arrows pointing in cardinal directions
arrow_size = 20
# Up arrow (top)
pygame.draw.polygon(self.led_surface, RED, [
(SCREEN_WIDTH//2, 50),
(SCREEN_WIDTH//2 - arrow_size, 50 + arrow_size),
(SCREEN_WIDTH//2 + arrow_size, 50 + arrow_size)
])
# Down arrow (bottom)
pygame.draw.polygon(self.led_surface, GREEN, [
(SCREEN_WIDTH//2, SCREEN_HEIGHT - 50),
(SCREEN_WIDTH//2 - arrow_size, SCREEN_HEIGHT - 50 - arrow_size),
(SCREEN_WIDTH//2 + arrow_size, SCREEN_HEIGHT - 50 - arrow_size)
])
# Left arrow
pygame.draw.polygon(self.led_surface, BLUE, [
(50, SCREEN_HEIGHT//2),
(50 + arrow_size, SCREEN_HEIGHT//2 - arrow_size),
(50 + arrow_size, SCREEN_HEIGHT//2 + arrow_size)
])
# Right arrow
pygame.draw.polygon(self.led_surface, YELLOW, [
(SCREEN_WIDTH - 50, SCREEN_HEIGHT//2),
(SCREEN_WIDTH - 50 - arrow_size, SCREEN_HEIGHT//2 - arrow_size),
(SCREEN_WIDTH - 50 - arrow_size, SCREEN_HEIGHT//2 + arrow_size)
])
# Center text
center_text = self.font_small.render("ARROWS", True, WHITE)
center_rect = center_text.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2))
self.led_surface.blit(center_text, center_rect)
def draw_test_pattern(self, pattern_type: str):
"""Draw the specified test pattern"""
# Clear the LED surface
self.led_surface.fill(BLACK)
if pattern_type == "color_bars":
self.draw_color_bars()
elif pattern_type == "grid":
self.draw_grid_pattern()
elif pattern_type == "gradient":
self.draw_gradient_circle()
elif pattern_type == "checkerboard":
self.draw_checkerboard()
elif pattern_type == "circles":
self.draw_concentric_circles()
elif pattern_type == "text_orientation":
self.draw_text_orientation()
elif pattern_type == "arrows":
self.draw_arrow_pattern()
elif pattern_type == "all":
# Draw multiple patterns in quadrants
# Top-left: Color bars
bar_height = SCREEN_HEIGHT // 4
bar_width = SCREEN_WIDTH // 4
colors = [RED, GREEN, BLUE, YELLOW]
for i, color in enumerate(colors):
y_start = i * (bar_height // 4)
pygame.draw.rect(self.led_surface, color, (0, y_start, bar_width, bar_height // 4))
# Top-right: Grid
grid_size = 10
for x in range(SCREEN_WIDTH // 4, SCREEN_WIDTH, grid_size):
pygame.draw.line(self.led_surface, WHITE, (x, 0), (x, SCREEN_HEIGHT // 4), 1)
for y in range(0, SCREEN_HEIGHT // 4, grid_size):
pygame.draw.line(self.led_surface, WHITE, (SCREEN_WIDTH // 4, y), (SCREEN_WIDTH, y), 1)
# Bottom-left: Checkerboard
square_size = 12
for x in range(0, SCREEN_WIDTH // 4, square_size):
for y in range(SCREEN_HEIGHT // 4, SCREEN_HEIGHT // 2, square_size):
if (x // square_size + y // square_size) % 2 == 0:
pygame.draw.rect(self.led_surface, WHITE, (x, y, square_size, square_size))
else:
pygame.draw.rect(self.led_surface, BLACK, (x, y, square_size, square_size))
# Bottom-right: Gradient circle
center_x = SCREEN_WIDTH - SCREEN_WIDTH // 8
center_y = SCREEN_HEIGHT - SCREEN_HEIGHT // 8
max_radius = SCREEN_WIDTH // 8
for radius in range(max_radius, 0, -2):
intensity = int(255 * (1 - radius / max_radius))
color = (intensity, 0, 255 - intensity)
pygame.draw.circle(self.led_surface, color, (center_x, center_y), radius)
# Clear the main screen
self.screen.fill(BLACK)
# Rotate the LED surface based on current rotation setting
rotated_surface = pygame.transform.rotate(self.led_surface, self.rotation_angles[self.current_rotation])
# Blit the LED content to the main screen at the LED position
self.screen.blit(rotated_surface, (self.led_x, self.led_y))
def run(self):
"""Main loop"""
patterns = ["text_orientation", "arrows", "color_bars", "grid", "gradient", "checkerboard", "circles", "all"]
pattern_index = 0
last_pattern_change = time.time()
pattern_duration = 5 # seconds per pattern
print("LED Test Pattern Generator - 320x320 LED Screen")
print("Controls:")
print(" ESC - Exit")
print(" SPACE - Cycle patterns")
print(" P - Cycle LED screen positions")
print(" R - Cycle rotation (0°, 90°, 180°, 270°)")
print("Patterns: text_orientation, arrows, color_bars, grid, gradient, checkerboard, circles, all")
print(f"Current pattern: {patterns[pattern_index]}")
print(f"LED position: {self.led_positions[self.current_led_position]}")
print(f"Current rotation: {self.rotation_angles[self.current_rotation]}°")
while self.running:
current_time = time.time()
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.running = False
elif event.key == pygame.K_SPACE:
pattern_index = (pattern_index + 1) % len(patterns)
print(f"Switched to pattern: {patterns[pattern_index]}")
elif event.key == pygame.K_p:
self.current_led_position = (self.current_led_position + 1) % len(self.led_positions)
self.led_x, self.led_y = self.led_positions[self.current_led_position]
print(f"LED position: {self.led_positions[self.current_led_position]}")
elif event.key == pygame.K_r:
self.current_rotation = (self.current_rotation + 1) % len(self.rotation_angles)
print(f"Rotation: {self.rotation_angles[self.current_rotation]}°")
# Auto-cycle patterns
if current_time - last_pattern_change >= pattern_duration:
pattern_index = (pattern_index + 1) % len(patterns)
last_pattern_change = current_time
print(f"Auto-switched to pattern: {patterns[pattern_index]}")
# Draw current pattern
self.draw_test_pattern(patterns[pattern_index])
# Update display
pygame.display.flip()
self.clock.tick(60) # 60 FPS
pygame.quit()
sys.exit()
def main():
"""Main function"""
try:
test_pattern = LEDTestPattern()
test_pattern.run()
except KeyboardInterrupt:
print("\nExiting...")
pygame.quit()
sys.exit()
except Exception as e:
print(f"Error: {e}")
pygame.quit()
sys.exit(1)
if __name__ == "__main__":
main()