Small things because fuck sd cards
This commit is contained in:
@@ -1,42 +1,51 @@
|
||||
import time
|
||||
import threading
|
||||
import grovepi
|
||||
from sensors.lock import grove_lock
|
||||
from sensors.lock import safe_grove_access
|
||||
from shared import config
|
||||
|
||||
BUZZER_PIN = 8 # Must be a PWM pin on GrovePi (D3, D5, D6, D8 support analogWrite)
|
||||
|
||||
# Initialize pin mode
|
||||
# Initialize pin mode safely
|
||||
try:
|
||||
with grove_lock:
|
||||
grovepi.pinMode(BUZZER_PIN, "OUTPUT")
|
||||
with safe_grove_access(timeout=1.0) as acquired:
|
||||
if acquired:
|
||||
grovepi.pinMode(BUZZER_PIN, "OUTPUT")
|
||||
except Exception as e:
|
||||
print(f"[BUZZER] Init error: {e}")
|
||||
|
||||
def _siren_worker(beats_nb: int, delay: float):
|
||||
if not config.BUZZER_ACTIVATED:
|
||||
return
|
||||
|
||||
with grove_lock:
|
||||
try:
|
||||
for _ in range(beats_nb):
|
||||
# 1. Send single PWM write to start sound (Duty cycle ~128/255)
|
||||
grovepi.analogWrite(BUZZER_PIN, 128)
|
||||
time.sleep(delay)
|
||||
|
||||
# 2. Send single PWM write to kill sound
|
||||
grovepi.analogWrite(BUZZER_PIN, 0)
|
||||
time.sleep(delay)
|
||||
except Exception as e:
|
||||
print(f"[BUZZER] Error during siren execution: {e}")
|
||||
finally:
|
||||
# Absolute safety reset
|
||||
def _stop_sound():
|
||||
"""Helper to ensure sound is silenced safely under lock."""
|
||||
with safe_grove_access(timeout=1.0) as acquired:
|
||||
if acquired:
|
||||
try:
|
||||
grovepi.analogWrite(BUZZER_PIN, 0)
|
||||
grovepi.digitalWrite(BUZZER_PIN, 0)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _siren_worker(beats_nb: int, delay: float):
|
||||
if not config.BUZZER_ACTIVATED:
|
||||
return
|
||||
|
||||
try:
|
||||
for _ in range(beats_nb):
|
||||
# 1. Turn sound ON (only acquire lock briefly for the I2C write)
|
||||
with safe_grove_access(timeout=1.0) as acquired:
|
||||
if acquired:
|
||||
grovepi.analogWrite(BUZZER_PIN, 128)
|
||||
time.sleep(delay) # Sleep WITHOUT holding the lock!
|
||||
|
||||
# 2. Turn sound OFF
|
||||
with safe_grove_access(timeout=1.0) as acquired:
|
||||
if acquired:
|
||||
grovepi.analogWrite(BUZZER_PIN, 0)
|
||||
time.sleep(delay) # Sleep WITHOUT holding the lock!
|
||||
except Exception as e:
|
||||
print(f"[BUZZER] Error during siren execution: {e}")
|
||||
finally:
|
||||
_stop_sound()
|
||||
|
||||
def buzzer_siren(beats_nb: int = 3, delay: float = 0.2, async_run: bool = True):
|
||||
if async_run:
|
||||
thread = threading.Thread(target=_siren_worker, args=(beats_nb, delay), daemon=True)
|
||||
|
||||
Reference in New Issue
Block a user