from machine import Pin, PWM, I2C
import time
from i2c_lcd import I2cLcd # Ensure this library is available in your project
# LCD setup (adjust address and dimensions as needed)
i2c = I2C(0, sda=Pin(18), scl=Pin(19), freq=400000) # Use your I2C pins
lcd = I2cLcd(i2c, 0x27, 2, 16) # Adjust the I2C address if necessary
# Buzzer setup
buzzer = PWM(Pin(28))
buzzer.duty_u16(0)
# Morse code dictionary
morse = [
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--", "--..",
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."
]
def text_to_morse(text):
result = ""
for ch in text:
if 'a' <= ch <= 'z':
ch = chr(ord(ch) - 32)
if 'A' <= ch <= 'Z':
result += morse[ord(ch) - ord('A')] + " "
elif '0' <= ch <= '9':
result += morse[ord(ch) - ord('0') + 26] + " "
elif ch == ' ':
result += "/"
else:
result += "?"
return result
# Keypad setup
rows = [Pin(0, Pin.OUT), Pin(1, Pin.OUT), Pin(2, Pin.OUT), Pin(3, Pin.OUT)]
cols = [Pin(4, Pin.IN, Pin.PULL_DOWN), Pin(5, Pin.IN, Pin.PULL_DOWN), Pin(6, Pin.IN, Pin.PULL_DOWN), Pin(7, Pin.IN, Pin.PULL_DOWN)]
keys = [
["1", "2", "3", "A"],
["4", "5", "6", "B"],
["7", "8", "9", "C"],
["*", "0", "#", "D"]
]
def read_keypad():
for i, row in enumerate(rows):
row.value(1)
for j, col in enumerate(cols):
if col.value() == 1:
row.value(0)
return keys[i][j]
row.value(0)
return ""
print("# - Enter Morse code, * - Backspace")
text = ""
while True:
key = str(read_keypad())
if key == "#":
if text == "":
lcd.clear()
lcd.putstr("Invalid input")
else:
morse_code = text_to_morse(text)
lcd.clear()
lcd.putstr(f"Text: {text}")
lcd.putstr("\nMorse: ")
for i in morse_code:
if i == '.':
buzzer.freq(1000)
buzzer.duty_u16(32768)
time.sleep(0.3)
buzzer.duty_u16(0)
time.sleep(0.1)
lcd.putstr(".")
elif i == '-':
buzzer.freq(1000)
buzzer.duty_u16(32768)
time.sleep(0.7)
buzzer.duty_u16(0)
time.sleep(0.1)
lcd.putstr("-")
elif i == "/" or i == " ":
lcd.putstr(" ")
time.sleep(1)
lcd.putstr("\nComplete")
time.sleep(0.3)
elif key == "*":
text = text[:-1]
lcd.clear()
lcd.putstr("Text: " + text)
elif key != "":
text += key
lcd.clear()
lcd.putstr("Text: " + text)
time.sleep(0.3)