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.

62 lines
1.7 KiB

  1. #pragma once
  2. #include "common.h"
  3. #include "esphome/core/component.h"
  4. #include "esphome/core/esphal.h"
  5. #include "esphome/components/i2c/i2c.h"
  6. namespace esphome {
  7. namespace yeelight {
  8. namespace bs2 {
  9. class FrontPanelHAL : public Component, public i2c::I2CDevice {
  10. public:
  11. void set_trigger_pin(GPIOPin *pin) { i2c_trigger_pin_ = pin; }
  12. void setup() {
  13. ESP_LOGCONFIG(TAG, "Setting up I2C trigger pin interrupt...");
  14. this->i2c_trigger_pin_->setup();
  15. this->i2c_trigger_pin_->attach_interrupt(
  16. FrontPanelHAL::isr, this, FALLING);
  17. }
  18. void dump_config() {
  19. ESP_LOGCONFIG(TAG, "I2C");
  20. LOG_PIN(" Interrupt pin: ", this->i2c_trigger_pin_);
  21. }
  22. void loop() {
  23. if (this->queue_length > 0) {
  24. this->queue_length--;
  25. ESP_LOGD(TAG, "EVENT!");
  26. }
  27. }
  28. protected:
  29. // The GPIO pin that is used by the front panel to notify the ESP that
  30. // a touch/release event can be read using I2C.
  31. GPIOPin *i2c_trigger_pin_;
  32. // The ISR that is used for handling event interrupts.
  33. static void isr(FrontPanelHAL *store);
  34. // The number of unhandled event interrupts.
  35. volatile int queue_length = 0;
  36. };
  37. /**
  38. * This ISR is used to handle IRQ triggers from the front panel.
  39. *
  40. * The front panel pulls the trigger pin low when a new event
  41. * is available. All we do here to handle the interrupt, is
  42. * increment a simple queue length counter. Reading the event
  43. * from the I2C bus will be handled in the main loop, based
  44. * on this counter.
  45. */
  46. void ICACHE_RAM_ATTR HOT FrontPanelHAL::isr(FrontPanelHAL *store) {
  47. store->queue_length++;
  48. }
  49. } // namespace bs2
  50. } // namespace yeelight
  51. } // namespace esphome