[gps] Size the listener list at compile time (#21201)

This commit is contained in:
J. Nick Koston
2026-10-21 13:05:34 -04:00
committed by GitHub
parent 55a658dca6
commit 1fa828bf9e
5 changed files with 22 additions and 5 deletions
+11
View File
@@ -16,6 +16,7 @@ from esphome.const import (
UNIT_KILOMETER_PER_HOUR,
UNIT_METER,
)
from esphome.cpp_generator import MockObj
from esphome.types import ConfigType
CONF_GPS_ID = "gps_id"
@@ -40,6 +41,16 @@ GPS = gps_ns.class_("GPS", cg.PollingComponent, uart.UARTDevice)
GPSListener = gps_ns.class_("GPSListener")
MULTI_CONF = True
_request_listener_slot = cg.slot_counter("GPS_LISTENER_COUNT")
async def register_listener(hub: MockObj, var: MockObj) -> None:
"""Register a listener with its GPS and count it for the compile-time list size."""
_request_listener_slot(str(hub))
cg.add(hub.register_listener(var))
CONFIG_SCHEMA = cv.All(
cv.Schema(
{
+2
View File
@@ -84,9 +84,11 @@ void GPS::loop() {
ESP_LOGV(TAG, "HDOP: %.3f", this->hdop_);
}
#ifdef GPS_LISTENER_COUNT
for (auto *listener : this->listeners_) {
listener->on_update(this->tiny_gps_);
}
#endif
}
}
+6 -3
View File
@@ -3,10 +3,9 @@
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/uart/uart.h"
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
#include <TinyGPSPlus.h>
#include <vector>
namespace esphome::gps {
class GPS;
@@ -32,10 +31,12 @@ class GPS final : public PollingComponent, public uart::UARTDevice {
void set_satellites_sensor(sensor::Sensor *satellites_sensor) { this->satellites_sensor_ = satellites_sensor; }
void set_hdop_sensor(sensor::Sensor *hdop_sensor) { this->hdop_sensor_ = hdop_sensor; }
#ifdef GPS_LISTENER_COUNT
void register_listener(GPSListener *listener) {
listener->parent_ = this;
this->listeners_.push_back(listener);
}
#endif
float get_setup_priority() const override { return setup_priority::HARDWARE; }
void dump_config() override;
@@ -63,7 +64,9 @@ class GPS final : public PollingComponent, public uart::UARTDevice {
sensor::Sensor *hdop_sensor_{nullptr};
TinyGPSPlus tiny_gps_;
std::vector<GPSListener *> listeners_{};
#ifdef GPS_LISTENER_COUNT
StaticVector<GPSListener *, GPS_LISTENER_COUNT> listeners_;
#endif
};
} // namespace esphome::gps
+2 -2
View File
@@ -4,7 +4,7 @@ import esphome.config_validation as cv
from esphome.const import CONF_ID
from esphome.types import ConfigType
from .. import CONF_GPS_ID, GPS, GPSListener, gps_ns
from .. import CONF_GPS_ID, GPS, GPSListener, gps_ns, register_listener
DEPENDENCIES = ["gps"]
@@ -26,4 +26,4 @@ async def to_code(config: ConfigType) -> None:
await cg.register_component(var, config)
paren = await cg.get_variable(config[CONF_GPS_ID])
cg.add(paren.register_listener(var))
await register_listener(paren, var)
+1
View File
@@ -162,6 +162,7 @@
#define USE_LVGL_TOUCHSCREEN
#define USE_MDNS
#define USE_MDNS_STORE_SERVICES
#define GPS_LISTENER_COUNT 1
#define HDMI_CEC_MESSAGE_CALLBACK_COUNT 2
#define I2C_PORT_FREQUENCY_COUNT 1
#define MDNS_SERVICE_COUNT 3