mirror of
https://github.com/esphome/esphome.git
synced 2025-10-31 23:21:54 +00:00
[sntp] Replace std::vector<std::string> with std::array<const char*> to save heap memory (#11525)
This commit is contained in:
@@ -27,7 +27,7 @@ void SNTPComponent::setup() {
|
|||||||
esp_sntp_setoperatingmode(ESP_SNTP_OPMODE_POLL);
|
esp_sntp_setoperatingmode(ESP_SNTP_OPMODE_POLL);
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
for (auto &server : this->servers_) {
|
for (auto &server : this->servers_) {
|
||||||
esp_sntp_setservername(i++, server.c_str());
|
esp_sntp_setservername(i++, server);
|
||||||
}
|
}
|
||||||
esp_sntp_set_sync_interval(this->get_update_interval());
|
esp_sntp_set_sync_interval(this->get_update_interval());
|
||||||
esp_sntp_set_time_sync_notification_cb([](struct timeval *tv) {
|
esp_sntp_set_time_sync_notification_cb([](struct timeval *tv) {
|
||||||
@@ -42,7 +42,7 @@ void SNTPComponent::setup() {
|
|||||||
|
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
for (auto &server : this->servers_) {
|
for (auto &server : this->servers_) {
|
||||||
sntp_setservername(i++, server.c_str());
|
sntp_setservername(i++, server);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(USE_ESP8266)
|
#if defined(USE_ESP8266)
|
||||||
@@ -59,7 +59,7 @@ void SNTPComponent::dump_config() {
|
|||||||
ESP_LOGCONFIG(TAG, "SNTP Time:");
|
ESP_LOGCONFIG(TAG, "SNTP Time:");
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
for (auto &server : this->servers_) {
|
for (auto &server : this->servers_) {
|
||||||
ESP_LOGCONFIG(TAG, " Server %zu: '%s'", i++, server.c_str());
|
ESP_LOGCONFIG(TAG, " Server %zu: '%s'", i++, server);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void SNTPComponent::update() {
|
void SNTPComponent::update() {
|
||||||
|
|||||||
@@ -2,10 +2,14 @@
|
|||||||
|
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/components/time/real_time_clock.h"
|
#include "esphome/components/time/real_time_clock.h"
|
||||||
|
#include <array>
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace sntp {
|
namespace sntp {
|
||||||
|
|
||||||
|
// Server count is calculated at compile time by Python codegen
|
||||||
|
// SNTP_SERVER_COUNT will always be defined
|
||||||
|
|
||||||
/// The SNTP component allows you to configure local timekeeping via Simple Network Time Protocol.
|
/// The SNTP component allows you to configure local timekeeping via Simple Network Time Protocol.
|
||||||
///
|
///
|
||||||
/// \note
|
/// \note
|
||||||
@@ -14,10 +18,7 @@ namespace sntp {
|
|||||||
/// \see https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
|
/// \see https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
|
||||||
class SNTPComponent : public time::RealTimeClock {
|
class SNTPComponent : public time::RealTimeClock {
|
||||||
public:
|
public:
|
||||||
SNTPComponent(const std::vector<std::string> &servers) : servers_(servers) {}
|
SNTPComponent(const std::array<const char *, SNTP_SERVER_COUNT> &servers) : servers_(servers) {}
|
||||||
|
|
||||||
// Note: set_servers() has been removed and replaced by a constructor - calling set_servers after setup would
|
|
||||||
// have had no effect anyway, and making the strings immutable avoids the need to strdup their contents.
|
|
||||||
|
|
||||||
void setup() override;
|
void setup() override;
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
@@ -29,7 +30,10 @@ class SNTPComponent : public time::RealTimeClock {
|
|||||||
void time_synced();
|
void time_synced();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::vector<std::string> servers_;
|
// Store const char pointers to string literals
|
||||||
|
// ESP8266: strings in rodata (RAM), but avoids std::string overhead (~24 bytes each)
|
||||||
|
// Other platforms: strings in flash
|
||||||
|
std::array<const char *, SNTP_SERVER_COUNT> servers_;
|
||||||
bool has_time_{false};
|
bool has_time_{false};
|
||||||
|
|
||||||
#if defined(USE_ESP32)
|
#if defined(USE_ESP32)
|
||||||
|
|||||||
@@ -43,6 +43,11 @@ CONFIG_SCHEMA = cv.All(
|
|||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
servers = config[CONF_SERVERS]
|
servers = config[CONF_SERVERS]
|
||||||
|
|
||||||
|
# Define server count at compile time
|
||||||
|
cg.add_define("SNTP_SERVER_COUNT", len(servers))
|
||||||
|
|
||||||
|
# Pass string literals to constructor - stored in flash/rodata by compiler
|
||||||
var = cg.new_Pvariable(config[CONF_ID], servers)
|
var = cg.new_Pvariable(config[CONF_ID], servers)
|
||||||
|
|
||||||
await cg.register_component(var, config)
|
await cg.register_component(var, config)
|
||||||
|
|||||||
@@ -87,6 +87,7 @@
|
|||||||
#define USE_MDNS_STORE_SERVICES
|
#define USE_MDNS_STORE_SERVICES
|
||||||
#define MDNS_SERVICE_COUNT 3
|
#define MDNS_SERVICE_COUNT 3
|
||||||
#define MDNS_DYNAMIC_TXT_COUNT 3
|
#define MDNS_DYNAMIC_TXT_COUNT 3
|
||||||
|
#define SNTP_SERVER_COUNT 3
|
||||||
#define USE_MEDIA_PLAYER
|
#define USE_MEDIA_PLAYER
|
||||||
#define USE_NEXTION_TFT_UPLOAD
|
#define USE_NEXTION_TFT_UPLOAD
|
||||||
#define USE_NUMBER
|
#define USE_NUMBER
|
||||||
|
|||||||
Reference in New Issue
Block a user