ESPHome 2026.8.0b4
Loading...
Searching...
No Matches
preferences.h
Go to the documentation of this file.
1#pragma once
2#ifdef USE_ESP32
3
5#include <soc/soc_caps.h>
6
7// RTC-backed preference storage is compiled in only when a config option actually selects it
8// (USE_ESP32_RTC_PREFERENCES, emitted during code generation) and the variant has RTC memory
9// (SOC_RTC_MEM_SUPPORTED; the ESP32-C2 and -C61 have none). Otherwise in_flash=false falls
10// back to NVS and no RTC memory is reserved.
11#if defined(USE_ESP32_RTC_PREFERENCES) && SOC_RTC_MEM_SUPPORTED
12#define USE_ESP32_RTC_PREFERENCES_STORAGE
13#endif
14
15namespace esphome::esp32 {
16
17struct NVSData;
18
19class ESP32Preferences final : public PreferencesMixin<ESP32Preferences> {
20 public:
22 void open();
24 // Two-argument form defaults to NVS (flash) storage, preserving historic ESP32 behavior.
27 bool load_from_key(uint32_t type, uint8_t *data, size_t len);
28 bool sync();
29 bool reset();
30
32
33 protected:
35 bool is_changed_(uint32_t nvs_handle, const NVSData &to_save, const char *key_str);
36
37#ifdef USE_ESP32_RTC_PREFERENCES_STORAGE
38 // RTC-backed storage (in_flash=false).
40 // Next free word offset in the RTC storage region (bump allocated in make_preference order).
42#endif
43};
44
46
47} // namespace esphome::esp32
48
49DECLARE_PREFERENCE_ALIASES(esphome::esp32::ESP32Preferences)
50
51#endif // USE_ESP32
CRTP mixin providing type-safe template make_preference<T>() helpers.
bool is_changed_(uint32_t nvs_handle, const NVSData &to_save, const char *key_str)
ESP32PreferenceBackend make_backend_(uint32_t type) const
ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)
bool load_from_key(uint32_t type, uint8_t *data, size_t len)
One-shot read of a stored preference by key, without allocating a backend.
ESPPreferenceObject make_rtc_preference_(size_t length, uint32_t type)
uint16_t type
void setup_preferences()
const void size_t len
Definition hal.h:64
static void uint32_t
uint16_t length
Definition tt21100.cpp:0