Buzzer
This commit is contained in:
@@ -299,6 +299,10 @@ async def process_messages_task():
|
||||
n_state = data["data"].get("new_cooking_state")
|
||||
print(f"[LoRa] Microwave {mw_id} state changed to: {n_state}")
|
||||
|
||||
if n_state in (CookingStates.DONE, CookingStates.STIRRING_REQUIRED, CookingStates.ALERT):
|
||||
print("buzzer alert triggered!")
|
||||
beats = 5 if n_state == CookingStates.ALERT else (1 if n_state == CookingStates.STIRRING_REQUIRED else 3)
|
||||
buzzer.buzzer_siren(beatsNb=beats)
|
||||
if n_state == CookingStates.IDLE and microwave_states.get(mw_id) == MicrowaveState.COOKING:
|
||||
microwave_states[mw_id] = MicrowaveState.DONE
|
||||
print(f"[{mw_id}] Cooking finished. Waiting for user to remove dish.")
|
||||
|
||||
@@ -4,4 +4,5 @@ import sensors.ultrasonicRanger as ultrasonicRanger
|
||||
import sensors.temp_hum as temp_hum
|
||||
import sensors.button as button
|
||||
import sensors.gps as gps
|
||||
import sensors.camera as camera
|
||||
import sensors.camera as camera
|
||||
import sensors.buzzer as buzzer
|
||||
@@ -0,0 +1,33 @@
|
||||
import time
|
||||
import grovepi
|
||||
from sensors.lock import grove_lock
|
||||
from shared import config
|
||||
|
||||
# Pin definition
|
||||
BUZZER_PIN = 8
|
||||
|
||||
grovepi.pinMode(BUZZER_PIN, "OUTPUT")
|
||||
buzzer_state = 0
|
||||
|
||||
def set_buzzer(state: int):
|
||||
global buzzer_state
|
||||
buzzer_state = state
|
||||
if not config.BUZZER_ACTIVATED:
|
||||
return
|
||||
try:
|
||||
with grove_lock:
|
||||
grovepi.digitalWrite(BUZZER_PIN, state)
|
||||
except Exception as e:
|
||||
print(f"[BUZZER] Error occurred while setting buzzer state: {e}")
|
||||
|
||||
def buzzer_toggle():
|
||||
global buzzer_state
|
||||
new_state = 1 - buzzer_state
|
||||
set_buzzer(new_state)
|
||||
|
||||
def buzzer_siren(beatsNb: int = 3, delay: float = 0.3):
|
||||
for _ in range(beatsNb):
|
||||
set_buzzer(1)
|
||||
time.sleep(delay)
|
||||
set_buzzer(0)
|
||||
time.sleep(delay)
|
||||
Reference in New Issue
Block a user