ESPHome  2024.10.2
gps.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ARDUINO
4 
8 #include <TinyGPS++.h>
9 
10 #include <vector>
11 
12 namespace esphome {
13 namespace gps {
14 
15 class GPS;
16 
17 class GPSListener {
18  public:
19  virtual void on_update(TinyGPSPlus &tiny_gps) = 0;
20  TinyGPSPlus &get_tiny_gps();
21 
22  protected:
23  friend GPS;
24 
26 };
27 
28 class GPS : public PollingComponent, public uart::UARTDevice {
29  public:
30  void set_latitude_sensor(sensor::Sensor *latitude_sensor) { latitude_sensor_ = latitude_sensor; }
31  void set_longitude_sensor(sensor::Sensor *longitude_sensor) { longitude_sensor_ = longitude_sensor; }
32  void set_speed_sensor(sensor::Sensor *speed_sensor) { speed_sensor_ = speed_sensor; }
33  void set_course_sensor(sensor::Sensor *course_sensor) { course_sensor_ = course_sensor; }
34  void set_altitude_sensor(sensor::Sensor *altitude_sensor) { altitude_sensor_ = altitude_sensor; }
35  void set_satellites_sensor(sensor::Sensor *satellites_sensor) { satellites_sensor_ = satellites_sensor; }
36 
37  void register_listener(GPSListener *listener) {
38  listener->parent_ = this;
39  this->listeners_.push_back(listener);
40  }
41  float get_setup_priority() const override { return setup_priority::HARDWARE; }
42 
43  void loop() override;
44  void update() override;
45 
46  TinyGPSPlus &get_tiny_gps() { return this->tiny_gps_; }
47 
48  protected:
49  float latitude_ = -1;
50  float longitude_ = -1;
51  float speed_ = -1;
52  float course_ = -1;
53  float altitude_ = -1;
54  int satellites_ = -1;
55 
56  sensor::Sensor *latitude_sensor_{nullptr};
57  sensor::Sensor *longitude_sensor_{nullptr};
58  sensor::Sensor *speed_sensor_{nullptr};
59  sensor::Sensor *course_sensor_{nullptr};
60  sensor::Sensor *altitude_sensor_{nullptr};
61  sensor::Sensor *satellites_sensor_{nullptr};
62 
63  bool has_time_{false};
64  TinyGPSPlus tiny_gps_;
65  std::vector<GPSListener *> listeners_{};
66 };
67 
68 } // namespace gps
69 } // namespace esphome
70 
71 #endif // USE_ARDUINO
TinyGPSPlus & get_tiny_gps()
Definition: gps.cpp:11
void loop()
TinyGPSPlus tiny_gps_
Definition: gps.h:64
void set_altitude_sensor(sensor::Sensor *altitude_sensor)
Definition: gps.h:34
void set_course_sensor(sensor::Sensor *course_sensor)
Definition: gps.h:33
This class simplifies creating components that periodically check a state.
Definition: component.h:283
float get_setup_priority() const override
Definition: gps.h:41
void set_latitude_sensor(sensor::Sensor *latitude_sensor)
Definition: gps.h:30
void set_speed_sensor(sensor::Sensor *speed_sensor)
Definition: gps.h:32
TinyGPSPlus & get_tiny_gps()
Definition: gps.h:46
void register_listener(GPSListener *listener)
Definition: gps.h:37
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
void set_longitude_sensor(sensor::Sensor *longitude_sensor)
Definition: gps.h:31
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_satellites_sensor(sensor::Sensor *satellites_sensor)
Definition: gps.h:35
virtual void on_update(TinyGPSPlus &tiny_gps)=0
Base-class for all sensors.
Definition: sensor.h:57