ESPHome 2026.4.0-dev
Loading...
Searching...
No Matches
progmem.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <cstdint>
6#include <new>
7
8#include "esphome/core/hal.h" // For PROGMEM definition
9
10// Platform-agnostic macros for PROGMEM string handling
11// On ESP8266/Arduino: Use Arduino's F() macro for PROGMEM strings
12// On other platforms: Use plain strings (no PROGMEM)
13
14#ifdef USE_ESP8266
15// ESP8266 uses Arduino macros
16#define ESPHOME_F(string_literal) F(string_literal)
17#define ESPHOME_PGM_P PGM_P
18#define ESPHOME_PSTR(s) PSTR(s)
19#define ESPHOME_strncpy_P strncpy_P
20#define ESPHOME_strncat_P strncat_P
21#define ESPHOME_snprintf_P snprintf_P
22#define ESPHOME_strcmp_P strcmp_P
23#define ESPHOME_strcasecmp_P strcasecmp_P
24#define ESPHOME_strncmp_P strncmp_P
25#define ESPHOME_strncasecmp_P strncasecmp_P
26// Type for pointers to PROGMEM strings (for use with ESPHOME_F return values)
27using ProgmemStr = const __FlashStringHelper *;
28#else
29#define ESPHOME_F(string_literal) (string_literal)
30#define ESPHOME_PGM_P const char *
31#define ESPHOME_PSTR(s) (s)
32#define ESPHOME_strncpy_P strncpy
33#define ESPHOME_strncat_P strncat
34#define ESPHOME_snprintf_P snprintf
35#define ESPHOME_strcmp_P strcmp
36#define ESPHOME_strcasecmp_P strcasecmp
37#define ESPHOME_strncmp_P strncmp
38#define ESPHOME_strncasecmp_P strncasecmp
39// Type for pointers to strings (no PROGMEM on non-ESP8266 platforms)
40using ProgmemStr = const char *;
41#endif
42
43namespace esphome {
44
46template<size_t N> struct FixedString {
47 char data[N]{};
48 constexpr FixedString(const char (&str)[N]) {
49 for (size_t i = 0; i < N; ++i)
50 data[i] = str[i];
51 }
52 constexpr size_t size() const { return N - 1; } // exclude null terminator
53};
54
63template<FixedString... Strs> struct ProgmemStringTable {
64 static constexpr size_t COUNT = sizeof...(Strs);
65 static constexpr size_t BLOB_SIZE = (0 + ... + (Strs.size() + 1));
66
68 static constexpr auto make_blob() {
69 std::array<char, BLOB_SIZE> result{};
70 size_t pos = 0;
71 auto copy = [&](const auto &str) {
72 for (size_t i = 0; i <= str.size(); ++i)
73 result[pos++] = str.data[i];
74 };
75 (copy(Strs), ...);
76 return result;
77 }
78
80 static constexpr auto make_offsets() {
81 static_assert(COUNT > 0, "PROGMEM_STRING_TABLE must contain at least one string");
82 static_assert(COUNT <= 255, "PROGMEM_STRING_TABLE supports at most 255 strings with uint8_t indices");
83 static_assert(BLOB_SIZE <= 255, "PROGMEM_STRING_TABLE blob exceeds 255 bytes; use fewer/shorter strings");
84 std::array<uint8_t, COUNT> result{};
85 size_t pos = 0, idx = 0;
86 ((result[idx++] = static_cast<uint8_t>(pos), pos += Strs.size() + 1), ...);
87 return result;
88 }
89};
90
91// Forward declaration for LogString (defined in log.h)
92struct LogString;
93
97#define PROGMEM_STRING_TABLE(Name, ...) \
98 struct Name { \
99 using Table = ::esphome::ProgmemStringTable<__VA_ARGS__>; \
100 static constexpr size_t COUNT = Table::COUNT; \
101 static constexpr uint8_t LAST_INDEX = COUNT - 1; \
102 static constexpr size_t BLOB_SIZE = Table::BLOB_SIZE; \
103 static constexpr auto BLOB PROGMEM = Table::make_blob(); \
104 static constexpr auto OFFSETS PROGMEM = Table::make_offsets(); \
105 static const char *get_(uint8_t idx, uint8_t fallback) { \
106 if (idx >= COUNT) \
107 idx = fallback; \
108 /* std::launder is used here to prevent the inter-procedural analysis that */ \
109 /* causes the false positive that the string is not null terminated */ \
110 return std::launder(&BLOB[::esphome::progmem_read_byte(&OFFSETS[idx])]); \
111 } \
112 static ::ProgmemStr get_progmem_str(uint8_t idx, uint8_t fallback) { \
113 return reinterpret_cast<::ProgmemStr>(get_(idx, fallback)); \
114 } \
115 static const ::esphome::LogString *get_log_str(uint8_t idx, uint8_t fallback) { \
116 return reinterpret_cast<const ::esphome::LogString *>(get_(idx, fallback)); \
117 } \
118 }
119
120} // namespace esphome
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
size_t size_t pos
Definition helpers.h:1082
const __FlashStringHelper * ProgmemStr
Definition progmem.h:27
Helper for C++20 string literal template arguments.
Definition progmem.h:46
constexpr size_t size() const
Definition progmem.h:52
constexpr FixedString(const char(&str)[N])
Definition progmem.h:48
Compile-time string table that packs strings into a single blob with offset lookup.
Definition progmem.h:63
static constexpr size_t COUNT
Definition progmem.h:64
static constexpr size_t BLOB_SIZE
Definition progmem.h:65
static constexpr auto make_blob()
Generate packed string blob at compile time.
Definition progmem.h:68
static constexpr auto make_offsets()
Generate offset table at compile time (uint8_t limits blob to 255 bytes)
Definition progmem.h:80