-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·164 lines (135 loc) · 5.73 KB
/
main.py
File metadata and controls
executable file
·164 lines (135 loc) · 5.73 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
import time
from led.led import Led
from effects.random_color import random_color_effect
from effects.rainbow_effect import rainbow_effect, vertical_rainbow_effect
from effects.audio_effect import AudioEffect
from effects.band_loop import band_loop_with_individual_band_colors
from effects.video_effect import capture_image, process_image, map_image_to_leds2
from effects.youtube_effect import YoutubeEffect
import numpy as np
import picamera
from config import BANDS, NUM_PIXELS
from audio.audio_processor import AudioProcessor
from rpi_ws281x import Color
from threading import Thread, Event
import signal
import time
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from threading import Thread, Event
import signal
import os
from pydantic import BaseModel
class EffectName(BaseModel):
effect: str
stop_event = Event()
audio_brightness_active = False # Track if AUDIO_BRIGHTNESS is active
def signal_handler(signum, frame):
print("Interrupt received, shutting down...")
stop_event.set()
def signal_handler(signum, frame):
print("Interrupt received, shutting down...")
stop_event.set()
signal.signal(signal.SIGINT, signal_handler)
current_effect = 'NONE'
def calculate_brightness(magnitude):
max_magnitude = 5000 # Maximum magnitude value
max_brightness = 255 # Maximum brightness value
return min(int((magnitude / max_magnitude) * max_brightness), 255)
def light_up_all_hexagons(led_strip, hexagon_ranges, color):
for hexagon_index in hexagon_ranges:
for led_index in hexagon_ranges[hexagon_index]:
led_strip.setPixelColor(led_index, color)
led_strip.show()
def audio_brightness_effect(strip, audio_processor):
while not stop_event.is_set():
global audio_brightness_active
if audio_brightness_active:
# Capture and process audio
freqs, magnitudes = audio_processor.process_audio()
# Calculate brightness based on the max magnitude
if magnitudes.size > 0:
max_magnitude = max(magnitudes)
brightness = calculate_brightness(max_magnitude)
strip.set_brightness(brightness)
time.sleep(0.01) # Sleep briefly to avoid CPU load
def main():
strip = Led()
hexagon_ranges = strip.create_hexagon_mappings_for_bands()
audio_processor = AudioProcessor()
camera = picamera.PiCamera()
camera.resolution = (32, 32)
frame_interval = 1.0 / 30
time.sleep(2) # Camera warm-up time
strip.turn_off_all_lights()
youtube_effect = YoutubeEffect(strip, (32, 32), './video/test.mp4')
youtube_effect2 = YoutubeEffect(strip, (32, 32), './video/test1.mp4')
youtube_effect3 = YoutubeEffect(strip, (32, 32), './video/test222.mp4')
audio_effect = AudioEffect(strip)
global audio_brightness_thread
# Create the audio processing thread
audio_brightness_thread = Thread(target=audio_brightness_effect, args=(strip, audio_processor))
audio_brightness_thread.start()
while not stop_event.is_set():
global current_effect
if current_effect == "RANDOM_COLOR":
random_color_effect(strip, hexagon_ranges, BANDS)
elif current_effect == "BAND_LOOP_INDIVIDUAL":
band_loop_with_individual_band_colors(strip, hexagon_ranges, BANDS)
elif current_effect == "RAINBOW":
rainbow_effect(strip, hexagon_ranges, BANDS, delay=0.1)
elif current_effect == "VERTICAL_RAINBOW":
vertical_rainbow_effect(strip, hexagon_ranges, BANDS)
elif current_effect == "YOUTUBE":
youtube_effect.process_frame()
elif current_effect == "YOUTUBE2":
youtube_effect2.process_frame()
elif current_effect == "YOUTUBE3":
youtube_effect3.process_frame()
elif current_effect == "AUDIO_REACTIVE":
# Capture and process audio
freqs, magnitudes = audio_processor.process_audio()
# If there's audio data, map it to LEDs
if freqs.size > 0 and magnitudes.size > 0:
audio_effect.map_audio_to_leds(freqs, magnitudes, hexagon_ranges)
elif current_effect == "VIDEO":
img = capture_image(camera)
processed_img = process_image(img)
map_image_to_leds2(processed_img, strip, BANDS, hexagon_ranges)
elif current_effect == "ALL_HEXAGONS":
light_up_all_hexagons(strip, hexagon_ranges, Color(255, 0, 255))
# Use interruptible sleep
stop_event.wait(timeout=0.01)
def set_effect(effect_name):
global current_effect
current_effect = effect_name
app = FastAPI()
templates = Jinja2Templates(directory="web")
stop_event = Event()
@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.post("/set_effect")
async def change_effect(effect: EffectName):
set_effect(effect.effect)
return {"message": f"Effect set to {effect.effect}"}
@app.post("/toggle_audio_brightness")
async def toggle_audio_brightness(request: Request):
global audio_brightness_active
audio_brightness_active = not audio_brightness_active # Toggle the state
return {"message": f"AUDIO_BRIGHTNESS {'activated' if audio_brightness_active else 'deactivated'}"}
if __name__ == "__main__":
disco_thread = Thread(target=main)
disco_thread.start()
try:
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=5000)
except KeyboardInterrupt:
print("Keyboard Interrupt, stopping...")
stop_event.set()
disco_thread.join(timeout=10)
print("Background thread stopped.")
finally:
print("Cleaning up and exiting.")
# Any additional cleanup code here