From b242d5cb6e0a8b8e71a7b5b2443e4ebdfbd0de13 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sat, 9 Apr 2022 01:10:20 +0700 Subject: [PATCH 1/3] ignore pycache --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e0e4bc3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +siwatlib/__pycache__/ From 99432ea074882c0fb6e6db3dde2a822445801d9a Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sat, 9 Apr 2022 01:10:31 +0700 Subject: [PATCH 2/3] add siwatlib --- siwatlib/debounce.py | 12 ++++++++++++ siwatlib/sound.py | 27 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 siwatlib/debounce.py create mode 100644 siwatlib/sound.py diff --git a/siwatlib/debounce.py b/siwatlib/debounce.py new file mode 100644 index 0000000..e614803 --- /dev/null +++ b/siwatlib/debounce.py @@ -0,0 +1,12 @@ +from time import perf_counter as seconds +class debounce: + def __init__(self,state: bool, debounce_time: float) -> None: + self.last_pressed = seconds() + self.debounce_time = debounce_time + self.state = state + def set_state(self, state: bool) -> bool: + if seconds()-self.last_pressed > self.debounce_time: + self.state = state + return self.state + else: + return self.state \ No newline at end of file diff --git a/siwatlib/sound.py b/siwatlib/sound.py new file mode 100644 index 0000000..71ae48a --- /dev/null +++ b/siwatlib/sound.py @@ -0,0 +1,27 @@ +from pysine import pysine +import winsound +import threading + +MODE = 'pysine' + +global __beeper__ + +def __beeper_therad__(frequency: float, duration: float): + if MODE == 'winsound': + winsound.Beep(frequency=int(frequency), duration=int(duration*1000)) + elif MODE == 'pysine': + pysine.sine(frequency=frequency,duration=duration) + +__beeper__ = threading.Thread(target=__beeper_therad__, + kwargs={'frequency': 0, 'duration': 0} + ) + +def beep(frequency: float, duration: float): + global __beeper__ + if not __beeper__.is_alive(): + __beeper__ = threading.Thread(target=__beeper_therad__, + kwargs={'frequency': frequency, 'duration': duration} + ) + __beeper__.start() + + From 5e7a923c3a5182f60628ef42d9a8f37147d61b36 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sat, 9 Apr 2022 01:10:39 +0700 Subject: [PATCH 3/3] add piano example --- piano.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/piano.py b/piano.py index 3c599cc..42ed849 100644 --- a/piano.py +++ b/piano.py @@ -1,20 +1,26 @@ from time import sleep from telemetrix_rpi_pico import telemetrix_rpi_pico +from siwatlib.debounce import debounce +from siwatlib.sound import beep + import winsound 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): - freq = sound[data[1]-index_shift] + freq = freqs[data[1]-index_shift] print(freq) - if data[2] == 0: - winsound.Beep(int(freq),500) - - + recieved_state = not data[2] + 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') + 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) while True: sleep(100) \ No newline at end of file