mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	cleanup
This commit is contained in:
		| @@ -22,7 +22,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() = default; |   template<typename... Args> SNTPComponent(Args... servers) : servers_{servers...} {} | ||||||
|  |  | ||||||
|   void setup() override; |   void setup() override; | ||||||
|   void dump_config() override; |   void dump_config() override; | ||||||
| @@ -33,15 +33,14 @@ class SNTPComponent : public time::RealTimeClock { | |||||||
|  |  | ||||||
|   void time_synced(); |   void time_synced(); | ||||||
|  |  | ||||||
|  |  protected: | ||||||
| #ifdef USE_ESP8266 | #ifdef USE_ESP8266 | ||||||
|   // On ESP8266, store pointers to PROGMEM strings to save RAM |   // 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 | #else | ||||||
|   // On other platforms, store regular const char pointers |   // 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 | #endif | ||||||
|  |  | ||||||
|  protected: |  | ||||||
|   bool has_time_{false}; |   bool has_time_{false}; | ||||||
|  |  | ||||||
| #if defined(USE_ESP32) | #if defined(USE_ESP32) | ||||||
|   | |||||||
| @@ -49,15 +49,10 @@ 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) | ||||||
|  |  | ||||||
|     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 |     # Generate PROGMEM strings for ESP8266, regular strings for other platforms | ||||||
|     if CORE.is_esp8266: |     if CORE.is_esp8266: | ||||||
|         # On ESP8266, use PROGMEM to store strings in flash |         # 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): |         for i, server in enumerate(servers): | ||||||
|             var_name = f"{config[CONF_ID].id}_server_{i}" |             var_name = f"{config[CONF_ID].id}_server_{i}" | ||||||
|             # Create PROGMEM string: static const char var_name[] PROGMEM = "server"; |             # 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) |                 "char", var_name, cg.safe_exp(server) | ||||||
|             ) |             ) | ||||||
|             cg.add(assignment) |             cg.add(assignment) | ||||||
|             # Assign pointer to array element |             server_vars.append(cg.RawExpression(var_name)) | ||||||
|             cg.add(cg.RawStatement(f"{var}->servers_[{i}] = {var_name};")) |         # Pass PROGMEM string pointers to constructor using ArrayInitializer | ||||||
|  |         var = cg.new_Pvariable(config[CONF_ID], cg.ArrayInitializer(*server_vars)) | ||||||
|     else: |     else: | ||||||
|         # On other platforms, use regular string literals |         # On other platforms, pass regular string literals to constructor | ||||||
|         for i, server in enumerate(servers): |         var = cg.new_Pvariable( | ||||||
|             cg.add(cg.RawStatement(f"{var}->servers_[{i}] = {cg.safe_exp(server)};")) |             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: |     if CORE.is_esp8266 and len(servers) > 1: | ||||||
|         # We need LwIP features enabled to get 3 SNTP servers (not just one) |         # We need LwIP features enabled to get 3 SNTP servers (not just one) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user