Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

51 Zeilen
1.4KB

  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", "H", 0.1, "L", "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 = int(gpio)
  14. GPIO.setmode(GPIO.BCM)
  15. GPIO.setup(self.gpio, GPIO.OUT)
  16. self.state = True
  17. cbpi.app.logger.info("BUZZER SETUP OK")
  18. except Exception as e:
  19. cbpi.app.logger.info("BUZZER EXCEPTION %s" % str(e))
  20. self.state = False
  21. def beep(self):
  22. if self.state is False:
  23. cbpi.app.logger.error("BUZZER not working")
  24. return
  25. def play(sound):
  26. try:
  27. for i in sound:
  28. if (isinstance(i, str)):
  29. if i == "H":
  30. GPIO.output(int(self.gpio), GPIO.HIGH)
  31. else:
  32. GPIO.output(int(self.gpio), GPIO.LOW)
  33. else:
  34. time.sleep(i)
  35. except Exception as e:
  36. pass
  37. start_new_thread(play, (self.sound,))
  38. @cbpi.initalizer(order=1)
  39. def init(cbpi):
  40. gpio = cbpi.get_config_parameter("buzzer", 16)
  41. cbpi.buzzer = Buzzer(gpio)
  42. cbpi.beep()
  43. cbpi.app.logger.info("INIT OK")