from mpython import *
import time
from machine import Timer,UART

def on_button_a_down(_):
    global k, yueqi
    time.sleep_ms(10)
    if button_a.value() == 1: return
    if k < len(yueqi) - 1:
        k = k + 1
    else:
        k = 0
    uart1.write(bytes([192]))
    uart1.write(bytes([yueqi[k][0]]))
    uart1.write(bytes([yueqi[k][0]]))
    oled.fill(0)
    oled.DispChar(yueqi[k][1], 44, 22, 1)
    oled.show()

uart1 = UART(1, baudrate=31250, tx=Pin.P13, rx=Pin.P8)

_status_p = _status_y = _status_t = _status_h = _status_o = _status_n = 0
def on_touchpad_P_pressed():pass
def on_touchpad_P_unpressed():pass
def on_touchpad_Y_pressed():pass
def on_touchpad_Y_unpressed():pass
def on_touchpad_T_pressed():pass
def on_touchpad_T_unpressed():pass
def on_touchpad_H_pressed():pass
def on_touchpad_H_unpressed():pass
def on_touchpad_O_pressed():pass
def on_touchpad_O_unpressed():pass
def on_touchpad_N_pressed():pass
def on_touchpad_N_unpressed():pass

tim12 = Timer(12)

def timer12_tick(_):
    global _status_p, _status_y, _status_t, _status_h, _status_o, _status_n
    try:
        touchPad_P.read();pass
    except:
        return
    if touchPad_P.read() < 400:
        if 1 != _status_p:_status_p = 1;on_touchpad_P_pressed()
    elif 0 != _status_p:_status_p = 0;on_touchpad_P_unpressed()
    if touchPad_Y.read() < 400:
        if 1 != _status_y:_status_y = 1;on_touchpad_Y_pressed()
    elif 0 != _status_y:_status_y = 0;on_touchpad_Y_unpressed()
    if touchPad_T.read() < 400:
        if 1 != _status_t:_status_t = 1;on_touchpad_T_pressed()
    elif 0 != _status_t:_status_t = 0;on_touchpad_T_unpressed()
    if touchPad_H.read() < 400:
        if 1 != _status_h:_status_h = 1;on_touchpad_H_pressed()
    elif 0 != _status_h:_status_h = 0;on_touchpad_H_unpressed()
    if touchPad_O.read() < 400:
        if 1 != _status_o:_status_o = 1;on_touchpad_O_pressed()
    elif 0 != _status_o:_status_o = 0;on_touchpad_O_unpressed()
    if touchPad_N.read() < 400:
        if 1 != _status_n:_status_n = 1;on_touchpad_N_pressed()
    elif 0 != _status_n:_status_n = 0;on_touchpad_N_unpressed()

tim12.init(period=100, mode=Timer.PERIODIC, callback=timer12_tick)

def on_touchpad_P_pressed():
    global k, yueqi
    uart1.write(bytes([0x90, 0x3C, 0x40]))

def on_touchpad_P_unpressed():
    global k, yueqi
    uart1.write(bytes([0x80, 0x3C, 0x40]))

def on_touchpad_Y_pressed():
    global k, yueqi
    uart1.write(bytes([0x90, 0x3E, 0x40]))

def on_touchpad_Y_unpressed():
    global k, yueqi
    uart1.write(bytes([0x80, 0x3E, 0x40]))

def on_touchpad_T_pressed():
    global k, yueqi
    uart1.write(bytes([0x90, 0x40, 0x40]))

def on_touchpad_T_unpressed():
    global k, yueqi
    uart1.write(bytes([0x80, 0x40, 0x40]))

def on_touchpad_H_pressed():
    global k, yueqi
    uart1.write(bytes([0x90, 0x41, 0x40]))

def on_touchpad_H_unpressed():
    global k, yueqi
    uart1.write(bytes([0x80, 0x41, 0x40]))

def on_touchpad_O_pressed():
    global k, yueqi
    uart1.write(bytes([0x90, 0x43, 0x40]))

def on_touchpad_O_unpressed():
    global k, yueqi
    uart1.write(bytes([0x80, 0x43, 0x40]))

def on_touchpad_N_pressed():
    global k, yueqi
    uart1.write(bytes([0x90, 0x45, 0x40]))

def on_touchpad_N_unpressed():
    global k, yueqi
    uart1.write(bytes([0x80, 0x45, 0x40]))

button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)


yueqi = ((0,"钢琴"),(10,"八音盒"),(22,"口琴"),(25,"吉他"),(40,"小提琴"),(56,"小号"))
k = 0
uart1.write(bytes([192]))
uart1.write(bytes([yueqi[k][0]]))
uart1.write(bytes([yueqi[k][0]]))
oled.fill(0)
oled.DispChar(yueqi[k][1], 44, 22, 1)
oled.show()
while True:
    pass
