add piano example

This commit is contained in:
Siwat Sirichai 2022-04-09 01:10:39 +07:00
parent 99432ea074
commit 5e7a923c3a
1 changed files with 12 additions and 6 deletions

View File

@ -1,20 +1,26 @@
from time import sleep from time import sleep
from telemetrix_rpi_pico import telemetrix_rpi_pico from telemetrix_rpi_pico import telemetrix_rpi_pico
from siwatlib.debounce import debounce
from siwatlib.sound import beep
import winsound import winsound
index_shift = 2 index_shift = 2
sound = [261.6256,293.6648,329.6276,349.2282,391.9954,440.0000,493.8833,523.2511] freqs = [261.6256,293.6648,329.6276,349.2282,391.9954,440.0000,493.8833,523.2511]
debouncers = []
def handle_button_press(data): def handle_button_press(data):
freq = sound[data[1]-index_shift] freq = freqs[data[1]-index_shift]
print(freq) print(freq)
if data[2] == 0: recieved_state = not data[2]
winsound.Beep(int(freq),500) if debouncers[data[1]-index_shift].set_state(state=recieved_state):
beep(frequency=freq,duration=0.25)
mcu = telemetrix_rpi_pico.TelemetrixRpiPico(com_port='COM10') mcu = telemetrix_rpi_pico.TelemetrixRpiPico(com_port='COM10')
for i in range(2,10): for i in range(2,10):
debouncers.append(debounce(state=False,debounce_time=0.15))
mcu.set_pin_mode_digital_input(i,callback=handle_button_press) mcu.set_pin_mode_digital_input(i,callback=handle_button_press)
while True: while True:
sleep(100) sleep(100)