refactor: move GPIO and pygame initialization into setup()
This commit is contained in:
@@ -59,26 +59,28 @@ def remove_pid_file():
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
# GPIO Setup
|
|
||||||
BUTTON_PIN = 17
|
BUTTON_PIN = 17
|
||||||
LED_PIN = 27
|
LED_PIN = 27
|
||||||
|
|
||||||
GPIO.setmode(GPIO.BCM)
|
|
||||||
GPIO.setup(LED_PIN, GPIO.OUT)
|
|
||||||
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
|
||||||
|
|
||||||
# Audio Setup (with optimized buffer)
|
def setup():
|
||||||
pygame.mixer.pre_init(frequency=44100, size=-16, channels=2, buffer=4096)
|
"""Initialize GPIO and audio hardware. Call once before running."""
|
||||||
pygame.mixer.init()
|
GPIO.setmode(GPIO.BCM)
|
||||||
|
GPIO.setup(LED_PIN, GPIO.OUT)
|
||||||
|
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||||
|
|
||||||
try:
|
# Audio Setup (with optimized buffer)
|
||||||
pygame.mixer.music.load("Laid Back - Sunshine Reggae.mp3")
|
pygame.mixer.pre_init(frequency=44100, size=-16, channels=2, buffer=4096)
|
||||||
pygame.mixer.music.set_volume(1.0)
|
pygame.mixer.init()
|
||||||
except Exception as e:
|
|
||||||
logging.error(f"Error loading audio file: {e}")
|
try:
|
||||||
# Don't exit in test mode
|
pygame.mixer.music.load("Laid Back - Sunshine Reggae.mp3")
|
||||||
if "pytest" not in sys.modules:
|
pygame.mixer.music.set_volume(1.0)
|
||||||
sys.exit(1)
|
except Exception as e:
|
||||||
|
logging.error(f"Error loading audio file: {e}")
|
||||||
|
# Don't exit in test mode
|
||||||
|
if "pytest" not in sys.modules:
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
# State Machine states for the flow
|
# State Machine states for the flow
|
||||||
STATE_RINGING = 0
|
STATE_RINGING = 0
|
||||||
@@ -199,6 +201,7 @@ class AlarmClock:
|
|||||||
|
|
||||||
|
|
||||||
def run_alarm(test_mode=False):
|
def run_alarm(test_mode=False):
|
||||||
|
setup()
|
||||||
logging.info("Alarm clock started. Music is playing in an endless loop.")
|
logging.info("Alarm clock started. Music is playing in an endless loop.")
|
||||||
if "pygame" in globals() and hasattr(pygame, "mixer") and pygame.mixer.get_init():
|
if "pygame" in globals() and hasattr(pygame, "mixer") and pygame.mixer.get_init():
|
||||||
pygame.mixer.music.play(-1)
|
pygame.mixer.music.play(-1)
|
||||||
|
|||||||
Reference in New Issue
Block a user