You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 line
1.3KB

  1. import time
  2. from thread import start_new_thread
  3. from modules import cbpi
  4. try:
  5. import RPi.GPIO as GPIO
  6. except Exception as e:
  7. pass
  8. class Buzzer(object):
  9. sound = ["H", 0.1, "L"]
  10. def __init__(self, gpio):
  11. try:
  12. cbpi.app.logger.info("INIT BUZZER NOW GPIO%s" % gpio)
  13. self.gpio = gpio
  14. GPIO.setmode(GPIO.BCM)
  15. GPIO.setup(gpio, GPIO.OUT)
  16. self.state = True
  17. cbpi.app.logger.info("BUZZER SETUP OK")
  18. except Exception as e:
  19. self.state = False
  20. def beep(self):
  21. if self.state is False:
  22. cbpi.app.logger.error("BUZZER not working")
  23. return
  24. def play(sound):
  25. try:
  26. for i in sound:
  27. if (isinstance(i, str)):
  28. if i == "H":
  29. GPIO.output(int(self.gpio), GPIO.HIGH)
  30. else:
  31. GPIO.output(int(self.gpio), GPIO.LOW)
  32. else:
  33. time.sleep(i)
  34. except Exception as e:
  35. pass
  36. start_new_thread(play, (self.sound,))
  37. @cbpi.initalizer(order=1)
  38. def init(cbpi):
  39. gpio = cbpi.get_config_parameter("buzzer", 16)
  40. cbpi.buzzer = Buzzer(gpio)
  41. cbpi.beep()
  42. cbpi.app.logger.info("INIT OK")