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.

95 lines
2.5 KiB

  1. #pragma once
  2. #include "esphome/components/gpio/output/gpio_binary_output.h"
  3. #include "esphome/components/ledc/ledc_output.h"
  4. #include "esphome/core/component.h"
  5. namespace esphome {
  6. namespace xiaomi {
  7. namespace bslamp2 {
  8. static const std::string LIGHT_MODE_UNKNOWN{"unknown"};
  9. static const std::string LIGHT_MODE_OFF{"off"};
  10. static const std::string LIGHT_MODE_RGB{"rgb"};
  11. static const std::string LIGHT_MODE_WHITE{"white"};
  12. static const std::string LIGHT_MODE_NIGHT{"night"};
  13. class GPIOOutputValues {
  14. public:
  15. float red = 0.0f;
  16. float green = 0.0f;
  17. float blue = 0.0f;
  18. float white = 0.0f;
  19. std::string light_mode = LIGHT_MODE_OFF;
  20. /**
  21. * Copies the current output values to another GPIOOutputValues object.
  22. */
  23. void copy_to(GPIOOutputValues *other) {
  24. other->red = red;
  25. other->green = green;
  26. other->blue = blue;
  27. other->white = white;
  28. other->light_mode = light_mode;
  29. }
  30. void log(const char *prefix) { ESP_LOGD(TAG, "%s: RGB=[%f,%f,%f], white=%f", prefix, red, green, blue, white); }
  31. };
  32. class LightHAL : Component, public GPIOOutputValues {
  33. public:
  34. void set_red_pin(ledc::LEDCOutput *pin) { red_pin_ = pin; }
  35. void set_green_pin(ledc::LEDCOutput *pin) { green_pin_ = pin; }
  36. void set_blue_pin(ledc::LEDCOutput *pin) { blue_pin_ = pin; }
  37. void set_white_pin(ledc::LEDCOutput *pin) { white_pin_ = pin; }
  38. void set_master1_pin(gpio::GPIOBinaryOutput *pin) { master1_pin_ = pin; }
  39. void set_master2_pin(gpio::GPIOBinaryOutput *pin) { master2_pin_ = pin; }
  40. /**
  41. * Turn on the master switch for the LEDs.
  42. */
  43. void turn_on() {
  44. master1_pin_->turn_on();
  45. master2_pin_->turn_on();
  46. }
  47. /**
  48. * Turn off the master switch for the LEDs.
  49. */
  50. void turn_off() {
  51. master1_pin_->turn_off();
  52. master2_pin_->turn_off();
  53. }
  54. void set_state(GPIOOutputValues *new_state) {
  55. new_state->copy_to(this);
  56. red_pin_->set_level(this->red);
  57. green_pin_->set_level(this->green);
  58. blue_pin_->set_level(this->blue);
  59. white_pin_->set_level(this->white);
  60. }
  61. void set_rgbw(float r, float g, float b, float w) {
  62. red_pin_->set_level(r);
  63. green_pin_->set_level(g);
  64. blue_pin_->set_level(b);
  65. white_pin_->set_level(w);
  66. this->red = r;
  67. this->green = g;
  68. this->blue = b;
  69. this->white = w;
  70. }
  71. protected:
  72. ledc::LEDCOutput *red_pin_;
  73. ledc::LEDCOutput *green_pin_;
  74. ledc::LEDCOutput *blue_pin_;
  75. ledc::LEDCOutput *white_pin_;
  76. gpio::GPIOBinaryOutput *master1_pin_;
  77. gpio::GPIOBinaryOutput *master2_pin_;
  78. };
  79. } // namespace bslamp2
  80. } // namespace xiaomi
  81. } // namespace esphome