1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-31 15:12:06 +00:00
This commit is contained in:
J. Nick Koston
2025-10-24 14:26:17 -07:00
parent 45770811d2
commit 54fb391f13
2 changed files with 15 additions and 16 deletions

View File

@@ -22,7 +22,7 @@ namespace sntp {
/// \see https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
class SNTPComponent : public time::RealTimeClock {
public:
SNTPComponent() = default;
template<typename... Args> SNTPComponent(Args... servers) : servers_{servers...} {}
void setup() override;
void dump_config() override;
@@ -33,15 +33,14 @@ class SNTPComponent : public time::RealTimeClock {
void time_synced();
protected:
#ifdef USE_ESP8266
// On ESP8266, store pointers to PROGMEM strings to save RAM
std::array<PGM_P, SNTP_SERVER_COUNT> servers_{};
std::array<PGM_P, SNTP_SERVER_COUNT> servers_;
#else
// On other platforms, store regular const char pointers
std::array<const char *, SNTP_SERVER_COUNT> servers_{};
std::array<const char *, SNTP_SERVER_COUNT> servers_;
#endif
protected:
bool has_time_{false};
#if defined(USE_ESP32)

View File

@@ -49,15 +49,10 @@ async def to_code(config):
# Define server count at compile time
cg.add_define("SNTP_SERVER_COUNT", server_count)
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await time_.register_time(var, config)
# Generate PROGMEM strings for ESP8266, regular strings for other platforms
if CORE.is_esp8266:
# On ESP8266, use PROGMEM to store strings in flash
# Use ProgmemAssignmentExpression to generate: static const char name[] PROGMEM = "value";
server_vars = []
for i, server in enumerate(servers):
var_name = f"{config[CONF_ID].id}_server_{i}"
# Create PROGMEM string: static const char var_name[] PROGMEM = "server";
@@ -65,12 +60,17 @@ async def to_code(config):
"char", var_name, cg.safe_exp(server)
)
cg.add(assignment)
# Assign pointer to array element
cg.add(cg.RawStatement(f"{var}->servers_[{i}] = {var_name};"))
server_vars.append(cg.RawExpression(var_name))
# Pass PROGMEM string pointers to constructor using ArrayInitializer
var = cg.new_Pvariable(config[CONF_ID], cg.ArrayInitializer(*server_vars))
else:
# On other platforms, use regular string literals
for i, server in enumerate(servers):
cg.add(cg.RawStatement(f"{var}->servers_[{i}] = {cg.safe_exp(server)};"))
# On other platforms, pass regular string literals to constructor
var = cg.new_Pvariable(
config[CONF_ID], cg.ArrayInitializer(*[cg.safe_exp(s) for s in servers])
)
await cg.register_component(var, config)
await time_.register_time(var, config)
if CORE.is_esp8266 and len(servers) > 1:
# We need LwIP features enabled to get 3 SNTP servers (not just one)