ESPHome  2024.11.0
preferences.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_HOST
4 
6 #include <map>
7 
8 namespace esphome {
9 namespace host {
10 
12  public:
13  explicit HostPreferenceBackend(uint32_t key) { this->key_ = key; }
14 
15  bool save(const uint8_t *data, size_t len) override;
16  bool load(uint8_t *data, size_t len) override;
17 
18  protected:
19  uint32_t key_{};
20 };
21 
23  public:
24  bool sync() override;
25  bool reset() override;
26 
27  ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash) override;
28  ESPPreferenceObject make_preference(size_t length, uint32_t type) override {
29  return make_preference(length, type, false);
30  }
31 
32  bool save(uint32_t key, const uint8_t *data, size_t len) {
33  if (len > 255)
34  return false;
35  this->setup_();
36  std::vector vec(data, data + len);
37  this->data[key] = vec;
38  return true;
39  }
40 
41  bool load(uint32_t key, uint8_t *data, size_t len) {
42  if (len > 255)
43  return false;
44  this->setup_();
45  if (this->data.count(key) == 0)
46  return false;
47  auto vec = this->data[key];
48  if (vec.size() != len)
49  return false;
50  memcpy(data, vec.data(), len);
51  return true;
52  }
53 
54  protected:
55  void setup_();
56  bool setup_complete_{};
57  std::string filename_{};
58  std::map<uint32_t, std::vector<uint8_t>> data{};
59 };
60 void setup_preferences();
61 extern HostPreferences *host_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
62 
63 } // namespace host
64 } // namespace esphome
65 
66 #endif // USE_HOST
uint16_t sync
Definition: sun_gtil2.cpp:14
bool load(uint32_t key, uint8_t *data, size_t len)
Definition: preferences.h:41
void setup_preferences()
Definition: preferences.cpp:69
bool save(const uint8_t *data, size_t len) override
Definition: preferences.cpp:75
HostPreferences * host_preferences
Definition: preferences.cpp:81
uint8_t type
uint16_t reset
Definition: ina226.h:39
ESPPreferenceObject make_preference(size_t length, uint32_t type) override
Definition: preferences.h:28
bool load(uint8_t *data, size_t len) override
Definition: preferences.cpp:79
bool save(uint32_t key, const uint8_t *data, size_t len)
Definition: preferences.h:32
std::string size_t len
Definition: helpers.h:293
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7