25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.8KB

  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", 0.1, "H", 0.1, "L", 0.1, "H", 0.1, "L"]
  10. def __init__(self, gpio, beep_level):
  11. try:
  12. cbpi.app.logger.info("INIT BUZZER NOW GPIO%s" % gpio)
  13. self.gpio = int(gpio)
  14. self.beep_level = beep_level
  15. GPIO.setmode(GPIO.BCM)
  16. GPIO.setup(self.gpio, GPIO.OUT)
  17. self.state = True
  18. cbpi.app.logger.info("BUZZER SETUP OK")
  19. except Exception as e:
  20. cbpi.app.logger.info("BUZZER EXCEPTION %s" % str(e))
  21. self.state = False
  22. def beep(self):
  23. if self.state is False:
  24. cbpi.app.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" and self.beep_level == "HIGH":
  31. GPIO.output(int(self.gpio), GPIO.HIGH)
  32. elif i == "H" and self.beep_level != "HIGH":
  33. GPIO.output(int(self.gpio), GPIO.LOW)
  34. elif i == "L" and self.beep_level == "HIGH":
  35. GPIO.output(int(self.gpio), GPIO.LOW)
  36. else:
  37. GPIO.output(int(self.gpio), GPIO.HIGH)
  38. else:
  39. time.sleep(i)
  40. except Exception as e:
  41. pass
  42. start_new_thread(play, (self.sound,))
  43. @cbpi.initalizer(order=1)
  44. def init(cbpi):
  45. gpio = cbpi.get_config_parameter("buzzer", 16)
  46. beep_level = cbpi.get_config_parameter("buzzer_beep_level", "HIGH")
  47. cbpi.buzzer = Buzzer(gpio, beep_level)
  48. cbpi.beep()
  49. cbpi.app.logger.info("INIT OK")