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.

89 lines
2.4 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. void turn_on() {
  41. master1_pin_->turn_on();
  42. master2_pin_->turn_on();
  43. }
  44. void turn_off() {
  45. master1_pin_->turn_off();
  46. master2_pin_->turn_off();
  47. }
  48. void set_state(GPIOOutputValues *new_state) {
  49. new_state->copy_to(this);
  50. red_pin_->set_level(this->red);
  51. green_pin_->set_level(this->green);
  52. blue_pin_->set_level(this->blue);
  53. white_pin_->set_level(this->white);
  54. }
  55. void set_rgbw(float r, float g, float b, float w) {
  56. red_pin_->set_level(r);
  57. green_pin_->set_level(g);
  58. blue_pin_->set_level(b);
  59. white_pin_->set_level(w);
  60. this->red = r;
  61. this->green = g;
  62. this->blue = b;
  63. this->white = w;
  64. }
  65. protected:
  66. ledc::LEDCOutput *red_pin_;
  67. ledc::LEDCOutput *green_pin_;
  68. ledc::LEDCOutput *blue_pin_;
  69. ledc::LEDCOutput *white_pin_;
  70. gpio::GPIOBinaryOutput *master1_pin_;
  71. gpio::GPIOBinaryOutput *master2_pin_;
  72. };
  73. } // namespace bslamp2
  74. } // namespace xiaomi
  75. } // namespace esphome