|
|
|
@@ -10,13 +10,16 @@ except Exception as e: |
|
|
|
class Buzzer(object): |
|
|
|
|
|
|
|
sound = ["H", 0.1, "L", 0.1, "H", 0.1, "L", 0.1, "H", 0.1, "L"] |
|
|
|
def __init__(self, gpio, beep_level): |
|
|
|
def __init__(self, gpio, buzzer_type, beep_level): |
|
|
|
try: |
|
|
|
cbpi.app.logger.info("INIT BUZZER NOW GPIO%s" % gpio) |
|
|
|
self.gpio = int(gpio) |
|
|
|
self.beep_level = beep_level |
|
|
|
self.buzzer_type = buzzer_type |
|
|
|
GPIO.setmode(GPIO.BCM) |
|
|
|
GPIO.setup(self.gpio, GPIO.OUT) |
|
|
|
if buzzer_type == "PASSIVE": |
|
|
|
self.p = GPIO.PWM(int(gpio), 5000) |
|
|
|
self.state = True |
|
|
|
cbpi.app.logger.info("BUZZER SETUP OK") |
|
|
|
except Exception as e: |
|
|
|
@@ -29,19 +32,29 @@ class Buzzer(object): |
|
|
|
return |
|
|
|
|
|
|
|
def play(sound): |
|
|
|
def output(level): |
|
|
|
if self.buzzer_type == "PASSIVE" and level == GPIO.LOW: |
|
|
|
self.p.stop() |
|
|
|
elif self.buzzer_type == "PASSIVE": |
|
|
|
self.p.start(50) |
|
|
|
else: |
|
|
|
GPIO.output(int(self.gpio), level) |
|
|
|
|
|
|
|
try: |
|
|
|
for i in sound: |
|
|
|
if (isinstance(i, str)): |
|
|
|
if i == "H" and self.beep_level == "HIGH": |
|
|
|
GPIO.output(int(self.gpio), GPIO.HIGH) |
|
|
|
output(GPIO.HIGH) |
|
|
|
elif i == "H" and self.beep_level != "HIGH": |
|
|
|
GPIO.output(int(self.gpio), GPIO.LOW) |
|
|
|
output(GPIO.LOW) |
|
|
|
elif i == "L" and self.beep_level == "HIGH": |
|
|
|
GPIO.output(int(self.gpio), GPIO.LOW) |
|
|
|
output(GPIO.LOW) |
|
|
|
else: |
|
|
|
GPIO.output(int(self.gpio), GPIO.HIGH) |
|
|
|
output(GPIO.HIGH) |
|
|
|
else: |
|
|
|
time.sleep(i) |
|
|
|
if self.buzzer_type == "PASSIVE": |
|
|
|
self.p.stop() |
|
|
|
except Exception as e: |
|
|
|
pass |
|
|
|
|
|
|
|
@@ -51,7 +64,8 @@ class Buzzer(object): |
|
|
|
def init(cbpi): |
|
|
|
gpio = cbpi.get_config_parameter("buzzer", 16) |
|
|
|
beep_level = cbpi.get_config_parameter("buzzer_beep_level", "HIGH") |
|
|
|
buzzer_type = cbpi.get_config_parameter("buzzer_type", "ACTIVE") |
|
|
|
|
|
|
|
cbpi.buzzer = Buzzer(gpio, beep_level) |
|
|
|
cbpi.buzzer = Buzzer(gpio, buzzer_type, beep_level) |
|
|
|
cbpi.beep() |
|
|
|
cbpi.app.logger.info("INIT OK") |