-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinusoidal.py
60 lines (52 loc) · 1.76 KB
/
sinusoidal.py
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
#!/usr/bin/env python3
# ========================================================================
# sinusoidal.py
#
# Description:
#
# Author: Jim Ing
# Date: 2024-09-06
# ========================================================================
import time
import math
from config import sense
sense.clear()
# Define colors based on height
colors = [
(255, 0, 0), # Red for heights 0-1
(255, 255, 0), # Yellow for heights 2-3
(0, 255, 0), # Green for heights 4-5
(0, 0, 255) # Blue for heights 6-7
]
# Animation parameters
speed = 0.1
wave_amplitude = 3 # Amplitude of the wave (vertical height)
wave_frequency = 0.5 # Controls the frequency of the wave
offset = 0 # Starting phase offset
def get_color_for_height(y):
"""Return the appropriate color based on the pixel height."""
if y <= 1:
return colors[0] # Red
elif y <= 3:
return colors[1] # Yellow
elif y <= 5:
return colors[2] # Green
else:
return colors[3] # Blue
def draw_wave():
"""Draw a sinusoidal wave on the Sense HAT matrix with colors based on height."""
sense.clear() # Clear the display
for x in range(8):
# Calculate the y position using a sine wave formula
y = int(3.5 + wave_amplitude * math.sin(x * wave_frequency + offset))
# Get the color for the current y-coordinate
color = get_color_for_height(y)
# Set the pixel for the wave with the appropriate color
sense.set_pixel(x, y, color)
try:
while True:
draw_wave() # Draw the current frame of the wave
offset += 0.3 # Move the wave to the left by shifting the phase
time.sleep(speed) # Control the speed of the animation
except KeyboardInterrupt:
sense.clear() # Clear the LED matrix on exit