-
Notifications
You must be signed in to change notification settings - Fork 0
/
buzzer.py
92 lines (73 loc) · 2.02 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import globals
from machine import Pin, PWM
import _thread
import utime
buzzer_pwm = None
multithreaded = True # For debug purposes
def start_buzzing(with_pwm=True):
global buzzer_pwm
stop_buzzing()
if with_pwm:
buzzer_pwm = PWM(Pin(globals.GLOBALS["Pins"]["BuzzerData"], Pin.OUT))
buzzer_pwm.freq(globals.GLOBALS["Buzzer"]["Frequency"])
buzzer_pwm.duty_u16(int(globals.GLOBALS["Buzzer"]["DutyCycle%"] * (1.0 / 100.0) * 65535.0))
else:
buzzer_pin = Pin(globals.GLOBALS["Pins"]["BuzzerData"], Pin.OUT)
buzzer_pin.value(1)
def stop_buzzing():
global buzzer_pwm
if buzzer_pwm != None:
buzzer_pwm.deinit()
buzzer_pwm = None
buzzer_pin = Pin(globals.GLOBALS["Pins"]["BuzzerData"], Pin.OUT)
buzzer_pin.value(0)
class BuzzerAlarm():
running = True
buzzing = False
def __init__(self):
if multithreaded:
globals.GLOBAL_LOCKS["Thread"].acquire() # Only allow one thread
try:
_thread.start_new_thread(self.alarm_thread, ())
finally:
globals.GLOBAL_LOCKS["Thread"].release()
else:
self.buzzing = True
self.alarm_thread()
def deinit(self):
self.stop()
self.running = False
def start(self):
self.buzzing = True
def stop(self):
self.buzzing = False
def alarm_thread(self):
globals.GLOBAL_LOCKS["Thread"].acquire() # Only allow one thread
try:
unit = globals.GLOBALS["Buzzer"]["BeepUnitMs"]
with_pwm = globals.GLOBALS["Buzzer"]["UsePwm"]
while self.running:
# Buzz sound pattern: I-I-I-I---- (each character is one unit)
if self.buzzing:
needs_cont = True
for _ in range(4):
start_buzzing(with_pwm)
if self.aware_sleep(unit): break
stop_buzzing()
if self.aware_sleep(unit): break
else:
needs_cont = False
if needs_cont: continue
if self.aware_sleep(unit * 3): continue
else:
stop_buzzing()
stop_buzzing()
finally:
globals.GLOBAL_LOCKS["Thread"].release()
pass
def aware_sleep(self, ms):
for _ in range(0, ms, 10):
utime.sleep_ms(10)
if not self.buzzing: # Should stop
return True
return False