ESPHome  2024.11.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  void setup() override;
22  void dump_config() override;
23  float get_setup_priority() const override { return setup_priority::DATA; }
24  void set_min_level(float min_level) { min_level_ = min_level; }
25  void set_idle_level(float idle_level) { idle_level_ = idle_level; }
26  void set_max_level(float max_level) { max_level_ = max_level; }
27  void set_restore(bool restore) { restore_ = restore; }
28  void set_auto_detach_time(uint32_t auto_detach_time) { auto_detach_time_ = auto_detach_time; }
29  void set_transition_length(uint32_t transition_length) { transition_length_ = transition_length; }
30 
31  bool has_reached_target() { return this->current_value_ == this->target_value_; }
32 
33  protected:
34  void save_level_(float v);
35 
37  float min_level_ = 0.0300f;
38  float idle_level_ = 0.0750f;
39  float max_level_ = 0.1200f;
40  bool restore_{false};
41  uint32_t auto_detach_time_ = 0;
42  uint32_t transition_length_ = 0;
44  uint8_t state_;
45  float target_value_ = 0;
46  float source_value_ = 0;
47  float current_value_ = 0;
48  uint32_t start_millis_ = 0;
49  enum State {
53  };
54 };
55 
56 template<typename... Ts> class ServoWriteAction : public Action<Ts...> {
57  public:
58  ServoWriteAction(Servo *servo) : servo_(servo) {}
59  TEMPLATABLE_VALUE(float, value)
60 
61  void play(Ts... x) override { this->servo_->write(this->value_.value(x...)); }
62 
63  protected:
65 };
66 
67 template<typename... Ts> class ServoDetachAction : public Action<Ts...> {
68  public:
69  ServoDetachAction(Servo *servo) : servo_(servo) {}
70 
71  void play(Ts... x) override { this->servo_->detach(); }
72 
73  protected:
75 };
76 
77 } // namespace servo
78 } // namespace esphome
uint8_t state_
Definition: servo.h:44
uint32_t transition_length_
Definition: servo.h:42
void save_level_(float v)
Definition: servo.cpp:101
float get_setup_priority() const override
Definition: servo.h:23
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void play(Ts... x) override
Definition: servo.h:71
void write(float value)
Definition: servo.cpp:70
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:45
uint16_t x
Definition: tt21100.cpp:17
void set_transition_length(uint32_t transition_length)
Definition: servo.h:29
void set_auto_detach_time(uint32_t auto_detach_time)
Definition: servo.h:28
uint32_t start_millis_
Definition: servo.h:48
float current_value_
Definition: servo.h:47
uint32_t auto_detach_time_
Definition: servo.h:41
ESPPreferenceObject rtc_
Definition: servo.h:43
bool has_reached_target()
Definition: servo.h:31
void loop() override
Definition: servo.cpp:38
TEMPLATABLE_VALUE(float, value) void play(Ts... x) override
Definition: servo.h:59
void internal_write(float value)
Definition: servo.cpp:84
output::FloatOutput * output_
Definition: servo.h:36
uint32_t global_servo_id
Definition: servo.cpp:11
float source_value_
Definition: servo.h:46
void setup() override
Definition: servo.cpp:22
void dump_config() override
Definition: servo.cpp:13
ServoDetachAction(Servo *servo)
Definition: servo.h:69
ServoWriteAction(Servo *servo)
Definition: servo.h:58
void set_idle_level(float idle_level)
Definition: servo.h:25
void set_max_level(float max_level)
Definition: servo.h:26
void set_restore(bool restore)
Definition: servo.h:27
void set_output(output::FloatOutput *output)
Definition: servo.h:16
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_min_level(float min_level)
Definition: servo.h:24