mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	must still be in ram on 8266
This commit is contained in:
		| @@ -42,16 +42,7 @@ void SNTPComponent::setup() { | |||||||
|  |  | ||||||
|   size_t i = 0; |   size_t i = 0; | ||||||
|   for (auto &server : this->servers_) { |   for (auto &server : this->servers_) { | ||||||
| #if defined(USE_ESP8266) |  | ||||||
|     // On ESP8266, server is PGM_P pointing to PROGMEM |  | ||||||
|     // LWIP's sntp_setservername is not PROGMEM-aware, so copy to stack buffer first |  | ||||||
|     char server_buf[64]; |  | ||||||
|     strncpy_P(server_buf, server, sizeof(server_buf) - 1); |  | ||||||
|     server_buf[sizeof(server_buf) - 1] = '\0'; |  | ||||||
|     sntp_setservername(i++, server_buf); |  | ||||||
| #else |  | ||||||
|     sntp_setservername(i++, server); |     sntp_setservername(i++, server); | ||||||
| #endif |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
| #if defined(USE_ESP8266) | #if defined(USE_ESP8266) | ||||||
| @@ -68,8 +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_) { | ||||||
|     // LOG_STR_ARG handles both PROGMEM (ESP8266) and regular pointers |     ESP_LOGCONFIG(TAG, "  Server %zu: '%s'", i++, server); | ||||||
|     ESP_LOGCONFIG(TAG, "  Server %zu: '%s'", i++, LOG_STR_ARG(server)); |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
| void SNTPComponent::update() { | void SNTPComponent::update() { | ||||||
|   | |||||||
| @@ -34,13 +34,8 @@ class SNTPComponent : public time::RealTimeClock { | |||||||
|   void time_synced(); |   void time_synced(); | ||||||
|  |  | ||||||
|  protected: |  protected: | ||||||
| #ifdef USE_ESP8266 |   // Store const char pointers - compiler stores string literals in flash on all platforms | ||||||
|   // On ESP8266, store pointers to PROGMEM strings to save RAM |  | ||||||
|   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 |  | ||||||
|   bool has_time_{false}; |   bool has_time_{false}; | ||||||
|  |  | ||||||
| #if defined(USE_ESP32) | #if defined(USE_ESP32) | ||||||
|   | |||||||
| @@ -12,7 +12,6 @@ from esphome.const import ( | |||||||
|     PLATFORM_RTL87XX, |     PLATFORM_RTL87XX, | ||||||
| ) | ) | ||||||
| from esphome.core import CORE | from esphome.core import CORE | ||||||
| from esphome.cpp_generator import ProgmemAssignmentExpression |  | ||||||
|  |  | ||||||
| DEPENDENCIES = ["network"] | DEPENDENCIES = ["network"] | ||||||
| sntp_ns = cg.esphome_ns.namespace("sntp") | sntp_ns = cg.esphome_ns.namespace("sntp") | ||||||
| @@ -49,25 +48,12 @@ async def to_code(config): | |||||||
|     # Define server count at compile time |     # Define server count at compile time | ||||||
|     cg.add_define("SNTP_SERVER_COUNT", server_count) |     cg.add_define("SNTP_SERVER_COUNT", server_count) | ||||||
|  |  | ||||||
|     # Generate PROGMEM strings for ESP8266, regular strings for other platforms |     # Pass string literals to constructor - stored in flash/rodata by compiler | ||||||
|     if CORE.is_esp8266: |     # On ESP8266, LWIP doesn't support PROGMEM pointers, so strings are in rodata (RAM) | ||||||
|         # On ESP8266, use PROGMEM to store strings in flash |     # but we still avoid the ~24 byte std::string overhead per server | ||||||
|         server_vars = [] |     var = cg.new_Pvariable( | ||||||
|         for i, server in enumerate(servers): |         config[CONF_ID], cg.ArrayInitializer(*[cg.safe_exp(s) for s in servers]) | ||||||
|             var_name = f"{config[CONF_ID].id}_server_{i}" |     ) | ||||||
|             # Create PROGMEM string: static const char var_name[] PROGMEM = "server"; |  | ||||||
|             assignment = ProgmemAssignmentExpression( |  | ||||||
|                 "char", var_name, cg.safe_exp(server) |  | ||||||
|             ) |  | ||||||
|             cg.add(assignment) |  | ||||||
|             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, 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 cg.register_component(var, config) | ||||||
|     await time_.register_time(var, config) |     await time_.register_time(var, config) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user