7、触摸按键点播六首曲子
#MicroPython动手做(19)——掌控板之蜂鸣器与音乐
# 触摸按键点播六首曲子
from mpython import *
import time
import music
from machine import Timer
def on_button_a_down(_):
time.sleep_ms(10)
if button_a.value() == 1: return
music.stop()
_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():
music.play(music.DONG_FANG_HONG, wait=False, loop=False)
def on_touchpad_Y_pressed():
music.play(music.BIRTHDAY, wait=False, loop=False)
def on_touchpad_T_pressed():
music.play(music.MO_LI_HUA, wait=False, loop=False)
def on_touchpad_H_pressed():
music.play(music.ODE, wait=False, loop=False)
def on_touchpad_O_pressed():
music.play(music.PRELUDE, wait=False, loop=False)
def on_touchpad_N_pressed():
music.play(music.CAI_YUN_ZHUI_YUE, wait=False, loop=False)
button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
while True:
oled.fill(0)
oled.DispChar("A键:停止", 35, 0, 1)
oled.DispChar("P:东方红 Y:生日快乐", 6, 20, 1)
oled.DispChar("T:茉莉花 H:欢乐颂", 11, 35, 1)
oled.DispChar("O:婚宴 N:彩云追月", 13, 50, 1)
oled.show()
|