ESPHome  2024.6.6
shelly_dimmer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ESP8266
4 
6 #include "esphome/core/log.h"
10 
11 #include <array>
12 
13 namespace esphome {
14 namespace shelly_dimmer {
15 
17  private:
18  static constexpr uint16_t SHELLY_DIMMER_BUFFER_SIZE = 256;
19 
20  public:
21  float get_setup_priority() const override { return setup_priority::LATE; }
22 
23  void setup() override;
24  void update() override;
25  void dump_config() override;
26 
28  auto traits = light::LightTraits();
29  traits.set_supported_color_modes({light::ColorMode::BRIGHTNESS});
30  return traits;
31  }
32 
33  void setup_state(light::LightState *state) override { this->state_ = state; }
34  void write_state(light::LightState *state) override;
35 
36  void set_nrst_pin(GPIOPin *nrst_pin) { this->pin_nrst_ = nrst_pin; }
37  void set_boot0_pin(GPIOPin *boot0_pin) { this->pin_boot0_ = boot0_pin; }
38 
39  void set_leading_edge(bool leading_edge) { this->leading_edge_ = leading_edge; }
40  void set_warmup_brightness(uint16_t warmup_brightness) { this->warmup_brightness_ = warmup_brightness; }
41  void set_warmup_time(uint16_t warmup_time) { this->warmup_time_ = warmup_time; }
42  void set_fade_rate(uint16_t fade_rate) { this->fade_rate_ = fade_rate; }
43  void set_min_brightness(uint16_t min_brightness) { this->min_brightness_ = min_brightness; }
44  void set_max_brightness(uint16_t max_brightness) { this->max_brightness_ = max_brightness; }
45 
46  void set_power_sensor(sensor::Sensor *power_sensor) { this->power_sensor_ = power_sensor; }
47  void set_voltage_sensor(sensor::Sensor *voltage_sensor) { this->voltage_sensor_ = voltage_sensor; }
48  void set_current_sensor(sensor::Sensor *current_sensor) { this->current_sensor_ = current_sensor; }
49 
50  protected:
53 
54  // Frame parser state.
55  uint8_t seq_{0};
56  std::array<uint8_t, SHELLY_DIMMER_BUFFER_SIZE> buffer_;
57  uint8_t buffer_pos_{0};
58 
59  // Firmware version.
60  uint8_t version_major_;
61  uint8_t version_minor_;
62 
63  // Configuration.
64  bool leading_edge_{false};
65  uint16_t warmup_brightness_{100};
66  uint16_t warmup_time_{20};
67  uint16_t fade_rate_{0};
68  uint16_t min_brightness_{0};
69  uint16_t max_brightness_{1000};
70 
75 
76  bool ready_{false};
77  uint16_t brightness_;
78 
80  uint16_t convert_brightness_(float brightness);
81 
83  void send_brightness_(uint16_t brightness);
84 
86  void send_settings_();
87 
89  bool upgrade_firmware_();
90 
92  bool send_command_(uint8_t cmd, const uint8_t *payload, uint8_t len);
93 
95  size_t frame_command_(uint8_t *data, uint8_t cmd, const uint8_t *payload, size_t len);
96 
100  int handle_byte_(uint8_t c);
101 
103  bool read_frame_();
104 
106  bool handle_frame_();
107 
109  void reset_(bool boot0);
110 
112  void reset_normal_boot_();
113 
115  void reset_dfu_boot_();
116 };
117 
118 } // namespace shelly_dimmer
119 } // namespace esphome
120 
121 #endif // USE_ESP8266
size_t frame_command_(uint8_t *data, uint8_t cmd, const uint8_t *payload, size_t len)
Frames a given command payload.
void reset_normal_boot_()
Reset STM32 to boot the regular firmware.
void set_warmup_time(uint16_t warmup_time)
Definition: shelly_dimmer.h:41
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:34
void send_settings_()
Sends dimmer configuration.
Interface to write LightStates to hardware.
Definition: light_output.h:12
std::array< uint8_t, SHELLY_DIMMER_BUFFER_SIZE > buffer_
Definition: shelly_dimmer.h:56
void set_max_brightness(uint16_t max_brightness)
Definition: shelly_dimmer.h:44
int handle_byte_(uint8_t c)
Handles a single byte as part of a protocol frame.
void set_min_brightness(uint16_t min_brightness)
Definition: shelly_dimmer.h:43
void reset_dfu_boot_()
Reset STM32 to boot into DFU mode to enable firmware upgrades.
bool read_frame_()
Reads a response frame.
void write_state(light::LightState *state) override
const float LATE
For components that should be initialized at the very end of the setup process.
Definition: component.cpp:28
void set_leading_edge(bool leading_edge)
Definition: shelly_dimmer.h:39
light::LightTraits get_traits() override
Definition: shelly_dimmer.h:27
This class simplifies creating components that periodically check a state.
Definition: component.h:283
void reset_(bool boot0)
Reset STM32 with the BOOT0 pin set to the given value.
void set_fade_rate(uint16_t fade_rate)
Definition: shelly_dimmer.h:42
bool send_command_(uint8_t cmd, const uint8_t *payload, uint8_t len)
Sends a command and waits for an acknowledgement.
void set_voltage_sensor(sensor::Sensor *voltage_sensor)
Definition: shelly_dimmer.h:47
void set_warmup_brightness(uint16_t warmup_brightness)
Definition: shelly_dimmer.h:40
uint16_t convert_brightness_(float brightness)
Convert relative brightness into a dimmer brightness value.
bool upgrade_firmware_()
Performs a firmware upgrade.
float get_setup_priority() const override
Definition: shelly_dimmer.h:21
Master brightness of the light can be controlled.
This class is used to represent the capabilities of a light.
Definition: light_traits.h:11
void set_power_sensor(sensor::Sensor *power_sensor)
Definition: shelly_dimmer.h:46
std::string size_t len
Definition: helpers.h:292
bool handle_frame_()
Handles a complete frame.
void setup_state(light::LightState *state) override
Definition: shelly_dimmer.h:33
void set_current_sensor(sensor::Sensor *current_sensor)
Definition: shelly_dimmer.h:48
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void set_nrst_pin(GPIOPin *nrst_pin)
Definition: shelly_dimmer.h:36
Base-class for all sensors.
Definition: sensor.h:57
void set_boot0_pin(GPIOPin *boot0_pin)
Definition: shelly_dimmer.h:37
stm32_cmd_t * cmd
Definition: stm32flash.h:96
void send_brightness_(uint16_t brightness)
Sends the given brightness value.
bool state
Definition: fan.h:34