ESPHome  2022.8.0
shelly_dimmer.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/log.h"
8 
9 #include <array>
10 
11 namespace esphome {
12 namespace shelly_dimmer {
13 
15  private:
16  static constexpr uint16_t SHELLY_DIMMER_BUFFER_SIZE = 256;
17 
18  public:
19  float get_setup_priority() const override { return setup_priority::LATE; }
20 
21  void setup() override;
22  void update() override;
23  void dump_config() override;
24 
26  auto traits = light::LightTraits();
27  traits.set_supported_color_modes({light::ColorMode::BRIGHTNESS});
28  return traits;
29  }
30 
31  void setup_state(light::LightState *state) override { this->state_ = state; }
32  void write_state(light::LightState *state) override;
33 
34  void set_nrst_pin(GPIOPin *nrst_pin) { this->pin_nrst_ = nrst_pin; }
35  void set_boot0_pin(GPIOPin *boot0_pin) { this->pin_boot0_ = boot0_pin; }
36 
37  void set_leading_edge(bool leading_edge) { this->leading_edge_ = leading_edge; }
38  void set_warmup_brightness(uint16_t warmup_brightness) { this->warmup_brightness_ = warmup_brightness; }
39  void set_warmup_time(uint16_t warmup_time) { this->warmup_time_ = warmup_time; }
40  void set_fade_rate(uint16_t fade_rate) { this->fade_rate_ = fade_rate; }
41  void set_min_brightness(uint16_t min_brightness) { this->min_brightness_ = min_brightness; }
42  void set_max_brightness(uint16_t max_brightness) { this->max_brightness_ = max_brightness; }
43 
44  void set_power_sensor(sensor::Sensor *power_sensor) { this->power_sensor_ = power_sensor; }
45  void set_voltage_sensor(sensor::Sensor *voltage_sensor) { this->voltage_sensor_ = voltage_sensor; }
46  void set_current_sensor(sensor::Sensor *current_sensor) { this->current_sensor_ = current_sensor; }
47 
48  protected:
51 
52  // Frame parser state.
53  uint8_t seq_{0};
54  std::array<uint8_t, SHELLY_DIMMER_BUFFER_SIZE> buffer_;
55  uint8_t buffer_pos_{0};
56 
57  // Firmware version.
58  uint8_t version_major_;
59  uint8_t version_minor_;
60 
61  // Configuration.
62  bool leading_edge_{false};
63  uint16_t warmup_brightness_{100};
64  uint16_t warmup_time_{20};
65  uint16_t fade_rate_{0};
66  uint16_t min_brightness_{0};
67  uint16_t max_brightness_{1000};
68 
73 
74  bool ready_{false};
75  uint16_t brightness_;
76 
78  uint16_t convert_brightness_(float brightness);
79 
81  void send_brightness_(uint16_t brightness);
82 
84  void send_settings_();
85 
87  bool upgrade_firmware_();
88 
90  bool send_command_(uint8_t cmd, const uint8_t *payload, uint8_t len);
91 
93  size_t frame_command_(uint8_t *data, uint8_t cmd, const uint8_t *payload, size_t len);
94 
98  int handle_byte_(uint8_t c);
99 
101  bool read_frame_();
102 
104  bool handle_frame_();
105 
107  void reset_(bool boot0);
108 
110  void reset_normal_boot_();
111 
113  void reset_dfu_boot_();
114 };
115 
116 } // namespace shelly_dimmer
117 } // namespace esphome
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:39
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:32
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:54
void set_max_brightness(uint16_t max_brightness)
Definition: shelly_dimmer.h:42
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:41
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:26
void set_leading_edge(bool leading_edge)
Definition: shelly_dimmer.h:37
light::LightTraits get_traits() override
Definition: shelly_dimmer.h:25
This class simplifies creating components that periodically check a state.
Definition: component.h:266
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:40
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:45
void set_warmup_brightness(uint16_t warmup_brightness)
Definition: shelly_dimmer.h:38
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:19
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:44
std::string size_t len
Definition: helpers.h:278
bool handle_frame_()
Handles a complete frame.
void setup_state(light::LightState *state) override
Definition: shelly_dimmer.h:31
void set_current_sensor(sensor::Sensor *current_sensor)
Definition: shelly_dimmer.h:46
Definition: a4988.cpp:4
void set_nrst_pin(GPIOPin *nrst_pin)
Definition: shelly_dimmer.h:34
Base-class for all sensors.
Definition: sensor.h:48
void set_boot0_pin(GPIOPin *boot0_pin)
Definition: shelly_dimmer.h:35
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