ESPHome  2022.6.3
globals_component.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/helpers.h"
6 #include <cstring>
7 
8 namespace esphome {
9 namespace globals {
10 
11 template<typename T> class GlobalsComponent : public Component {
12  public:
13  using value_type = T;
14  explicit GlobalsComponent() = default;
15  explicit GlobalsComponent(T initial_value) : value_(initial_value) {}
16  explicit GlobalsComponent(std::array<typename std::remove_extent<T>::type, std::extent<T>::value> initial_value) {
17  memcpy(this->value_, initial_value.data(), sizeof(T));
18  }
19 
20  T &value() { return this->value_; }
21  void setup() override {}
22 
23  protected:
24  T value_{};
25 };
26 
27 template<typename T> class RestoringGlobalsComponent : public Component {
28  public:
29  using value_type = T;
30  explicit RestoringGlobalsComponent() = default;
31  explicit RestoringGlobalsComponent(T initial_value) : value_(initial_value) {}
33  std::array<typename std::remove_extent<T>::type, std::extent<T>::value> initial_value) {
34  memcpy(this->value_, initial_value.data(), sizeof(T));
35  }
36 
37  T &value() { return this->value_; }
38 
39  void setup() override {
40  this->rtc_ = global_preferences->make_preference<T>(1944399030U ^ this->name_hash_);
41  this->rtc_.load(&this->value_);
42  memcpy(&this->prev_value_, &this->value_, sizeof(T));
43  }
44 
45  float get_setup_priority() const override { return setup_priority::HARDWARE; }
46 
47  void loop() override {
48  int diff = memcmp(&this->value_, &this->prev_value_, sizeof(T));
49  if (diff != 0) {
50  this->rtc_.save(&this->value_);
51  memcpy(&this->prev_value_, &this->value_, sizeof(T));
52  }
53  }
54 
55  void set_name_hash(uint32_t name_hash) { this->name_hash_ = name_hash; }
56 
57  protected:
58  T value_{};
59  T prev_value_{};
60  uint32_t name_hash_{};
62 };
63 
64 template<class C, typename... Ts> class GlobalVarSetAction : public Action<Ts...> {
65  public:
66  explicit GlobalVarSetAction(C *parent) : parent_(parent) {}
67 
68  using T = typename C::value_type;
69 
70  TEMPLATABLE_VALUE(T, value);
71 
72  void play(Ts... x) override { this->parent_->value() = this->value_.value(x...); }
73 
74  protected:
75  C *parent_;
76 };
77 
78 template<typename T> T &id(GlobalsComponent<T> *value) { return value->value(); }
79 template<typename T> T &id(RestoringGlobalsComponent<T> *value) { return value->value(); }
80 
81 } // namespace globals
82 } // namespace esphome
GlobalsComponent(std::array< typename std::remove_extent< T >::type, std::extent< T >::value > initial_value)
ESPPreferences * global_preferences
uint8_t type
RestoringGlobalsComponent(std::array< typename std::remove_extent< T >::type, std::extent< T >::value > initial_value)
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:17
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
Definition: a4988.cpp:4
T & id(GlobalsComponent< T > *value)