RpiUtils.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from threading import Thread
  2. try:
  3. # only import if we are on a Rpi
  4. import RPi.GPIO as GPIO
  5. except RuntimeError:
  6. pass
  7. import time
  8. import logging
  9. from kalliope.core.Models.RpiSettings import RpiSettings
  10. logging.basicConfig()
  11. logger = logging.getLogger("kalliope")
  12. class RpiUtils(Thread):
  13. def __init__(self, rpi_settings=None, callback=None):
  14. """
  15. Class used to:
  16. - manage RPI GPIO
  17. - thread to catch mute button signal
  18. The object receive a rpi settings object which contains pin number to use on the Rpi
  19. When a signal is caught form the mute button, the callback method from the main controller is called
  20. :param rpi_settings: Settings object with GPIO pin number to use
  21. :type rpi_settings: RpiSettings
  22. :param callback: Callback function from the main controller to call when the mute button is pressed
  23. """
  24. super(RpiUtils, self).__init__()
  25. GPIO.setmode(GPIO.BCM) # Use GPIO name
  26. GPIO.setwarnings(False)
  27. self.rpi_settings = rpi_settings
  28. self.callback = callback
  29. self.init_gpio(self.rpi_settings)
  30. def run(self):
  31. """
  32. Start the thread to make kalliope waiting for an input GPIO signal
  33. """
  34. # run the main thread
  35. try:
  36. while True: # keep the thread alive
  37. time.sleep(0.1)
  38. except (KeyboardInterrupt, SystemExit):
  39. self.destroy()
  40. self.destroy()
  41. def switch_kalliope_mute_led(self, event):
  42. """
  43. Switch the state of the MUTE LED
  44. :param event: not used
  45. """
  46. logger.debug("[RpiUtils] Event button caught. Switching mute led")
  47. # get led status
  48. led_mute_kalliope = GPIO.input(self.rpi_settings.pin_led_muted)
  49. # switch state
  50. if led_mute_kalliope == GPIO.HIGH:
  51. logger.debug("[RpiUtils] Switching pin_led_muted to OFF")
  52. self.switch_pin_to_off(self.rpi_settings.pin_led_muted)
  53. self.callback(muted=False)
  54. else:
  55. logger.debug("[RpiUtils] Switching pin_led_muted to ON")
  56. self.switch_pin_to_on(self.rpi_settings.pin_led_muted)
  57. self.callback(muted=True)
  58. @staticmethod
  59. def destroy():
  60. """
  61. Cleanup GPIO to not keep a pin to HIGH status
  62. :return:
  63. """
  64. logger.debug("[RpiUtils] Cleanup GPIO configuration")
  65. GPIO.cleanup()
  66. def init_gpio(self, rpi_settings):
  67. """
  68. Initialize GPIO pin to a default value. Leds are off by default
  69. Mute button is set as an input
  70. :param rpi_settings: RpiSettings object
  71. """
  72. # All led are off by default
  73. if self.rpi_settings.pin_led_muted:
  74. GPIO.setup(rpi_settings.pin_led_muted, GPIO.OUT, initial=GPIO.LOW)
  75. if self.rpi_settings.pin_led_started:
  76. GPIO.setup(rpi_settings.pin_led_started, GPIO.OUT, initial=GPIO.LOW)
  77. if self.rpi_settings.pin_led_listening:
  78. GPIO.setup(rpi_settings.pin_led_listening, GPIO.OUT, initial=GPIO.LOW)
  79. if self.rpi_settings.pin_led_talking:
  80. GPIO.setup(rpi_settings.pin_led_talking, GPIO.OUT, initial=GPIO.LOW)
  81. # MUTE button
  82. GPIO.setup(rpi_settings.pin_mute_button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  83. GPIO.add_event_detect(rpi_settings.pin_mute_button, GPIO.FALLING,
  84. callback=self.switch_kalliope_mute_led,
  85. bouncetime=500)
  86. @classmethod
  87. def switch_pin_to_on(cls, pin_number):
  88. """
  89. Switch the pin_number of the RPI GPIO board to HIGH status
  90. :param pin_number: integer pin number to switch HIGH
  91. """
  92. logger.debug("[RpiUtils] Switching pin number %s to ON" % pin_number)
  93. GPIO.output(pin_number, GPIO.HIGH)
  94. @classmethod
  95. def switch_pin_to_off(cls, pin_number):
  96. """
  97. Switch the pin_number of the RPI GPIO board to LOW status
  98. :param pin_number: integer pin number to switch LOW
  99. """
  100. logger.debug("[RpiUtils] Switching pin number %s to OFF" % pin_number)
  101. GPIO.output(pin_number, GPIO.LOW)