ESPHome  2023.5.5
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 { store_value_(); }
48 
49  void on_shutdown() override { store_value_(); }
50 
51  void set_name_hash(uint32_t name_hash) { this->name_hash_ = name_hash; }
52 
53  protected:
54  void store_value_() {
55  int diff = memcmp(&this->value_, &this->prev_value_, sizeof(T));
56  if (diff != 0) {
57  this->rtc_.save(&this->value_);
58  memcpy(&this->prev_value_, &this->value_, sizeof(T));
59  }
60  }
61 
62  T value_{};
63  T prev_value_{};
64  uint32_t name_hash_{};
66 };
67 
68 template<class C, typename... Ts> class GlobalVarSetAction : public Action<Ts...> {
69  public:
70  explicit GlobalVarSetAction(C *parent) : parent_(parent) {}
71 
72  using T = typename C::value_type;
73 
74  TEMPLATABLE_VALUE(T, value);
75 
76  void play(Ts... x) override { this->parent_->value() = this->value_.value(x...); }
77 
78  protected:
79  C *parent_;
80 };
81 
82 template<typename T> T &id(GlobalsComponent<T> *value) { return value->value(); }
83 template<typename T> T &id(RestoringGlobalsComponent<T> *value) { return value->value(); }
84 
85 } // namespace globals
86 } // 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)