Created
January 21, 2023 15:37
-
-
Save ahryshan/a4149451d8d5197eae27a3bc2793c31b to your computer and use it in GitHub Desktop.
Python Raspberry morse buzzer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from gpiozero import Buzzer | |
from time import sleep | |
long_sleep: float = 0.25 | |
short_long_ratio: float = 1/3 | |
short_sleep: float = long_sleep * short_long_ratio | |
def long(b: Buzzer): | |
b.on() | |
sleep(long_sleep) | |
b.off() | |
sleep(short_sleep) | |
def short(b: Buzzer): | |
b.on() | |
sleep(short_sleep) | |
b.off() | |
sleep(short_sleep) | |
def text_to_morse(text): | |
# Morse code dictionary | |
morse_code = { 'A':'.-', 'B':'-...', | |
'C':'-.-.', 'D':'-..', 'E':'.', | |
'F':'..-.', 'G':'--.', 'H':'....', | |
'I':'..', 'J':'.---', 'K':'-.-', | |
'L':'.-..', 'M':'--', 'N':'-.', | |
'O':'---', 'P':'.--.', 'Q':'--.-', | |
'R':'.-.', 'S':'...', 'T':'-', | |
'U':'..-', 'V':'...-', 'W':'.--', | |
'X':'-..-', 'Y':'-.--', 'Z':'--..', | |
'1':'.----', '2':'..---', '3':'...--', | |
'4':'....-', '5':'.....', '6':'-....', | |
'7':'--...', '8':'---..', '9':'----.', | |
'0':'-----', ', ':'--..--', '.':'.-.-.-', | |
'?':'..--..', '!': '–..–', '/':'-..-.', '-':'-....-', | |
'(':'-.--.', ')':'-.--.-'} | |
morse_text = "" | |
text = text.upper() | |
for letter in text: | |
if letter != " ": | |
morse_text += morse_code[letter] + " " | |
else: | |
morse_text += " " | |
return morse_text | |
def beep_morse(morse: str): | |
buzzer = Buzzer(27) | |
for symbol in morse: | |
if symbol == '.': | |
short(buzzer) | |
elif symbol == '-': | |
long(buzzer) | |
elif symbol == ' ': | |
sleep(long_sleep) | |
morse = text_to_morse("SOS SOS SOS") | |
print(morse) | |
beep_morse(morse) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment