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.

85 lines
2.8 KiB

3 years ago
Introduced a HUB component + front panel IRQ handling A HUB component was introduced. This HUB component has all the knowledge about the Yeelight Bedside Lamp 2 hardware. It known what pins are used, that PWM frequencies to use, what pins to switch in binary mode, etc. etc. No configuration is required for this HUB component. It's automatically loaded when the light component is loaded. The light component will use the HUB component to access the pins that are required for driving the LED circuitry. Note that this simplifies the configuration by A LOT. There's no need anymore to configure the pinouts in the YAML file. This is a logical route to take, since we're talking about a factory-produced PCB with a soldered on ESP32 chip, which uses the same GPIO's and settings on all produced devices (I presume). It would be quite redundant to force every user into configuring these pinouts themselves. ** Beware to update your device yaml configuration ** There are a few pinouts left to move into the HUB. I will do that in the next commit. Your device yaml configuration can be simplified along with these changes. Some of the keys in the existing light configuration block will no longer work and will have to be removed (red, green, blue, white). ** Further development ** The HUB will be extended make it the central component that also handles the I2C communication. This way, there is a central place to regulate the traffic to and from the front panel. We will be able to build upon this by implementing extra, fully separated components that handle for example the front panel light level, the power button, the color button and the slider. ** Interrupt handler for the I2C IRQ trigger pin ** One requirement for the I2C communication has already been implemented: an interrupt handler for the GPIO that is used by the front panel to signal the ESP that a new touch or release event is avilable to be read. It doens't do anything functionally right now, but if you watch the log file, you will see that touch events are detected and that they trigger some log messages.
3 years ago
Introduced a HUB component + front panel IRQ handling A HUB component was introduced. This HUB component has all the knowledge about the Yeelight Bedside Lamp 2 hardware. It known what pins are used, that PWM frequencies to use, what pins to switch in binary mode, etc. etc. No configuration is required for this HUB component. It's automatically loaded when the light component is loaded. The light component will use the HUB component to access the pins that are required for driving the LED circuitry. Note that this simplifies the configuration by A LOT. There's no need anymore to configure the pinouts in the YAML file. This is a logical route to take, since we're talking about a factory-produced PCB with a soldered on ESP32 chip, which uses the same GPIO's and settings on all produced devices (I presume). It would be quite redundant to force every user into configuring these pinouts themselves. ** Beware to update your device yaml configuration ** There are a few pinouts left to move into the HUB. I will do that in the next commit. Your device yaml configuration can be simplified along with these changes. Some of the keys in the existing light configuration block will no longer work and will have to be removed (red, green, blue, white). ** Further development ** The HUB will be extended make it the central component that also handles the I2C communication. This way, there is a central place to regulate the traffic to and from the front panel. We will be able to build upon this by implementing extra, fully separated components that handle for example the front panel light level, the power button, the color button and the slider. ** Interrupt handler for the I2C IRQ trigger pin ** One requirement for the I2C communication has already been implemented: an interrupt handler for the GPIO that is used by the front panel to signal the ESP that a new touch or release event is avilable to be read. It doens't do anything functionally right now, but if you watch the log file, you will see that touch events are detected and that they trigger some log messages.
3 years ago
3 years ago
  1. #pragma once
  2. #include "../common.h"
  3. #include "../light_hal.h"
  4. #include "color_handler_chain.h"
  5. #include "light_transformer.h"
  6. #include "esphome/core/component.h"
  7. #include "esphome/components/ledc/ledc_output.h"
  8. namespace esphome {
  9. namespace xiaomi {
  10. namespace bslamp2 {
  11. /**
  12. * A LightOutput class for the Xiaomi Mijia Bedside Lamp 2.
  13. *
  14. * The function of this class is to translate a required light state
  15. * into actual physicial GPIO output signals to drive the device's LED
  16. * circuitry. It forms the glue between the physical device and the
  17. * logical light color input.
  18. */
  19. class XiaomiBslamp2LightOutput : public Component, public light::LightOutput {
  20. public:
  21. void set_parent(LightHAL *light) { light_ = light; }
  22. /**
  23. * Returns a LightTraits object, which is used to explain to the outside
  24. * world (e.g. Home Assistant) what features are supported by this device.
  25. */
  26. light::LightTraits get_traits() override {
  27. auto traits = light::LightTraits();
  28. traits.set_supported_color_modes({light::ColorMode::RGB, light::ColorMode::COLOR_TEMPERATURE});
  29. traits.set_min_mireds(MIRED_MIN);
  30. traits.set_max_mireds(MIRED_MAX);
  31. return traits;
  32. }
  33. std::unique_ptr<light::LightTransformer> create_default_transition() override {
  34. return make_unique<XiaomiBslamp2LightTransitionTransformer>(
  35. light_, light_mode_callback_, state_callback_);
  36. }
  37. void add_on_light_mode_callback(std::function<void(std::string)> &&callback) {
  38. light_mode_callback_.add(std::move(callback));
  39. }
  40. void add_on_state_callback(std::function<void(light::LightColorValues)> &&callback) {
  41. state_callback_.add(std::move(callback));
  42. }
  43. /**
  44. * Applies a requested light state to the physicial GPIO outputs.
  45. */
  46. void write_state(light::LightState *state) {
  47. auto values = state->current_values;
  48. color_handler_chain->set_light_color_values(values);
  49. light_mode_callback_.call(color_handler_chain->light_mode);
  50. state_callback_.call(values);
  51. // Note: one might think that it is more logical to turn on the LED
  52. // circuitry master switch after setting the individual channels,
  53. // but this is the order that was used by the original firmware. I
  54. // tried to stay as close as possible to the original behavior, so
  55. // that's why these GPIOs are turned on at this point.
  56. if (values.get_state() != 0)
  57. light_->turn_on();
  58. // Apply the GPIO output levels as defined by the color handler.
  59. light_->set_state(color_handler_chain);
  60. if (values.get_state() == 0)
  61. light_->turn_off();
  62. }
  63. protected:
  64. LightHAL *light_;
  65. ColorHandler *color_handler_chain = new ColorHandlerChain();
  66. CallbackManager<void(std::string)> light_mode_callback_{};
  67. CallbackManager<void(light::LightColorValues)> state_callback_{};
  68. };
  69. } // namespace bslamp2
  70. } // namespace xiaomi
  71. } // namespace esphome