ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
preferences.h
Go to the documentation of this file.
1#pragma once
2#ifdef USE_HOST
3
5#include <cstring>
6#include <map>
7#include <string>
8#include <vector>
9
10namespace esphome::host {
11
12class HostPreferences final : public PreferencesMixin<HostPreferences> {
13 public:
15 bool sync();
16 bool reset();
17
20
21 bool save(uint32_t key, const uint8_t *data, size_t len) {
22 if (len > 255)
23 return false;
24 this->setup_();
25 std::vector vec(data, data + len);
26 this->data[key] = vec;
27 return true;
28 }
29
30 bool load(uint32_t key, uint8_t *data, size_t len) {
31 if (len > 255)
32 return false;
33 this->setup_();
34 auto it = this->data.find(key);
35 if (it == this->data.end())
36 return false;
37 const auto &vec = it->second;
38 if (vec.size() != len)
39 return false;
40 memcpy(data, vec.data(), len);
41 return true;
42 }
43
44 protected:
45 void setup_();
47 std::string filename_{};
48 std::map<uint32_t, std::vector<uint8_t>> data{};
49};
50
52extern HostPreferences *host_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
53
54} // namespace esphome::host
55
56DECLARE_PREFERENCE_ALIASES(esphome::host::HostPreferences)
57
58#endif // USE_HOST
CRTP mixin providing type-safe template make_preference<T>() helpers.
bool load(uint32_t key, uint8_t *data, size_t len)
Definition preferences.h:30
bool save(uint32_t key, const uint8_t *data, size_t len)
Definition preferences.h:21
ESPPreferenceObject make_preference(size_t length, uint32_t type)
Definition preferences.h:19
ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)
std::map< uint32_t, std::vector< uint8_t > > data
Definition preferences.h:48
uint16_t type
void setup_preferences()
HostPreferences * host_preferences
std::string size_t len
Definition helpers.h:1045
static void uint32_t
uint16_t length
Definition tt21100.cpp:0