thread_dd.py 1.12 KB
from threading import Thread
import time
from RPi import GPIO

class DD(Thread):
	
	#Constructeur
	def __init__(self, pin, t_notes, t_tps):
		Thread.__init__(self);
		self.pin = pin
		self.t_notes = t_notes
		self.t_tps = t_tps

	def run(self):
		p = GPIO.PWM(self.pin,220)
		p.start(15)
		i = 0
		while(i<len(self.t_notes)-1):
			p.ChangeFrequency(self.t_notes[i])
			time.sleep(self.t_tps[i])
			i+=1
		GPIO.cleanup()

notes=[220,1,220,1,220,1,174.5,1,261.5,1,220,1,174.5,1,261.5,1,220,1]
tps = [1,0.02,1,0.02,1,0.02,0.4,0.05,0.6,0.05,0.4,0.05,0.6,0.05,0.4,0.05,0.6,0.05]

GPIO.setmode(GPIO.BCM)
#DD1
GPIO.setup(23,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)
GPIO.setup(25,GPIO.OUT)
#DD2
GPIO.setup(16,GPIO.OUT)
GPIO.setup(20,GPIO.OUT)
GPIO.setup(21,GPIO.OUT)
#PWM
GPIO.setup(18,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
#Act DD1
GPIO.output(23,GPIO.LOW)
GPIO.output(24,GPIO.LOW)
GPIO.output(25,GPIO.LOW)
#Act DD2
GPIO.output(16,GPIO.LOW)
GPIO.output(20,GPIO.LOW)
GPIO.output(21,GPIO.LOW)

#Creation des threads
thread1 = DD(18,notes,tps)
thread2 = DD(13,notes,tps)

#Lancement des threads
thread1.start()
thread2.start()

#Synchro
thread1.join()
thread2.join()