from machine import I2C, Pin
from pico_i2c_lcd import I2cLcd
from time import sleep
from picozero import Button, LED, Speaker

speaker = Speaker(16)
dash = Button(14, pull_up = True)
dot = Button(15, pull_up = True)
letter_btn = Button(13, pull_up = True)     # press to finish a letter
enter_btn = Button(12, pull_up = True)      # press to enter/check the word

i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)   #Data pin is pin 0, clock pin is pin 1
I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)                  #2 rows on LCD, 16 columns

dit = 0.2
pitch = 400                  # beep pitch in Hz
WORDS = ["WATER", "FOOD", "PAIN"]   # the words to learn

MORSE = {
    "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": "--..",
}
DECODE = {code: letter for letter, code in MORSE.items()}   # reverse lookup: ".-" -> "A"


def show(top, bottom=""):
    #Write two lines on the LCD screen
    lcd.clear()
    lcd.move_to(0, 0)
    lcd.putstr(top[:16])
    lcd.move_to(0, 1)
    lcd.putstr(bottom[:16])


def beep(length):
    #Play the speaker 
    speaker.play(pitch, length)


def wait_for_release(button):
    #To prevent glitchy pressing of the button
    while button.is_pressed:
        sleep(0.01)


def play_word(word):
    #playback the word once it is entered in correctly
    for letter in word:
        for symbol in MORSE[letter]:
            beep(dit if symbol == "." else dit * 3)
            sleep(dit)          # 1 dit between dots/dashes
        sleep(dit * 2)          # +2 dits = 3 dits between letters


#give the prompt
show("Tap out a word:", "WATER FOOD PAIN")
current = ""            # dots/dashes of the letter being typed, e.g. ".--"
word = ""               # letters decoded so far, e.g. "WA"

while True:
    # 1. Dot and dash buttons add a symbol
    if dot.is_pressed:
        current += "."
        beep(dit)
        wait_for_release(dot)
        show(word, current)
    elif dash.is_pressed:
        current += "-"
        beep(dit * 3)
        wait_for_release(dash)
        show(word, current)

    # 2. The letter button finishes the letter
    elif letter_btn.is_pressed:
        if current:                             # ignore it if nothing was typed
            word += DECODE.get(current, "?")    # "?" if the pattern isn't a letter
            current = ""
            show(word, "")
        wait_for_release(letter_btn)

    # 3. The enter button checks the word
    elif enter_btn.is_pressed:
        wait_for_release(enter_btn)
        if current:                             # double check the letters currently typed to add to the final word
            word += DECODE.get(current, "?")
            current = ""
        if word:                                # ignore it if nothing was typed
            if word in WORDS:
                show("Correct!", word)
                play_word(word)                 # buzz it back when completed and correct
            else:
                show("Not a word:", word)
            sleep(2)
            word = ""
            show("Tap out one of these words:", "WATER FOOD PAIN")

    sleep(0.01)

