-
Notifications
You must be signed in to change notification settings - Fork 0
/
buzzer.py
54 lines (45 loc) · 1.19 KB
/
buzzer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import threading
import os
import time
import RPi.GPIO as GPIO
if __name__ == "__main__":
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.cleanup()
class Buzzer(threading.Thread):
def __init__(self, pin):
self.isStop = False
GPIO.setup(pin,GPIO.OUT)
self.pin = pin
self.buzzer=None
self.timer=None
pass
def beep(self, duration, freq):
if self.buzzer == None:
self.buzzer=GPIO.PWM(self.pin, freq)
self.buzzer.start(50)
else:
self.buzzer.ChangeFrequency(freq)
self.timer = threading.Timer(duration, self.stop)
self.timer.start()
pass
def buzz(self, freq):
if self.buzzer == None:
self.buzzer=GPIO.PWM(self.pin, freq)
self.buzzer.start(50)
else:
self.buzzer.ChangeFrequency(freq)
def stop(self):
self.buzzer.stop()
if self.timer != None:
self.timer.cancel()
pass
def force_stop(self):
self.stop()
pass
# buzz = Buzzer(24)
# buzz.beep(2, 440)
# time.sleep(5)
# buzz.beep(1, 880)
# time.sleep(5)
# buzz.stop()