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