ESPHome  2022.8.0
servo.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/helpers.h"
8 
9 namespace esphome {
10 namespace servo {
11 
12 extern uint32_t global_servo_id; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
13 
14 class Servo : public Component {
15  public:
16  void set_output(output::FloatOutput *output) { output_ = output; }
17  void loop() override;
18  void write(float value);
19  void internal_write(float value);
20  void detach() {
21  this->output_->set_level(0.0f);
22  this->save_level_(0.0f);
23  }
24  void setup() override {
25  float v;
26  if (this->restore_) {
28  global_servo_id++;
29  if (this->rtc_.load(&v)) {
30  this->output_->set_level(v);
31  return;
32  }
33  }
34  this->detach();
35  }
36  void dump_config() override;
37  float get_setup_priority() const override { return setup_priority::DATA; }
38  void set_min_level(float min_level) { min_level_ = min_level; }
39  void set_idle_level(float idle_level) { idle_level_ = idle_level; }
40  void set_max_level(float max_level) { max_level_ = max_level; }
41  void set_restore(bool restore) { restore_ = restore; }
42  void set_auto_detach_time(uint32_t auto_detach_time) { auto_detach_time_ = auto_detach_time; }
43  void set_transition_length(uint32_t transition_length) { transition_length_ = transition_length; }
44 
45  protected:
46  void save_level_(float v) { this->rtc_.save(&v); }
47 
49  float min_level_ = 0.0300f;
50  float idle_level_ = 0.0750f;
51  float max_level_ = 0.1200f;
52  bool restore_{false};
53  uint32_t auto_detach_time_ = 0;
54  uint32_t transition_length_ = 0;
56  uint8_t state_;
57  float target_value_ = 0;
58  float source_value_ = 0;
59  float current_value_ = 0;
60  uint32_t start_millis_ = 0;
61  enum State {
65  };
66 };
67 
68 template<typename... Ts> class ServoWriteAction : public Action<Ts...> {
69  public:
70  ServoWriteAction(Servo *servo) : servo_(servo) {}
71  TEMPLATABLE_VALUE(float, value)
72 
73  void play(Ts... x) override { this->servo_->write(this->value_.value(x...)); }
74 
75  protected:
77 };
78 
79 template<typename... Ts> class ServoDetachAction : public Action<Ts...> {
80  public:
81  ServoDetachAction(Servo *servo) : servo_(servo) {}
82 
83  void play(Ts... x) override { this->servo_->detach(); }
84 
85  protected:
87 };
88 
89 } // namespace servo
90 } // namespace esphome
uint8_t state_
Definition: servo.h:56
uint32_t transition_length_
Definition: servo.h:54
void save_level_(float v)
Definition: servo.h:46
float get_setup_priority() const override
Definition: servo.h:37
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
void play(Ts... x) override
Definition: servo.h:83
void write(float value)
Definition: servo.cpp:54
Base class for all output components that can output a variable level, like PWM.
Definition: float_output.h:31
float target_value_
Definition: servo.h:57
void set_transition_length(uint32_t transition_length)
Definition: servo.h:43
void set_auto_detach_time(uint32_t auto_detach_time)
Definition: servo.h:42
uint32_t start_millis_
Definition: servo.h:60
bool save(const T *src)
Definition: preferences.h:21
void set_level(float state)
Set the level of this float output, this is called from the front-end.
float current_value_
Definition: servo.h:59
uint32_t auto_detach_time_
Definition: servo.h:53
ESPPreferenceObject rtc_
Definition: servo.h:55
void loop() override
Definition: servo.cpp:21
TEMPLATABLE_VALUE(float, value) void play(Ts... x) override
Definition: servo.h:71
void internal_write(float value)
Definition: servo.cpp:65
ESPPreferences * global_preferences
output::FloatOutput * output_
Definition: servo.h:48
uint32_t global_servo_id
Definition: servo.cpp:10
float source_value_
Definition: servo.h:58
void setup() override
Definition: servo.h:24
void dump_config() override
Definition: servo.cpp:12
ServoDetachAction(Servo *servo)
Definition: servo.h:81
ServoWriteAction(Servo *servo)
Definition: servo.h:70
void set_idle_level(float idle_level)
Definition: servo.h:39
void set_max_level(float max_level)
Definition: servo.h:40
void set_restore(bool restore)
Definition: servo.h:41
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
void set_output(output::FloatOutput *output)
Definition: servo.h:16
Definition: a4988.cpp:4
void set_min_level(float min_level)
Definition: servo.h:38