import RPi.GPIO as gpio
import time
from threading import Thread
piano1 = list([261, 293, 329, 349, 391, 440, 493, 523])
piano2 = list([261, 391, 329, 391, 261, 391, 329, 391])
buzzer = 17
buzzer2 = 27
gpio.setmode(gpio.BCM)
class PlayTone(Thread):
def __init__(self, pin):
super().__init__()
self.pin = pin
gpio.setup(self.pin, gpio.OUT)
def tone(self, pitch, sec):
half_pitch = (1 / pitch) / 2
t = int(pitch * sec)
for i in range(t):
gpio.output(self.pin, gpio.HIGH)
time.sleep(half_pitch)
gpio.output(self.pin, gpio.LOW)
time.sleep(half_pitch)
class Play(PlayTone):
def __init__(self, pin, piano):
super().__init__(pin)
self.piano = piano
def run(self):
for p in self.piano:
self.tone(p, 1)
p1 = Play(17, piano1)
p1.start()
p2 = Play(27, piano2)
p2.start()
while p1.is_alive():
time.sleep(0.5)
gpio.cleanup()