mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	[gps] Patches to build on IDF, other optimizations (#9728)
This commit is contained in:
		| @@ -1 +1 @@ | |||||||
| 7920671c938a5ea6a11ac4594204b5ec8f38d579c962bf1f185e8d5e3ad879be | 8016e9cbe199bf1a65a0c90e7a37d768ec774f4fff70de530a9b55708af71e74 | ||||||
|   | |||||||
| @@ -84,7 +84,6 @@ CONFIG_SCHEMA = cv.All( | |||||||
|     ) |     ) | ||||||
|     .extend(cv.polling_component_schema("20s")) |     .extend(cv.polling_component_schema("20s")) | ||||||
|     .extend(uart.UART_DEVICE_SCHEMA), |     .extend(uart.UART_DEVICE_SCHEMA), | ||||||
|     cv.only_with_arduino, |  | ||||||
| ) | ) | ||||||
| FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema("gps", require_rx=True) | FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema("gps", require_rx=True) | ||||||
|  |  | ||||||
| @@ -123,4 +122,9 @@ async def to_code(config): | |||||||
|         cg.add(var.set_hdop_sensor(sens)) |         cg.add(var.set_hdop_sensor(sens)) | ||||||
|  |  | ||||||
|     # https://platformio.org/lib/show/1655/TinyGPSPlus |     # https://platformio.org/lib/show/1655/TinyGPSPlus | ||||||
|     cg.add_library("mikalhart/TinyGPSPlus", "1.1.0") |     # Using fork of TinyGPSPlus patched to build on ESP-IDF | ||||||
|  |     cg.add_library( | ||||||
|  |         "TinyGPSPlus", | ||||||
|  |         None, | ||||||
|  |         "https://github.com/esphome/TinyGPSPlus.git#v1.1.0", | ||||||
|  |     ) | ||||||
|   | |||||||
| @@ -1,5 +1,3 @@ | |||||||
| #ifdef USE_ARDUINO |  | ||||||
|  |  | ||||||
| #include "gps.h" | #include "gps.h" | ||||||
| #include "esphome/core/log.h" | #include "esphome/core/log.h" | ||||||
|  |  | ||||||
| @@ -22,63 +20,69 @@ void GPS::dump_config() { | |||||||
| } | } | ||||||
|  |  | ||||||
| void GPS::update() { | void GPS::update() { | ||||||
|   if (this->latitude_sensor_ != nullptr) |   if (this->latitude_sensor_ != nullptr) { | ||||||
|     this->latitude_sensor_->publish_state(this->latitude_); |     this->latitude_sensor_->publish_state(this->latitude_); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   if (this->longitude_sensor_ != nullptr) |   if (this->longitude_sensor_ != nullptr) { | ||||||
|     this->longitude_sensor_->publish_state(this->longitude_); |     this->longitude_sensor_->publish_state(this->longitude_); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   if (this->speed_sensor_ != nullptr) |   if (this->speed_sensor_ != nullptr) { | ||||||
|     this->speed_sensor_->publish_state(this->speed_); |     this->speed_sensor_->publish_state(this->speed_); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   if (this->course_sensor_ != nullptr) |   if (this->course_sensor_ != nullptr) { | ||||||
|     this->course_sensor_->publish_state(this->course_); |     this->course_sensor_->publish_state(this->course_); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   if (this->altitude_sensor_ != nullptr) |   if (this->altitude_sensor_ != nullptr) { | ||||||
|     this->altitude_sensor_->publish_state(this->altitude_); |     this->altitude_sensor_->publish_state(this->altitude_); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   if (this->satellites_sensor_ != nullptr) |   if (this->satellites_sensor_ != nullptr) { | ||||||
|     this->satellites_sensor_->publish_state(this->satellites_); |     this->satellites_sensor_->publish_state(this->satellites_); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   if (this->hdop_sensor_ != nullptr) |   if (this->hdop_sensor_ != nullptr) { | ||||||
|     this->hdop_sensor_->publish_state(this->hdop_); |     this->hdop_sensor_->publish_state(this->hdop_); | ||||||
|   } |   } | ||||||
|  | } | ||||||
|  |  | ||||||
| void GPS::loop() { | void GPS::loop() { | ||||||
|   while (this->available() > 0 && !this->has_time_) { |   while (this->available() > 0 && !this->has_time_) { | ||||||
|     if (this->tiny_gps_.encode(this->read())) { |     if (!this->tiny_gps_.encode(this->read())) { | ||||||
|  |       return; | ||||||
|  |     } | ||||||
|     if (this->tiny_gps_.location.isUpdated()) { |     if (this->tiny_gps_.location.isUpdated()) { | ||||||
|       this->latitude_ = this->tiny_gps_.location.lat(); |       this->latitude_ = this->tiny_gps_.location.lat(); | ||||||
|       this->longitude_ = this->tiny_gps_.location.lng(); |       this->longitude_ = this->tiny_gps_.location.lng(); | ||||||
|  |       ESP_LOGV(TAG, "Latitude, Longitude: %.6f°, %.6f°", this->latitude_, this->longitude_); | ||||||
|         ESP_LOGD(TAG, "Location:"); |  | ||||||
|         ESP_LOGD(TAG, "  Lat: %.6f °", this->latitude_); |  | ||||||
|         ESP_LOGD(TAG, "  Lon: %.6f °", this->longitude_); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (this->tiny_gps_.speed.isUpdated()) { |     if (this->tiny_gps_.speed.isUpdated()) { | ||||||
|       this->speed_ = this->tiny_gps_.speed.kmph(); |       this->speed_ = this->tiny_gps_.speed.kmph(); | ||||||
|         ESP_LOGD(TAG, "Speed: %.3f km/h", this->speed_); |       ESP_LOGV(TAG, "Speed: %.3f km/h", this->speed_); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (this->tiny_gps_.course.isUpdated()) { |     if (this->tiny_gps_.course.isUpdated()) { | ||||||
|       this->course_ = this->tiny_gps_.course.deg(); |       this->course_ = this->tiny_gps_.course.deg(); | ||||||
|         ESP_LOGD(TAG, "Course: %.2f °", this->course_); |       ESP_LOGV(TAG, "Course: %.2f°", this->course_); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (this->tiny_gps_.altitude.isUpdated()) { |     if (this->tiny_gps_.altitude.isUpdated()) { | ||||||
|       this->altitude_ = this->tiny_gps_.altitude.meters(); |       this->altitude_ = this->tiny_gps_.altitude.meters(); | ||||||
|         ESP_LOGD(TAG, "Altitude: %.2f m", this->altitude_); |       ESP_LOGV(TAG, "Altitude: %.2f m", this->altitude_); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (this->tiny_gps_.satellites.isUpdated()) { |     if (this->tiny_gps_.satellites.isUpdated()) { | ||||||
|       this->satellites_ = this->tiny_gps_.satellites.value(); |       this->satellites_ = this->tiny_gps_.satellites.value(); | ||||||
|         ESP_LOGD(TAG, "Satellites: %d", this->satellites_); |       ESP_LOGV(TAG, "Satellites: %d", this->satellites_); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (this->tiny_gps_.hdop.isUpdated()) { |     if (this->tiny_gps_.hdop.isUpdated()) { | ||||||
|       this->hdop_ = this->tiny_gps_.hdop.hdop(); |       this->hdop_ = this->tiny_gps_.hdop.hdop(); | ||||||
|         ESP_LOGD(TAG, "HDOP: %.3f", this->hdop_); |       ESP_LOGV(TAG, "HDOP: %.3f", this->hdop_); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     for (auto *listener : this->listeners_) { |     for (auto *listener : this->listeners_) { | ||||||
| @@ -86,9 +90,6 @@ void GPS::loop() { | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
| } | } | ||||||
| } |  | ||||||
|  |  | ||||||
| }  // namespace gps | }  // namespace gps | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|  |  | ||||||
| #endif  // USE_ARDUINO |  | ||||||
|   | |||||||
| @@ -1,10 +1,8 @@ | |||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
| #ifdef USE_ARDUINO |  | ||||||
|  |  | ||||||
| #include "esphome/core/component.h" |  | ||||||
| #include "esphome/components/uart/uart.h" |  | ||||||
| #include "esphome/components/sensor/sensor.h" | #include "esphome/components/sensor/sensor.h" | ||||||
|  | #include "esphome/components/uart/uart.h" | ||||||
|  | #include "esphome/core/component.h" | ||||||
| #include <TinyGPSPlus.h> | #include <TinyGPSPlus.h> | ||||||
|  |  | ||||||
| #include <vector> | #include <vector> | ||||||
| @@ -53,8 +51,9 @@ class GPS : public PollingComponent, public uart::UARTDevice { | |||||||
|   float speed_{NAN}; |   float speed_{NAN}; | ||||||
|   float course_{NAN}; |   float course_{NAN}; | ||||||
|   float altitude_{NAN}; |   float altitude_{NAN}; | ||||||
|   uint16_t satellites_{0}; |  | ||||||
|   float hdop_{NAN}; |   float hdop_{NAN}; | ||||||
|  |   uint16_t satellites_{0}; | ||||||
|  |   bool has_time_{false}; | ||||||
|  |  | ||||||
|   sensor::Sensor *latitude_sensor_{nullptr}; |   sensor::Sensor *latitude_sensor_{nullptr}; | ||||||
|   sensor::Sensor *longitude_sensor_{nullptr}; |   sensor::Sensor *longitude_sensor_{nullptr}; | ||||||
| @@ -64,12 +63,9 @@ class GPS : public PollingComponent, public uart::UARTDevice { | |||||||
|   sensor::Sensor *satellites_sensor_{nullptr}; |   sensor::Sensor *satellites_sensor_{nullptr}; | ||||||
|   sensor::Sensor *hdop_sensor_{nullptr}; |   sensor::Sensor *hdop_sensor_{nullptr}; | ||||||
|  |  | ||||||
|   bool has_time_{false}; |  | ||||||
|   TinyGPSPlus tiny_gps_; |   TinyGPSPlus tiny_gps_; | ||||||
|   std::vector<GPSListener *> listeners_{}; |   std::vector<GPSListener *> listeners_{}; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| }  // namespace gps | }  // namespace gps | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|  |  | ||||||
| #endif  // USE_ARDUINO |  | ||||||
|   | |||||||
| @@ -1,5 +1,3 @@ | |||||||
| #ifdef USE_ARDUINO |  | ||||||
|  |  | ||||||
| #include "gps_time.h" | #include "gps_time.h" | ||||||
| #include "esphome/core/log.h" | #include "esphome/core/log.h" | ||||||
|  |  | ||||||
| @@ -9,12 +7,10 @@ namespace gps { | |||||||
| static const char *const TAG = "gps.time"; | static const char *const TAG = "gps.time"; | ||||||
|  |  | ||||||
| void GPSTime::from_tiny_gps_(TinyGPSPlus &tiny_gps) { | void GPSTime::from_tiny_gps_(TinyGPSPlus &tiny_gps) { | ||||||
|   if (!tiny_gps.time.isValid() || !tiny_gps.date.isValid()) |   if (!tiny_gps.time.isValid() || !tiny_gps.date.isValid() || !tiny_gps.time.isUpdated() || | ||||||
|     return; |       !tiny_gps.date.isUpdated() || tiny_gps.date.year() < 2025) { | ||||||
|   if (!tiny_gps.time.isUpdated() || !tiny_gps.date.isUpdated()) |  | ||||||
|     return; |  | ||||||
|   if (tiny_gps.date.year() < 2019) |  | ||||||
|     return; |     return; | ||||||
|  |   } | ||||||
|  |  | ||||||
|   ESPTime val{}; |   ESPTime val{}; | ||||||
|   val.year = tiny_gps.date.year(); |   val.year = tiny_gps.date.year(); | ||||||
| @@ -34,5 +30,3 @@ void GPSTime::from_tiny_gps_(TinyGPSPlus &tiny_gps) { | |||||||
|  |  | ||||||
| }  // namespace gps | }  // namespace gps | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|  |  | ||||||
| #endif  // USE_ARDUINO |  | ||||||
|   | |||||||
| @@ -1,10 +1,8 @@ | |||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
| #ifdef USE_ARDUINO |  | ||||||
|  |  | ||||||
| #include "esphome/core/component.h" |  | ||||||
| #include "esphome/components/time/real_time_clock.h" |  | ||||||
| #include "esphome/components/gps/gps.h" | #include "esphome/components/gps/gps.h" | ||||||
|  | #include "esphome/components/time/real_time_clock.h" | ||||||
|  | #include "esphome/core/component.h" | ||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
| namespace gps { | namespace gps { | ||||||
| @@ -13,9 +11,10 @@ class GPSTime : public time::RealTimeClock, public GPSListener { | |||||||
|  public: |  public: | ||||||
|   void update() override { this->from_tiny_gps_(this->get_tiny_gps()); }; |   void update() override { this->from_tiny_gps_(this->get_tiny_gps()); }; | ||||||
|   void on_update(TinyGPSPlus &tiny_gps) override { |   void on_update(TinyGPSPlus &tiny_gps) override { | ||||||
|     if (!this->has_time_) |     if (!this->has_time_) { | ||||||
|       this->from_tiny_gps_(tiny_gps); |       this->from_tiny_gps_(tiny_gps); | ||||||
|     } |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  protected: |  protected: | ||||||
|   void from_tiny_gps_(TinyGPSPlus &tiny_gps); |   void from_tiny_gps_(TinyGPSPlus &tiny_gps); | ||||||
| @@ -24,5 +23,3 @@ class GPSTime : public time::RealTimeClock, public GPSListener { | |||||||
|  |  | ||||||
| }  // namespace gps | }  // namespace gps | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|  |  | ||||||
| #endif  // USE_ARDUINO |  | ||||||
|   | |||||||
| @@ -73,7 +73,7 @@ lib_deps = | |||||||
|     heman/AsyncMqttClient-esphome@1.0.0                   ; mqtt |     heman/AsyncMqttClient-esphome@1.0.0                   ; mqtt | ||||||
|     ESP32Async/ESPAsyncWebServer@3.7.8                    ; web_server_base |     ESP32Async/ESPAsyncWebServer@3.7.8                    ; web_server_base | ||||||
|     fastled/FastLED@3.9.16                                ; fastled_base |     fastled/FastLED@3.9.16                                ; fastled_base | ||||||
|     mikalhart/TinyGPSPlus@1.1.0                           ; gps |     https://github.com/esphome/TinyGPSPlus.git#v1.1.0     ; gps | ||||||
|     freekode/TM1651@1.0.1                                 ; tm1651 |     freekode/TM1651@1.0.1                                 ; tm1651 | ||||||
|     glmnet/Dsmr@0.7                                       ; dsmr |     glmnet/Dsmr@0.7                                       ; dsmr | ||||||
|     rweather/Crypto@0.4.0                                 ; dsmr |     rweather/Crypto@0.4.0                                 ; dsmr | ||||||
|   | |||||||
							
								
								
									
										5
									
								
								tests/components/gps/test.esp32-c3-idf.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								tests/components/gps/test.esp32-c3-idf.yaml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | |||||||
|  | substitutions: | ||||||
|  |   tx_pin: GPIO4 | ||||||
|  |   rx_pin: GPIO5 | ||||||
|  |  | ||||||
|  | <<: !include common.yaml | ||||||
							
								
								
									
										5
									
								
								tests/components/gps/test.esp32-idf.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								tests/components/gps/test.esp32-idf.yaml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | |||||||
|  | substitutions: | ||||||
|  |   tx_pin: GPIO12 | ||||||
|  |   rx_pin: GPIO14 | ||||||
|  |  | ||||||
|  | <<: !include common.yaml | ||||||
		Reference in New Issue
	
	Block a user