# esp32 - using buzzer to generate sos morse code
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2025-09-11  initial release

import	time
from	machine	import	Pin

# ======================= hardware =======================

buzzer = Pin(32, Pin.OUT, value=0)

words_per_minute =10
T                = 60000 // (50 * words_per_minute)

# ========================= morse ========================

def wait(n):
    time.sleep_ms(n*T)
        
def dith():
    buzzer(1); wait(1)
    buzzer(0); wait(1)
    

def dah():
    buzzer(1); wait(3)
    buzzer(0); wait(1)
    
    
def wait_ch():
    wait(2)
    

def wait_word():
    wait(4)


def s():
    dith(); dith(); dith()


def o():
    dah();  dah();  dah()


def sos():
    s(); wait_ch(); o(); wait_ch(); s();
    
    
def emergency():
    try:
        while True:
            sos(); wait_word()
    finally : buzzer(0)
        
        
#sos()
emergency()
    
# ================================ end ==============================
