midi: init read of CCs on startup (delay, brightness, RGB, beat enable); track bpm/pattern for GUI\nmain: integrate MidiHandler; add status panel for delay/brightness/RGB/pattern/BPM

This commit is contained in:
2025-09-14 05:23:46 +12:00
parent 9ff38aa875
commit 1da2e30d4c
2 changed files with 115 additions and 685 deletions

View File

@@ -4,6 +4,7 @@ import networking
import socket
import json
import logging # Added logging import
import time # Added for initial state read
# Configure logging
DEBUG_MODE = True # Set to False for INFO level logging
@@ -29,6 +30,19 @@ class MidiHandler:
self.beat_sending_enabled = True # New: Local flag for beat sending
self.sound_control_host = SOUND_CONTROL_HOST
self.sound_control_port = SOUND_CONTROL_PORT
# RGB controlled by CC 30/31/32 (default green)
self.color_r = 0
self.color_g = 255
self.color_b = 0
# Current state for GUI display
self.current_bpm: float | None = None
self.current_pattern: str = ""
def _current_color_hex(self) -> str:
r = max(0, min(255, int(self.color_r)))
g = max(0, min(255, int(self.color_g)))
b = max(0, min(255, int(self.color_b)))
return f"#{r:02x}{g:02x}{b:02x}"
async def _send_reset_to_sound(self):
try:
@@ -61,13 +75,14 @@ class MidiHandler:
try:
# Attempt to parse as float (BPM) from sound.py
bpm_value = float(message)
self.current_bpm = bpm_value
# Construct JSON message using the current MIDI-controlled delay and brightness
json_message = {
"names": ["0"],
"settings": {
"pattern": "pulse",
"delay": self.delay, # Use MIDI-controlled delay
"colors": ["#00ff00"],
"colors": [self._current_color_hex()],
"brightness": self.brightness,
"num_leds": 200,
},
@@ -100,6 +115,32 @@ class MidiHandler:
async with server:
await server.serve_forever()
async def _read_initial_cc_state(self, port, timeout_s: float = 0.5):
"""Read initial CC values from the MIDI device for a short period to populate state."""
start = time.time()
while time.time() - start < timeout_s:
msg = port.receive(block=False)
if msg and msg.type == 'control_change':
if msg.control == 36:
self.delay = msg.value * 4
logging.info(f"[Init] Delay set to {self.delay} ms from CC36")
elif msg.control == 37:
self.brightness = round((msg.value / 127) * 100)
logging.info(f"[Init] Brightness set to {self.brightness} from CC37")
elif msg.control == 30:
self.color_r = round((msg.value / 127) * 255)
logging.info(f"[Init] Red set to {self.color_r} from CC30")
elif msg.control == 31:
self.color_g = round((msg.value / 127) * 255)
logging.info(f"[Init] Green set to {self.color_g} from CC31")
elif msg.control == 32:
self.color_b = round((msg.value / 127) * 255)
logging.info(f"[Init] Blue set to {self.color_b} from CC32")
elif msg.control == 27:
self.beat_sending_enabled = (msg.value == 127)
logging.info(f"[Init] Beat sending {'ENABLED' if self.beat_sending_enabled else 'DISABLED'} from CC27")
await asyncio.sleep(0.001)
async def _midi_listener(self):
logging.info("Midi function") # Changed to info
"""
@@ -125,6 +166,8 @@ class MidiHandler:
try:
with mido.open_input(midi_port_name) as port:
logging.info(f"MIDI port '{midi_port_name}' opened. Press Ctrl+C to stop.") # Changed to info
# Read initial controller state briefly
await self._read_initial_cc_state(port)
while True:
msg = port.receive(block=False) # Non-blocking read
if msg:
@@ -137,24 +180,26 @@ class MidiHandler:
match msg.note:
case 48: # Original Note 48 for 'pulse'
pattern_name = "pulse"
self.current_pattern = pattern_name
await self.ws_client.send_data({
"names": ["0"],
"settings": {
"pattern": pattern_name,
"delay": self.delay, # Use MIDI-controlled delay
"colors": ["#00ff00"],
"colors": [self._current_color_hex()],
"brightness": self.brightness,
"num_leds": 120,
}
})
case 49: # Original Note 49 for 'theater_chase'
pattern_name = "theater_chase"
self.current_pattern = pattern_name
await self.ws_client.send_data({
"names": ["0"],
"settings": {
"pattern": pattern_name,
"delay": self.delay, # Use MIDI-controlled delay
"colors": ["#00ff00"],
"colors": [self._current_color_hex()],
"brightness": self.brightness,
"num_leds": 120,
"on_width": 10,
@@ -165,13 +210,14 @@ class MidiHandler:
})
case 50: # Original Note 50 for 'alternating'
pattern_name = "alternating"
self.current_pattern = pattern_name
logging.debug("Triggering Alternating Pattern") # Changed to debug
await self.ws_client.send_data({
"names": ["0"],
"settings": {
"pattern": pattern_name,
"delay": self.delay, # Use MIDI-controlled delay
"colors": ["#00ff00", "#0000ff"],
"colors": [self._current_color_hex(), "#0000ff"],
"brightness": self.brightness,
"num_leds": 120,
"n1": 10,
@@ -199,6 +245,18 @@ class MidiHandler:
# Map 0-127 to 0-100 brightness scale
self.brightness = round((msg.value / 127) * 100)
logging.info(f"Brightness set to {self.brightness} by MIDI controller")
case 30:
# Red 0-127 -> 0-255
self.color_r = round((msg.value / 127) * 255)
logging.info(f"Red set to {self.color_r}")
case 31:
# Green 0-127 -> 0-255
self.color_g = round((msg.value / 127) * 255)
logging.info(f"Green set to {self.color_g}")
case 32:
# Blue 0-127 -> 0-255
self.color_b = round((msg.value / 127) * 255)
logging.info(f"Blue set to {self.color_b}")
await asyncio.sleep(0.001) # Important: Yield control to asyncio event loop