No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

54 líneas
1.5KB

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