ESPHome  2024.10.2
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  bool is_running_configured_version() const;
24  void handle_firmware();
25  void setup() override;
26  void update() override;
27  void dump_config() override;
28 
30  auto traits = light::LightTraits();
31  traits.set_supported_color_modes({light::ColorMode::BRIGHTNESS});
32  return traits;
33  }
34 
35  void setup_state(light::LightState *state) override { this->state_ = state; }
36  void write_state(light::LightState *state) override;
37 
38  void set_nrst_pin(GPIOPin *nrst_pin) { this->pin_nrst_ = nrst_pin; }
39  void set_boot0_pin(GPIOPin *boot0_pin) { this->pin_boot0_ = boot0_pin; }
40 
41  void set_leading_edge(bool leading_edge) { this->leading_edge_ = leading_edge; }
42  void set_warmup_brightness(uint16_t warmup_brightness) { this->warmup_brightness_ = warmup_brightness; }
43  void set_warmup_time(uint16_t warmup_time) { this->warmup_time_ = warmup_time; }
44  void set_fade_rate(uint16_t fade_rate) { this->fade_rate_ = fade_rate; }
45  void set_min_brightness(uint16_t min_brightness) { this->min_brightness_ = min_brightness; }
46  void set_max_brightness(uint16_t max_brightness) { this->max_brightness_ = max_brightness; }
47 
48  void set_power_sensor(sensor::Sensor *power_sensor) { this->power_sensor_ = power_sensor; }
49  void set_voltage_sensor(sensor::Sensor *voltage_sensor) { this->voltage_sensor_ = voltage_sensor; }
50  void set_current_sensor(sensor::Sensor *current_sensor) { this->current_sensor_ = current_sensor; }
51 
52  protected:
55 
56  // Frame parser state.
57  uint8_t seq_{0};
58  std::array<uint8_t, SHELLY_DIMMER_BUFFER_SIZE> buffer_;
59  uint8_t buffer_pos_{0};
60 
61  // Firmware version.
62  uint8_t version_major_;
63  uint8_t version_minor_;
64 
65  // Configuration.
66  bool leading_edge_{false};
67  uint16_t warmup_brightness_{100};
68  uint16_t warmup_time_{20};
69  uint16_t fade_rate_{0};
70  uint16_t min_brightness_{0};
71  uint16_t max_brightness_{1000};
72 
77 
78  bool ready_{false};
79  uint16_t brightness_;
80 
82  uint16_t convert_brightness_(float brightness);
83 
85  void send_brightness_(uint16_t brightness);
86 
88  void send_settings_();
89 
91  bool upgrade_firmware_();
92 
94  bool send_command_(uint8_t cmd, const uint8_t *payload, uint8_t len);
95 
97  size_t frame_command_(uint8_t *data, uint8_t cmd, const uint8_t *payload, size_t len);
98 
102  int handle_byte_(uint8_t c);
103 
105  bool read_frame_();
106 
108  bool handle_frame_();
109 
111  void reset_(bool boot0);
112 
114  void reset_normal_boot_();
115 
117  void reset_dfu_boot_();
118 };
119 
120 } // namespace shelly_dimmer
121 } // namespace esphome
122 
123 #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:43
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:58
void set_max_brightness(uint16_t max_brightness)
Definition: shelly_dimmer.h:46
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:45
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:41
light::LightTraits get_traits() override
Definition: shelly_dimmer.h:29
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:44
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:49
void set_warmup_brightness(uint16_t warmup_brightness)
Definition: shelly_dimmer.h:42
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:48
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:35
void set_current_sensor(sensor::Sensor *current_sensor)
Definition: shelly_dimmer.h:50
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_nrst_pin(GPIOPin *nrst_pin)
Definition: shelly_dimmer.h:38
Base-class for all sensors.
Definition: sensor.h:57
void set_boot0_pin(GPIOPin *boot0_pin)
Definition: shelly_dimmer.h:39
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