# Play tones
# If you click mouse (left click) on screen, then Python will play music for you
# Press buttons to play individual tones.
# To keep simplicity, could happen, that pressed button will play two tones. Fix it, if you like.
# Attention!! Random tones could be very annoying to people around you :)


from libraries.EduSense import Uart
from libraries.EduSense import Sound


def on_key_down(key):
    if key == keys.ESCAPE:
        exit()

def on_mouse_down():
    # every mouse click will add one melody to queue in pad. Watchout for overload.
    Sound.play_tones(uart, 3, Sound.MELODY_SAMPLE_INTRO)



def check_buttons():
    # now it will be self sustained function, run every 0.1 sec
    clock.schedule(check_buttons, 0.1)

    status, result = uart.cmd_buttons_get()
    if status:
        if result[Uart.ButtonField.YELLOW]:
            tone = 10
        elif result[Uart.ButtonField.BLUE]:
            tone = 50
        elif result[Uart.ButtonField.WHITE]:
            tone = 70
        elif result[Uart.ButtonField.RED]:
            tone = 100
        elif result[Uart.ButtonField.GREEN]:
            tone = 150
        else:
            tone = 0

        if tone:
            uart.cmd_sound_play(tone, 50)
            uart.cmd_sound_play(0, 10)
            print("You played at:", tone / 100, "kHz")
    else:
        print("Error during reading pad")



uart = Uart.Uart()
uart.open()
check_buttons()


