From 974564b89ce959fcd4c8a24bab2e113ddf6bb39d Mon Sep 17 00:00:00 2001 From: agessaman Date: Mon, 17 Aug 2026 21:04:06 -0700 Subject: [PATCH 01/53] feat(rak3401): cold-sleep RAK4631 onboard SX1262 on RAK13302 builds A RAK4631 core paired with a RAK13302 leaves the core's own SX1262 powered but never initialized, idling in STDBY_RC for the life of the board. Firmware cannot cut its supply -- P1.05 is the RF switch rail, not the radio's -- so bring it up on its own pins just long enough to issue a cold sleep, before std_init() repins SPI onto the RAK13302. Gated behind RAK4631_ONBOARD_SX1262_SLEEP and exposed through seven RAK_4631_13302_* envs, so RAK_3401_* artifacts are unchanged. The sleep is issued regardless of what begin() returns: Module::init() runs first, so SPI stays usable even if TCXO calibration fails. NSS is then held high, since the SX1262 wakes on an NSS falling edge. Verified on RAK19007 + RAK4631 + RAK13302: begin=0 sleep=0, RAK13302 init clean (device_errors=0x0000), TX and RX both confirmed. The current saving itself is derived from the datasheet (600 uA STDBY_RC -> 160 nA cold sleep) and is not measured. --- variants/rak3401/platformio.ini | 125 ++++++++++++++++++++++++++++++++ variants/rak3401/target.cpp | 33 +++++++++ variants/rak3401/variant.h | 15 ++++ 3 files changed, 173 insertions(+) diff --git a/variants/rak3401/platformio.ini b/variants/rak3401/platformio.ini index 48e71922..56883e75 100644 --- a/variants/rak3401/platformio.ini +++ b/variants/rak3401/platformio.ini @@ -134,3 +134,128 @@ build_src_filter = ${rak3401.build_src_filter} extends = rak3401 build_src_filter = ${rak3401.build_src_filter} +<../examples/kiss_modem/> + +; ------------------------- RAK4631 core + RAK13302 ------------------------- +; Same RF hardware as the RAK3401 1W kit, but the RAK4631 core carries a second, +; unused SX1262 that would otherwise idle in standby forever. These envs match +; the RAK_3401_* ones and additionally cold-sleep that radio at boot. +; Do not flash them to a genuine RAK3401 -- there is no onboard radio to sleep. + +[rak4631_13302] +extends = rak3401 +board = rak4631 +build_flags = ${rak3401.build_flags} + -D RAK4631_ONBOARD_SX1262_SLEEP +build_src_filter = ${rak3401.build_src_filter} +lib_deps = ${rak3401.lib_deps} + +[env:RAK_4631_13302_repeater] +extends = rak4631_13302 +build_flags = + ${rak4631_13302.build_flags} + -D DISPLAY_CLASS=SSD1306Display + -D ADVERT_NAME='"RAK4631 1W Repeater"' + -D ADVERT_LAT=0.0 + -D ADVERT_LON=0.0 + -D ADMIN_PASSWORD='"password"' + -D MAX_NEIGHBOURS=50 + ;-D MESH_PACKET_LOGGING=1 + ;-D MESH_DEBUG=1 +build_src_filter = ${rak4631_13302.build_src_filter} + + + +<../examples/simple_repeater> + +[env:RAK_4631_13302_room_server] +extends = rak4631_13302 +build_flags = + ${rak4631_13302.build_flags} + -D DISPLAY_CLASS=SSD1306Display + -D ADVERT_NAME='"Test Room"' + -D ADVERT_LAT=0.0 + -D ADVERT_LON=0.0 + -D ADMIN_PASSWORD='"password"' + -D ROOM_PASSWORD='"hello"' + ;-D MESH_PACKET_LOGGING=1 + ;-D MESH_DEBUG=1 +build_src_filter = ${rak4631_13302.build_src_filter} + + + +<../examples/simple_room_server> + +[env:RAK_4631_13302_companion_radio_usb] +extends = rak4631_13302 +board_build.ldscript = boards/nrf52840_s140_v6_extrafs.ld +board_upload.maximum_size = 712704 +build_flags = + ${rak4631_13302.build_flags} + -I examples/companion_radio/ui-new + -D PIN_USER_BTN_ANA=31 + -D DISPLAY_CLASS=SSD1306Display + -D MAX_CONTACTS=350 + -D MAX_GROUP_CHANNELS=40 + -D ENABLE_USB_INTERFACE +; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1 +; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1 +build_src_filter = ${rak4631_13302.build_src_filter} + +<../examples/companion_radio/*.cpp> + +<../examples/companion_radio/ui-new/*.cpp> +lib_deps = + ${rak4631_13302.lib_deps} + densaugeo/base64 @ ~1.4.0 + +[env:RAK_4631_13302_companion_radio_ble] +extends = rak4631_13302 +board_build.ldscript = boards/nrf52840_s140_v6_extrafs.ld +board_upload.maximum_size = 712704 +build_flags = + ${rak4631_13302.build_flags} + -I examples/companion_radio/ui-new + -D PIN_USER_BTN_ANA=31 + -D DISPLAY_CLASS=SSD1306Display + -D MAX_CONTACTS=350 + -D MAX_GROUP_CHANNELS=40 + -D BLE_PIN_CODE=123456 + -D BLE_DEBUG_LOGGING=1 + -D OFFLINE_QUEUE_SIZE=256 + ;-D MESH_PACKET_LOGGING=1 + ;-D MESH_DEBUG=1 +build_src_filter = ${rak4631_13302.build_src_filter} + + + +<../examples/companion_radio/*.cpp> + +<../examples/companion_radio/ui-new/*.cpp> +lib_deps = + ${rak4631_13302.lib_deps} + densaugeo/base64 @ ~1.4.0 + +[env:RAK_4631_13302_terminal_chat] +extends = rak4631_13302 +build_flags = + ${rak4631_13302.build_flags} + -D MAX_CONTACTS=100 + -D MAX_GROUP_CHANNELS=1 + ;-D MESH_PACKET_LOGGING=1 + ;-D MESH_DEBUG=1 +build_src_filter = ${rak4631_13302.build_src_filter} + +<../examples/simple_secure_chat/main.cpp> +lib_deps = + ${rak4631_13302.lib_deps} + densaugeo/base64 @ ~1.4.0 + +[env:RAK_4631_13302_sensor] +extends = rak4631_13302 +build_flags = + ${rak4631_13302.build_flags} + -D DISPLAY_CLASS=SSD1306Display + -D ADVERT_NAME='"RAK4631 Sensor"' + -D ADVERT_LAT=0.0 + -D ADVERT_LON=0.0 + -D ADMIN_PASSWORD='"password"' + ;-D MESH_PACKET_LOGGING=1 + ;-D MESH_DEBUG=1 +build_src_filter = ${rak4631_13302.build_src_filter} + + + +<../examples/simple_sensor> + +[env:RAK_4631_13302_kiss_modem] +extends = rak4631_13302 +build_src_filter = ${rak4631_13302.build_src_filter} + +<../examples/kiss_modem/> diff --git a/variants/rak3401/target.cpp b/variants/rak3401/target.cpp index 5309e6b2..640b4d02 100644 --- a/variants/rak3401/target.cpp +++ b/variants/rak3401/target.cpp @@ -32,8 +32,41 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); EnvironmentSensorManager sensors; #endif +#ifdef RAK4631_ONBOARD_SX1262_SLEEP +// With a RAK13302 fitted the RAK4631's own SX1262 never gets initialized, so it +// sits in STDBY_RC drawing standby current for the life of the board. Firmware +// cannot cut its supply -- P1.05 is the RF switch rail, not the radio's -- so +// bring it up on its own pins just long enough to put it into cold sleep. +static void sleep_onboard_radio() { + pinMode(ONBOARD_LORA_ANT_SW, OUTPUT); + digitalWrite(ONBOARD_LORA_ANT_SW, LOW); // nothing downstream needs the RF switch + + SPI.setPins(ONBOARD_LORA_MISO, ONBOARD_LORA_SCLK, ONBOARD_LORA_MOSI); + SPI.begin(); + + Module mod(ONBOARD_LORA_NSS, ONBOARD_LORA_DIO_1, ONBOARD_LORA_RESET, ONBOARD_LORA_BUSY, SPI); + SX1262 onboard(&mod); + + // begin() resets the chip and leaves it in standby. A TCXO calibration failure + // still leaves SPI usable, so issue the sleep regardless of what it returns. + int status = onboard.begin(434.0f, 125.0f, 9, 7, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, + 0, 8, ONBOARD_LORA_TCXO_VOLTAGE, false); + int slept = onboard.sleep(false); // cold sleep: config discarded, RTC off + MESH_DEBUG_PRINTLN("onboard SX1262: begin=%d sleep=%d", status, slept); + + SPI.end(); // release SPIM3 so std_init() can repin it onto the RAK13302 + + // The SX1262 wakes on an NSS falling edge, so keep NSS driven high from here on. + digitalWrite(ONBOARD_LORA_NSS, HIGH); + pinMode(ONBOARD_LORA_NSS, OUTPUT); +} +#endif + bool radio_init() { rtc_clock.begin(Wire); +#ifdef RAK4631_ONBOARD_SX1262_SLEEP + sleep_onboard_radio(); +#endif return radio.std_init(&SPI); } diff --git a/variants/rak3401/variant.h b/variants/rak3401/variant.h index 98827886..0576dadb 100644 --- a/variants/rak3401/variant.h +++ b/variants/rak3401/variant.h @@ -175,6 +175,21 @@ static const uint8_t AREF = PIN_AREF; #define P_LORA_BUSY SX126X_BUSY #define P_LORA_RESET SX126X_RESET +// The RAK4631 core carries its own SX1262, which is unused when a RAK13302 +// supplies the RF path. RAK4631_ONBOARD_SX1262_SLEEP builds put it into cold +// sleep at boot; on a genuine RAK3401 these pins are unconnected. +#ifdef RAK4631_ONBOARD_SX1262_SLEEP +#define ONBOARD_LORA_NSS (42) // P1.10 +#define ONBOARD_LORA_SCLK (43) // P1.11 +#define ONBOARD_LORA_MOSI (44) // P1.12 +#define ONBOARD_LORA_MISO (45) // P1.13 +#define ONBOARD_LORA_BUSY (46) // P1.14 +#define ONBOARD_LORA_DIO_1 (47) // P1.15 +#define ONBOARD_LORA_RESET (38) // P1.06 +#define ONBOARD_LORA_ANT_SW (37) // P1.05, powers the onboard RF switch +#define ONBOARD_LORA_TCXO_VOLTAGE 1.8 +#endif + // enables 3.3V periphery like GPS or IO Module // Do not toggle this for GPS power savings #define PIN_3V3_EN (34) From 94a4c0b0ed1bc9a84308fab2ac4d2dfb4d1592c3 Mon Sep 17 00:00:00 2001 From: agessaman Date: Mon, 17 Aug 2026 22:19:49 -0700 Subject: [PATCH 02/53] feat(rak3401): report onboard SX1262 sleep state via BUSY BUSY is driven low in STDBY_RC and high while asleep, so reading it either side of the sleep gives a non-invasive readback of the state the chip was found in and the state it was left in. Confirmed on hardware. This is what makes the sleep observable at all. The only other symptom of a failed sleep is ~600 uA, which is below what an inline USB meter can resolve, so without this a regression would be silent. It immediately earned itself: busy_before reads 0 on every boot, showing the cold sleep does not survive an MCU reset. GPIOs revert to input during reset, NSS stops being driven high, and the falling edge wakes the chip. The helper re-sleeps it, so this is self-healing, but it means the chip is never found already asleep. --- variants/rak3401/target.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/variants/rak3401/target.cpp b/variants/rak3401/target.cpp index 640b4d02..dac12ede 100644 --- a/variants/rak3401/target.cpp +++ b/variants/rak3401/target.cpp @@ -47,12 +47,20 @@ static void sleep_onboard_radio() { Module mod(ONBOARD_LORA_NSS, ONBOARD_LORA_DIO_1, ONBOARD_LORA_RESET, ONBOARD_LORA_BUSY, SPI); SX1262 onboard(&mod); + // BUSY is driven low in STDBY_RC and high while asleep, so it doubles as a + // non-invasive readback of what state we found the chip in and left it in. + pinMode(ONBOARD_LORA_BUSY, INPUT); + int busy_before = digitalRead(ONBOARD_LORA_BUSY); + // begin() resets the chip and leaves it in standby. A TCXO calibration failure // still leaves SPI usable, so issue the sleep regardless of what it returns. int status = onboard.begin(434.0f, 125.0f, 9, 7, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, 0, 8, ONBOARD_LORA_TCXO_VOLTAGE, false); int slept = onboard.sleep(false); // cold sleep: config discarded, RTC off - MESH_DEBUG_PRINTLN("onboard SX1262: begin=%d sleep=%d", status, slept); + delay(2); + int busy_after = digitalRead(ONBOARD_LORA_BUSY); + MESH_DEBUG_PRINTLN("onboard SX1262: begin=%d sleep=%d busy %d->%d", + status, slept, busy_before, busy_after); SPI.end(); // release SPIM3 so std_init() can repin it onto the RAK13302 From 6dc7f2b0470037814bc9c889dfbf7961ca408c37 Mon Sep 17 00:00:00 2001 From: agessaman Date: Fri, 16 Oct 2026 19:02:23 -0700 Subject: [PATCH 03/53] feat(rak3401): auto-sleep optional onboard SX1262 Use the existing RAK3401 firmware roles for RAK4631 cores paired with a RAK13302. Probe the optional onboard radio and put it into cold sleep before handing SPI to the primary radio, removing seven duplicate build environments. Preserve BUSY startup readiness, release probe pulls before sleep, and disconnect unused GPIO input buffers during cleanup. Add focused tests for sequencing, missing signatures, timeouts, and failed sleep readback. Validated with four native tests, seven firmware-role builds, and repeater boot checks on RAK4631 + RAK13302. Supply current was not measured. --- platformio.ini | 6 + .../test_onboard_radio.cpp | 66 +++++++++ variants/rak3401/OnboardRadioSleep.h | 34 +++++ variants/rak3401/platformio.ini | 125 ------------------ variants/rak3401/target.cpp | 110 ++++++++++----- variants/rak3401/variant.h | 7 +- 6 files changed, 184 insertions(+), 164 deletions(-) create mode 100644 test/test_rak3401_onboard_radio/test_onboard_radio.cpp create mode 100644 variants/rak3401/OnboardRadioSleep.h diff --git a/platformio.ini b/platformio.ini index e78124a4..e305d177 100644 --- a/platformio.ini +++ b/platformio.ini @@ -173,6 +173,12 @@ build_src_filter = lib_deps = google/googletest @ 1.17.0 +[env:native_rak3401_onboard_radio] +; Header-only tests without unrelated production sources; also included in env:native CI. +extends = env:native +test_build_src = no +test_filter = test_rak3401_onboard_radio + [env:native_kiss_modem] platform = native test_framework = googletest diff --git a/test/test_rak3401_onboard_radio/test_onboard_radio.cpp b/test/test_rak3401_onboard_radio/test_onboard_radio.cpp new file mode 100644 index 00000000..cc616612 --- /dev/null +++ b/test/test_rak3401_onboard_radio/test_onboard_radio.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include "../../variants/rak3401/OnboardRadioSleep.h" + +namespace { +struct FakeRadioIO { + std::string version = "SX1261"; + std::array ready = {true, true, true}; + std::string calls; + unsigned waits = 0; + bool probePulls = true; + + void begin() { calls += "begin "; } + void reset() { calls += "reset "; } + bool waitBusy(bool high, unsigned timeout) { + calls += high ? "busy-high " : "busy-low "; + EXPECT_EQ(timeout, 20u); + if (high) EXPECT_FALSE(probePulls); + const bool result = ready.at(waits++); + return result; + } + void readVersion(char* out) { calls += "version "; memcpy(out, version.data(), 6); } + void releaseProbePulls() { calls += "release-pulls "; probePulls = false; } + void sleep() { EXPECT_FALSE(probePulls); calls += "sleep "; } + void end() { calls += "end "; } +}; +} + +TEST(RakOnboardRadio, PresentRadioSleepsBeforeBusRelease) { + FakeRadioIO io; + EXPECT_EQ(rak3401::sleepOnboardRadio(io, "SX1261"), rak3401::OnboardRadioState::Asleep); + EXPECT_EQ(io.calls, "begin reset busy-low version release-pulls busy-low sleep busy-high end "); +} + +TEST(RakOnboardRadio, AbsentAndFloatingResponsesNeverReceiveSleep) { + for (const std::string version : {std::string(6, '\0'), std::string(6, '\xff'), std::string("SX126X")}) { + FakeRadioIO io; + io.version = version; + EXPECT_EQ(rak3401::sleepOnboardRadio(io, "SX1261"), rak3401::OnboardRadioState::Absent); + EXPECT_EQ(io.calls, "begin reset busy-low version end "); + } +} + +TEST(RakOnboardRadio, BusyTimeoutsReleaseBusWithoutSleep) { + for (unsigned failure = 0; failure < 2; failure++) { + FakeRadioIO io; + io.ready[failure] = false; + EXPECT_EQ(rak3401::sleepOnboardRadio(io, "SX1261"), rak3401::OnboardRadioState::BusyTimeout); + EXPECT_EQ(io.calls.find("sleep "), std::string::npos); + EXPECT_EQ(io.calls, failure == 0 ? "begin reset busy-low end " : + "begin reset busy-low version release-pulls busy-low end "); + } +} + +TEST(RakOnboardRadio, MissingSleepReadbackIsReportedAndBusReleased) { + FakeRadioIO io; + io.ready[2] = false; + EXPECT_EQ(rak3401::sleepOnboardRadio(io, "SX1261"), rak3401::OnboardRadioState::SleepFailed); + EXPECT_EQ(io.calls.substr(io.calls.size() - 4), "end "); +} + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/variants/rak3401/OnboardRadioSleep.h b/variants/rak3401/OnboardRadioSleep.h new file mode 100644 index 00000000..7f6d1c55 --- /dev/null +++ b/variants/rak3401/OnboardRadioSleep.h @@ -0,0 +1,34 @@ +#pragma once + +#include + +namespace rak3401 { + +enum class OnboardRadioState { Absent, Asleep, BusyTimeout, SleepFailed }; + +// IO owns the temporary SPI pin assignment; every outcome must release it. +template +OnboardRadioState sleepOnboardRadio(IO& io, const char* expectedVersion) { + io.begin(); + io.reset(); + const auto state = [&]() { + // A missing chip also times out: BUSY is pulled high until a radio drives it low. + if (!io.waitBusy(false, 20)) return OnboardRadioState::BusyTimeout; + + char version[6]; + io.readVersion(version); + if (memcmp(version, expectedVersion, sizeof(version)) != 0) { + return OnboardRadioState::Absent; + } + io.releaseProbePulls(); + if (!io.waitBusy(false, 20)) return OnboardRadioState::BusyTimeout; + + io.sleep(); + // Reading over SPI here would wake the radio again. BUSY is a passive check. + return io.waitBusy(true, 20) ? OnboardRadioState::Asleep : OnboardRadioState::SleepFailed; + }(); + io.end(); + return state; +} + +} diff --git a/variants/rak3401/platformio.ini b/variants/rak3401/platformio.ini index 56883e75..48e71922 100644 --- a/variants/rak3401/platformio.ini +++ b/variants/rak3401/platformio.ini @@ -134,128 +134,3 @@ build_src_filter = ${rak3401.build_src_filter} extends = rak3401 build_src_filter = ${rak3401.build_src_filter} +<../examples/kiss_modem/> - -; ------------------------- RAK4631 core + RAK13302 ------------------------- -; Same RF hardware as the RAK3401 1W kit, but the RAK4631 core carries a second, -; unused SX1262 that would otherwise idle in standby forever. These envs match -; the RAK_3401_* ones and additionally cold-sleep that radio at boot. -; Do not flash them to a genuine RAK3401 -- there is no onboard radio to sleep. - -[rak4631_13302] -extends = rak3401 -board = rak4631 -build_flags = ${rak3401.build_flags} - -D RAK4631_ONBOARD_SX1262_SLEEP -build_src_filter = ${rak3401.build_src_filter} -lib_deps = ${rak3401.lib_deps} - -[env:RAK_4631_13302_repeater] -extends = rak4631_13302 -build_flags = - ${rak4631_13302.build_flags} - -D DISPLAY_CLASS=SSD1306Display - -D ADVERT_NAME='"RAK4631 1W Repeater"' - -D ADVERT_LAT=0.0 - -D ADVERT_LON=0.0 - -D ADMIN_PASSWORD='"password"' - -D MAX_NEIGHBOURS=50 - ;-D MESH_PACKET_LOGGING=1 - ;-D MESH_DEBUG=1 -build_src_filter = ${rak4631_13302.build_src_filter} - + - +<../examples/simple_repeater> - -[env:RAK_4631_13302_room_server] -extends = rak4631_13302 -build_flags = - ${rak4631_13302.build_flags} - -D DISPLAY_CLASS=SSD1306Display - -D ADVERT_NAME='"Test Room"' - -D ADVERT_LAT=0.0 - -D ADVERT_LON=0.0 - -D ADMIN_PASSWORD='"password"' - -D ROOM_PASSWORD='"hello"' - ;-D MESH_PACKET_LOGGING=1 - ;-D MESH_DEBUG=1 -build_src_filter = ${rak4631_13302.build_src_filter} - + - +<../examples/simple_room_server> - -[env:RAK_4631_13302_companion_radio_usb] -extends = rak4631_13302 -board_build.ldscript = boards/nrf52840_s140_v6_extrafs.ld -board_upload.maximum_size = 712704 -build_flags = - ${rak4631_13302.build_flags} - -I examples/companion_radio/ui-new - -D PIN_USER_BTN_ANA=31 - -D DISPLAY_CLASS=SSD1306Display - -D MAX_CONTACTS=350 - -D MAX_GROUP_CHANNELS=40 - -D ENABLE_USB_INTERFACE -; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1 -; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1 -build_src_filter = ${rak4631_13302.build_src_filter} - +<../examples/companion_radio/*.cpp> - +<../examples/companion_radio/ui-new/*.cpp> -lib_deps = - ${rak4631_13302.lib_deps} - densaugeo/base64 @ ~1.4.0 - -[env:RAK_4631_13302_companion_radio_ble] -extends = rak4631_13302 -board_build.ldscript = boards/nrf52840_s140_v6_extrafs.ld -board_upload.maximum_size = 712704 -build_flags = - ${rak4631_13302.build_flags} - -I examples/companion_radio/ui-new - -D PIN_USER_BTN_ANA=31 - -D DISPLAY_CLASS=SSD1306Display - -D MAX_CONTACTS=350 - -D MAX_GROUP_CHANNELS=40 - -D BLE_PIN_CODE=123456 - -D BLE_DEBUG_LOGGING=1 - -D OFFLINE_QUEUE_SIZE=256 - ;-D MESH_PACKET_LOGGING=1 - ;-D MESH_DEBUG=1 -build_src_filter = ${rak4631_13302.build_src_filter} - + - +<../examples/companion_radio/*.cpp> - +<../examples/companion_radio/ui-new/*.cpp> -lib_deps = - ${rak4631_13302.lib_deps} - densaugeo/base64 @ ~1.4.0 - -[env:RAK_4631_13302_terminal_chat] -extends = rak4631_13302 -build_flags = - ${rak4631_13302.build_flags} - -D MAX_CONTACTS=100 - -D MAX_GROUP_CHANNELS=1 - ;-D MESH_PACKET_LOGGING=1 - ;-D MESH_DEBUG=1 -build_src_filter = ${rak4631_13302.build_src_filter} - +<../examples/simple_secure_chat/main.cpp> -lib_deps = - ${rak4631_13302.lib_deps} - densaugeo/base64 @ ~1.4.0 - -[env:RAK_4631_13302_sensor] -extends = rak4631_13302 -build_flags = - ${rak4631_13302.build_flags} - -D DISPLAY_CLASS=SSD1306Display - -D ADVERT_NAME='"RAK4631 Sensor"' - -D ADVERT_LAT=0.0 - -D ADVERT_LON=0.0 - -D ADMIN_PASSWORD='"password"' - ;-D MESH_PACKET_LOGGING=1 - ;-D MESH_DEBUG=1 -build_src_filter = ${rak4631_13302.build_src_filter} - + - +<../examples/simple_sensor> - -[env:RAK_4631_13302_kiss_modem] -extends = rak4631_13302 -build_src_filter = ${rak4631_13302.build_src_filter} - +<../examples/kiss_modem/> diff --git a/variants/rak3401/target.cpp b/variants/rak3401/target.cpp index dac12ede..b9d142b3 100644 --- a/variants/rak3401/target.cpp +++ b/variants/rak3401/target.cpp @@ -1,6 +1,8 @@ #include #include "target.h" #include +#include "OnboardRadioSleep.h" +#include RAK3401Board board; @@ -32,49 +34,91 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); EnvironmentSensorManager sensors; #endif -#ifdef RAK4631_ONBOARD_SX1262_SLEEP -// With a RAK13302 fitted the RAK4631's own SX1262 never gets initialized, so it -// sits in STDBY_RC drawing standby current for the life of the board. Firmware -// cannot cut its supply -- P1.05 is the RF switch rail, not the radio's -- so -// bring it up on its own pins just long enough to put it into cold sleep. -static void sleep_onboard_radio() { - pinMode(ONBOARD_LORA_ANT_SW, OUTPUT); - digitalWrite(ONBOARD_LORA_ANT_SW, LOW); // nothing downstream needs the RF switch +namespace { +class OnboardRadioIO { +public: + void begin() { + digitalWrite(ONBOARD_LORA_NSS, HIGH); + pinMode(ONBOARD_LORA_NSS, OUTPUT); + digitalWrite(ONBOARD_LORA_RESET, HIGH); + pinMode(ONBOARD_LORA_RESET, OUTPUT); + digitalWrite(ONBOARD_LORA_ANT_SW, LOW); + pinMode(ONBOARD_LORA_ANT_SW, OUTPUT); + // BUSY is weakly pulled high during startup; never override it with a pull-down. + pinMode(ONBOARD_LORA_BUSY, INPUT_PULLUP); + SPI.setPins(ONBOARD_LORA_MISO, ONBOARD_LORA_SCLK, ONBOARD_LORA_MOSI); + SPI.begin(); + pinMode(ONBOARD_LORA_MISO, INPUT_PULLDOWN); + } - SPI.setPins(ONBOARD_LORA_MISO, ONBOARD_LORA_SCLK, ONBOARD_LORA_MOSI); - SPI.begin(); + void reset() { + digitalWrite(ONBOARD_LORA_RESET, LOW); + delay(1); + digitalWrite(ONBOARD_LORA_RESET, HIGH); + delay(5); + } - Module mod(ONBOARD_LORA_NSS, ONBOARD_LORA_DIO_1, ONBOARD_LORA_RESET, ONBOARD_LORA_BUSY, SPI); - SX1262 onboard(&mod); + bool waitBusy(bool high, uint32_t timeout) { + uint32_t start = millis(); + while ((digitalRead(ONBOARD_LORA_BUSY) != LOW) != high) { + if (millis() - start >= timeout) return false; + delay(1); + } + return true; + } - // BUSY is driven low in STDBY_RC and high while asleep, so it doubles as a - // non-invasive readback of what state we found the chip in and left it in. - pinMode(ONBOARD_LORA_BUSY, INPUT); - int busy_before = digitalRead(ONBOARD_LORA_BUSY); + void readVersion(char* version) { + SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0)); + digitalWrite(ONBOARD_LORA_NSS, LOW); + SPI.transfer(RADIOLIB_SX126X_CMD_READ_REGISTER); + SPI.transfer(RADIOLIB_SX126X_REG_VERSION_STRING >> 8); + SPI.transfer(RADIOLIB_SX126X_REG_VERSION_STRING & 0xFF); + SPI.transfer(0); + for (unsigned i = 0; i < 6; i++) version[i] = SPI.transfer(0); + digitalWrite(ONBOARD_LORA_NSS, HIGH); + SPI.endTransaction(); + } - // begin() resets the chip and leaves it in standby. A TCXO calibration failure - // still leaves SPI usable, so issue the sleep regardless of what it returns. - int status = onboard.begin(434.0f, 125.0f, 9, 7, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, - 0, 8, ONBOARD_LORA_TCXO_VOLTAGE, false); - int slept = onboard.sleep(false); // cold sleep: config discarded, RTC off - delay(2); - int busy_after = digitalRead(ONBOARD_LORA_BUSY); - MESH_DEBUG_PRINTLN("onboard SX1262: begin=%d sleep=%d busy %d->%d", - status, slept, busy_before, busy_after); + void releaseProbePulls() { + pinMode(ONBOARD_LORA_BUSY, INPUT); + pinMode(ONBOARD_LORA_MISO, INPUT); + } - SPI.end(); // release SPIM3 so std_init() can repin it onto the RAK13302 + void sleep() { + SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0)); + digitalWrite(ONBOARD_LORA_NSS, LOW); + SPI.transfer(RADIOLIB_SX126X_CMD_SET_SLEEP); + SPI.transfer(RADIOLIB_SX126X_SLEEP_START_COLD | RADIOLIB_SX126X_SLEEP_RTC_OFF); + digitalWrite(ONBOARD_LORA_NSS, HIGH); + SPI.endTransaction(); + delay(1); + } - // The SX1262 wakes on an NSS falling edge, so keep NSS driven high from here on. - digitalWrite(ONBOARD_LORA_NSS, HIGH); - pinMode(ONBOARD_LORA_NSS, OUTPUT); + void end() { + SPI.end(); + // INPUT leaves buffers enabled on floating pins; restore Nordic's disconnected default. + nrf_gpio_cfg_default(g_ADigitalPinMap[ONBOARD_LORA_SCLK]); + nrf_gpio_cfg_default(g_ADigitalPinMap[ONBOARD_LORA_MOSI]); + nrf_gpio_cfg_default(g_ADigitalPinMap[ONBOARD_LORA_MISO]); + nrf_gpio_cfg_default(g_ADigitalPinMap[ONBOARD_LORA_BUSY]); + // An NSS falling edge wakes the radio, so leave it driven high. + digitalWrite(ONBOARD_LORA_NSS, HIGH); + } +}; } -#endif bool radio_init() { rtc_clock.begin(Wire); -#ifdef RAK4631_ONBOARD_SX1262_SLEEP - sleep_onboard_radio(); -#endif + // Called once from setup, before SPI is started for the RAK13302. + OnboardRadioIO onboard; + const uint32_t started = millis(); + // The SX1262 version register shares the "SX1261" prefix used by RadioLib. + const auto state = rak3401::sleepOnboardRadio(onboard, RADIOLIB_SX1262_CHIP_TYPE); + const char* outcome = state == rak3401::OnboardRadioState::Absent ? "absent" : + state == rak3401::OnboardRadioState::Asleep ? "asleep" : + state == rak3401::OnboardRadioState::BusyTimeout ? "busy-timeout" : "sleep-failed"; + MESH_DEBUG_PRINTLN("onboard SX1262: %s elapsed=%lu ms", outcome, + (unsigned long)(millis() - started)); return radio.std_init(&SPI); } diff --git a/variants/rak3401/variant.h b/variants/rak3401/variant.h index 0576dadb..779cb3cd 100644 --- a/variants/rak3401/variant.h +++ b/variants/rak3401/variant.h @@ -175,10 +175,7 @@ static const uint8_t AREF = PIN_AREF; #define P_LORA_BUSY SX126X_BUSY #define P_LORA_RESET SX126X_RESET -// The RAK4631 core carries its own SX1262, which is unused when a RAK13302 -// supplies the RF path. RAK4631_ONBOARD_SX1262_SLEEP builds put it into cold -// sleep at boot; on a genuine RAK3401 these pins are unconnected. -#ifdef RAK4631_ONBOARD_SX1262_SLEEP +// Optional RAK4631 onboard radio; these internal pins are unused on RAK3401. #define ONBOARD_LORA_NSS (42) // P1.10 #define ONBOARD_LORA_SCLK (43) // P1.11 #define ONBOARD_LORA_MOSI (44) // P1.12 @@ -187,8 +184,6 @@ static const uint8_t AREF = PIN_AREF; #define ONBOARD_LORA_DIO_1 (47) // P1.15 #define ONBOARD_LORA_RESET (38) // P1.06 #define ONBOARD_LORA_ANT_SW (37) // P1.05, powers the onboard RF switch -#define ONBOARD_LORA_TCXO_VOLTAGE 1.8 -#endif // enables 3.3V periphery like GPS or IO Module // Do not toggle this for GPS power savings From 277cae93ac719f76c082df9c1326d687f829ce47 Mon Sep 17 00:00:00 2001 From: agessaman Date: Fri, 16 Oct 2026 20:46:04 -0700 Subject: [PATCH 04/53] fix(rak3401): sleep onboard SX1262 before boot voltage check Move the onboard radio probe into RAK3401Board::begin() so a low-voltage SYSTEMOFF at boot no longer leaves the RAK4631's onboard SX1262 in standby. Add board startup tests and run them in CI. --- .github/workflows/run-unit-tests.yml | 2 +- platformio.ini | 8 ++- .../mocks/Arduino.h | 18 +++++ test/test_rak3401_board_startup/mocks/Wire.h | 6 ++ .../mocks/helpers/NRF52Board.h | 44 ++++++++++++ .../test_board_startup.cpp | 70 +++++++++++++++++++ variants/rak3401/OnboardRadioSleep.h | 3 + variants/rak3401/RAK3401Board.cpp | 3 + variants/rak3401/target.cpp | 9 ++- 9 files changed, 157 insertions(+), 6 deletions(-) create mode 100644 test/test_rak3401_board_startup/mocks/Arduino.h create mode 100644 test/test_rak3401_board_startup/mocks/Wire.h create mode 100644 test/test_rak3401_board_startup/mocks/helpers/NRF52Board.h create mode 100644 test/test_rak3401_board_startup/test_board_startup.cpp diff --git a/.github/workflows/run-unit-tests.yml b/.github/workflows/run-unit-tests.yml index 5d48f4c6..f8145ecd 100644 --- a/.github/workflows/run-unit-tests.yml +++ b/.github/workflows/run-unit-tests.yml @@ -20,7 +20,7 @@ jobs: uses: ./.github/actions/setup-build-environment - name: Run Unit Tests - run: pio test -e native -e native_kiss_modem -vv + run: pio test -e native -e native_kiss_modem -e native_rak3401_onboard_radio -vv - name: Upload Test Results # Upload test results even if the test step failed. diff --git a/platformio.ini b/platformio.ini index e305d177..ef6b1f39 100644 --- a/platformio.ini +++ b/platformio.ini @@ -165,6 +165,7 @@ build_flags = -std=c++17 -I test/mocks test_build_src = yes test_ignore = test_kiss_modem + test_rak3401_board_startup build_src_filter = -<*> +<../src/Utils.cpp> @@ -174,10 +175,13 @@ lib_deps = google/googletest @ 1.17.0 [env:native_rak3401_onboard_radio] -; Header-only tests without unrelated production sources; also included in env:native CI. +; Board startup needs isolated GPIO and power-management mocks. extends = env:native test_build_src = no -test_filter = test_rak3401_onboard_radio +test_filter = test_rak3401_* +test_ignore = +build_flags = -I test/test_rak3401_board_startup/mocks + ${env:native.build_flags} [env:native_kiss_modem] platform = native diff --git a/test/test_rak3401_board_startup/mocks/Arduino.h b/test/test_rak3401_board_startup/mocks/Arduino.h new file mode 100644 index 00000000..d505af36 --- /dev/null +++ b/test/test_rak3401_board_startup/mocks/Arduino.h @@ -0,0 +1,18 @@ +#pragma once + +#include "../../mocks/Arduino.h" + +#define NRF52_POWER_MANAGEMENT 1 +#define PWRMGT_LPCOMP_AIN 3 +#define PWRMGT_LPCOMP_REFSEL 4 +#define PWRMGT_VOLTAGE_BOOTLOCK 3300 +#define SX126X_POWER_EN 21 +#define INPUT 0 +#define OUTPUT 1 +#define LOW 0 +#define HIGH 1 + +void pinMode(uint32_t pin, uint32_t mode); +void digitalWrite(uint32_t pin, uint32_t value); +void analogReadResolution(uint32_t bits); +uint32_t analogRead(uint32_t pin); diff --git a/test/test_rak3401_board_startup/mocks/Wire.h b/test/test_rak3401_board_startup/mocks/Wire.h new file mode 100644 index 00000000..c24e6306 --- /dev/null +++ b/test/test_rak3401_board_startup/mocks/Wire.h @@ -0,0 +1,6 @@ +#pragma once + +struct StartupWire { + void begin() {} +}; +inline StartupWire Wire; diff --git a/test/test_rak3401_board_startup/mocks/helpers/NRF52Board.h b/test/test_rak3401_board_startup/mocks/helpers/NRF52Board.h new file mode 100644 index 00000000..96c892ae --- /dev/null +++ b/test/test_rak3401_board_startup/mocks/helpers/NRF52Board.h @@ -0,0 +1,44 @@ +#pragma once + +#include +#include +#include + +#define SHUTDOWN_REASON_LOW_VOLTAGE 0x4C +#define SHUTDOWN_REASON_BOOT_PROTECT 0x42 + +struct PowerMgtConfig { + uint8_t lpcomp_ain_channel; + uint8_t lpcomp_refsel; + uint16_t voltage_bootlock; +}; + +namespace startup_test { +extern std::vector events; +extern bool permitBoot; +struct SystemOff { uint8_t reason; }; +} + +class NRF52Board { +protected: + bool checkBootVoltage(const PowerMgtConfig*) { + startup_test::events.push_back("voltage"); + if (!startup_test::permitBoot) initiateShutdown(SHUTDOWN_REASON_BOOT_PROTECT); + return true; + } + virtual void initiateShutdown(uint8_t reason) { enterSystemOff(reason); } + void configureVoltageWake(uint8_t, uint8_t) {} + void enterSystemOff(uint8_t reason) { + startup_test::events.push_back("off"); + throw startup_test::SystemOff{reason}; + } +public: + explicit NRF52Board(const char* = nullptr) {} + virtual uint16_t getBattMilliVolts() = 0; + virtual const char* getManufacturerName() const = 0; +}; + +class NRF52BoardDCDC : virtual public NRF52Board { +public: + void begin() {} +}; diff --git a/test/test_rak3401_board_startup/test_board_startup.cpp b/test/test_rak3401_board_startup/test_board_startup.cpp new file mode 100644 index 00000000..0bbf2e5a --- /dev/null +++ b/test/test_rak3401_board_startup/test_board_startup.cpp @@ -0,0 +1,70 @@ +#include +#include +// Exercise the real startup code with a power-management stub that stops at SYSTEMOFF. +#include "../../variants/rak3401/RAK3401Board.cpp" + +namespace startup_test { +std::vector events; +bool permitBoot; +std::array outputs; +rak3401::OnboardRadioState radioState; +} + +void pinMode(uint32_t, uint32_t) {} +void digitalWrite(uint32_t pin, uint32_t value) { startup_test::outputs.at(pin) = value; } +void analogReadResolution(uint32_t) {} +uint32_t analogRead(uint32_t) { return 2500; } + +rak3401::OnboardRadioState rak3401::sleepOnboardRadio() { + startup_test::events.push_back("onboard"); + return startup_test::radioState; +} + +class RakBoardStartup : public testing::Test { +protected: + void SetUp() override { + startup_test::events.clear(); + startup_test::outputs.fill(HIGH); + startup_test::permitBoot = true; + startup_test::radioState = rak3401::OnboardRadioState::Asleep; + } +}; + +TEST_F(RakBoardStartup, OnboardRadioHandledBeforeVoltageCheck) { + RAK3401Board board; + board.begin(); + EXPECT_EQ(startup_test::events, (std::vector{"onboard", "voltage"})); + EXPECT_EQ(startup_test::outputs.at(SX126X_POWER_EN), HIGH); + EXPECT_EQ(startup_test::outputs.at(PIN_3V3_EN), HIGH); +} + +TEST_F(RakBoardStartup, LowVoltageExitStillHandlesOnboardRadio) { + startup_test::permitBoot = false; + RAK3401Board board; + try { + board.begin(); + FAIL() << "Expected protective shutdown"; + } catch (const startup_test::SystemOff& off) { + EXPECT_EQ(off.reason, SHUTDOWN_REASON_BOOT_PROTECT); + } + EXPECT_EQ(startup_test::events, (std::vector{"onboard", "voltage", "off"})); + EXPECT_EQ(startup_test::outputs.at(SX126X_POWER_EN), LOW); + EXPECT_EQ(startup_test::outputs.at(PIN_3V3_EN), LOW); +} + +TEST_F(RakBoardStartup, OptionalRadioFailuresDoNotBypassBootProtection) { + for (auto state : {rak3401::OnboardRadioState::Absent, rak3401::OnboardRadioState::BusyTimeout, + rak3401::OnboardRadioState::SleepFailed}) { + startup_test::events.clear(); + startup_test::permitBoot = false; + startup_test::radioState = state; + RAK3401Board board; + EXPECT_THROW(board.begin(), startup_test::SystemOff); + EXPECT_EQ(startup_test::events, (std::vector{"onboard", "voltage", "off"})); + } +} + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/variants/rak3401/OnboardRadioSleep.h b/variants/rak3401/OnboardRadioSleep.h index 7f6d1c55..a9690cbf 100644 --- a/variants/rak3401/OnboardRadioSleep.h +++ b/variants/rak3401/OnboardRadioSleep.h @@ -6,6 +6,9 @@ namespace rak3401 { enum class OnboardRadioState { Absent, Asleep, BusyTimeout, SleepFailed }; +// Board startup only, before the primary radio starts SPI. +OnboardRadioState sleepOnboardRadio(); + // IO owns the temporary SPI pin assignment; every outcome must release it. template OnboardRadioState sleepOnboardRadio(IO& io, const char* expectedVersion) { diff --git a/variants/rak3401/RAK3401Board.cpp b/variants/rak3401/RAK3401Board.cpp index cbf7c108..6f59be40 100644 --- a/variants/rak3401/RAK3401Board.cpp +++ b/variants/rak3401/RAK3401Board.cpp @@ -2,6 +2,7 @@ #include #include "RAK3401Board.h" +#include "OnboardRadioSleep.h" #ifdef NRF52_POWER_MANAGEMENT // Static configuration for power management @@ -30,6 +31,8 @@ void RAK3401Board::initiateShutdown(uint8_t reason) { void RAK3401Board::begin() { NRF52BoardDCDC::begin(); + // Voltage protection can enter SYSTEMOFF before radio_init() is reached. + rak3401::sleepOnboardRadio(); pinMode(PIN_VBAT_READ, INPUT); #ifdef PIN_USER_BTN pinMode(PIN_USER_BTN, INPUT_PULLUP); diff --git a/variants/rak3401/target.cpp b/variants/rak3401/target.cpp index b9d142b3..bd971c55 100644 --- a/variants/rak3401/target.cpp +++ b/variants/rak3401/target.cpp @@ -107,9 +107,7 @@ public: }; } -bool radio_init() { - rtc_clock.begin(Wire); - // Called once from setup, before SPI is started for the RAK13302. +rak3401::OnboardRadioState rak3401::sleepOnboardRadio() { OnboardRadioIO onboard; const uint32_t started = millis(); // The SX1262 version register shares the "SX1261" prefix used by RadioLib. @@ -119,6 +117,11 @@ bool radio_init() { state == rak3401::OnboardRadioState::BusyTimeout ? "busy-timeout" : "sleep-failed"; MESH_DEBUG_PRINTLN("onboard SX1262: %s elapsed=%lu ms", outcome, (unsigned long)(millis() - started)); + return state; +} + +bool radio_init() { + rtc_clock.begin(Wire); return radio.std_init(&SPI); } From bae1b189d40241d4c294ef08437b2b89f4eb692b Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 17 Oct 2026 23:08:26 +0200 Subject: [PATCH 05/53] Add repeater environment for Wio Tracker L1 E-Ink Adds WioTrackerL1Eink_repeater, based on the OLED WioTrackerL1_repeater env but using the E-Ink base config and GxEPDDisplay. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_012uhHFHwrigVgmzkD9GFAfA --- variants/wio-tracker-l1-eink/platformio.ini | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/variants/wio-tracker-l1-eink/platformio.ini b/variants/wio-tracker-l1-eink/platformio.ini index 2c74bade..08f759b7 100644 --- a/variants/wio-tracker-l1-eink/platformio.ini +++ b/variants/wio-tracker-l1-eink/platformio.ini @@ -69,6 +69,21 @@ lib_deps = ${WioTrackerL1Eink.lib_deps} end2endzone/NonBlockingRTTTL@^1.3.0 debug_tool=stlink +[env:WioTrackerL1Eink_repeater] +extends = WioTrackerL1Eink +build_src_filter = ${WioTrackerL1Eink.build_src_filter} + +<../examples/simple_repeater> +build_flags = + ${WioTrackerL1Eink.build_flags} + -D ADVERT_NAME='"WioTrackerL1 Eink Repeater"' + -D ADMIN_PASSWORD='"password"' + -D MAX_NEIGHBOURS=50 + -D DISPLAY_CLASS=GxEPDDisplay +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +lib_deps = ${WioTrackerL1Eink.lib_deps} + adafruit/RTClib @ ^2.1.3 + [env:WioTrackerL1Eink_kiss_modem] extends = WioTrackerL1Eink build_src_filter = ${WioTrackerL1Eink.build_src_filter} From e296c49580a487e69319445af4262d899d88b8aa Mon Sep 17 00:00:00 2001 From: Luca Olivieri Date: Tue, 20 Oct 2026 19:58:14 +0200 Subject: [PATCH 06/53] Add LilyGO T3 STM32 support --- .github/workflows/pr-build-check.yml | 2 + README.md | 1 + boards/lilygo_t3_stm32.json | 32 ++++++++ src/helpers/ui/SSD1306Display.cpp | 7 +- src/helpers/ui/SSD1306Display.h | 8 ++ variants/lilygo_t3_stm32/README.md | 66 +++++++++++++++ variants/lilygo_t3_stm32/STM32ArduinoCompat.h | 11 +++ variants/lilygo_t3_stm32/platformio.ini | 52 ++++++++++++ variants/lilygo_t3_stm32/target.cpp | 82 +++++++++++++++++++ variants/lilygo_t3_stm32/target.h | 28 +++++++ 10 files changed, 288 insertions(+), 1 deletion(-) create mode 100644 boards/lilygo_t3_stm32.json create mode 100644 variants/lilygo_t3_stm32/README.md create mode 100644 variants/lilygo_t3_stm32/STM32ArduinoCompat.h create mode 100644 variants/lilygo_t3_stm32/platformio.ini create mode 100644 variants/lilygo_t3_stm32/target.cpp create mode 100644 variants/lilygo_t3_stm32/target.h diff --git a/.github/workflows/pr-build-check.yml b/.github/workflows/pr-build-check.yml index cebf0cfe..0dcb159e 100644 --- a/.github/workflows/pr-build-check.yml +++ b/.github/workflows/pr-build-check.yml @@ -40,6 +40,8 @@ jobs: - PicoW_repeater # STM32 - wio-e5-mini_repeater + - LilyGo_T3_STM32_repeater + - LilyGo_T3_STM32_companion_radio_usb # ESP32-C6 - LilyGo_Tlora_C6_repeater_ # LR1110 (nRF52) diff --git a/README.md b/README.md index 6cc45d7f..a8f72e0f 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ For developers: - Install [PlatformIO](https://docs.platformio.org) in [Visual Studio Code](https://code.visualstudio.com). - Clone and open the MeshCore repository in Visual Studio Code. +- For the LilyGO T3 STM32, see the [board build and flashing guide](./variants/lilygo_t3_stm32/README.md). - See the example applications you can modify and run: - [Companion Radio](./examples/companion_radio) - For use with an external chat app, over BLE, USB or Wi-Fi. - [KISS Modem](./examples/kiss_modem) - Serial KISS protocol bridge for host applications. ([protocol docs](./docs/kiss_modem_protocol.md)) diff --git a/boards/lilygo_t3_stm32.json b/boards/lilygo_t3_stm32.json new file mode 100644 index 00000000..3f323402 --- /dev/null +++ b/boards/lilygo_t3_stm32.json @@ -0,0 +1,32 @@ +{ + "build": { + "arduino": { + "variant_h": "variant_generic.h" + }, + "core": "stm32", + "cpu": "cortex-m4", + "extra_flags": "-DSTM32WL -DSTM32WLxx -DSTM32WL55xx", + "f_cpu": "48000000L", + "framework_extra_flags": { + "arduino": "-DUSE_CM4_STARTUP_FILE -DARDUINO_GENERIC_WL55CCUX" + }, + "mcu": "stm32wl55ccu6", + "product_line": "STM32WL55xx", + "variant": "STM32WLxx/WL54CCU_WL55CCU_WLE4C(8-B-C)U_WLE5C(8-B-C)U" + }, + "debug": { + "jlink_device": "STM32WL55CC", + "openocd_target": "stm32wlx", + "svd_path": "STM32WL5x_CM4.svd" + }, + "frameworks": ["arduino"], + "name": "LilyGO T3 STM32 V1.0 (STM32WL55CCU6)", + "upload": { + "maximum_ram_size": 65536, + "maximum_size": 262144, + "protocol": "serial", + "protocols": ["serial", "stlink", "jlink"] + }, + "url": "https://github.com/Xinyuan-LilyGO/T3-STM32", + "vendor": "LilyGO" +} diff --git a/src/helpers/ui/SSD1306Display.cpp b/src/helpers/ui/SSD1306Display.cpp index ab2d77fb..4c33f6f8 100644 --- a/src/helpers/ui/SSD1306Display.cpp +++ b/src/helpers/ui/SSD1306Display.cpp @@ -25,7 +25,12 @@ bool SSD1306Display::begin() { #ifdef DISPLAY_ROTATION display.setRotation(DISPLAY_ROTATION); #endif - return display.begin(SSD1306_SWITCHCAPVCC, DISPLAY_ADDRESS, true, false) && i2c_probe(Wire, DISPLAY_ADDRESS); + bool success = display.begin(SSD1306_SWITCHCAPVCC, DISPLAY_ADDRESS, true, false); + #if defined(PIN_OLED_CS) && defined(PIN_OLED_DC) + return success; + #else + return success && i2c_probe(Wire, DISPLAY_ADDRESS); + #endif } void SSD1306Display::turnOn() { diff --git a/src/helpers/ui/SSD1306Display.h b/src/helpers/ui/SSD1306Display.h index 5dabbc58..e8f1ded3 100644 --- a/src/helpers/ui/SSD1306Display.h +++ b/src/helpers/ui/SSD1306Display.h @@ -15,6 +15,10 @@ #define DISPLAY_ADDRESS 0x3C #endif +#if defined(PIN_OLED_CS) != defined(PIN_OLED_DC) + #error "SPI OLED requires both PIN_OLED_CS and PIN_OLED_DC" +#endif + class SSD1306Display : public DisplayDriver { Adafruit_SSD1306 display; bool _isOn; @@ -24,7 +28,11 @@ class SSD1306Display : public DisplayDriver { bool i2c_probe(TwoWire& wire, uint8_t addr); public: SSD1306Display(RefCountedDigitalPin* peripher_power=NULL) : DisplayDriver(128, 64), +#if defined(PIN_OLED_CS) && defined(PIN_OLED_DC) + display(128, 64, &SPI, PIN_OLED_DC, PIN_OLED_RESET, PIN_OLED_CS), +#else display(128, 64, &Wire, PIN_OLED_RESET), +#endif _peripher_power(peripher_power) { _isOn = false; diff --git a/variants/lilygo_t3_stm32/README.md b/variants/lilygo_t3_stm32/README.md new file mode 100644 index 00000000..2686aa7f --- /dev/null +++ b/variants/lilygo_t3_stm32/README.md @@ -0,0 +1,66 @@ +# LilyGO T3 STM32 V1.0 + +MVP support for the STM32WL55CCU6 board with its integrated Sub-GHz radio and +128×64 SSD1315 SPI OLED. Supports a USB companion and a repeater, with a default +transmit power of **20 dBm**. The companion uses USB serial; the board has no BLE. + +## Build + +```sh +pio run -e LilyGo_T3_STM32_companion_radio_usb +pio run -e LilyGo_T3_STM32_repeater +``` + +The companion supports 100 contacts and eight channels. Radio settings inherit +the repository defaults; select settings appropriate to the board's RF band. +Existing saved preferences override build defaults. + +## Flash and connect + +Connect the USB serial port. Hold BOOT, press and release RST, then release BOOT: + +```sh +pio run -e LilyGo_T3_STM32_companion_radio_usb -t upload --upload-port /dev/ttyACM0 +``` + +Choose your actual serial port, then press RST after programming. The repeater +CLI and USB companion protocol both use USART1 at **115200 baud**. + +The bundled `stm32flash` 0.7 may reject device ID `0x497` due to an +[upstream packaging issue](https://github.com/platformio/platform-ststm32/issues/899). +Use an updated [stm32flash](https://sourceforge.net/p/stm32flash/code/ci/master/tree/) +with STM32WL support, or STM32CubeProgrammer in UART mode. For CubeProgrammer, +use `.pio/build//firmware.hex`, or `firmware.bin` at `0x08000000`. +ST-Link and J-Link uploads are also available through the board's SWD connector. + +Application flash is limited to **224 KiB**. InternalFS occupies the final +32 KiB at `0x08038000`–`0x0803FFFF`; preserve it when updating to retain the +identity and settings. + +## Hardware mapping + +| Function | Pins / setting | +| --- | --- | +| USART1 | TX PA9, RX PA10 | +| Radio RF switch | PB2/PB6: idle 0/0, RX 1/0, HP TX 0/1 | +| Radio oscillator | 32 MHz crystal; TCXO mode disabled | +| SPI1 | SCK PA5, MISO PA6, MOSI PA7 | +| OLED | CS PB12, DC PA8, no software reset pin | +| SD card | CS PA4, held high | +| User button | PH3/BOOT, active high | +| LEDs | PA0/PA1, left off | + +Initialization follows the manufacturer's +[hardware files](https://github.com/Xinyuan-LilyGO/T3-STM32/tree/master/hardware) +and [reference firmware](https://github.com/Xinyuan-LilyGO/T3-STM32/tree/master/examples/6_SubGHz_TXRX). + +## MVP validation and limitations + +Hardware checks covered OLED startup, USB companion communication, advertisement +reception, Public message transmission, and matching repeater trace replies at +20 dBm. Public packets were captured and decrypted independently. Some repeater +pings timed out; long-term reliability has not been established. Higher transmit +powers are outside the validated MVP setting. + +The repeater builds successfully; forwarding has not been hardware-tested. +Battery telemetry, SD storage, and low-power optimization are not implemented. diff --git a/variants/lilygo_t3_stm32/STM32ArduinoCompat.h b/variants/lilygo_t3_stm32/STM32ArduinoCompat.h new file mode 100644 index 00000000..4c705c20 --- /dev/null +++ b/variants/lilygo_t3_stm32/STM32ArduinoCompat.h @@ -0,0 +1,11 @@ +#pragma once + +#ifdef __cplusplus + // Include the device headers first so subsequent Arduino includes cannot + // reintroduce the peripheral macro. It conflicts with both mesh::RNG and + // Crypto's global RNG object. Leave C HAL sources' register macros intact. + #include + #ifdef RNG + #undef RNG + #endif +#endif diff --git a/variants/lilygo_t3_stm32/platformio.ini b/variants/lilygo_t3_stm32/platformio.ini new file mode 100644 index 00000000..ba56cf52 --- /dev/null +++ b/variants/lilygo_t3_stm32/platformio.ini @@ -0,0 +1,52 @@ +[lilygo_t3_stm32] +extends = stm32_base +board = lilygo_t3_stm32 +board_upload.maximum_size = 229376 ; final 32 KiB reserved for InternalFS +build_flags = ${stm32_base.build_flags} + -include variants/lilygo_t3_stm32/STM32ArduinoCompat.h + -I variants/lilygo_t3_stm32 + -D RADIO_CLASS=CustomSTM32WLx + -D WRAPPER_CLASS=CustomSTM32WLxWrapper + -D LORA_TX_POWER=20 + -D SX126X_CURRENT_LIMIT=140 + -D RX_BOOSTED_GAIN=true + -D STM32WL_TCXO_VOLTAGE=0 ; crystal oscillator; manufacturer sets IS_TCXO_SUPPORTED=0 + -D PIN_SERIAL_TX=PA9 + -D PIN_SERIAL_RX=PA10 + -D SERIAL_UART_INSTANCE=1 + -D PIN_SPI_SCK=PA5 + -D PIN_SPI_MISO=PA6 + -D PIN_SPI_MOSI=PA7 + -D PIN_OLED_CS=PB12 + -D PIN_OLED_DC=PA8 + -D PIN_OLED_RESET=-1 + -D PIN_USER_BTN=PH3 + -D USER_BTN_PRESSED=HIGH + -D DISPLAY_CLASS=SSD1306Display +build_src_filter = ${stm32_base.build_src_filter} + +<../variants/lilygo_t3_stm32> + + +lib_deps = ${stm32_base.lib_deps} + adafruit/Adafruit SSD1306 @ ^2.5.13 + +[env:LilyGo_T3_STM32_repeater] +extends = lilygo_t3_stm32 +build_flags = ${lilygo_t3_stm32.build_flags} + -D ADVERT_NAME='"T3 STM32 Repeater"' + -D ADMIN_PASSWORD='"password"' + -D MAX_NEIGHBOURS=50 +build_src_filter = ${lilygo_t3_stm32.build_src_filter} + +<../examples/simple_repeater/*.cpp> + +[env:LilyGo_T3_STM32_companion_radio_usb] +extends = lilygo_t3_stm32 +build_flags = ${lilygo_t3_stm32.build_flags} + -I examples/companion_radio/ui-orig + -D MAX_CONTACTS=100 + -D MAX_GROUP_CHANNELS=8 + -D ENABLE_USB_INTERFACE +build_src_filter = ${lilygo_t3_stm32.build_src_filter} + +<../examples/companion_radio/*.cpp> + +<../examples/companion_radio/ui-orig/*.cpp> +lib_deps = ${lilygo_t3_stm32.lib_deps} + densaugeo/base64 @ ~1.4.0 diff --git a/variants/lilygo_t3_stm32/target.cpp b/variants/lilygo_t3_stm32/target.cpp new file mode 100644 index 00000000..f98783fe --- /dev/null +++ b/variants/lilygo_t3_stm32/target.cpp @@ -0,0 +1,82 @@ +#include +#include +#include "target.h" + +LilyGoT3STM32Board board; +RADIO_CLASS radio = new STM32WLx_Module(); +WRAPPER_CLASS radio_driver(radio, board); +VolatileRTCClock rtc_clock; +SensorManager sensors; +DISPLAY_CLASS display; +// PH3 is also BOOT0: the board's button is active high with an external pull-down. +MomentaryButton user_btn(PIN_USER_BTN, 1000, false); + +static const uint32_t rfswitch_pins[] = { + PB2, PB6, RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC +}; +static const Module::RfSwitchMode_t rfswitch_table[] = { + {STM32WLx::MODE_IDLE, {LOW, LOW}}, + {STM32WLx::MODE_RX, {HIGH, LOW}}, + {STM32WLx::MODE_TX_HP, {LOW, HIGH}}, + END_OF_MODE_TABLE, +}; + +void LilyGoT3STM32Board::begin() { + STM32Board::begin(); + + // The SD card shares SPI1 with the OLED; keep it deselected. + digitalWrite(PA4, HIGH); + pinMode(PA4, OUTPUT); + digitalWrite(PIN_OLED_CS, HIGH); + pinMode(PIN_OLED_CS, OUTPUT); + pinMode(PIN_USER_BTN, INPUT); + // Both onboard LEDs are active high. Leave them off until explicitly needed. + digitalWrite(PA0, LOW); + pinMode(PA0, OUTPUT); + digitalWrite(PA1, LOW); + pinMode(PA1, OUTPUT); + + SPI.setSCLK(PIN_SPI_SCK); + SPI.setMISO(PIN_SPI_MISO); + SPI.setMOSI(PIN_SPI_MOSI); + SPI.begin(); + delay(200); // OLED power-on settling time used by the manufacturer's example. +} + +#ifndef LORA_CR + #define LORA_CR 5 +#endif + +static bool check_radio_status(int status, const char* stage) { + if (status == RADIOLIB_ERR_NONE) return true; + + Serial.print("ERROR: "); + Serial.print(stage); + Serial.print(": "); + Serial.println(status); + return false; +} + +bool radio_init() { + radio.setRfSwitchTable(rfswitch_pins, rfswitch_table); + int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, + RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, + 16, STM32WL_TCXO_VOLTAGE, 0); + if (!check_radio_status(status, "radio.begin")) return false; + + // Match the manufacturer's 3.3 V power-LDO trim (REG_DRV_CTRL). + const uint8_t power_ldo_3v3 = 0x0E; + status = radio.writeRegister(0x091F, &power_ldo_3v3, 1); + if (!check_radio_status(status, "power LDO trim")) return false; + + status = radio.setCurrentLimit(SX126X_CURRENT_LIMIT); + if (!check_radio_status(status, "current limit")) return false; + status = radio.setRxBoostedGainMode(RX_BOOSTED_GAIN); + if (!check_radio_status(status, "RX gain")) return false; + return check_radio_status(radio.setCRC(1), "CRC"); +} + +mesh::LocalIdentity radio_new_identity() { + RadioNoiseListener rng(radio); + return mesh::LocalIdentity(&rng); +} diff --git a/variants/lilygo_t3_stm32/target.h b/variants/lilygo_t3_stm32/target.h new file mode 100644 index 00000000..83235cbd --- /dev/null +++ b/variants/lilygo_t3_stm32/target.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +class LilyGoT3STM32Board : public STM32Board { +public: + void begin() override; + + const char* getManufacturerName() const override { + return "LilyGO T3 STM32"; + } +}; + +extern LilyGoT3STM32Board board; +extern WRAPPER_CLASS radio_driver; +extern VolatileRTCClock rtc_clock; +extern SensorManager sensors; +extern DISPLAY_CLASS display; +extern MomentaryButton user_btn; + +bool radio_init(); +mesh::LocalIdentity radio_new_identity(); From ff0ce69c1614da7dab2eb482a8e289ae00806833 Mon Sep 17 00:00:00 2001 From: Martin-hub1 Date: Thu, 22 Oct 2026 17:53:57 +0800 Subject: [PATCH 07/53] Add DFRobot LoRaWAN ESP32-S3 Companion BLE firmware Add a board variant for the DFRobot DFR1195 (ESP32-S3-WROOM-1-N4, SX1262, 80x160 ST7735S) and a Companion BLE environment. The display and radio use separate SPI controllers: the SX1262 is on FSPI and the ST7735 is on HSPI. ST7735Display selects that HSPI bus and the 160x80 init sequence only when DFROBOT_LORAWAN_ESP32S3 is defined. --- boards/dfrobot_lorawan_esp32s3.json | 39 ++++++++++ src/helpers/ui/ST7735Display.cpp | 5 +- .../DFRobotLoRaBoard.cpp | 20 +++++ .../DFRobotLoRaBoard.h | 15 ++++ .../dfrobot_lorawan_esp32s3/platformio.ini | 73 +++++++++++++++++++ variants/dfrobot_lorawan_esp32s3/target.cpp | 34 +++++++++ variants/dfrobot_lorawan_esp32s3/target.h | 26 +++++++ 7 files changed, 210 insertions(+), 2 deletions(-) create mode 100644 boards/dfrobot_lorawan_esp32s3.json create mode 100644 variants/dfrobot_lorawan_esp32s3/DFRobotLoRaBoard.cpp create mode 100644 variants/dfrobot_lorawan_esp32s3/DFRobotLoRaBoard.h create mode 100644 variants/dfrobot_lorawan_esp32s3/platformio.ini create mode 100644 variants/dfrobot_lorawan_esp32s3/target.cpp create mode 100644 variants/dfrobot_lorawan_esp32s3/target.h diff --git a/boards/dfrobot_lorawan_esp32s3.json b/boards/dfrobot_lorawan_esp32s3.json new file mode 100644 index 00000000..62e6cfed --- /dev/null +++ b/boards/dfrobot_lorawan_esp32s3.json @@ -0,0 +1,39 @@ +{ + "build": { + "arduino": { + "ldscript": "esp32s3_out.ld" + }, + "core": "esp32", + "extra_flags": [ + "-D ARDUINO_USB_CDC_ON_BOOT=0", + "-D ARDUINO_USB_MSC_ON_BOOT=0", + "-D ARDUINO_USB_DFU_ON_BOOT=0", + "-D ARDUINO_USB_MODE=0", + "-D ARDUINO_RUNNING_CORE=1", + "-D ARDUINO_EVENT_RUNNING_CORE=1" + ], + "f_cpu": "240000000L", + "f_flash": "80000000L", + "flash_mode": "qio", + "hwids": [["0x303A", "0x1001"]], + "mcu": "esp32s3", + "variant": "esp32s3" + }, + "connectivity": ["wifi", "bluetooth", "lora"], + "debug": { + "default_tool": "esp-builtin", + "onboard_tools": ["esp-builtin"], + "openocd_target": "esp32s3.cfg" + }, + "frameworks": ["arduino", "espidf"], + "name": "DFRobot LoRaWAN ESP32-S3 (4 MB Flash, No PSRAM)", + "upload": { + "flash_size": "4MB", + "maximum_ram_size": 524288, + "maximum_size": 4194304, + "require_upload_port": true, + "speed": 921600 + }, + "url": "https://www.dfrobot.com", + "vendor": "DFRobot" +} diff --git a/src/helpers/ui/ST7735Display.cpp b/src/helpers/ui/ST7735Display.cpp index 344f5b1d..9143ee01 100644 --- a/src/helpers/ui/ST7735Display.cpp +++ b/src/helpers/ui/ST7735Display.cpp @@ -102,7 +102,8 @@ static TFT_eSPI lcd = TFT_eSPI(160, 80); static uint32_t curr_color; -#if defined(HELTEC_LORA_V3) || defined(HELTEC_TRACKER_V2) +#if defined(HELTEC_LORA_V3) || defined(HELTEC_TRACKER_V2) || defined(DFROBOT_LORAWAN_ESP32S3) + // ESP32-S3: HSPI is SPI3. The SX1262 uses FSPI (SPI2) so the two buses do not share a controller. static SPIClass tft_spi(HSPI); #define _spi (&tft_spi) #else @@ -490,7 +491,7 @@ void ST7735Display::_resetAndInit() { // run init commands displayInit(Rcmd1); -#if defined(HELTEC_TRACKER_V2) || defined(HELTEC_T096) +#if defined(HELTEC_TRACKER_V2) || defined(HELTEC_T096) || defined(DFROBOT_LORAWAN_ESP32S3) displayInit(Rcmd2green160x80); //uint8_t madctl = ST77XX_MADCTL_MY | ST77XX_MADCTL_MV |ST7735_MADCTL_BGR;//Adjust color to BGR //display.sendCommand(ST77XX_MADCTL, &madctl, 1); diff --git a/variants/dfrobot_lorawan_esp32s3/DFRobotLoRaBoard.cpp b/variants/dfrobot_lorawan_esp32s3/DFRobotLoRaBoard.cpp new file mode 100644 index 00000000..fd04a971 --- /dev/null +++ b/variants/dfrobot_lorawan_esp32s3/DFRobotLoRaBoard.cpp @@ -0,0 +1,20 @@ +#include "DFRobotLoRaBoard.h" + +void DFRobotLoRaBoard::begin() { + // SX1262 module power. Held high for the whole boot; Companion does not + // drop into deep sleep, so the pin does not need an RTC hold. + pinMode(LORA_POWER_EN, OUTPUT); + digitalWrite(LORA_POWER_EN, HIGH); + delay(10); + + periph_power.begin(); + + pinMode(PIN_LED, OUTPUT); + digitalWrite(PIN_LED, HIGH); + + ESP32Board::begin(); +} + +const char* DFRobotLoRaBoard::getManufacturerName() const { + return "DFRobot LoRaWAN ESP32-S3"; +} diff --git a/variants/dfrobot_lorawan_esp32s3/DFRobotLoRaBoard.h b/variants/dfrobot_lorawan_esp32s3/DFRobotLoRaBoard.h new file mode 100644 index 00000000..80423486 --- /dev/null +++ b/variants/dfrobot_lorawan_esp32s3/DFRobotLoRaBoard.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include +#include + +class DFRobotLoRaBoard : public ESP32Board { +public: + RefCountedDigitalPin periph_power; + + DFRobotLoRaBoard() : periph_power(PIN_VEXT_EN, PIN_VEXT_EN_ACTIVE) { } + + void begin(); + const char* getManufacturerName() const override; +}; diff --git a/variants/dfrobot_lorawan_esp32s3/platformio.ini b/variants/dfrobot_lorawan_esp32s3/platformio.ini new file mode 100644 index 00000000..0f69e276 --- /dev/null +++ b/variants/dfrobot_lorawan_esp32s3/platformio.ini @@ -0,0 +1,73 @@ +; DFRobot LoRaWAN ESP32-S3 (DFR1195) +; ESP32-S3-WROOM-1-N4 (4 MB flash, no PSRAM), SX1262, SPI ST7735S 80x160. +; Pin map follows variants/esp32s3/DFRobot_LoRaWAN_ESP32-S3 in the Meshtastic tree. +; LoRa is on FSPI. The display is on HSPI (SPI3), matching ST7735_SPI_HOST. +[DFRobot_LoRaWAN_ESP32S3] +extends = esp32_base +board = dfrobot_lorawan_esp32s3 +; 4 MB module: app slots are 1.9 MB. The default 4 MB table only leaves 1.25 MB. +board_build.partitions = min_spiffs.csv +build_flags = + ${esp32_base.build_flags} + -I variants/dfrobot_lorawan_esp32s3 + -D DFROBOT_LORAWAN_ESP32S3 + -D ESP32_CPU_FREQ=80 + -D RADIO_CLASS=CustomSX1262 + -D WRAPPER_CLASS=CustomSX1262Wrapper + -D LORA_TX_POWER=22 + -D P_LORA_NSS=10 + -D P_LORA_DIO_1=4 + -D P_LORA_RESET=41 + -D P_LORA_BUSY=40 + -D P_LORA_SCLK=7 + -D P_LORA_MISO=5 + -D P_LORA_MOSI=6 + -D LORA_POWER_EN=42 + -D SX126X_DIO2_AS_RF_SWITCH=true + -D SX126X_DIO3_TCXO_VOLTAGE=3.3 + -D SX126X_CURRENT_LIMIT=140 + -D SX126X_RX_BOOSTED_GAIN=1 + -D PIN_USER_BTN=18 + -D PIN_LED=21 + -D PIN_VEXT_EN=48 + -D PIN_VEXT_EN_ACTIVE=LOW + -D PIN_BOARD_SDA=8 + -D PIN_BOARD_SCL=9 + -D PIN_TFT_CS=17 + -D PIN_TFT_DC=14 + -D PIN_TFT_RST=15 + -D PIN_TFT_SDA=11 + -D PIN_TFT_SCL=12 + -D PIN_TFT_LEDA_CTL=16 + -D PIN_TFT_LEDA_CTL_ACTIVE=HIGH +build_src_filter = ${esp32_base.build_src_filter} + +<../variants/dfrobot_lorawan_esp32s3> +lib_deps = + ${esp32_base.lib_deps} + bodmer/TFT_eSPI @ ^2.4.31 + +; Landscape UI on the 80x160 panel. Use 3 if the picture is upside down. +[env:DFRobot_LoRaWAN_ESP32S3_companion_radio_ble] +extends = DFRobot_LoRaWAN_ESP32S3 +build_flags = + ${DFRobot_LoRaWAN_ESP32S3.build_flags} + -I src/helpers/ui + -I examples/companion_radio/ui-new + -D DISPLAY_ROTATION=3 + -D DISPLAY_CLASS=ST7735Display + -D MAX_CONTACTS=350 + -D MAX_GROUP_CHANNELS=40 + -D BLE_PIN_CODE=123456 + -D OFFLINE_QUEUE_SIZE=256 +; -D BLE_DEBUG_LOGGING=1 +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +build_src_filter = ${DFRobot_LoRaWAN_ESP32S3.build_src_filter} + + + + + + + +<../examples/companion_radio/*.cpp> + +<../examples/companion_radio/ui-new/*.cpp> +lib_deps = + ${DFRobot_LoRaWAN_ESP32S3.lib_deps} + densaugeo/base64 @ ~1.4.0 diff --git a/variants/dfrobot_lorawan_esp32s3/target.cpp b/variants/dfrobot_lorawan_esp32s3/target.cpp new file mode 100644 index 00000000..bf723516 --- /dev/null +++ b/variants/dfrobot_lorawan_esp32s3/target.cpp @@ -0,0 +1,34 @@ +#include +#include +#include "target.h" + +DFRobotLoRaBoard board; + +// FSPI is SPI2. The ST7735 driver takes HSPI (SPI3); the default SPIClass +// constructor on ESP32-S3 is HSPI and would collide with the display. +static SPIClass spi(FSPI); +RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, spi); + +WRAPPER_CLASS radio_driver(radio, board); + +ESP32RTCClock fallback_clock; +AutoDiscoverRTCClock rtc_clock(fallback_clock); +SensorManager sensors; + +#ifdef DISPLAY_CLASS + DISPLAY_CLASS display(&board.periph_power); + // Active-low button, internal pull-up. + MomentaryButton user_btn(PIN_USER_BTN, 1000, true, true); +#endif + +bool radio_init() { + fallback_clock.begin(); + rtc_clock.begin(Wire); + + return radio.std_init(&spi); +} + +mesh::LocalIdentity radio_new_identity() { + RadioNoiseListener rng(radio); + return mesh::LocalIdentity(&rng); // create new random identity +} diff --git a/variants/dfrobot_lorawan_esp32s3/target.h b/variants/dfrobot_lorawan_esp32s3/target.h new file mode 100644 index 00000000..07cec79b --- /dev/null +++ b/variants/dfrobot_lorawan_esp32s3/target.h @@ -0,0 +1,26 @@ +#pragma once + +#define RADIOLIB_STATIC_ONLY 1 +#include +#include +#include +#include +#include +#include +#ifdef DISPLAY_CLASS + #include + #include +#endif + +extern DFRobotLoRaBoard board; +extern WRAPPER_CLASS radio_driver; +extern AutoDiscoverRTCClock rtc_clock; +extern SensorManager sensors; + +#ifdef DISPLAY_CLASS + extern DISPLAY_CLASS display; + extern MomentaryButton user_btn; +#endif + +bool radio_init(); +mesh::LocalIdentity radio_new_identity(); From ff4c28985c18ce58db8d4c495674df3f5e641259 Mon Sep 17 00:00:00 2001 From: Luca Olivieri Date: Fri, 23 Oct 2026 21:35:02 +0200 Subject: [PATCH 08/53] Added INA219 sensor --- .github/workflows/pr-build-check.yml | 2 ++ variants/lilygo_t3_stm32/README.md | 37 ++++++++++++++++++++++++- variants/lilygo_t3_stm32/platformio.ini | 29 +++++++++++++++++++ variants/lilygo_t3_stm32/target.cpp | 12 ++++++++ variants/lilygo_t3_stm32/target.h | 10 ++++++- 5 files changed, 88 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pr-build-check.yml b/.github/workflows/pr-build-check.yml index 0dcb159e..529a1a21 100644 --- a/.github/workflows/pr-build-check.yml +++ b/.github/workflows/pr-build-check.yml @@ -42,6 +42,8 @@ jobs: - wio-e5-mini_repeater - LilyGo_T3_STM32_repeater - LilyGo_T3_STM32_companion_radio_usb + - LilyGo_T3_STM32_repeater_ina219 + - LilyGo_T3_STM32_companion_radio_usb_ina219 # ESP32-C6 - LilyGo_Tlora_C6_repeater_ # LR1110 (nRF52) diff --git a/variants/lilygo_t3_stm32/README.md b/variants/lilygo_t3_stm32/README.md index 2686aa7f..efc12c5c 100644 --- a/variants/lilygo_t3_stm32/README.md +++ b/variants/lilygo_t3_stm32/README.md @@ -15,6 +15,41 @@ The companion supports 100 contacts and eight channels. Radio settings inherit the repository defaults; select settings appropriate to the board's RF band. Existing saved preferences override build defaults. +## Optional INA219 telemetry + +The INA219 builds use MeshCore's existing environment sensor driver to report +voltage, current, and power. Build either role with: + +```sh +pio run -e LilyGo_T3_STM32_repeater_ina219 +pio run -e LilyGo_T3_STM32_companion_radio_usb_ina219 +``` + +Connect the sensor's logic supply to **3.3 V** and use a common ground: + +| INA219 connection | T3 STM32 connection | +| --- | --- | +| VCC / logic supply | 3.3 V | +| GND | GND | +| SDA | PB7 (JP2 pin 9) | +| SCL | PB8 (JP2 pin 10) | + +PB7/PB8 are the alternate I2C1 pins exposed on the board headers. The default +STM32 I2C pins, PA9/PA10, are reserved for USB serial. The OLED uses SPI. +The sensor is detected at startup at address **0x40**; to use another address, +override `TELEM_INA219_ADDRESS` in the build flags. An absent sensor produces no +additional telemetry. The base builds do not initialize I2C or include the driver. + +The measurement terminals (VIN+ and VIN-) go in series with the positive supply +of the circuit being measured; these are separate from the sensor's logic supply. +See the [INA219 wiring guide](https://learn.adafruit.com/adafruit-ina219-current-sensor-breakout/wiring). +USB power can bypass a sensor placed only in the battery lead. + +Readings use environment telemetry permissions. Existing telemetry encodes current +in 1 mA steps and power in whole watts; it does not provide accumulated mAh or +battery percentage. INA219 telemetry has been confirmed on hardware with the +repeater build; the USB companion sensor build has been compile-tested only. + ## Flash and connect Connect the USB serial port. Hold BOOT, press and release RST, then release BOOT: @@ -63,4 +98,4 @@ pings timed out; long-term reliability has not been established. Higher transmit powers are outside the validated MVP setting. The repeater builds successfully; forwarding has not been hardware-tested. -Battery telemetry, SD storage, and low-power optimization are not implemented. +Built-in battery telemetry, SD storage, and low-power optimization are not implemented. diff --git a/variants/lilygo_t3_stm32/platformio.ini b/variants/lilygo_t3_stm32/platformio.ini index ba56cf52..39e059b7 100644 --- a/variants/lilygo_t3_stm32/platformio.ini +++ b/variants/lilygo_t3_stm32/platformio.ini @@ -50,3 +50,32 @@ build_src_filter = ${lilygo_t3_stm32.build_src_filter} +<../examples/companion_radio/ui-orig/*.cpp> lib_deps = ${lilygo_t3_stm32.lib_deps} densaugeo/base64 @ ~1.4.0 + +; Optional INA219 telemetry on I2C1. Keep the base builds sensor-free. +[lilygo_t3_stm32_ina219] +build_flags = + -D ENV_INCLUDE_INA219=1 + -D PIN_WIRE_SDA=PB7 + -D PIN_WIRE_SCL=PB8 +build_src_filter = + + +lib_deps = + adafruit/Adafruit INA219 @ ^1.2.3 + +[env:LilyGo_T3_STM32_repeater_ina219] +extends = env:LilyGo_T3_STM32_repeater +build_flags = ${env:LilyGo_T3_STM32_repeater.build_flags} + ${lilygo_t3_stm32_ina219.build_flags} +build_src_filter = ${env:LilyGo_T3_STM32_repeater.build_src_filter} + ${lilygo_t3_stm32_ina219.build_src_filter} +lib_deps = ${lilygo_t3_stm32.lib_deps} + ${lilygo_t3_stm32_ina219.lib_deps} + +[env:LilyGo_T3_STM32_companion_radio_usb_ina219] +extends = env:LilyGo_T3_STM32_companion_radio_usb +build_flags = ${env:LilyGo_T3_STM32_companion_radio_usb.build_flags} + ${lilygo_t3_stm32_ina219.build_flags} +build_src_filter = ${env:LilyGo_T3_STM32_companion_radio_usb.build_src_filter} + ${lilygo_t3_stm32_ina219.build_src_filter} +lib_deps = ${env:LilyGo_T3_STM32_companion_radio_usb.lib_deps} + ${lilygo_t3_stm32_ina219.lib_deps} diff --git a/variants/lilygo_t3_stm32/target.cpp b/variants/lilygo_t3_stm32/target.cpp index f98783fe..b7a14c48 100644 --- a/variants/lilygo_t3_stm32/target.cpp +++ b/variants/lilygo_t3_stm32/target.cpp @@ -1,12 +1,19 @@ #include #include +#if ENV_INCLUDE_INA219 + #include +#endif #include "target.h" LilyGoT3STM32Board board; RADIO_CLASS radio = new STM32WLx_Module(); WRAPPER_CLASS radio_driver(radio, board); VolatileRTCClock rtc_clock; +#if ENV_INCLUDE_INA219 +EnvironmentSensorManager sensors; +#else SensorManager sensors; +#endif DISPLAY_CLASS display; // PH3 is also BOOT0: the board's button is active high with an external pull-down. MomentaryButton user_btn(PIN_USER_BTN, 1000, false); @@ -24,6 +31,11 @@ static const Module::RfSwitchMode_t rfswitch_table[] = { void LilyGoT3STM32Board::begin() { STM32Board::begin(); +#if ENV_INCLUDE_INA219 + // These builds remap Wire to PB7/PB8; PA9/PA10 are used by USART1. + Wire.begin(); +#endif + // The SD card shares SPI1 with the OLED; keep it deselected. digitalWrite(PA4, HIGH); pinMode(PA4, OUTPUT); diff --git a/variants/lilygo_t3_stm32/target.h b/variants/lilygo_t3_stm32/target.h index 83235cbd..2db3173b 100644 --- a/variants/lilygo_t3_stm32/target.h +++ b/variants/lilygo_t3_stm32/target.h @@ -4,7 +4,11 @@ #include #include #include -#include +#if ENV_INCLUDE_INA219 + #include +#else + #include +#endif #include #include @@ -20,7 +24,11 @@ public: extern LilyGoT3STM32Board board; extern WRAPPER_CLASS radio_driver; extern VolatileRTCClock rtc_clock; +#if ENV_INCLUDE_INA219 +extern EnvironmentSensorManager sensors; +#else extern SensorManager sensors; +#endif extern DISPLAY_CLASS display; extern MomentaryButton user_btn; From 180c22ba79bbd447ae35614b8a2b3963a69f08ab Mon Sep 17 00:00:00 2001 From: Blake Latchford Date: Wed, 28 Oct 2026 20:46:00 -0400 Subject: [PATCH 09/53] Fix Generic_ESPNOW_* builds on boards without a LoRa radio Compile out code when the corresponding macro defines are missing. Failing builds: comp_radio_usb, repeatr, room_svr, terminal_chat Co-Authored-By: Claude Opus 5.5 --- src/helpers/ESP32Board.cpp | 2 ++ src/helpers/ESP32Board.h | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/src/helpers/ESP32Board.cpp b/src/helpers/ESP32Board.cpp index d190fca1..4cad7536 100644 --- a/src/helpers/ESP32Board.cpp +++ b/src/helpers/ESP32Board.cpp @@ -63,11 +63,13 @@ void ESP32Board::enterDeepSleep(uint32_t secs) { radio_driver.powerOff(); // Keep LoRa inactive during deepsleep +#ifdef P_LORA_NSS digitalWrite(P_LORA_NSS, HIGH); #if defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C6) gpio_hold_en((gpio_num_t)P_LORA_NSS); #else rtc_gpio_hold_en((gpio_num_t)P_LORA_NSS); +#endif #endif // Power off GPS if any diff --git a/src/helpers/ESP32Board.h b/src/helpers/ESP32Board.h index cd274cd9..a4798125 100644 --- a/src/helpers/ESP32Board.h +++ b/src/helpers/ESP32Board.h @@ -69,9 +69,11 @@ public: virtual void powerOff() override; void enterDeepSleep(uint32_t secs); +#ifdef P_LORA_DIO_1 uint32_t getIRQGpio() override { return P_LORA_DIO_1; // default for SX1262 } +#endif void sleep(uint32_t secs) override { // Skip if not allow to sleep @@ -82,6 +84,10 @@ public: // Set GPIO wakeup gpio_num_t wakeupPin = (gpio_num_t)getIRQGpio(); + if (wakeupPin == GPIO_NUM_NC) { // no radio IRQ pin to wake on + delay(1); + return; + } // Configure timer wakeup if (secs > 0) { From d8b5724c38395df188b479568346ea30b8a2338b Mon Sep 17 00:00:00 2001 From: akalinin-softvelum <142931037+akalinin-softvelum@users.noreply.github.com> Date: Tue, 27 Oct 2026 15:52:54 +0000 Subject: [PATCH 10/53] companion: keep mesh/BLE running during e-ink panel refresh Use GxEPD2's busy callback to run the mesh, interface and sensor loops (loopCore(), shared with loop()) while the panel refreshes, instead of blocking the main loop. Covers the partial refresh in endFrame(), the full wipe in startFrame(), and the sleep icon / blank refresh on turnOff(). --- examples/companion_radio/main.cpp | 25 ++++++++++++++++++++++++- src/helpers/ui/DisplayDriver.h | 1 + src/helpers/ui/GxEPDDisplay.cpp | 9 +++++++++ src/helpers/ui/GxEPDDisplay.h | 7 +++++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index 839d4958..9ed3bdcd 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -132,6 +132,10 @@ void halt() { bool wifi_enabled = false; // set at boot from prefs; false also when the effective SSID is blank #endif +#ifdef DISPLAY_CLASS +static void loopDuringDisplayRefresh(); +#endif + void setup() { Serial.begin(115200); board.begin(); @@ -284,15 +288,34 @@ void setup() { #ifdef DISPLAY_CLASS ui_task.begin(disp, &sensors, the_mesh.getNodePrefs()); // still want to pass this in as dependency, as prefs might be moved the_mesh.setListener(&ui_task); + if (disp != NULL) disp->setBusyHook(loopDuringDisplayRefresh); #endif board.onBootComplete(); } -void loop() { +// Work that must keep running even while the UI is blocked (e.g. by an e-ink refresh). +// Called from loop() and from the display busy hook, so both always run the same set. +static void loopCore() { the_mesh.loop(); interface_manager.loop(); sensors.loop(); +} + +#ifdef DISPLAY_CLASS +// Runs from inside the e-ink busy wait, so radio/BLE/sensors keep going during a panel refresh. +// Must not touch the display: UI listener callbacks only update state and schedule the next frame. +static void loopDuringDisplayRefresh() { + static bool active = false; + if (active) return; + active = true; + loopCore(); + active = false; +} +#endif + +void loop() { + loopCore(); #ifdef DISPLAY_CLASS ui_task.loop(); #endif diff --git a/src/helpers/ui/DisplayDriver.h b/src/helpers/ui/DisplayDriver.h index 0e41dab5..2fd22977 100644 --- a/src/helpers/ui/DisplayDriver.h +++ b/src/helpers/ui/DisplayDriver.h @@ -24,6 +24,7 @@ public: virtual bool isOn() = 0; virtual bool isEink() { return false; } // default to non-eink, override in eink drivers virtual void forceFullRefresh() {} // next refresh will be full for eink + virtual void setBusyHook(void (*hook)()) {} // eink: hook is called repeatedly while waiting for panel refresh virtual void turnOn() = 0; virtual void turnOn(int max_partial) { turnOn(); } virtual void turnOff() = 0; diff --git a/src/helpers/ui/GxEPDDisplay.cpp b/src/helpers/ui/GxEPDDisplay.cpp index d7f9562b..209d1b46 100644 --- a/src/helpers/ui/GxEPDDisplay.cpp +++ b/src/helpers/ui/GxEPDDisplay.cpp @@ -88,7 +88,9 @@ void GxEPDDisplay::drawSleepIcon() { display.fillRect((x - 5) * scale_x - 1, y * scale_y, (W+5) * scale_x + 2, (H-2) * scale_y + 1, GxEPD_WHITE); _curr_color = GxEPD_BLACK; drawXbm(x, y, sleep_icon_24x24, W, H); + if (_busy_hook) display.epd2.setBusyCallback(busyCallback, this); display.display(true); + display.epd2.setBusyCallback(NULL); } void GxEPDDisplay::turnOff(bool blank) { @@ -104,8 +106,10 @@ void GxEPDDisplay::turnOff(bool blank) { void GxEPDDisplay::blankScreen() { // do full refresh before powering off to clear screen // no full refresh needed at wakeup + if (_busy_hook) display.epd2.setBusyCallback(busyCallback, this); display.clearScreen(0xFF); // Clears microcontroller side RAM display.writeScreenBuffer(0xFF); // Forces 0xFF (White) into the display controller's history registers + display.epd2.setBusyCallback(NULL); _partial_refresh_count = 0; last_display_crc_value = 0; // we can hibernate as screen has been cleaned @@ -126,8 +130,10 @@ void GxEPDDisplay::startFrame(ColorVal bkg) { || (_max_partial_refresh > 0 && _partial_refresh_count >= _max_partial_refresh)) { // forces a full wipe of the screen ... + if (_busy_hook) display.epd2.setBusyCallback(busyCallback, this); display.clearScreen(0xFF); // Clears microcontroller side RAM display.writeScreenBuffer(0xFF); // Forces 0xFF (White) into the display controller's history registers + display.epd2.setBusyCallback(NULL); // we'll need a partial refresh after that (whatever crc value is) last_display_crc_value = 0; _partial_refresh_count = 0; @@ -239,7 +245,10 @@ void GxEPDDisplay::endFrame() { if (_isOn == false) return; uint32_t crc = display_crc.finalize(); if (crc != last_display_crc_value) { + // keep the rest of the firmware running while the panel refreshes (~0.5s partial) + if (_busy_hook) display.epd2.setBusyCallback(busyCallback, this); display.display(true); + display.epd2.setBusyCallback(NULL); _partial_refresh_count++; } last_display_crc_value = crc; diff --git a/src/helpers/ui/GxEPDDisplay.h b/src/helpers/ui/GxEPDDisplay.h index 9d918443..0aeaeca9 100644 --- a/src/helpers/ui/GxEPDDisplay.h +++ b/src/helpers/ui/GxEPDDisplay.h @@ -39,6 +39,12 @@ class GxEPDDisplay : public DisplayDriver { bool _force_full_refresh = false; int _partial_refresh_count = 0; int _max_partial_refresh = 0; + void (*_busy_hook)() = NULL; + + static void busyCallback(const void* self) { + ((const GxEPDDisplay*)self)->_busy_hook(); + delay(1); // same as GxEPD2 does without a callback + } public: #if defined(EINK_DISPLAY_MODEL) @@ -53,6 +59,7 @@ public: bool isEink() override { return true; } void forceFullRefresh() override { _force_full_refresh = true; } void turnOn(int max_partial) override; + void setBusyHook(void (*hook)()) override { _busy_hook = hook; } void turnOn() override; void turnOff(bool blank) override; void turnOff() override { turnOff(false); } From e184a81423d700b8437602e4d50b76e64485cd9b Mon Sep 17 00:00:00 2001 From: akalinin-softvelum <142931037+akalinin-softvelum@users.noreply.github.com> Date: Thu, 29 Oct 2026 13:21:11 +0000 Subject: [PATCH 11/53] companion: enable e-ink refresh loop only with EINK_REFRESH_LOOP Only builds that define EINK_REFRESH_LOOP install the busy hook; for now the T-Echo companion BLE/USB envs. --- examples/companion_radio/main.cpp | 6 ++++-- variants/lilygo_techo/platformio.ini | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index 9ed3bdcd..cfc2b2bf 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -132,7 +132,7 @@ void halt() { bool wifi_enabled = false; // set at boot from prefs; false also when the effective SSID is blank #endif -#ifdef DISPLAY_CLASS +#if defined(DISPLAY_CLASS) && defined(EINK_REFRESH_LOOP) static void loopDuringDisplayRefresh(); #endif @@ -288,7 +288,9 @@ void setup() { #ifdef DISPLAY_CLASS ui_task.begin(disp, &sensors, the_mesh.getNodePrefs()); // still want to pass this in as dependency, as prefs might be moved the_mesh.setListener(&ui_task); +#ifdef EINK_REFRESH_LOOP if (disp != NULL) disp->setBusyHook(loopDuringDisplayRefresh); +#endif #endif board.onBootComplete(); @@ -302,7 +304,7 @@ static void loopCore() { sensors.loop(); } -#ifdef DISPLAY_CLASS +#if defined(DISPLAY_CLASS) && defined(EINK_REFRESH_LOOP) // Runs from inside the e-ink busy wait, so radio/BLE/sensors keep going during a panel refresh. // Must not touch the display: UI listener callbacks only update state and schedule the next frame. static void loopDuringDisplayRefresh() { diff --git a/variants/lilygo_techo/platformio.ini b/variants/lilygo_techo/platformio.ini index 896e42a6..2c2a3e43 100644 --- a/variants/lilygo_techo/platformio.ini +++ b/variants/lilygo_techo/platformio.ini @@ -93,6 +93,7 @@ build_flags = ${LilyGo_T-Echo.build_flags} -I src/helpers/ui -I examples/companion_radio/ui-new + -D EINK_REFRESH_LOOP -D MAX_CONTACTS=350 -D MAX_GROUP_CHANNELS=40 -D QSPIFLASH=1 @@ -120,6 +121,7 @@ build_flags = ${LilyGo_T-Echo.build_flags} -I src/helpers/ui -I examples/companion_radio/ui-new + -D EINK_REFRESH_LOOP -D MAX_CONTACTS=350 -D MAX_GROUP_CHANNELS=40 -D OFFLINE_QUEUE_SIZE=256 From 287c5e21cf2867477cfe94fd6c5afc905fd11526 Mon Sep 17 00:00:00 2001 From: Will MacGregor Date: Wed, 4 Nov 2026 09:46:27 -0500 Subject: [PATCH 12/53] Enable simple_room_server on the STM32 platform The room server gated its filesystem paths on NRF52_PLATFORM only, so an STM32 build fell through to the ESP32 branch and failed to compile: MyMesh.cpp:147: no matching function for call to 'Adafruit_LittleFS::open(const char*&, const char [2], bool)' main.cpp:69: 'store' was not declared in this scope STM32 uses the same InternalFS / Adafruit_LittleFS API as nRF52, including append semantics (FILE_O_WRITE opens LFS_O_RDWR|LFS_O_CREAT and then seeks to the end), so the nRF52 branch is already correct for it. Add STM32_PLATFORM alongside NRF52_PLATFORM in the four remaining gates; saveIdentity() already did this. This unblocks the room_server example on the existing STM32WL boards (wio-e5-mini, wio-e5-dev, rak3x72, tiny_relay) as well as new ones. Co-Authored-By: Claude Opus 5 (1M context) --- examples/simple_room_server/MyMesh.cpp | 4 ++-- examples/simple_room_server/MyMesh.h | 2 +- examples/simple_room_server/main.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/simple_room_server/MyMesh.cpp b/examples/simple_room_server/MyMesh.cpp index 935fd7f9..b1a88dd8 100644 --- a/examples/simple_room_server/MyMesh.cpp +++ b/examples/simple_room_server/MyMesh.cpp @@ -140,7 +140,7 @@ mesh::Packet *MyMesh::createSelfAdvert() { } File MyMesh::openAppend(const char *fname) { -#if defined(NRF52_PLATFORM) +#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) return _fs->open(fname, FILE_O_WRITE); #elif defined(RP2040_PLATFORM) return _fs->open(fname, "a"); @@ -782,7 +782,7 @@ void MyMesh::applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, } bool MyMesh::formatFileSystem() { -#if defined(NRF52_PLATFORM) +#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) return InternalFS.format(); #elif defined(RP2040_PLATFORM) return LittleFS.format(); diff --git a/examples/simple_room_server/MyMesh.h b/examples/simple_room_server/MyMesh.h index 4dee8bdb..974e633a 100644 --- a/examples/simple_room_server/MyMesh.h +++ b/examples/simple_room_server/MyMesh.h @@ -3,7 +3,7 @@ #include // needed for PlatformIO #include -#if defined(NRF52_PLATFORM) +#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) #include #elif defined(RP2040_PLATFORM) #include diff --git a/examples/simple_room_server/main.cpp b/examples/simple_room_server/main.cpp index 65fc55ef..4bfad4e5 100644 --- a/examples/simple_room_server/main.cpp +++ b/examples/simple_room_server/main.cpp @@ -50,7 +50,7 @@ void setup() { fast_rng.begin(radio_driver.getRngSeed()); FILESYSTEM* fs; -#if defined(NRF52_PLATFORM) +#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) InternalFS.begin(); fs = &InternalFS; IdentityStore store(InternalFS, ""); From afb56bb976bccb9cf95d44fe8aff2f6dca13b47d Mon Sep 17 00:00:00 2001 From: Will MacGregor Date: Wed, 4 Nov 2026 09:46:40 -0500 Subject: [PATCH 13/53] Add variant for ST Nucleo-WL55JC1 Adds repeater, room_server, companion_radio_usb and kiss_modem environments for the NUCLEO-WL55JC1 (STM32WL55JC, integrated SubGHz radio). Notes on the board setup: - Uses the upstream nucleo_wl55jc board. PlatformIO's boards_remap maps it to the core's NUCLEO_WL55JC1 variant, so the LEDs, user buttons and the LPUART1 -> ST-LINK VCP serial mapping all come from the core rather than being redefined here. - The RF switch table is built from the core's LORAWAN_RFSWITCH_* macros (SP3T front end on PC3/PC4/PC5) so it tracks upstream if those are ever revised. Both RFO_LP and RFO_HP are registered, so RadioLib selects HP above 14 dBm and LP below it. - board_build.extra_flags pins the device to STM32WL55xx. The stock board manifest declares product_line STM32WL55xx but extra_flags -DSTM32WLE5xx; that only works because every #elif chain happens to test WL55xx first. - The Nucleo's LD1..LD3 are driven active HIGH, so the TX LED hooks are inverted relative to STM32Board's active-LOW default. - 32 KB reserved at the top of flash for LittleFS, as on the other STM32WL variants. Verified on hardware: all four environments build; repeater and room_server flash and run over ST-LINK; identity and prefs persist across reboot and across a firmware swap; and GPIOC reads back the documented RX levels at idle, confirming the front-end switch is driven correctly. Co-Authored-By: Claude Opus 5 (1M context) --- variants/nucleo_wl55jc/platformio.ini | 57 +++++++++++++++++++++++ variants/nucleo_wl55jc/target.cpp | 65 +++++++++++++++++++++++++++ variants/nucleo_wl55jc/target.h | 47 +++++++++++++++++++ variants/nucleo_wl55jc/variant.h | 5 +++ 4 files changed, 174 insertions(+) create mode 100644 variants/nucleo_wl55jc/platformio.ini create mode 100644 variants/nucleo_wl55jc/target.cpp create mode 100644 variants/nucleo_wl55jc/target.h create mode 100644 variants/nucleo_wl55jc/variant.h diff --git a/variants/nucleo_wl55jc/platformio.ini b/variants/nucleo_wl55jc/platformio.ini new file mode 100644 index 00000000..0de5ba6f --- /dev/null +++ b/variants/nucleo_wl55jc/platformio.ini @@ -0,0 +1,57 @@ +[nucleo_wl55jc] +extends = stm32_base +board = nucleo_wl55jc +; The upstream board manifest tags the part as STM32WLE5xx even though its +; product_line is STM32WL55xx. Pin both to WL55 so a single device header and +; startup file (startup_stm32wl55xx_cm4.s) are in play. +board_build.extra_flags = -DSTM32WL -DSTM32WLxx -DSTM32WL55xx +board_upload.maximum_size = 229376 ; 32kb at the top of flash reserved for LittleFS +build_flags = ${stm32_base.build_flags} + -D RADIO_CLASS=CustomSTM32WLx + -D WRAPPER_CLASS=CustomSTM32WLxWrapper + -D LORA_TX_POWER=22 + -D SPI_INTERFACES_COUNT=0 + -D RX_BOOSTED_GAIN=true + -D STM32WL_TCXO_VOLTAGE=1.7 + -D P_LORA_TX_LED=LED_BLUE + -D PIN_USER_BTN=USER_BTN + -D USER_BTN_PRESSED=LOW + -I variants/nucleo_wl55jc +build_src_filter = ${stm32_base.build_src_filter} + +<../variants/nucleo_wl55jc> + +[env:nucleo_wl55jc_repeater] +extends = nucleo_wl55jc +build_flags = ${nucleo_wl55jc.build_flags} + -D ADVERT_NAME='"Nucleo WL55JC Repeater"' + -D ADMIN_PASSWORD='"password"' + -D MAX_NEIGHBOURS=50 +build_src_filter = ${nucleo_wl55jc.build_src_filter} + +<../examples/simple_repeater/*.cpp> + +[env:nucleo_wl55jc_companion_radio_usb] +extends = nucleo_wl55jc +build_flags = ${nucleo_wl55jc.build_flags} + -D MAX_CONTACTS=100 + -D MAX_GROUP_CHANNELS=8 + -D ENABLE_USB_INTERFACE ; binds to Serial, i.e. the ST-LINK virtual COM port +build_src_filter = ${nucleo_wl55jc.build_src_filter} + +<../examples/companion_radio/*.cpp> +lib_deps = ${nucleo_wl55jc.lib_deps} + densaugeo/base64 @ ~1.4.0 + +[env:nucleo_wl55jc_kiss_modem] +extends = nucleo_wl55jc +build_src_filter = ${nucleo_wl55jc.build_src_filter} + +<../examples/kiss_modem/> + +[env:nucleo_wl55jc_room_server] +extends = nucleo_wl55jc +build_flags = ${nucleo_wl55jc.build_flags} + -D ADVERT_NAME='"Nucleo WL55JC Room"' + -D ADVERT_LAT=0.0 + -D ADVERT_LON=0.0 + -D ADMIN_PASSWORD='"password"' + -D ROOM_PASSWORD='"hello"' +build_src_filter = ${nucleo_wl55jc.build_src_filter} + +<../examples/simple_room_server> diff --git a/variants/nucleo_wl55jc/target.cpp b/variants/nucleo_wl55jc/target.cpp new file mode 100644 index 00000000..8e773adf --- /dev/null +++ b/variants/nucleo_wl55jc/target.cpp @@ -0,0 +1,65 @@ +#include +#include "target.h" +#include + +NucleoWL55JCBoard board; + +RADIO_CLASS radio = new STM32WLx_Module(); + +WRAPPER_CLASS radio_driver(radio, board); + +// The NUCLEO-WL55JC1 drives an SP3T front-end switch from FE_CTRL1..3 (PC4, PC5, PC3), +// routing the single SMA between RX, RFO_LP and RFO_HP. Pin order and levels are taken +// straight from the core's board definition (variant_NUCLEO_WL55JC1.h) so they stay in +// sync with upstream. +static const uint32_t rfswitch_pins[] = {LORAWAN_RFSWITCH_PINS, RADIOLIB_NC, RADIOLIB_NC}; +static const Module::RfSwitchMode_t rfswitch_table[] = { + {STM32WLx::MODE_IDLE, {LORAWAN_RFSWITCH_OFF_VALUES}}, + {STM32WLx::MODE_RX, {LORAWAN_RFSWITCH_RX_VALUES}}, + {STM32WLx::MODE_TX_LP, {LORAWAN_RFSWITCH_RFO_LP_VALUES}}, + {STM32WLx::MODE_TX_HP, {LORAWAN_RFSWITCH_RFO_HP_VALUES}}, + END_OF_MODE_TABLE, +}; + +VolatileRTCClock rtc_clock; +SensorManager sensors; + +#ifndef LORA_CR + #define LORA_CR 5 +#endif + +#ifndef STM32WL_TCXO_VOLTAGE + // The board is fitted with a 32MHz TCXO powered from the radio's DIO3 at 1.7V + #define STM32WL_TCXO_VOLTAGE 1.7 +#endif + +#ifndef LORA_TX_POWER + #define LORA_TX_POWER 22 +#endif + +bool radio_init() { + radio.setRfSwitchTable(rfswitch_pins, rfswitch_table); + + // Both PA paths are registered above, so RadioLib picks RFO_HP whenever the + // requested power is above 14dBm, and RFO_LP below that. + int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, 16, STM32WL_TCXO_VOLTAGE, 0); + + if (status != RADIOLIB_ERR_NONE) { + Serial.print("ERROR: radio init failed: "); + Serial.println(status); + return false; // fail + } + + #ifdef RX_BOOSTED_GAIN + radio.setRxBoostedGainMode(RX_BOOSTED_GAIN); + #endif + + radio.setCRC(1); + + return true; // success +} + +mesh::LocalIdentity radio_new_identity() { + RadioNoiseListener rng(radio); + return mesh::LocalIdentity(&rng); // create new random identity +} diff --git a/variants/nucleo_wl55jc/target.h b/variants/nucleo_wl55jc/target.h new file mode 100644 index 00000000..4b1c3ada --- /dev/null +++ b/variants/nucleo_wl55jc/target.h @@ -0,0 +1,47 @@ +#pragma once + +#define RADIOLIB_STATIC_ONLY 1 +#include +#include +#include +#include +#include +#include + +class NucleoWL55JCBoard : public STM32Board { +public: + void begin() override { + STM32Board::begin(); + + pinMode(P_LORA_TX_LED, OUTPUT); + digitalWrite(P_LORA_TX_LED, LOW); // see note on LED polarity below + pinMode(PIN_USER_BTN, INPUT_PULLUP); + } + + const char* getManufacturerName() const override { + return "ST Nucleo WL55JC1"; + } + + // LD1..LD3 on the Nucleo are driven active HIGH, whereas STM32Board's + // default TX-LED handling assumes the active LOW wiring used by the + // Wio-E5 boards. Invert both halves here. + void onBeforeTransmit() override { + digitalWrite(P_LORA_TX_LED, HIGH); // turn TX LED on + } + void onAfterTransmit() override { + digitalWrite(P_LORA_TX_LED, LOW); // turn TX LED off + } + + // No battery on this dev board, it is USB/ST-LINK powered. + uint16_t getBattMilliVolts() override { + return 0; + } +}; + +extern NucleoWL55JCBoard board; +extern WRAPPER_CLASS radio_driver; +extern VolatileRTCClock rtc_clock; +extern SensorManager sensors; + +bool radio_init(); +mesh::LocalIdentity radio_new_identity(); diff --git a/variants/nucleo_wl55jc/variant.h b/variants/nucleo_wl55jc/variant.h new file mode 100644 index 00000000..a006d1f1 --- /dev/null +++ b/variants/nucleo_wl55jc/variant.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +#undef RNG From 9af19c870c6236d0aa310137badd7616c5116cd0 Mon Sep 17 00:00:00 2001 From: Will MacGregor Date: Wed, 4 Nov 2026 09:51:33 -0500 Subject: [PATCH 14/53] Build nucleo_wl55jc_repeater in PR build check Adds the new variant to the PR build matrix, next to the existing STM32 entry, so it is covered by CI going forward. Co-Authored-By: Claude Opus 5 (1M context) --- .github/workflows/pr-build-check.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/pr-build-check.yml b/.github/workflows/pr-build-check.yml index cebf0cfe..1d431451 100644 --- a/.github/workflows/pr-build-check.yml +++ b/.github/workflows/pr-build-check.yml @@ -40,6 +40,7 @@ jobs: - PicoW_repeater # STM32 - wio-e5-mini_repeater + - nucleo_wl55jc_repeater # ESP32-C6 - LilyGo_Tlora_C6_repeater_ # LR1110 (nRF52) From 1e135e0bd2547f56292dd5bb6ee5ebbbb468f752 Mon Sep 17 00:00:00 2001 From: Li Hardy Extreme Date: Fri, 6 Nov 2026 00:10:59 +0100 Subject: [PATCH 15/53] feat: mDNS ESP32 - enable local network discovery over mDNS --- examples/companion_radio/main.cpp | 32 +++++++++++++++++++++++++++++++ variants/heltec_v4/platformio.ini | 2 ++ 2 files changed, 34 insertions(+) diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index 839d4958..1ff74470 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -2,6 +2,11 @@ #include #include "MyMesh.h" +#if defined(ENABLE_WIFI_INTERFACE) && defined(ENABLE_ESP_MDNS) +#define HAS_ESP_MDNS +#include +#endif + // Believe it or not, this std C function is busted on some platforms! static uint32_t _atoi(const char* sp) { uint32_t n = 0; @@ -266,6 +271,33 @@ void setup() { interface_manager.addInterface(InterfaceType::Ethernet, ðernet_interface); #endif +#ifdef HAS_ESP_MDNS + String nodeName = the_mesh.getNodeName(); + String sanitizedNodeName; + sanitizedNodeName.reserve(nodeName.length()); + for (size_t i = 0; i < nodeName.length(); ++i) { + const char c = nodeName[i]; + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9') || c == '-' || c == '_') { + sanitizedNodeName += c; + } + } + + String mdnsName = "meshcore_"; + mdnsName += sanitizedNodeName; + if (!MDNS.begin(mdnsName.c_str())) { + WIFI_DEBUG_PRINTLN("mDNS failed to start!"); + } else { + MDNS.addService("meshcore", "tcp", TCP_PORT); + MDNS.addServiceTxt("meshcore", "tcp", "node-name", the_mesh.getNodeName()); + MDNS.addServiceTxt("meshcore", "tcp", "firmware-version", FIRMWARE_VERSION); + String protocol; protocol+=FIRMWARE_VER_CODE; + MDNS.addServiceTxt("meshcore", "tcp", "protocol-version", protocol.c_str()); + MDNS.addServiceTxt("meshcore", "tcp", "build-date", __DATE__); + MDNS.addServiceTxt("meshcore", "tcp", "build-time", __TIME__); + } +#endif + // add hardware serial interface #if defined(SERIAL_RX) companion_serial.setPins(SERIAL_RX, SERIAL_TX); diff --git a/variants/heltec_v4/platformio.ini b/variants/heltec_v4/platformio.ini index c91e31a8..69ac9ad8 100644 --- a/variants/heltec_v4/platformio.ini +++ b/variants/heltec_v4/platformio.ini @@ -212,6 +212,7 @@ build_flags = -D BLE_DEBUG_LOGGING=1 -D OFFLINE_QUEUE_SIZE=256 -D ENABLE_WIFI_INTERFACE + -D ENABLE_ESP_MDNS ; -D MESH_PACKET_LOGGING=1 ; -D MESH_DEBUG=1 build_src_filter = ${heltec_v4_oled.build_src_filter} @@ -231,6 +232,7 @@ build_flags = ${heltec_v4_oled.build_flags} -I examples/companion_radio/ui-new -D ENABLE_WIFI_INTERFACE + -D ENABLE_ESP_MDNS -D MAX_CONTACTS=350 -D MAX_GROUP_CHANNELS=40 -D OFFLINE_QUEUE_SIZE=256 From 81afafcb357e832d46aef4f4a79540044ee7158d Mon Sep 17 00:00:00 2001 From: Florent Date: Sun, 1 Nov 2026 20:08:16 -0400 Subject: [PATCH 16/53] gps: add GpsDriver, handle the hardware and software switches in the drivers --- examples/companion_radio/ui-new/UITask.cpp | 8 +- examples/companion_radio/ui-tiny/UITask.cpp | 8 +- examples/simple_repeater/MyMesh.h | 3 + examples/simple_room_server/MyMesh.h | 3 + examples/simple_sensor/SensorMesh.h | 3 + src/helpers/CommonCLI.cpp | 18 ++ src/helpers/CommonCLI.h | 2 + src/helpers/sensors/AirohaGpsDriver.h | 83 +++++ .../sensors/EnvironmentSensorManager.cpp | 249 ++------------- .../sensors/EnvironmentSensorManager.h | 5 - src/helpers/sensors/GpsDriver.h | 18 ++ src/helpers/sensors/LocationProvider.h | 19 ++ .../sensors/MicroNMEALocationProvider.h | 293 +++++++++--------- src/helpers/sensors/NmeaGpsDriver.h | 262 ++++++++++++++++ src/helpers/sensors/RakGpsDriver.h | 112 +++++++ src/helpers/sensors/RakLocationProvider.h | 144 +++++++++ variants/gat562_30s_mesh_kit/target.cpp | 7 +- variants/lilygo_techo/target.cpp | 5 +- variants/rak3401/target.cpp | 8 +- variants/rak4631/target.cpp | 7 +- variants/t1000-e/platformio.ini | 1 + variants/t1000-e/target.cpp | 24 +- variants/t1000-e/target.h | 6 +- variants/thinknode_m1/target.cpp | 59 +--- variants/thinknode_m1/target.h | 3 - variants/thinknode_m1/variant.h | 2 + variants/thinknode_m5/target.cpp | 5 +- variants/thinknode_m5/variant.h | 3 +- 28 files changed, 892 insertions(+), 468 deletions(-) create mode 100644 src/helpers/sensors/AirohaGpsDriver.h create mode 100644 src/helpers/sensors/GpsDriver.h create mode 100644 src/helpers/sensors/NmeaGpsDriver.h create mode 100644 src/helpers/sensors/RakGpsDriver.h create mode 100644 src/helpers/sensors/RakLocationProvider.h diff --git a/examples/companion_radio/ui-new/UITask.cpp b/examples/companion_radio/ui-new/UITask.cpp index 55cae1fe..cfe34cf2 100644 --- a/examples/companion_radio/ui-new/UITask.cpp +++ b/examples/companion_radio/ui-new/UITask.cpp @@ -392,16 +392,16 @@ public: char buf[50]; int y = 18; bool gps_state = _task->getGPSState(); -#ifdef PIN_GPS_SWITCH - bool hw_gps_state = digitalRead(PIN_GPS_SWITCH); + bool hw_gps_state = nmea != NULL && nmea->isHardwareEnabled(); + if (nmea != NULL && nmea->hasHardwareEnableState()) { if (gps_state != hw_gps_state) { strcpy(buf, gps_state ? "gps off(hw)" : "gps off(sw)"); } else { strcpy(buf, gps_state ? "gps on" : "gps off"); } -#else + } else { strcpy(buf, gps_state ? "gps on" : "gps off"); -#endif + } display.setColor(UIColor::primary_txt); display.drawTextLeftAlign(0, y, buf); if (nmea == NULL) { diff --git a/examples/companion_radio/ui-tiny/UITask.cpp b/examples/companion_radio/ui-tiny/UITask.cpp index 6bf33849..035340a8 100644 --- a/examples/companion_radio/ui-tiny/UITask.cpp +++ b/examples/companion_radio/ui-tiny/UITask.cpp @@ -254,16 +254,16 @@ public: char buf[50]; int y = 8; bool gps_state = _task->getGPSState(); -#ifdef PIN_GPS_SWITCH - bool hw_gps_state = digitalRead(PIN_GPS_SWITCH); + bool hw_gps_state = nmea != NULL && nmea->isHardwareEnabled(); + if (nmea != NULL && nmea->hasHardwareEnableState()) { if (gps_state != hw_gps_state) { strcpy(buf, gps_state ? "gps off(hw)" : "gps off(sw)"); } else { strcpy(buf, gps_state ? "gps on" : "gps off"); } -#else + } else { strcpy(buf, gps_state ? "gps on" : "gps off"); -#endif + } display.setColor(UIColor::primary_txt); display.drawTextLeftAlign(0, y, buf); if (nmea == NULL) { diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index dc893258..1d975a94 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -159,6 +159,9 @@ protected: #if ENV_INCLUDE_GPS == 1 void applyGpsPrefs() { + char period[11]; + snprintf(period, sizeof(period), "%lu", (unsigned long)_prefs.gps_period); + sensors.setSettingValue("gps_period", period); sensors.setSettingValue("gps", _prefs.gps_enabled?"1":"0"); } #endif diff --git a/examples/simple_room_server/MyMesh.h b/examples/simple_room_server/MyMesh.h index 4dee8bdb..5f8de73a 100644 --- a/examples/simple_room_server/MyMesh.h +++ b/examples/simple_room_server/MyMesh.h @@ -168,6 +168,9 @@ protected: #if ENV_INCLUDE_GPS == 1 void applyGpsPrefs() { + char period[11]; + snprintf(period, sizeof(period), "%lu", (unsigned long)_prefs.gps_period); + sensors.setSettingValue("gps_period", period); sensors.setSettingValue("gps", _prefs.gps_enabled?"1":"0"); } #endif diff --git a/examples/simple_sensor/SensorMesh.h b/examples/simple_sensor/SensorMesh.h index 590ca0c7..b93939de 100644 --- a/examples/simple_sensor/SensorMesh.h +++ b/examples/simple_sensor/SensorMesh.h @@ -173,6 +173,9 @@ private: #if ENV_INCLUDE_GPS == 1 void applyGpsPrefs() { + char period[11]; + snprintf(period, sizeof(period), "%lu", (unsigned long)_prefs.gps_period); + sensors.setSettingValue("gps_period", period); sensors.setSettingValue("gps", _prefs.gps_enabled?"1":"0"); } #endif diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index 5d7501d9..613f2578 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -368,6 +368,24 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re } else { strcpy(reply, "gps toggle not found"); } + } else if (memcmp(command, "gps period ", 11) == 0) { + char* end = nullptr; + unsigned long period = strtoul(command + 11, &end, 10); + if (command[11] == '\0' || *end != '\0' || period > 86400) { + strcpy(reply, "gps period must be 0-86400 seconds"); + } else { + char period_value[11]; + snprintf(period_value, sizeof(period_value), "%lu", period); + if (_sensors->setSettingValue("gps_period", period_value) && + _sensors->setSettingValue("gps", "1")) { + _prefs->gps_period = (uint32_t)period; + _prefs->gps_enabled = 1; + savePrefs(); + strcpy(reply, "ok"); + } else { + strcpy(reply, "gps provider not found"); + } + } } else if (memcmp(command, "gps sync", 8) == 0) { LocationProvider * l = _sensors->getLocationProvider(); if (l != NULL) { diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index 675df7a4..06778a60 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -61,6 +61,7 @@ public: // Gps settings uint8_t gps_enabled = 0; uint32_t gps_interval = 0; // in seconds + uint32_t gps_period = 0; // periodic acquisition interval in seconds uint8_t advert_loc_policy = 0; uint32_t discovery_mod_timestamp = 0; float adc_multiplier = 0; @@ -158,6 +159,7 @@ private: void structure() override { def("en", _parent->gps_enabled); // boolean def("int", _parent->gps_interval); // interval in seconds + def("period", _parent->gps_period); // periodic acquisition interval in seconds def("adv_loc", _parent->advert_loc_policy); } public: diff --git a/src/helpers/sensors/AirohaGpsDriver.h b/src/helpers/sensors/AirohaGpsDriver.h new file mode 100644 index 00000000..36d5eb6b --- /dev/null +++ b/src/helpers/sensors/AirohaGpsDriver.h @@ -0,0 +1,83 @@ +#pragma once + +#include "NmeaGpsDriver.h" + +class AirohaGpsDriver : public NmeaGpsDriver { + bool waitFor(const char* prefix, uint32_t timeout_ms) { // TODO: Refactor so it doesn't block the loop ! + char sentence[100]; + size_t length = 0; + const size_t prefixLength = strlen(prefix); + const uint32_t deadline = millis() + timeout_ms; + + while ((int32_t)(millis() - deadline) < 0) { + if (available() <= 0) { + yield(); + continue; + } + + const char c = (char)read(); +#ifdef GPS_NMEA_DEBUG + Serial.print(c); +#endif + if (c == '$') length = 0; + if (length < sizeof(sentence) - 1) { + sentence[length++] = c; + sentence[length] = '\0'; + if (length >= prefixLength && strncmp(sentence, prefix, prefixLength) == 0) { + return true; + } + } + } + return false; + } + + void enterBackupSleep() { + for (uint8_t attempt = 0; attempt < 3; attempt++) { + drain(); + sendNmeaSentence("$PAIR650,0"); + if (waitFor("$PAIR001,650,0", 50)) { +#ifdef GPS_NMEA_DEBUG + Serial.printf("Airoha RTC backup sleep accepted after %u attempts\r\n", attempt + 1); +#endif + waitFor("$PAIR650,0", 50); + return; + } + } + } + +public: + AirohaGpsDriver(HardwareSerial& serial) + : NmeaGpsDriver(serial, -1, -1, -1, -1, nullptr, HIGH, LOW, 115200) {} + + void initialize() { + NmeaGpsDriver::begin(); + reset(); + } + + void begin() override { + NmeaGpsDriver::begin(); + digitalWrite(GPS_EN, HIGH); + delay(10); + digitalWrite(GPS_RTC_INT, HIGH); + delay(5); + digitalWrite(GPS_RTC_INT, LOW); + } + + void reset() override { + digitalWrite(GPS_RESET, HIGH); + delay(10); + digitalWrite(GPS_RESET, LOW); + } + + void stop() override { + digitalWrite(GPS_VRTC_EN, HIGH); + digitalWrite(GPS_RTC_INT, LOW); + enterBackupSleep(); + digitalWrite(GPS_EN, LOW); + NmeaGpsDriver::stop(); + } + + bool isEnabled() const override { + return digitalRead(GPS_EN) == HIGH; + } +}; diff --git a/src/helpers/sensors/EnvironmentSensorManager.cpp b/src/helpers/sensors/EnvironmentSensorManager.cpp index 0dc892c3..2861fac2 100644 --- a/src/helpers/sensors/EnvironmentSensorManager.cpp +++ b/src/helpers/sensors/EnvironmentSensorManager.cpp @@ -160,56 +160,6 @@ static Adafruit_VL53L0X VL53L0X; static RAK12035_SoilMoisture RAK12035; #endif -#if ENV_INCLUDE_GPS && defined(RAK_BOARD) && !defined(RAK_WISMESH_TAG) -#define RAK_WISBLOCK_GPS -#endif - -#ifdef RAK_WISBLOCK_GPS -static uint32_t gpsResetPin = 0; -static bool i2cGPSFlag = false; -static bool serialGPSFlag = false; -#ifndef TELEM_RAK12500_ADDRESS -#define TELEM_RAK12500_ADDRESS 0x42 //RAK12500 Ublox GPS via i2c -#endif -#include -static SFE_UBLOX_GNSS ublox_GNSS; - -class RAK12500LocationProvider : public LocationProvider { - long _lat = 0; - long _lng = 0; - long _alt = 0; - int _sats = 0; - long _epoch = 0; - bool _fix = false; -public: - long getLatitude() override { return _lat; } - long getLongitude() override { return _lng; } - long getAltitude() override { return _alt; } - long satellitesCount() override { return _sats; } - bool isValid() override { return _fix; } - long getTimestamp() override { return _epoch; } - void sendSentence(const char * sentence) override { } - void reset() override { } - void begin() override { } - void stop() override { } - void loop() override { - if (ublox_GNSS.getGnssFixOk(8)) { - _fix = true; - _lat = ublox_GNSS.getLatitude(2) / 10; - _lng = ublox_GNSS.getLongitude(2) / 10; - _alt = ublox_GNSS.getAltitude(2); - _sats = ublox_GNSS.getSIV(2); - } else { - _fix = false; - } - _epoch = ublox_GNSS.getUnixEpoch(2); - } - bool isEnabled() override { return true; } -}; - -static RAK12500LocationProvider RAK12500_provider; -#endif - // ============================================================ // I2C bus scanner // Probes every valid address and records which ones ACK. @@ -624,12 +574,8 @@ static const size_t SENSOR_TABLE_SIZE = (sizeof(SENSOR_TABLE) / sizeof(SENSOR_TA bool EnvironmentSensorManager::begin() { #if ENV_INCLUDE_GPS - #ifdef RAK_WISBLOCK_GPS - rakGPSInit(); - #else initBasicGPS(); #endif - #endif #if ENV_PIN_SDA && ENV_PIN_SCL #ifdef NRF52_PLATFORM @@ -684,9 +630,11 @@ bool EnvironmentSensorManager::begin() { bool EnvironmentSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) { next_available_channel = TELEM_CHANNEL_SELF + 1; - if (requester_permissions & TELEM_PERM_LOCATION && gps_active) { + #if ENV_INCLUDE_GPS + if (requester_permissions & TELEM_PERM_LOCATION && _location->isEnabled()) { telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude); } + #endif if (requester_permissions & TELEM_PERM_ENVIRONMENT) { for (int i = 0; i < _active_sensor_count; i++) { @@ -702,7 +650,7 @@ bool EnvironmentSensorManager::querySensors(uint8_t requester_permissions, Cayen int EnvironmentSensorManager::getNumSettings() const { int settings = 0; #if ENV_INCLUDE_GPS - if (gps_detected) settings++; // only show GPS setting if GPS is detected + if (gps_detected) settings += 2; // GPS enablement and periodic acquisition #endif return settings; } @@ -713,6 +661,9 @@ const char* EnvironmentSensorManager::getSettingName(int i) const { if (gps_detected && i == settings++) { return "gps"; } + if (gps_detected && i == settings++) { + return "gps_period"; + } #endif return NULL; } @@ -721,7 +672,13 @@ const char* EnvironmentSensorManager::getSettingValue(int i) const { int settings = 0; #if ENV_INCLUDE_GPS if (gps_detected && i == settings++) { - return gps_active ? "1" : "0"; + return _location->isGpsOn() ? "1" : "0"; + } + if (gps_detected && i == settings++) { + static char period[11]; + snprintf(period, sizeof(period), "%lu", + (unsigned long)_location->getGpsPeriod()); + return period; } #endif return NULL; @@ -737,6 +694,13 @@ bool EnvironmentSensorManager::setSettingValue(const char* name, const char* val } return true; } + if (gps_detected && strcmp(name, "gps_period") == 0) { + char* end = nullptr; + unsigned long period = strtoul(value, &end, 10); + if (*value == '\0' || *end != '\0' || period > 86400) return false; + _location->setGpsPeriod((uint32_t)period); + return true; + } if (strcmp(name, "gps_interval") == 0) { uint32_t interval_seconds = atoi(value); gps_update_interval_sec = interval_seconds > 0 ? interval_seconds : 1; @@ -748,157 +712,35 @@ bool EnvironmentSensorManager::setSettingValue(const char* name, const char* val #if ENV_INCLUDE_GPS void EnvironmentSensorManager::initBasicGPS() { - - Serial1.setPins(PIN_GPS_TX, PIN_GPS_RX); - - #ifdef GPS_BAUD_RATE - Serial1.begin(GPS_BAUD_RATE); - #else - Serial1.begin(9600); - #endif - - // Try to detect if GPS is physically connected to determine if we should expose the setting - _location->begin(); - _location->reset(); + start_gps(); #ifndef PIN_GPS_EN MESH_DEBUG_PRINTLN("No GPS wake/reset pin found for this board. Continuing on..."); #endif - // Give the GPS time to power up and emit its first NMEA sentence. Cold-start - // modules can take several seconds, so poll for up to ~5s instead of checking - // once after a fixed 1s delay -- returning as soon as any data appears, so - // fast modules add no boot delay (issue #2258). #ifdef ENV_SKIP_GPS_DETECT gps_detected = true; #else - gps_detected = false; - for (int i = 0; i < 20 && !gps_detected; i++) { - delay(250); - gps_detected = (Serial1.available() > 0); - } + gps_detected = _location->detect(5000); #endif if (gps_detected) { MESH_DEBUG_PRINTLN("GPS detected"); - #ifdef PERSISTANT_GPS - gps_active = true; + #if defined(PERSISTANT_GPS) return; + #else + if (_location->keepEnabledAfterDetection()) { + return; + } #endif } else { MESH_DEBUG_PRINTLN("No GPS detected"); } - _location->stop(); - gps_active = false; //Set GPS visibility off until setting is changed -} - -// gps code for rak might be moved to MicroNMEALoactionProvider -// or make a new location provider ... -#ifdef RAK_WISBLOCK_GPS -void EnvironmentSensorManager::rakGPSInit(){ - - Serial1.setPins(PIN_GPS_TX, PIN_GPS_RX); - - #ifdef GPS_BAUD_RATE - Serial1.begin(GPS_BAUD_RATE); - #else - Serial1.begin(9600); - #endif - - //search for the correct IO standby pin depending on socket used - if(gpsIsAwake(WB_IO2)){ - } - else if(gpsIsAwake(WB_IO4)){ - } - else if(gpsIsAwake(WB_IO5)){ - } - else{ - MESH_DEBUG_PRINTLN("No GPS found"); - gps_active = false; - gps_detected = false; - Serial1.end(); - return; - } - - #ifndef FORCE_GPS_ALIVE // for use with repeaters, until GPS toggle is implimented - //Now that GPS is found and set up, set to sleep for initial state stop_gps(); - #endif } -bool EnvironmentSensorManager::gpsIsAwake(uint8_t ioPin){ - - #if defined(ETHERNET_ENABLED) && defined(RAK_BOARD) - if (ioPin == WB_IO2) { - // WB_IO2 powers the Ethernet module on RAK baseboards. - return false; - } - #endif - - #ifndef RAK_3401 - //set initial waking state - pinMode(ioPin,OUTPUT); - digitalWrite(ioPin,LOW); - delay(500); - digitalWrite(ioPin,HIGH); - #endif - - // give gps time to power up - delay(500); - - //Try to init RAK12500 on I2C - if (ublox_GNSS.begin(Wire) == true){ - MESH_DEBUG_PRINTLN("RAK12500 GPS init correctly with pin %i",ioPin); - ublox_GNSS.setI2COutput(COM_TYPE_UBX); - ublox_GNSS.enableGNSS(true, SFE_UBLOX_GNSS_ID_GPS); - ublox_GNSS.enableGNSS(true, SFE_UBLOX_GNSS_ID_GALILEO); - ublox_GNSS.enableGNSS(true, SFE_UBLOX_GNSS_ID_GLONASS); - ublox_GNSS.enableGNSS(true, SFE_UBLOX_GNSS_ID_SBAS); - ublox_GNSS.enableGNSS(true, SFE_UBLOX_GNSS_ID_BEIDOU); - ublox_GNSS.enableGNSS(true, SFE_UBLOX_GNSS_ID_IMES); - ublox_GNSS.enableGNSS(true, SFE_UBLOX_GNSS_ID_QZSS); - ublox_GNSS.setMeasurementRate(1000); - ublox_GNSS.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); - gpsResetPin = ioPin; - i2cGPSFlag = true; - gps_active = true; - gps_detected = true; - - _location = &RAK12500_provider; - return true; - } else if (Serial1.available()) { - MESH_DEBUG_PRINTLN("Serial GPS init correctly and is turned on"); -#ifdef PIN_GPS_EN - if(PIN_GPS_EN){ - gpsResetPin = PIN_GPS_EN; - } -#endif - serialGPSFlag = true; - gps_active = true; - gps_detected = true; - return true; - } - - #ifndef RAK_3401 - pinMode(ioPin, INPUT); - #endif - MESH_DEBUG_PRINTLN("GPS did not init with this IO pin... try the next"); - return false; -} -#endif - void EnvironmentSensorManager::start_gps() { - gps_active = true; - #ifdef RAK_WISBLOCK_GPS - #ifndef RAK_3401 - pinMode(gpsResetPin, OUTPUT); - digitalWrite(gpsResetPin, HIGH); - #endif - return; - #endif - - _location->begin(); - _location->reset(); + _location->setGpsOn(true); #ifndef PIN_GPS_EN MESH_DEBUG_PRINTLN("Start GPS is N/A on this board. Actual GPS state unchanged"); @@ -906,20 +748,11 @@ void EnvironmentSensorManager::start_gps() { } void EnvironmentSensorManager::stop_gps() { - gps_active = false; - #ifdef RAK_WISBLOCK_GPS - #ifndef RAK_3401 // rak3401 shouldn't turn off WB_IO2 as it powers the PA - pinMode(gpsResetPin, OUTPUT); - digitalWrite(gpsResetPin, LOW); - #endif - return; - #endif + _location->setGpsOn(false); - _location->stop(); - - #ifndef PIN_GPS_EN +#ifndef PIN_GPS_EN MESH_DEBUG_PRINTLN("Stop GPS is N/A on this board. Actual GPS state unchanged"); - #endif +#endif } #endif // ENV_INCLUDE_GPS @@ -928,30 +761,16 @@ void EnvironmentSensorManager::loop() { #if ENV_INCLUDE_GPS static unsigned long next_gps_update = 0; - if (gps_active) { - _location->loop(); - } + _location->loop(); if ((long)(millis() - next_gps_update) > 0) { - if(gps_active){ - #ifdef RAK_WISBLOCK_GPS - if ((i2cGPSFlag || serialGPSFlag) && _location->isValid()) { + if (_location->isEnabled() && _location->isValid()) { node_lat = ((double)_location->getLatitude())/1000000.; node_lon = ((double)_location->getLongitude())/1000000.; MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon); node_altitude = ((double)_location->getAltitude()) / 1000.0; MESH_DEBUG_PRINTLN("lat %f lon %f alt %f", node_lat, node_lon, node_altitude); } - #else - if (_location->isValid()) { - node_lat = ((double)_location->getLatitude())/1000000.; - node_lon = ((double)_location->getLongitude())/1000000.; - MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon); - node_altitude = ((double)_location->getAltitude()) / 1000.0; - MESH_DEBUG_PRINTLN("lat %f lon %f alt %f", node_lat, node_lon, node_altitude); - } - #endif - } next_gps_update = millis() + (gps_update_interval_sec * 1000); } #endif diff --git a/src/helpers/sensors/EnvironmentSensorManager.h b/src/helpers/sensors/EnvironmentSensorManager.h index 17706cbc..425415a1 100644 --- a/src/helpers/sensors/EnvironmentSensorManager.h +++ b/src/helpers/sensors/EnvironmentSensorManager.h @@ -20,7 +20,6 @@ protected: uint8_t next_available_channel = TELEM_CHANNEL_SELF + 1; bool gps_detected = false; - bool gps_active = false; uint32_t gps_update_interval_sec = 1; #if ENV_INCLUDE_GPS @@ -28,10 +27,6 @@ protected: void start_gps(); void stop_gps(); void initBasicGPS(); - #ifdef RAK_BOARD - void rakGPSInit(); - bool gpsIsAwake(uint8_t ioPin); - #endif #endif public: diff --git a/src/helpers/sensors/GpsDriver.h b/src/helpers/sensors/GpsDriver.h new file mode 100644 index 00000000..b8cf4a5d --- /dev/null +++ b/src/helpers/sensors/GpsDriver.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +class GpsDriver { +public: + virtual ~GpsDriver() = default; + + virtual void begin() = 0; + virtual void reset() = 0; + virtual void stop() = 0; + virtual bool isEnabled() const = 0; + virtual bool hasHardwareEnableState() const { return false; } + virtual bool isHardwareEnabled() const { return true; } + // Returns true when the switch explicitly enables GPS in software. + virtual bool pollGpsSwitch() { return false; } + virtual bool detect(uint32_t timeout_ms) = 0; +}; diff --git a/src/helpers/sensors/LocationProvider.h b/src/helpers/sensors/LocationProvider.h index bc4f1fe7..93cecf5b 100644 --- a/src/helpers/sensors/LocationProvider.h +++ b/src/helpers/sensors/LocationProvider.h @@ -8,8 +8,11 @@ class LocationProvider { protected: bool _time_sync_needed = true; + bool _gpsOn = false; public: + virtual ~LocationProvider() = default; + virtual void syncTime() { _time_sync_needed = true; } virtual bool waitingTimeSync() { return _time_sync_needed; } virtual long getLatitude() = 0; @@ -21,6 +24,22 @@ public: virtual void sendSentence(const char * sentence); virtual bool waitFor(const char* prefix, uint32_t timeout_ms) { return false; } virtual void drain() { } + virtual bool detect(uint32_t timeout_ms) { return false; } + virtual bool keepEnabledAfterDetection() const { return false; } + virtual void setGpsPeriod(uint32_t seconds) { } + virtual uint32_t getGpsPeriod() const { return 0; } + virtual bool hasHardwareEnableState() const { return false; } + virtual bool isHardwareEnabled() const { return true; } + virtual bool isGpsOn() const { return _gpsOn; } + virtual void setGpsOn(bool on) { + _gpsOn = on; + if (on) { + begin(); + reset(); + } else { + stop(); + } + } virtual void reset() = 0; virtual void begin() = 0; virtual void stop() = 0; diff --git a/src/helpers/sensors/MicroNMEALocationProvider.h b/src/helpers/sensors/MicroNMEALocationProvider.h index 37ef5762..2e9ff9cc 100644 --- a/src/helpers/sensors/MicroNMEALocationProvider.h +++ b/src/helpers/sensors/MicroNMEALocationProvider.h @@ -1,69 +1,30 @@ #pragma once +#include "NmeaGpsDriver.h" #include "LocationProvider.h" #include #include -#include #include -#ifndef GPS_EN - #ifdef PIN_GPS_EN - #define GPS_EN PIN_GPS_EN - #else - #define GPS_EN (-1) - #endif -#endif - -#ifndef GPS_EN_ACTIVE - #ifdef PIN_GPS_EN_ACTIVE - #define GPS_EN_ACTIVE PIN_GPS_EN_ACTIVE - #else - #define GPS_EN_ACTIVE HIGH - #endif -#endif - -#ifndef GPS_RESET - #ifdef PIN_GPS_RESET - #define GPS_RESET PIN_GPS_RESET - #else - #define GPS_RESET (-1) - #endif -#endif - -#ifndef GPS_RESET_ACTIVE - #ifdef PIN_GPS_RESET_ACTIVE - #define GPS_RESET_ACTIVE PIN_GPS_RESET_ACTIVE - #else - #define GPS_RESET_ACTIVE LOW - #endif +#ifndef GPS_PERIODIC_ACQUISITION_TIMEOUT_MS + #define GPS_PERIODIC_ACQUISITION_TIMEOUT_MS 120000UL #endif class MicroNMEALocationProvider : public LocationProvider { char _nmeaBuffer[100]; MicroNMEA nmea; mesh::RTCClock* _clock; - Stream* _gps_serial; - RefCountedDigitalPin* _peripher_power; - int8_t _claims = 0; - int _pin_reset; - int _pin_en; + NmeaGpsDriver* _driver; + bool _ownsDriver = false; unsigned long next_check = 0; long time_valid = 0; unsigned long _last_time_sync = 0; - static const unsigned long TIME_SYNC_INTERVAL = 1800000; // Re-sync every 30 minutes + uint32_t _gpsPeriodSeconds = 0; + unsigned long _nextAcquisition = 0; + unsigned long _acquisitionStarted = 0; + bool _acquiringPeriodically = false; + static const unsigned long TIME_SYNC_INTERVAL = 1800000; -#ifdef GPS_L76K - void configureL76K() { - // Reapply the constellation mode at each provider start instead of - // assuming it survives a receiver power cycle. - // Mode 7 enables GPS + BeiDou + GLONASS. - delay(250); - MicroNMEA::sendSentence(*_gps_serial, "$PCAS04,7"); - delay(250); - } -#endif - // Observation-only diagnostics. These fields do not change GPS power, - // timing, reset behaviour, or parser input. uint32_t _uart_bytes = 0; uint32_t _nmea_checksum_ok = 0; uint32_t _nmea_checksum_bad = 0; @@ -88,79 +49,121 @@ class MicroNMEALocationProvider : public LocationProvider { } } -public : - MicroNMEALocationProvider(Stream& ser, mesh::RTCClock* clock = NULL, int pin_reset = GPS_RESET, int pin_en = GPS_EN,RefCountedDigitalPin* peripher_power=NULL) : - nmea(_nmeaBuffer, sizeof(_nmeaBuffer)), _clock(clock), _gps_serial(&ser), _peripher_power(peripher_power), _pin_reset(pin_reset), _pin_en(pin_en) { - if (_pin_reset != -1) { - pinMode(_pin_reset, OUTPUT); - digitalWrite(_pin_reset, GPS_RESET_ACTIVE); - } - if (_pin_en != -1) { - pinMode(_pin_en, OUTPUT); - digitalWrite(_pin_en, !GPS_EN_ACTIVE); - } + void startPeriodicAcquisition() { + nmea.clear(); + time_valid = 0; + _driver->begin(); + _driver->reset(); + _acquiringPeriodically = true; + _acquisitionStarted = millis(); } - void claim() { - _claims++; - if (_peripher_power) _peripher_power->claim(); + void finishPeriodicAcquisition() { + _driver->stop(); + _acquiringPeriodically = false; + _nextAcquisition = millis() + _gpsPeriodSeconds * 1000UL; } - void release() { - if (_claims == 0) return; // avoid negative _claims - _claims--; - if (_peripher_power) _peripher_power->release(); + void updateTime() { + if (!isValid()) time_valid = 0; + + if ((long)(millis() - next_check) <= 0) return; + next_check = millis() + 1000; + + if (!_time_sync_needed && _clock != nullptr && + (millis() - _last_time_sync) > TIME_SYNC_INTERVAL) { + _time_sync_needed = true; + } + if (_time_sync_needed && time_valid > 2 && _clock != nullptr) { + _clock->setCurrentTime(getTimestamp()); + _time_sync_needed = false; + _last_time_sync = millis(); + } + if (isValid()) time_valid++; + } + +public: + MicroNMEALocationProvider(NmeaGpsDriver& driver, mesh::RTCClock* clock = nullptr) + : nmea(_nmeaBuffer, sizeof(_nmeaBuffer)), _clock(clock), _driver(&driver) {} + + // Temporary migration adapter for targets and downstream boards. New code + // should inject a GpsDriver; this constructor will be removed once targets + // have been migrated. + MicroNMEALocationProvider(HardwareSerial& serial, mesh::RTCClock* clock = nullptr, + int pin_reset = GPS_RESET, int pin_en = GPS_EN, + RefCountedDigitalPin* peripher_power = nullptr) + : nmea(_nmeaBuffer, sizeof(_nmeaBuffer)), _clock(clock), + _driver(new NmeaGpsDriver(serial, PIN_GPS_TX, PIN_GPS_RX, pin_reset, pin_en, + peripher_power)), + _ownsDriver(true) {} + + MicroNMEALocationProvider(Stream& serial, mesh::RTCClock* clock = nullptr, + int pin_reset = GPS_RESET, int pin_en = GPS_EN, + RefCountedDigitalPin* peripher_power = nullptr) + : nmea(_nmeaBuffer, sizeof(_nmeaBuffer)), _clock(clock), + _driver(new NmeaGpsDriver(serial, PIN_GPS_TX, PIN_GPS_RX, pin_reset, pin_en, + peripher_power)), + _ownsDriver(true) {} + + ~MicroNMEALocationProvider() override { + if (_ownsDriver) delete _driver; } void begin() override { _begin_calls++; - claim(); - if (_pin_en != -1) { - digitalWrite(_pin_en, GPS_EN_ACTIVE); - } - if (_pin_reset != -1) { - digitalWrite(_pin_reset, !GPS_RESET_ACTIVE); + _driver->begin(); + if (_gpsPeriodSeconds > 0 && _driver->isEnabled()) { + nmea.clear(); + time_valid = 0; + _acquiringPeriodically = true; + _acquisitionStarted = millis(); } } void reset() override { - if (_pin_reset != -1) { - digitalWrite(_pin_reset, GPS_RESET_ACTIVE); - delay(10); - digitalWrite(_pin_reset, !GPS_RESET_ACTIVE); - } -#ifdef GPS_L76K - // EnvironmentSensorManager calls reset() immediately after begin(). - // Configure only after that reset so the setting is not discarded. - configureL76K(); -#endif + _driver->reset(); } void stop() override { _stop_calls++; - if (_pin_en != -1) { - digitalWrite(_pin_en, !GPS_EN_ACTIVE); - } - if (_pin_reset != -1) { - digitalWrite(_pin_reset, GPS_RESET_ACTIVE); - } - release(); + _acquiringPeriodically = false; + _driver->stop(); } bool isEnabled() override { - // directly read the enable pin if present as gps can be - // activated/deactivated outside of here ... - if (_pin_en != -1) { - return digitalRead(_pin_en) == GPS_EN_ACTIVE; - } else { - return true; // no enable so must be active - } + return _driver->isEnabled(); + } + + bool hasHardwareEnableState() const override { + return _driver->hasHardwareEnableState(); + } + + bool isHardwareEnabled() const override { + return _driver->isHardwareEnabled(); + } + + bool detect(uint32_t timeout_ms) override { + return _driver->detect(timeout_ms); + } + + void setGpsPeriod(uint32_t seconds) override { + _gpsPeriodSeconds = seconds; + _nextAcquisition = millis(); + _acquiringPeriodically = false; + } + + uint32_t getGpsPeriod() const override { + return _gpsPeriodSeconds; + } + + void syncTime() override { + nmea.clear(); + LocationProvider::syncTime(); } - void syncTime() override { nmea.clear(); LocationProvider::syncTime(); } long getLatitude() override { return nmea.getLatitude(); } long getLongitude() override { return nmea.getLongitude(); } - long getAltitude() override { + long getAltitude() override { long alt = 0; nmea.getAltitude(alt); return alt; @@ -168,29 +171,31 @@ public : long satellitesCount() override { return nmea.getNumSatellites(); } bool isValid() override { return nmea.isValid(); } - long getTimestamp() override { - DateTime dt(nmea.getYear(), nmea.getMonth(),nmea.getDay(),nmea.getHour(),nmea.getMinute(),nmea.getSecond()); + long getTimestamp() override { + DateTime dt(nmea.getYear(), nmea.getMonth(), nmea.getDay(), + nmea.getHour(), nmea.getMinute(), nmea.getSecond()); return dt.unixtime(); - } - - void drain() { while (_gps_serial->available()) _gps_serial->read(); } - - const char* getSentence() const { return nmea.getSentence(); } - - void sendSentence(const char *sentence) override { - nmea.sendSentence(*_gps_serial, sentence); } - bool waitFor(const char* prefix, uint32_t timeout_ms) { - size_t plen = strlen(prefix); - uint32_t timeout = millis() + timeout_ms; - while ((int32_t)(millis() - timeout) < 0) { - if (_gps_serial->available()) { - char c = _gps_serial->read(); - #ifdef GPS_NMEA_DEBUG + void drain() override { _driver->drain(); } + const char* getSentence() const { return nmea.getSentence(); } + + void sendSentence(const char* sentence) override { + _driver->sendNmeaSentence(sentence); + } + + bool waitFor(const char* prefix, uint32_t timeout_ms) override { + const size_t prefixLength = strlen(prefix); + const uint32_t deadline = millis() + timeout_ms; + while ((int32_t)(millis() - deadline) < 0) { + if (_driver->available() > 0) { + const char c = (char)_driver->read(); +#ifdef GPS_NMEA_DEBUG Serial.print(c); - #endif - if (nmea.process(c) && strncmp(nmea.getSentence(), prefix, plen) == 0) return true; +#endif + if (nmea.process(c) && strncmp(nmea.getSentence(), prefix, prefixLength) == 0) { + return true; + } } else { yield(); } @@ -199,34 +204,35 @@ public : } void loop() override { + if (_driver->pollGpsSwitch()) _gpsOn = true; - while (_gps_serial->available()) { - char c = _gps_serial->read(); + if (_gpsOn && _gpsPeriodSeconds > 0 && !_acquiringPeriodically && + (long)(millis() - _nextAcquisition) >= 0) { + startPeriodicAcquisition(); + } + + while (_driver->available() > 0) { + const char c = (char)_driver->read(); _uart_bytes++; _last_uart_ms = millis(); _uart_seen = true; - #ifdef GPS_NMEA_DEBUG +#ifdef GPS_NMEA_DEBUG Serial.print(c); - #endif - bool parsed = nmea.process(c); +#endif + const bool parsed = nmea.process(c); - // MicroNMEA leaves the completed sentence in its buffer after the - // first line terminator. CRLF therefore counts once: CR observes the - // sentence, then LF clears the now-empty buffer. if ((c == '\0' || c == '\r' || c == '\n') && nmea.getSentence()[0] != '\0') { if (MicroNMEA::testChecksum(nmea.getSentence())) { _nmea_checksum_ok++; _last_nmea_ms = millis(); _nmea_seen = true; - // Refresh fix age only when a newly parsed GGA/RMC sentence - // carries a valid fix. Other valid NMEA sentences must not - // make an old position look fresh. - const char* message_id = nmea.getMessageID(); + const char* messageId = nmea.getMessageID(); if (parsed && isValid() && - (strcmp(message_id, "GGA") == 0 || strcmp(message_id, "RMC") == 0)) { + (strcmp(messageId, "GGA") == 0 || strcmp(messageId, "RMC") == 0)) { _last_fix_ms = millis(); _fix_seen = true; + if (_acquiringPeriodically) finishPeriodicAcquisition(); } } else { _nmea_checksum_bad++; @@ -234,25 +240,12 @@ public : } } - if (!isValid()) time_valid = 0; - - if ((long)(millis() - next_check) > 0) { - next_check = millis() + 1000; - // Re-enable time sync periodically when GPS has valid fix - if (!_time_sync_needed && _clock != NULL && (millis() - _last_time_sync) > TIME_SYNC_INTERVAL) { - _time_sync_needed = true; - } - if (_time_sync_needed && time_valid > 2) { - if (_clock != NULL) { - _clock->setCurrentTime(getTimestamp()); - _time_sync_needed = false; - _last_time_sync = millis(); - } - } - if (isValid()) { - time_valid ++; - } + if (_acquiringPeriodically && + (uint32_t)(millis() - _acquisitionStarted) >= GPS_PERIODIC_ACQUISITION_TIMEOUT_MS) { + finishPeriodicAcquisition(); } + + updateTime(); } void formatDiagnostics(char* out, size_t out_size) override { @@ -265,8 +258,6 @@ public : formatAge(nmea_age, sizeof(nmea_age), _nmea_seen, _last_nmea_ms); formatAge(fix_age, sizeof(fix_age), _fix_seen, _last_fix_ms); - // Compact enough for CommonCLI's 160-byte reply buffer, including the - // "req:" prefix added by CommonCLI. snprintf(out, out_size, "en:%u ub:%lu ua:%s ok:%lu bad:%lu na:%s sat:%ld fix:%u fa:%s bc:%lu sc:%lu", isEnabled() ? 1U : 0U, diff --git a/src/helpers/sensors/NmeaGpsDriver.h b/src/helpers/sensors/NmeaGpsDriver.h new file mode 100644 index 00000000..f9ea5124 --- /dev/null +++ b/src/helpers/sensors/NmeaGpsDriver.h @@ -0,0 +1,262 @@ +#pragma once + +#include "GpsDriver.h" + +#include +#include + +#ifndef PIN_GPS_TX + #define PIN_GPS_TX (-1) +#endif + +#ifndef PIN_GPS_RX + #define PIN_GPS_RX (-1) +#endif + +#ifndef GPS_EN + #ifdef PIN_GPS_EN + #define GPS_EN PIN_GPS_EN + #else + #define GPS_EN (-1) + #endif +#endif + +#ifndef GPS_EN_ACTIVE + #ifdef PIN_GPS_EN_ACTIVE + #define GPS_EN_ACTIVE PIN_GPS_EN_ACTIVE + #else + #define GPS_EN_ACTIVE HIGH + #endif +#endif + +#ifndef GPS_RESET + #ifdef PIN_GPS_RESET + #define GPS_RESET PIN_GPS_RESET + #else + #define GPS_RESET (-1) + #endif +#endif + +#ifndef GPS_RESET_ACTIVE + #ifdef PIN_GPS_RESET_ACTIVE + #define GPS_RESET_ACTIVE PIN_GPS_RESET_ACTIVE + #else + #define GPS_RESET_ACTIVE LOW + #endif +#endif + +#ifndef GPS_BAUD_RATE + #define GPS_BAUD_RATE 9600 +#endif + +#ifndef PIN_GPS_SWITCH + #define PIN_GPS_SWITCH (-1) +#endif + +#ifndef GPS_SWITCH_ACTIVE + #define GPS_SWITCH_ACTIVE HIGH +#endif + +#ifndef GPS_SWITCH_IS_HARDWARE + #define GPS_SWITCH_IS_HARDWARE 1 +#endif + +#ifndef GPS_SWITCH_TURNS_GPS_ON + #define GPS_SWITCH_TURNS_GPS_ON 0 +#endif + +class NmeaGpsDriver : public GpsDriver { + Stream* _stream = nullptr; + HardwareSerial* _hardwareSerial = nullptr; + RefCountedDigitalPin* _peripheralPower = nullptr; + int _txPin = -1; + int _rxPin = -1; + int _resetPin = -1; + int _enablePin = -1; + int _enableActive = HIGH; + int _resetActive = LOW; + uint32_t _baudRate = 9600; + int _hardwareEnablePin = -1; + int _hardwareEnableActive = HIGH; + bool _started = false; +#if !GPS_SWITCH_IS_HARDWARE + bool _requestedOn = false; + bool _switchStateKnown = false; + bool _lastSwitchEnabled = false; + unsigned long _nextSwitchPoll = 0; +#endif + + void startHardware() { + if (_stream == nullptr || _started) return; + + if (_hardwareSerial != nullptr) { +#ifdef ESP32_PLATFORM + _hardwareSerial->setPins(_txPin, _rxPin); +#endif + _hardwareSerial->begin(_baudRate); + } + + if (_peripheralPower != nullptr) _peripheralPower->claim(); + + if (_enablePin != -1) digitalWrite(_enablePin, _enableActive); + if (_resetPin != -1) digitalWrite(_resetPin, !_resetActive); + _started = true; + } + + void stopHardware() { + if (!_started) return; + + if (_enablePin != -1) digitalWrite(_enablePin, !_enableActive); + if (_resetPin != -1) digitalWrite(_resetPin, _resetActive); + + if (_peripheralPower != nullptr) _peripheralPower->release(); + _started = false; + } + +public: + NmeaGpsDriver() = default; + + NmeaGpsDriver(Stream& stream, int txPin = PIN_GPS_TX, int rxPin = PIN_GPS_RX, + int resetPin = GPS_RESET, int enablePin = GPS_EN, + RefCountedDigitalPin* peripheralPower = nullptr, + int enableActive = GPS_EN_ACTIVE, int resetActive = GPS_RESET_ACTIVE, + uint32_t baudRate = GPS_BAUD_RATE, + int hardwareEnablePin = PIN_GPS_SWITCH, + int hardwareEnableActive = GPS_SWITCH_ACTIVE) + : _stream(&stream), _peripheralPower(peripheralPower), _txPin(txPin), _rxPin(rxPin), + _resetPin(resetPin), _enablePin(enablePin), _enableActive(enableActive), + _resetActive(resetActive), _baudRate(baudRate), + _hardwareEnablePin(hardwareEnablePin), _hardwareEnableActive(hardwareEnableActive) { + if (_resetPin != -1) { + pinMode(_resetPin, OUTPUT); + digitalWrite(_resetPin, _resetActive); + } + if (_enablePin != -1) { + pinMode(_enablePin, OUTPUT); + digitalWrite(_enablePin, !_enableActive); + } + if (_hardwareEnablePin != -1) { + pinMode(_hardwareEnablePin, INPUT); + } + } + + NmeaGpsDriver(HardwareSerial& serial, int txPin = PIN_GPS_TX, int rxPin = PIN_GPS_RX, + int resetPin = GPS_RESET, int enablePin = GPS_EN, + RefCountedDigitalPin* peripheralPower = nullptr, + int enableActive = GPS_EN_ACTIVE, int resetActive = GPS_RESET_ACTIVE, + uint32_t baudRate = GPS_BAUD_RATE, + int hardwareEnablePin = PIN_GPS_SWITCH, + int hardwareEnableActive = GPS_SWITCH_ACTIVE) + : NmeaGpsDriver(static_cast(serial), txPin, rxPin, resetPin, enablePin, + peripheralPower, enableActive, resetActive, baudRate, + hardwareEnablePin, hardwareEnableActive) { + _hardwareSerial = &serial; + } + + void begin() override { +#if !GPS_SWITCH_IS_HARDWARE + _requestedOn = true; + if (!isHardwareEnabled()) return; +#endif + startHardware(); + } + + void reset() override { + if (!_started) return; + if (_resetPin != -1) { + digitalWrite(_resetPin, _resetActive); + delay(10); + digitalWrite(_resetPin, !_resetActive); + } + +#ifdef GPS_L76K + delay(250); + sendNmeaSentence("$PCAS04,7"); + delay(250); +#endif + } + + void stop() override { +#if !GPS_SWITCH_IS_HARDWARE + _requestedOn = false; +#endif + stopHardware(); + } + + bool isEnabled() const override { + return _started && isHardwareEnabled(); + } + + bool hasHardwareEnableState() const override { + return _hardwareEnablePin != -1; + } + + bool isHardwareEnabled() const override { + return _hardwareEnablePin == -1 || + digitalRead(_hardwareEnablePin) == _hardwareEnableActive; + } + + bool pollGpsSwitch() override { +#if !GPS_SWITCH_IS_HARDWARE + if (_hardwareEnablePin == -1 || + (long)(millis() - _nextSwitchPoll) < 0) return false; + + _nextSwitchPoll = millis() + 1000; + const bool switchEnabled = isHardwareEnabled(); + const bool enablesGps = GPS_SWITCH_TURNS_GPS_ON && _switchStateKnown && + !_lastSwitchEnabled && switchEnabled; + _switchStateKnown = true; + _lastSwitchEnabled = switchEnabled; + + if (enablesGps) _requestedOn = true; + if (_requestedOn && switchEnabled) { + startHardware(); + } else { + stopHardware(); + } + return enablesGps; +#else + return false; +#endif + } + + bool detect(uint32_t timeout_ms) override { + if (available() > 0) return true; + + const uint32_t deadline = millis() + timeout_ms; + while ((int32_t)(millis() - deadline) < 0) { + if (available() > 0) return true; + delay(10); + } + return false; + } + + int available() { + return _stream == nullptr ? 0 : _stream->available(); + } + + int read() { + return _stream == nullptr ? -1 : _stream->read(); + } + + void drain() { + while (available() > 0) read(); + } + + void sendNmeaSentence(const char* sentence) { + if (_stream == nullptr || sentence == nullptr || sentence[0] != '$') return; + + uint8_t checksum = 0; + const char* p = sentence + 1; + while (*p != '\0' && *p != '*') checksum ^= (uint8_t)*p++; + + _stream->print(sentence); + if (*p == '\0') { + char checksumSuffix[6]; + snprintf(checksumSuffix, sizeof(checksumSuffix), "*%02X\r\n", checksum); + _stream->print(checksumSuffix); + } else { + _stream->print("\r\n"); + } + } +}; diff --git a/src/helpers/sensors/RakGpsDriver.h b/src/helpers/sensors/RakGpsDriver.h new file mode 100644 index 00000000..fb4b772b --- /dev/null +++ b/src/helpers/sensors/RakGpsDriver.h @@ -0,0 +1,112 @@ +#pragma once + +#include "NmeaGpsDriver.h" + +#include + +class RakGpsDriver : public GpsDriver { + NmeaGpsDriver _serialDriver; + TwoWire& _wire; + SFE_UBLOX_GNSS _gnss; + uint8_t _powerPin = 0; + bool _usingRak12500 = false; + bool _detected = false; + + bool probeRak12500(uint8_t powerPin) { +#if defined(ETHERNET_ENABLED) && defined(RAK_BOARD) + if (powerPin == WB_IO2) return false; +#endif + +#ifndef RAK_3401 + pinMode(powerPin, OUTPUT); + digitalWrite(powerPin, LOW); + delay(500); + digitalWrite(powerPin, HIGH); +#endif + delay(500); + + if (_gnss.begin(_wire)) { + _gnss.setI2COutput(COM_TYPE_UBX); + _gnss.enableGNSS(true, SFE_UBLOX_GNSS_ID_GPS); + _gnss.enableGNSS(true, SFE_UBLOX_GNSS_ID_GALILEO); + _gnss.enableGNSS(true, SFE_UBLOX_GNSS_ID_GLONASS); + _gnss.enableGNSS(true, SFE_UBLOX_GNSS_ID_SBAS); + _gnss.enableGNSS(true, SFE_UBLOX_GNSS_ID_BEIDOU); + _gnss.enableGNSS(true, SFE_UBLOX_GNSS_ID_IMES); + _gnss.enableGNSS(true, SFE_UBLOX_GNSS_ID_QZSS); + _gnss.setMeasurementRate(1000); + _gnss.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); + _powerPin = powerPin; + _usingRak12500 = true; + return true; + } + +#ifndef RAK_3401 + pinMode(powerPin, INPUT); +#endif + return false; + } + +public: + RakGpsDriver(HardwareSerial& serial = Serial1, TwoWire& wire = Wire) + : _serialDriver(serial), _wire(wire) {} + + void begin() override { + if (_usingRak12500) { +#ifndef RAK_3401 + pinMode(_powerPin, OUTPUT); + digitalWrite(_powerPin, HIGH); +#endif + return; + } + _serialDriver.begin(); + } + + void reset() override { + if (!_usingRak12500) _serialDriver.reset(); + } + + void stop() override { + if (_usingRak12500) { +#ifndef RAK_3401 + pinMode(_powerPin, OUTPUT); + digitalWrite(_powerPin, LOW); +#endif + return; + } + _serialDriver.stop(); + } + + bool isEnabled() const override { + return _usingRak12500 ? true : _serialDriver.isEnabled(); + } + + bool detect(uint32_t) override { + if (_detected) return true; + + _serialDriver.begin(); + if (probeRak12500(WB_IO2) || probeRak12500(WB_IO4) || probeRak12500(WB_IO5)) { + _detected = true; + return true; + } + + if (_serialDriver.detect(0)) { + _detected = true; + return true; + } + + _serialDriver.stop(); + return false; + } + + bool usesRak12500() const { return _usingRak12500; } + bool isDetected() const { return _detected; } + NmeaGpsDriver& nmeaDriver() { return _serialDriver; } + + bool hasFix() { return _usingRak12500 && _gnss.getGnssFixOk(8); } + long getLatitude() { return _gnss.getLatitude(2) / 10; } + long getLongitude() { return _gnss.getLongitude(2) / 10; } + long getAltitude() { return _gnss.getAltitude(2); } + long getSatellites() { return _gnss.getSIV(2); } + long getTimestamp() { return _gnss.getUnixEpoch(2); } +}; diff --git a/src/helpers/sensors/RakLocationProvider.h b/src/helpers/sensors/RakLocationProvider.h new file mode 100644 index 00000000..c909b7c4 --- /dev/null +++ b/src/helpers/sensors/RakLocationProvider.h @@ -0,0 +1,144 @@ +#pragma once + +#include "MicroNMEALocationProvider.h" +#include "RakGpsDriver.h" + +class RakLocationProvider : public LocationProvider { + RakGpsDriver& _driver; + MicroNMEALocationProvider _nmea; + uint32_t _gpsPeriodSeconds = 0; + unsigned long _nextAcquisition = 0; + unsigned long _acquisitionStarted = 0; + bool _acquiringPeriodically = false; + +public: + RakLocationProvider(RakGpsDriver& driver, mesh::RTCClock* clock = nullptr) + : _driver(driver), _nmea(driver.nmeaDriver(), clock) {} + + void syncTime() override { + LocationProvider::syncTime(); + if (!_driver.usesRak12500()) _nmea.syncTime(); + } + + long getLatitude() override { + return _driver.usesRak12500() ? _driver.getLatitude() : _nmea.getLatitude(); + } + + long getLongitude() override { + return _driver.usesRak12500() ? _driver.getLongitude() : _nmea.getLongitude(); + } + + long getAltitude() override { + return _driver.usesRak12500() ? _driver.getAltitude() : _nmea.getAltitude(); + } + + long satellitesCount() override { + return _driver.usesRak12500() ? _driver.getSatellites() : _nmea.satellitesCount(); + } + + bool isValid() override { + return _driver.usesRak12500() ? _driver.hasFix() : _nmea.isValid(); + } + + long getTimestamp() override { + return _driver.usesRak12500() ? _driver.getTimestamp() : _nmea.getTimestamp(); + } + + void sendSentence(const char* sentence) override { + if (!_driver.usesRak12500()) _nmea.sendSentence(sentence); + } + + bool waitFor(const char* prefix, uint32_t timeout_ms) override { + return _driver.usesRak12500() ? false : _nmea.waitFor(prefix, timeout_ms); + } + + void drain() override { + if (!_driver.usesRak12500()) _nmea.drain(); + } + + void reset() override { + _driver.reset(); + } + + void begin() override { + if (_driver.isDetected() && _driver.usesRak12500()) { + _driver.begin(); + _acquiringPeriodically = _gpsPeriodSeconds > 0; + _acquisitionStarted = millis(); + } else if (_driver.isDetected()) { + _nmea.begin(); + } else { + _driver.begin(); + } + } + + void stop() override { + _acquiringPeriodically = false; + if (_driver.usesRak12500()) { + _driver.stop(); + } else { + _nmea.stop(); + } + } + + bool detect(uint32_t timeout_ms) override { + if (!_driver.detect(timeout_ms)) return false; + + if (_driver.usesRak12500()) { + _acquiringPeriodically = _gpsPeriodSeconds > 0; + _acquisitionStarted = millis(); + } else { + _nmea.setGpsPeriod(_gpsPeriodSeconds); + _nmea.begin(); + } + return true; + } + + bool keepEnabledAfterDetection() const override { +#ifdef FORCE_GPS_ALIVE + return true; +#else + return false; +#endif + } + + void loop() override { + if (_driver.usesRak12500()) { + if (_gpsOn && _gpsPeriodSeconds > 0) { + if (!_acquiringPeriodically && + (long)(millis() - _nextAcquisition) >= 0) { + _driver.begin(); + _acquiringPeriodically = true; + _acquisitionStarted = millis(); + } + if (_acquiringPeriodically && isValid()) { + _driver.stop(); + _acquiringPeriodically = false; + _nextAcquisition = millis() + _gpsPeriodSeconds * 1000UL; + } else if (_acquiringPeriodically && + (uint32_t)(millis() - _acquisitionStarted) >= + GPS_PERIODIC_ACQUISITION_TIMEOUT_MS) { + _driver.stop(); + _acquiringPeriodically = false; + _nextAcquisition = millis() + _gpsPeriodSeconds * 1000UL; + } + } + } else { + _nmea.loop(); + } + } + + bool isEnabled() override { + return _driver.isEnabled(); + } + + void setGpsPeriod(uint32_t seconds) override { + _gpsPeriodSeconds = seconds; + _nextAcquisition = millis(); + if (!_driver.usesRak12500()) _nmea.setGpsPeriod(seconds); + } + + uint32_t getGpsPeriod() const override { + return _gpsPeriodSeconds; + } +}; diff --git a/variants/gat562_30s_mesh_kit/target.cpp b/variants/gat562_30s_mesh_kit/target.cpp index 8f84a7a1..ba81a690 100644 --- a/variants/gat562_30s_mesh_kit/target.cpp +++ b/variants/gat562_30s_mesh_kit/target.cpp @@ -26,9 +26,10 @@ VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS - #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); - EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); + #include + RakGpsDriver gps_driver; + RakLocationProvider location(gps_driver, &rtc_clock); + EnvironmentSensorManager sensors(location); #else EnvironmentSensorManager sensors; #endif diff --git a/variants/lilygo_techo/target.cpp b/variants/lilygo_techo/target.cpp index 12b40d7e..394d79c5 100644 --- a/variants/lilygo_techo/target.cpp +++ b/variants/lilygo_techo/target.cpp @@ -13,7 +13,8 @@ VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #ifdef ENV_INCLUDE_GPS -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea = MicroNMEALocationProvider(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors = EnvironmentSensorManager(); @@ -36,5 +37,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } - - diff --git a/variants/rak3401/target.cpp b/variants/rak3401/target.cpp index 5309e6b2..26501977 100644 --- a/variants/rak3401/target.cpp +++ b/variants/rak3401/target.cpp @@ -25,9 +25,10 @@ VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS - #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); - EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); + #include + RakGpsDriver gps_driver; + RakLocationProvider location(gps_driver, &rtc_clock); + EnvironmentSensorManager sensors(location); #else EnvironmentSensorManager sensors; #endif @@ -41,4 +42,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/rak4631/target.cpp b/variants/rak4631/target.cpp index a41ba720..06ccd23d 100644 --- a/variants/rak4631/target.cpp +++ b/variants/rak4631/target.cpp @@ -25,9 +25,10 @@ VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS - #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); - EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); + #include + RakGpsDriver gps_driver; + RakLocationProvider location(gps_driver, &rtc_clock); + EnvironmentSensorManager sensors(location); #else EnvironmentSensorManager sensors; #endif diff --git a/variants/t1000-e/platformio.ini b/variants/t1000-e/platformio.ini index 8456dc91..ab8b0de0 100644 --- a/variants/t1000-e/platformio.ini +++ b/variants/t1000-e/platformio.ini @@ -106,6 +106,7 @@ build_flags = ${t1000-e.build_flags} ; -D BLE_DEBUG_LOGGING=1 ; -D MESH_PACKET_LOGGING=1 ; -D MESH_DEBUG=1 +; -D GPS_NMEA_DEBUG -D OFFLINE_QUEUE_SIZE=256 -D DISPLAY_CLASS=NullDisplayDriver -D PIN_BUZZER=25 diff --git a/variants/t1000-e/target.cpp b/variants/t1000-e/target.cpp index 70a64359..0d7710b3 100644 --- a/variants/t1000-e/target.cpp +++ b/variants/t1000-e/target.cpp @@ -1,8 +1,8 @@ #include #include "t1000e_sensors.h" #include "target.h" +#include #include -#include T1000eBoard board; @@ -11,8 +11,9 @@ RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BU WRAPPER_CLASS radio_driver(radio, board); VolatileRTCClock rtc_clock; -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); -T1000SensorManager sensors = T1000SensorManager(nmea); +AirohaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); +T1000SensorManager sensors(nmea, gps_driver); #ifdef DISPLAY_CLASS NullDisplayDriver display; @@ -82,28 +83,17 @@ mesh::LocalIdentity radio_new_identity() { void T1000SensorManager::start_gps() { gps_active = true; - digitalWrite(GPS_EN, HIGH); - delay(10); - digitalWrite(GPS_RTC_INT, HIGH); - delay(5); - digitalWrite(GPS_RTC_INT, LOW); + _nmea->begin(); } void T1000SensorManager::stop_gps() { gps_active = false; - digitalWrite(GPS_VRTC_EN, HIGH); // keep GPS RTC alive for faster fix on wake - digitalWrite(GPS_RTC_INT, LOW); // make sure this is LOW so we can pulse it to wake - airohaEnterSleep(_nmea); // send command to put the GPS into RTC backup sleep - digitalWrite(GPS_EN, LOW); + _nmea->stop(); } bool T1000SensorManager::begin() { - // init GPS - Serial1.begin(115200); - digitalWrite(GPS_RESET, HIGH); - delay(10); - digitalWrite(GPS_RESET, LOW); + _gps_driver->initialize(); return true; } diff --git a/variants/t1000-e/target.h b/variants/t1000-e/target.h index db003cc5..4a1a2bb7 100644 --- a/variants/t1000-e/target.h +++ b/variants/t1000-e/target.h @@ -12,15 +12,19 @@ #include "NullDisplayDriver.h" #endif +class AirohaGpsDriver; + class T1000SensorManager: public SensorManager { bool gps_active = false; LocationProvider * _nmea; + AirohaGpsDriver* _gps_driver; void start_gps(); void sleep_gps(); void stop_gps(); public: - T1000SensorManager(LocationProvider &nmea): _nmea(&nmea) { } + T1000SensorManager(LocationProvider &nmea, AirohaGpsDriver& gps_driver) + : _nmea(&nmea), _gps_driver(&gps_driver) { } bool begin() override; bool querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) override; void loop() override; diff --git a/variants/thinknode_m1/target.cpp b/variants/thinknode_m1/target.cpp index c430cee0..78f5a88e 100644 --- a/variants/thinknode_m1/target.cpp +++ b/variants/thinknode_m1/target.cpp @@ -2,6 +2,7 @@ #include "target.h" #include #include +#include ThinkNodeM1Board board; @@ -11,7 +12,8 @@ WRAPPER_CLASS radio_driver(radio, board); VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea = MicroNMEALocationProvider(gps_driver, &rtc_clock); ThinkNodeM1SensorManager sensors = ThinkNodeM1SensorManager(nmea); #ifdef DISPLAY_CLASS @@ -31,34 +33,15 @@ mesh::LocalIdentity radio_new_identity() { } void ThinkNodeM1SensorManager::start_gps() { - if (!gps_active) { - gps_active = true; - _location->begin(); - } + _location->setGpsOn(true); } void ThinkNodeM1SensorManager::stop_gps() { - if (gps_active) { - gps_active = false; - _location->stop(); - } + _location->setGpsOn(false); } bool ThinkNodeM1SensorManager::begin() { - Serial1.begin(9600); - - // Initialize GPS switch pin - pinMode(PIN_GPS_SWITCH, INPUT); - last_gps_switch_state = digitalRead(PIN_GPS_SWITCH); - - // Initialize GPS power pin - pinMode(GPS_EN, OUTPUT); - - // Check initial switch state to determine if GPS should be active - if (last_gps_switch_state == HIGH) { // Switch is HIGH when ON - start_gps(); - } - + start_gps(); return true; } @@ -71,36 +54,11 @@ bool ThinkNodeM1SensorManager::querySensors(uint8_t requester_permissions, Cayen void ThinkNodeM1SensorManager::loop() { static long next_gps_update = 0; - static long last_switch_check = 0; - - // Check GPS switch state every second - if (millis() - last_switch_check > 1000) { - bool current_switch_state = digitalRead(PIN_GPS_SWITCH); - - // Detect switch state change - if (current_switch_state != last_gps_switch_state) { - last_gps_switch_state = current_switch_state; - - if (current_switch_state == HIGH) { // Switch is ON - MESH_DEBUG_PRINTLN("GPS switch ON"); - start_gps(); - } else { // Switch is OFF - MESH_DEBUG_PRINTLN("GPS switch OFF"); - stop_gps(); - } - } - - last_switch_check = millis(); - } - - if (!gps_active) { - return; // GPS is not active, skip further processing - } _location->loop(); if (millis() > next_gps_update) { - if (_location->isValid()) { + if (_location->isEnabled() && _location->isValid()) { node_lat = ((double)_location->getLatitude())/1000000.; node_lon = ((double)_location->getLongitude())/1000000.; node_altitude = ((double)_location->getAltitude()) / 1000.0; @@ -120,7 +78,7 @@ const char* ThinkNodeM1SensorManager::getSettingName(int i) const { const char* ThinkNodeM1SensorManager::getSettingValue(int i) const { if (i == 0) { - return gps_active ? "1" : "0"; + return _location->isGpsOn() ? "1" : "0"; } return NULL; } @@ -136,4 +94,3 @@ bool ThinkNodeM1SensorManager::setSettingValue(const char* name, const char* val } return false; // not supported } - diff --git a/variants/thinknode_m1/target.h b/variants/thinknode_m1/target.h index d2d6aed1..9c8a6e80 100644 --- a/variants/thinknode_m1/target.h +++ b/variants/thinknode_m1/target.h @@ -14,8 +14,6 @@ #endif class ThinkNodeM1SensorManager : public SensorManager { - bool gps_active = false; - bool last_gps_switch_state = false; LocationProvider* _location; void start_gps(); @@ -45,4 +43,3 @@ extern ThinkNodeM1SensorManager sensors; bool radio_init(); mesh::LocalIdentity radio_new_identity(); - diff --git a/variants/thinknode_m1/variant.h b/variants/thinknode_m1/variant.h index aecf197e..3e3f03a7 100644 --- a/variants/thinknode_m1/variant.h +++ b/variants/thinknode_m1/variant.h @@ -146,3 +146,5 @@ extern const int SCK; #define PIN_GPS_PPS (36) #define PIN_GPS_STANDBY (34) #define PIN_GPS_SWITCH (33) +#define GPS_SWITCH_IS_HARDWARE (0) +#define GPS_SWITCH_TURNS_GPS_ON (1) diff --git a/variants/thinknode_m5/target.cpp b/variants/thinknode_m5/target.cpp index ec524df7..0ed9a0d0 100644 --- a/variants/thinknode_m5/target.cpp +++ b/variants/thinknode_m5/target.cpp @@ -17,7 +17,8 @@ ESP32RTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #ifdef ENV_INCLUDE_GPS -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea = MicroNMEALocationProvider(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors = EnvironmentSensorManager(); @@ -46,5 +47,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } - - diff --git a/variants/thinknode_m5/variant.h b/variants/thinknode_m5/variant.h index 9b82416b..fefb45d1 100644 --- a/variants/thinknode_m5/variant.h +++ b/variants/thinknode_m5/variant.h @@ -25,4 +25,5 @@ #define PIN_GPS_RESET (13) #define PIN_GPS_RX (20) #define PIN_GPS_TX (19) -#define PIN_GPS_SWITCH (10) \ No newline at end of file +#define PIN_GPS_SWITCH (10) +#define GPS_SWITCH_IS_HARDWARE (1) \ No newline at end of file From dea38936d7c24a3680044ca9871bf6bcf775cd29 Mon Sep 17 00:00:00 2001 From: Florent Date: Mon, 2 Nov 2026 11:09:16 -0400 Subject: [PATCH 17/53] gps: move the gps CLI to GpsPrefs and convert all targets --- examples/companion_radio/MyMesh.cpp | 5 + examples/companion_radio/MyMesh.h | 11 +- examples/companion_radio/NodePrefs.h | 14 ++- examples/companion_radio/ui-new/UITask.cpp | 8 +- examples/companion_radio/ui-orig/UITask.cpp | 5 +- examples/companion_radio/ui-tiny/UITask.cpp | 8 +- examples/simple_repeater/MyMesh.h | 12 +- examples/simple_room_server/MyMesh.h | 12 +- examples/simple_sensor/SensorMesh.h | 10 +- src/helpers/CommonCLI.cpp | 76 +----------- src/helpers/CommonCLI.h | 12 +- src/helpers/CommonGpsPrefs.cpp | 110 ++++++++++++++++++ src/helpers/CommonGpsPrefs.h | 26 +++++ .../sensors/EnvironmentSensorManager.cpp | 18 +-- variants/gat562_mesh_evb_pro/target.cpp | 3 +- variants/gat562_mesh_tracker_pro/target.cpp | 3 +- variants/heltec_mesh_solar/target.cpp | 15 +-- variants/heltec_mesh_solar/target.h | 1 - variants/heltec_rc32/target.cpp | 4 +- variants/heltec_t096/target.cpp | 4 +- variants/heltec_t1/target.cpp | 4 +- variants/heltec_t114/target.cpp | 3 +- variants/heltec_tower_v2/target.cpp | 3 +- variants/heltec_tracker/target.cpp | 30 +++-- variants/heltec_tracker/target.h | 1 - variants/heltec_tracker_v2/target.cpp | 5 +- variants/heltec_v3/target.cpp | 4 +- variants/heltec_v4/target.cpp | 4 +- variants/heltec_v4_r8/target.cpp | 4 +- variants/keepteen_lt1/target.cpp | 4 +- variants/lilygo_t_impulse_plus/target.cpp | 4 +- variants/lilygo_tbeam_1w/target.cpp | 4 +- variants/lilygo_tbeam_SX1262/target.cpp | 4 +- variants/lilygo_tbeam_SX1276/target.cpp | 3 +- .../lilygo_tbeam_supreme_SX1262/target.cpp | 4 +- variants/lilygo_tdeck/target.cpp | 3 +- variants/lilygo_techo_card/target.cpp | 3 +- variants/lilygo_techo_lite/target.cpp | 4 +- variants/meshadventurer/target.cpp | 19 ++- variants/meshadventurer/target.h | 3 +- variants/meshnology_w12/target.cpp | 4 +- variants/meshtracker_x1/target.cpp | 27 ++--- variants/meshtracker_x1/target.h | 2 - variants/minewsemi_me25ls01/target.cpp | 3 +- variants/muzi_base/target.cpp | 22 +--- variants/muzi_base/target.h | 6 +- variants/muzi_base/variant.h | 2 + variants/muziworks_r1_neo/target.cpp | 3 +- variants/nano_g2_ultra/NanoG2UltraGpsDriver.h | 31 +++++ variants/nano_g2_ultra/target.cpp | 35 ++---- variants/nano_g2_ultra/target.h | 8 +- variants/promicro/target.cpp | 4 +- variants/rak3112/target.cpp | 4 +- variants/rak_wismesh_tag/target.cpp | 4 +- variants/sensecap_indicator-espnow/target.cpp | 3 +- variants/sensecap_solar/target.cpp | 4 +- variants/station_g2/target.cpp | 4 +- variants/station_g3_esp32/target.cpp | 3 +- variants/t1000-e/target.cpp | 10 +- variants/t1000-e/target.h | 1 - variants/thinknode_m3/target.cpp | 3 +- variants/thinknode_m4/target.cpp | 3 +- variants/thinknode_m6/target.cpp | 4 +- variants/thinknode_m7/target.cpp | 4 +- variants/thinknode_m8/target.cpp | 3 +- variants/thinknode_m9/target.cpp | 3 +- variants/wio-tracker-l1-1w/target.cpp | 4 +- variants/wio-tracker-l1/target.cpp | 4 +- variants/xiao_c3/target.cpp | 4 +- 69 files changed, 381 insertions(+), 301 deletions(-) create mode 100644 src/helpers/CommonGpsPrefs.cpp create mode 100644 src/helpers/CommonGpsPrefs.h create mode 100644 variants/nano_g2_ultra/NanoG2UltraGpsDriver.h diff --git a/examples/companion_radio/MyMesh.cpp b/examples/companion_radio/MyMesh.cpp index 755c56d1..d12c7b8a 100644 --- a/examples/companion_radio/MyMesh.cpp +++ b/examples/companion_radio/MyMesh.cpp @@ -2093,6 +2093,11 @@ bool MyMesh::handleCommand(const char* command, uint32_t sender_timestamp, char* return true; } + if (_prefs.getGpsPrefs()->handleCommand(command, sensors, reply)) { + if (_prefs.getGpsPrefs()->isDirty()) { savePrefs(); } + return true; + } + // hook for variant-specific CLI processing if (board.handleCommand(command, sender_timestamp, reply)) { if (_prefs.isDirty()) { savePrefs(); } diff --git a/examples/companion_radio/MyMesh.h b/examples/companion_radio/MyMesh.h index 944790df..846708b5 100644 --- a/examples/companion_radio/MyMesh.h +++ b/examples/companion_radio/MyMesh.h @@ -203,12 +203,13 @@ public: #if ENV_INCLUDE_GPS == 1 void applyGpsPrefs() { - sensors.setSettingValue("gps", _prefs.gps_enabled ? "1" : "0"); - if (_prefs.gps_interval > 0) { - char interval_str[12]; // Max: 24 hours = 86400 seconds (5 digits + null) - sprintf(interval_str, "%u", _prefs.gps_interval); - sensors.setSettingValue("gps_interval", interval_str); + if (_prefs.gps_interval > 0 && + sensors.getSettingByKey("gps_interval") != nullptr) { + char interval[11]; + snprintf(interval, sizeof(interval), "%lu", (unsigned long)_prefs.gps_interval); + sensors.setSettingValue("gps_interval", interval); } + _prefs.getGpsPrefs()->apply(sensors); } #endif diff --git a/examples/companion_radio/NodePrefs.h b/examples/companion_radio/NodePrefs.h index c5d1df04..a54eca75 100644 --- a/examples/companion_radio/NodePrefs.h +++ b/examples/companion_radio/NodePrefs.h @@ -2,6 +2,7 @@ #include // For uint8_t, uint32_t #include #include +#include #include #define TELEM_MODE_DENY 0 @@ -35,6 +36,7 @@ public: uint8_t vibe_quiet = 0; uint8_t gps_enabled = 0; // GPS enabled flag (0=disabled, 1=enabled) uint32_t gps_interval = 0; // GPS read interval in seconds + uint32_t gps_period = 0; // periodic acquisition interval in seconds uint8_t autoadd_config = 0; // bitmask for auto-add contacts config uint8_t rx_boosted_gain = 0; // SX126x RX boosted gain mode (0=power saving, 1=boosted) uint8_t radio_fem_rxgain = 0; // external LoRa FEM RX gain (LNA) @@ -151,16 +153,21 @@ private: }; RadioPrefs radio; - class GPSPrefs : public ConfigSerializer { // COPIED from CommonCLI (for now) + class GPSPrefs : public CommonGpsPrefs { NodePrefs* _parent; protected: void structure() override { def("en", _parent->gps_enabled); // boolean def("int", _parent->gps_interval); // interval in seconds + def("period", _parent->gps_period); // periodic acquisition interval in seconds def("adv_loc", _parent->advert_loc_policy); } public: GPSPrefs(NodePrefs* parent) : _parent(parent) { } + bool isGpsEnabled() const override { return _parent->gps_enabled; } + void setGpsEnabled(bool enabled) override { _parent->gps_enabled = enabled; markDirty(); } + uint32_t getGpsPeriod() const override { return _parent->gps_period; } + void setGpsPeriod(uint32_t seconds) override { _parent->gps_period = seconds; markDirty(); } }; GPSPrefs gps; @@ -275,8 +282,9 @@ public: void setRepeatEn(bool en) { repeat.disable_fwd = en ? 0 : 1; } CommonRadioPrefs* getRadioPrefs() { return &radio; } + CommonGpsPrefs* getGpsPrefs() { return &gps; } KeyValueStore* getCustom() { return &custom; } - bool isDirty() const override { return ConfigSerializer::isDirty() || radio.isDirty() || custom.isDirty(); } - void clearDirty() override { ConfigSerializer::clearDirty(); radio.clearDirty(); custom.clearDirty(); } + bool isDirty() const override { return ConfigSerializer::isDirty() || radio.isDirty() || gps.isDirty() || custom.isDirty(); } + void clearDirty() override { ConfigSerializer::clearDirty(); radio.clearDirty(); gps.clearDirty(); custom.clearDirty(); } }; diff --git a/examples/companion_radio/ui-new/UITask.cpp b/examples/companion_radio/ui-new/UITask.cpp index cfe34cf2..a1400669 100644 --- a/examples/companion_radio/ui-new/UITask.cpp +++ b/examples/companion_radio/ui-new/UITask.cpp @@ -1182,16 +1182,14 @@ void UITask::toggleGPS() { for (int i = 0; i < num; i++) { if (strcmp(_sensors->getSettingName(i), "gps") == 0) { if (strcmp(_sensors->getSettingValue(i), "1") == 0) { - _sensors->setSettingValue("gps", "0"); - _node_prefs->gps_enabled = 0; + _node_prefs->getGpsPrefs()->setGpsEnabled(*_sensors, false); notify(UIEventType::ack); } else { - _sensors->setSettingValue("gps", "1"); - _node_prefs->gps_enabled = 1; + _node_prefs->getGpsPrefs()->setGpsEnabled(*_sensors, true); notify(UIEventType::ack); } the_mesh.savePrefs(); - showAlert(_node_prefs->gps_enabled ? "GPS: Enabled" : "GPS: Disabled", 800); + showAlert(_node_prefs->getGpsPrefs()->isGpsEnabled() ? "GPS: Enabled" : "GPS: Disabled", 800); _next_refresh = 0; break; } diff --git a/examples/companion_radio/ui-orig/UITask.cpp b/examples/companion_radio/ui-orig/UITask.cpp index 8ae57c7a..c2fd80fb 100644 --- a/examples/companion_radio/ui-orig/UITask.cpp +++ b/examples/companion_radio/ui-orig/UITask.cpp @@ -609,14 +609,15 @@ void UITask::handleButtonQuadruplePress() { for (int i = 0; i < num; i++) { if (strcmp(_sensors->getSettingName(i), "gps") == 0) { if (strcmp(_sensors->getSettingValue(i), "1") == 0) { - _sensors->setSettingValue("gps", "0"); + _node_prefs->getGpsPrefs()->setGpsEnabled(*_sensors, false); notify(UIEventType::ack); sprintf(_alert, "GPS: Disabled"); } else { - _sensors->setSettingValue("gps", "1"); + _node_prefs->getGpsPrefs()->setGpsEnabled(*_sensors, true); notify(UIEventType::ack); sprintf(_alert, "GPS: Enabled"); } + the_mesh.savePrefs(); break; } } diff --git a/examples/companion_radio/ui-tiny/UITask.cpp b/examples/companion_radio/ui-tiny/UITask.cpp index 035340a8..1b48cca8 100644 --- a/examples/companion_radio/ui-tiny/UITask.cpp +++ b/examples/companion_radio/ui-tiny/UITask.cpp @@ -833,16 +833,14 @@ void UITask::toggleGPS() { for (int i = 0; i < num; i++) { if (strcmp(_sensors->getSettingName(i), "gps") == 0) { if (strcmp(_sensors->getSettingValue(i), "1") == 0) { - _sensors->setSettingValue("gps", "0"); - _node_prefs->gps_enabled = 0; + _node_prefs->getGpsPrefs()->setGpsEnabled(*_sensors, false); notify(UIEventType::ack); } else { - _sensors->setSettingValue("gps", "1"); - _node_prefs->gps_enabled = 1; + _node_prefs->getGpsPrefs()->setGpsEnabled(*_sensors, true); notify(UIEventType::ack); } the_mesh.savePrefs(); - showAlert(_node_prefs->gps_enabled ? "GPS: Enabled" : "GPS: Disabled", 800); + showAlert(_node_prefs->getGpsPrefs()->isGpsEnabled() ? "GPS: Enabled" : "GPS: Disabled", 800); _next_refresh = 0; break; } diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index 1d975a94..7e4cfd4f 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -159,10 +159,14 @@ protected: #if ENV_INCLUDE_GPS == 1 void applyGpsPrefs() { - char period[11]; - snprintf(period, sizeof(period), "%lu", (unsigned long)_prefs.gps_period); - sensors.setSettingValue("gps_period", period); - sensors.setSettingValue("gps", _prefs.gps_enabled?"1":"0"); + if (sensors.getSettingByKey("gps_interval") != nullptr) { + // gps_interval was introduced only for EnvironmentSensorManager + // and has little interest ... kept only as a legacy + char interval[11]; + snprintf(interval, sizeof(interval), "%lu", (unsigned long)_prefs.gps_interval); + sensors.setSettingValue("gps_interval", interval); + } + _prefs.getGpsPrefs()->apply(sensors); } #endif diff --git a/examples/simple_room_server/MyMesh.h b/examples/simple_room_server/MyMesh.h index 5f8de73a..11367b12 100644 --- a/examples/simple_room_server/MyMesh.h +++ b/examples/simple_room_server/MyMesh.h @@ -168,10 +168,14 @@ protected: #if ENV_INCLUDE_GPS == 1 void applyGpsPrefs() { - char period[11]; - snprintf(period, sizeof(period), "%lu", (unsigned long)_prefs.gps_period); - sensors.setSettingValue("gps_period", period); - sensors.setSettingValue("gps", _prefs.gps_enabled?"1":"0"); + if (sensors.getSettingByKey("gps_interval") != nullptr) { + // gps_interval was introduced only for EnvironmentSensorManager + // and has little interest ... kept only as a legacy + char interval[11]; + snprintf(interval, sizeof(interval), "%lu", (unsigned long)_prefs.gps_interval); + sensors.setSettingValue("gps_interval", interval); + } + _prefs.getGpsPrefs()->apply(sensors); } #endif diff --git a/examples/simple_sensor/SensorMesh.h b/examples/simple_sensor/SensorMesh.h index b93939de..e6746d47 100644 --- a/examples/simple_sensor/SensorMesh.h +++ b/examples/simple_sensor/SensorMesh.h @@ -173,10 +173,12 @@ private: #if ENV_INCLUDE_GPS == 1 void applyGpsPrefs() { - char period[11]; - snprintf(period, sizeof(period), "%lu", (unsigned long)_prefs.gps_period); - sensors.setSettingValue("gps_period", period); - sensors.setSettingValue("gps", _prefs.gps_enabled?"1":"0"); + if (sensors.getSettingByKey("gps_interval") != nullptr) { + char interval[11]; + snprintf(interval, sizeof(interval), "%lu", (unsigned long)_prefs.gps_interval); + sensors.setSettingValue("gps_interval", interval); + } + _prefs.getGpsPrefs()->apply(sensors); } #endif }; diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index 613f2578..ed1b5199 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -349,65 +349,11 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re *(dp-1) = 0; // remove last CR } } + } else if (_prefs->getGpsPrefs()->handleCommand(command, *_sensors, reply)) { + if (_prefs->getGpsPrefs()->isDirty()) savePrefs(); } else if (memcmp(command, "region", 6) == 0) { handleRegionCmd(command, reply); #if ENV_INCLUDE_GPS == 1 - } else if (memcmp(command, "gps on", 6) == 0) { - if (_sensors->setSettingValue("gps", "1")) { - _prefs->gps_enabled = 1; - savePrefs(); - strcpy(reply, "ok"); - } else { - strcpy(reply, "gps toggle not found"); - } - } else if (memcmp(command, "gps off", 7) == 0) { - if (_sensors->setSettingValue("gps", "0")) { - _prefs->gps_enabled = 0; - savePrefs(); - strcpy(reply, "ok"); - } else { - strcpy(reply, "gps toggle not found"); - } - } else if (memcmp(command, "gps period ", 11) == 0) { - char* end = nullptr; - unsigned long period = strtoul(command + 11, &end, 10); - if (command[11] == '\0' || *end != '\0' || period > 86400) { - strcpy(reply, "gps period must be 0-86400 seconds"); - } else { - char period_value[11]; - snprintf(period_value, sizeof(period_value), "%lu", period); - if (_sensors->setSettingValue("gps_period", period_value) && - _sensors->setSettingValue("gps", "1")) { - _prefs->gps_period = (uint32_t)period; - _prefs->gps_enabled = 1; - savePrefs(); - strcpy(reply, "ok"); - } else { - strcpy(reply, "gps provider not found"); - } - } - } else if (memcmp(command, "gps sync", 8) == 0) { - LocationProvider * l = _sensors->getLocationProvider(); - if (l != NULL) { - l->syncTime(); - strcpy(reply, "ok"); - } else { - strcpy(reply, "gps provider not found"); - } - } else if (strcmp(command, "gps diag") == 0) { - LocationProvider * l = _sensors->getLocationProvider(); - if (l != NULL) { - const char* setting = _sensors->getSettingByKey("gps"); - bool requested = setting != NULL && strcmp(setting, "1") == 0; - int prefix_len = snprintf(reply, 160, "req:%u ", requested ? 1U : 0U); - if (prefix_len < 0 || prefix_len >= 160) { - reply[159] = 0; - } else { - l->formatDiagnostics(reply + prefix_len, 160 - prefix_len); - } - } else { - strcpy(reply, "gps provider not found"); - } } else if (memcmp(command, "gps setloc", 10) == 0) { _prefs->node_lat = _sensors->node_lat; _prefs->node_lon = _sensors->node_lon; @@ -443,24 +389,6 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re } else { strcpy(reply, "error"); } - } else if (memcmp(command, "gps", 3) == 0) { - LocationProvider * l = _sensors->getLocationProvider(); - if (l != NULL) { - bool enabled = l->isEnabled(); // is EN pin on ? - bool fix = l->isValid(); // has fix ? - int sats = l->satellitesCount(); - bool active = !strcmp(_sensors->getSettingByKey("gps"), "1"); - if (enabled) { - sprintf(reply, "on, %s, %s, %d sats", - active?"active":"deactivated", - fix?"fix":"no fix", - sats); - } else { - strcpy(reply, "off"); - } - } else { - strcpy(reply, "Can't find GPS"); - } #endif } else if (memcmp(command, "powersaving on", 14) == 0) { #if defined(NRF52_PLATFORM) diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index 06778a60..33e27d2d 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #if defined(WITH_RS232_BRIDGE) || defined(WITH_ESPNOW_BRIDGE) @@ -153,7 +154,7 @@ private: }; BridgePrefs bridge; - class GPSPrefs : public ConfigSerializer { + class GPSPrefs : public CommonGpsPrefs { NodePrefs* _parent; protected: void structure() override { @@ -164,6 +165,10 @@ private: } public: GPSPrefs(NodePrefs* parent) : _parent(parent) { } + bool isGpsEnabled() const override { return _parent->gps_enabled; } + void setGpsEnabled(bool enabled) override { _parent->gps_enabled = enabled; markDirty(); } + uint32_t getGpsPeriod() const override { return _parent->gps_period; } + void setGpsPeriod(uint32_t seconds) override { _parent->gps_period = seconds; markDirty(); } }; GPSPrefs gps; @@ -236,10 +241,11 @@ public: } CommonRadioPrefs* getRadioPrefs() { return &radio; } + CommonGpsPrefs* getGpsPrefs() { return &gps; } KeyValueStore* getCustom() { return &custom; } - bool isDirty() const override { return ConfigSerializer::isDirty() || radio.isDirty() || custom.isDirty(); } - void clearDirty() override { ConfigSerializer::clearDirty(); radio.clearDirty(); custom.clearDirty(); } + bool isDirty() const override { return ConfigSerializer::isDirty() || radio.isDirty() || gps.isDirty() || custom.isDirty(); } + void clearDirty() override { ConfigSerializer::clearDirty(); radio.clearDirty(); gps.clearDirty(); custom.clearDirty(); } }; class CommonCLICallbacks { diff --git a/src/helpers/CommonGpsPrefs.cpp b/src/helpers/CommonGpsPrefs.cpp new file mode 100644 index 00000000..fe29c0c0 --- /dev/null +++ b/src/helpers/CommonGpsPrefs.cpp @@ -0,0 +1,110 @@ +#include "CommonGpsPrefs.h" +#include + +bool CommonGpsPrefs::setGpsEnabled(SensorManager& sensors, bool enabled) { + LocationProvider* location = sensors.getLocationProvider(); + if (location == nullptr) return false; + location->setGpsOn(enabled); + setGpsEnabled(enabled); + return true; +} + +bool CommonGpsPrefs::setGpsPeriod(SensorManager& sensors, uint32_t seconds) { + LocationProvider* location = sensors.getLocationProvider(); + if (location == nullptr) return false; + location->setGpsPeriod(seconds); + setGpsPeriod(seconds); + return true; +} + +bool CommonGpsPrefs::apply(SensorManager& sensors) const { + LocationProvider* location = sensors.getLocationProvider(); + if (location == nullptr) return false; + location->setGpsPeriod(getGpsPeriod()); + location->setGpsOn(isGpsEnabled()); + return true; +} + +bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors, char* reply) { + if (strcmp(command, "gps") == 0) { + LocationProvider* location = sensors.getLocationProvider(); + char mode[32]; + if (getGpsPeriod() == 0) { + strcpy(mode, "tracking"); + } else { + snprintf(mode, sizeof(mode), "periodic (%lus)", + (unsigned long)getGpsPeriod()); + } + if (location == nullptr) { + strcpy(reply, "gps provider not found"); + } else if (location->isEnabled()) { + sprintf(reply, "on, %s, %s, %d sats, mode: %s", + isGpsEnabled() ? "active" : "deactivated", + location->isValid() ? "fix" : "no fix", + location->satellitesCount(), mode); + } else { + snprintf(reply, 160, "off, mode: %s", mode); + } + return true; + } + if (memcmp(command, "gps on", 6) == 0) { + if (!setGpsEnabled(sensors, true)) { + strcpy(reply, "gps toggle not found"); + } else { + strcpy(reply, "ok"); + } + return true; + } + if (memcmp(command, "gps off", 7) == 0) { + if (!setGpsEnabled(sensors, false)) { + strcpy(reply, "gps toggle not found"); + } else { + strcpy(reply, "ok"); + } + return true; + } + if (strcmp(command, "gps period") == 0) { + if (getGpsPeriod() == 0) { + strcpy(reply, "> 0 (continuous)"); + } else { + sprintf(reply, "> %lu", (unsigned long)getGpsPeriod()); + } + return true; + } + if (memcmp(command, "gps period ", 11) == 0) { + char* end = nullptr; + unsigned long period = strtoul(command + 11, &end, 10); + if (command[11] == '\0' || *end != '\0' || period > 86400) { + strcpy(reply, "gps period must be 0-86400 seconds"); + } else { + if (!setGpsPeriod(sensors, (uint32_t)period) || + !setGpsEnabled(sensors, true)) { + strcpy(reply, "gps provider not found"); + } else { + strcpy(reply, "ok"); + } + } + return true; + } + if (memcmp(command, "gps sync", 8) == 0) { + LocationProvider* location = sensors.getLocationProvider(); + if (location == nullptr) { + strcpy(reply, "gps provider not found"); + } else { + location->syncTime(); + strcpy(reply, "ok"); + } + return true; + } + if (strcmp(command, "gps diag") == 0) { + LocationProvider* location = sensors.getLocationProvider(); + if (location == nullptr) { + strcpy(reply, "gps provider not found"); + } else { + int prefix = snprintf(reply, 160, "req:%u ", isGpsEnabled() ? 1U : 0U); + if (prefix >= 0 && prefix < 160) location->formatDiagnostics(reply + prefix, 160 - prefix); + } + return true; + } + return false; +} diff --git a/src/helpers/CommonGpsPrefs.h b/src/helpers/CommonGpsPrefs.h new file mode 100644 index 00000000..25cce1b1 --- /dev/null +++ b/src/helpers/CommonGpsPrefs.h @@ -0,0 +1,26 @@ +#pragma once + +#include "ConfigSerializer.h" +#include "SensorManager.h" + +class CommonGpsPrefs : public ConfigSerializer { + bool _is_dirty = false; + +protected: + CommonGpsPrefs() = default; + +public: + void markDirty() { _is_dirty = true; } + void clearDirty() { _is_dirty = false; } + bool isDirty() const { return _is_dirty; } + + virtual bool isGpsEnabled() const = 0; + virtual void setGpsEnabled(bool enabled) = 0; + virtual uint32_t getGpsPeriod() const = 0; + virtual void setGpsPeriod(uint32_t seconds) = 0; + + bool setGpsEnabled(SensorManager& sensors, bool enabled); + bool setGpsPeriod(SensorManager& sensors, uint32_t seconds); + bool apply(SensorManager& sensors) const; + bool handleCommand(const char* command, SensorManager& sensors, char* reply); +}; diff --git a/src/helpers/sensors/EnvironmentSensorManager.cpp b/src/helpers/sensors/EnvironmentSensorManager.cpp index 2861fac2..2cf8b66b 100644 --- a/src/helpers/sensors/EnvironmentSensorManager.cpp +++ b/src/helpers/sensors/EnvironmentSensorManager.cpp @@ -650,7 +650,7 @@ bool EnvironmentSensorManager::querySensors(uint8_t requester_permissions, Cayen int EnvironmentSensorManager::getNumSettings() const { int settings = 0; #if ENV_INCLUDE_GPS - if (gps_detected) settings += 2; // GPS enablement and periodic acquisition + if (gps_detected) settings += 1; // GPS enablement #endif return settings; } @@ -661,9 +661,6 @@ const char* EnvironmentSensorManager::getSettingName(int i) const { if (gps_detected && i == settings++) { return "gps"; } - if (gps_detected && i == settings++) { - return "gps_period"; - } #endif return NULL; } @@ -674,12 +671,6 @@ const char* EnvironmentSensorManager::getSettingValue(int i) const { if (gps_detected && i == settings++) { return _location->isGpsOn() ? "1" : "0"; } - if (gps_detected && i == settings++) { - static char period[11]; - snprintf(period, sizeof(period), "%lu", - (unsigned long)_location->getGpsPeriod()); - return period; - } #endif return NULL; } @@ -694,13 +685,6 @@ bool EnvironmentSensorManager::setSettingValue(const char* name, const char* val } return true; } - if (gps_detected && strcmp(name, "gps_period") == 0) { - char* end = nullptr; - unsigned long period = strtoul(value, &end, 10); - if (*value == '\0' || *end != '\0' || period > 86400) return false; - _location->setGpsPeriod((uint32_t)period); - return true; - } if (strcmp(name, "gps_interval") == 0) { uint32_t interval_seconds = atoi(value); gps_update_interval_sec = interval_seconds > 0 ? interval_seconds : 1; diff --git a/variants/gat562_mesh_evb_pro/target.cpp b/variants/gat562_mesh_evb_pro/target.cpp index 8dc203de..0aac297a 100644 --- a/variants/gat562_mesh_evb_pro/target.cpp +++ b/variants/gat562_mesh_evb_pro/target.cpp @@ -26,7 +26,8 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; diff --git a/variants/gat562_mesh_tracker_pro/target.cpp b/variants/gat562_mesh_tracker_pro/target.cpp index d16b4538..225aa161 100644 --- a/variants/gat562_mesh_tracker_pro/target.cpp +++ b/variants/gat562_mesh_tracker_pro/target.cpp @@ -27,7 +27,8 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; diff --git a/variants/heltec_mesh_solar/target.cpp b/variants/heltec_mesh_solar/target.cpp index 94e2684c..9468f8fc 100644 --- a/variants/heltec_mesh_solar/target.cpp +++ b/variants/heltec_mesh_solar/target.cpp @@ -11,7 +11,8 @@ WRAPPER_CLASS radio_driver(radio, board); VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); SolarSensorManager sensors = SolarSensorManager(nmea); SolarExternalWatchdog external_watchdog; @@ -30,17 +31,11 @@ mesh::LocalIdentity radio_new_identity() { } void SolarSensorManager::start_gps() { - if (!gps_active) { - gps_active = true; - _location->begin(); - } + _location->setGpsOn(true); } void SolarSensorManager::stop_gps() { - if (gps_active) { - gps_active = false; - _location->stop(); - } + _location->setGpsOn(false); } bool SolarSensorManager::begin() { @@ -91,7 +86,7 @@ const char* SolarSensorManager::getSettingName(int i) const { const char* SolarSensorManager::getSettingValue(int i) const { if (gps_detected && i == 0) { - return gps_active ? "1" : "0"; + return _location->isGpsOn() ? "1" : "0"; } return NULL; } diff --git a/variants/heltec_mesh_solar/target.h b/variants/heltec_mesh_solar/target.h index 8e13a960..97550a00 100644 --- a/variants/heltec_mesh_solar/target.h +++ b/variants/heltec_mesh_solar/target.h @@ -14,7 +14,6 @@ #endif class SolarSensorManager : public SensorManager { - bool gps_active = false; bool gps_detected = false; LocationProvider* _location; diff --git a/variants/heltec_rc32/target.cpp b/variants/heltec_rc32/target.cpp index cc7e4deb..e095baab 100644 --- a/variants/heltec_rc32/target.cpp +++ b/variants/heltec_rc32/target.cpp @@ -20,7 +20,9 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock, PIN_GPS_RESET, PIN_GPS_EN); + NmeaGpsDriver gps_driver(Serial1, PIN_GPS_TX, PIN_GPS_RX, PIN_GPS_RESET, + PIN_GPS_EN); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; diff --git a/variants/heltec_t096/target.cpp b/variants/heltec_t096/target.cpp index 30509977..d0c2c07e 100644 --- a/variants/heltec_t096/target.cpp +++ b/variants/heltec_t096/target.cpp @@ -22,7 +22,9 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock, GPS_RESET, GPS_EN, &board.periph_power); +NmeaGpsDriver gps_driver(Serial1, PIN_GPS_TX, PIN_GPS_RX, GPS_RESET, GPS_EN, + &board.periph_power); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; diff --git a/variants/heltec_t1/target.cpp b/variants/heltec_t1/target.cpp index af771582..1d9c8baa 100644 --- a/variants/heltec_t1/target.cpp +++ b/variants/heltec_t1/target.cpp @@ -21,7 +21,9 @@ VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock, GPS_RESET, GPS_EN, &board.periph_power); +NmeaGpsDriver gps_driver(Serial1, PIN_GPS_TX, PIN_GPS_RX, GPS_RESET, GPS_EN, + &board.periph_power); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; diff --git a/variants/heltec_t114/target.cpp b/variants/heltec_t114/target.cpp index 90a902f8..dc62f83b 100644 --- a/variants/heltec_t114/target.cpp +++ b/variants/heltec_t114/target.cpp @@ -22,7 +22,8 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; diff --git a/variants/heltec_tower_v2/target.cpp b/variants/heltec_tower_v2/target.cpp index ec8e7a17..ae627a0b 100644 --- a/variants/heltec_tower_v2/target.cpp +++ b/variants/heltec_tower_v2/target.cpp @@ -12,7 +12,8 @@ WRAPPER_CLASS radio_driver(radio, board); VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); TowerV2ExternalWatchdog external_watchdog; diff --git a/variants/heltec_tracker/target.cpp b/variants/heltec_tracker/target.cpp index f32c41ff..5903f659 100644 --- a/variants/heltec_tracker/target.cpp +++ b/variants/heltec_tracker/target.cpp @@ -17,7 +17,20 @@ WRAPPER_CLASS radio_driver(radio, board); ESP32RTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); // GPS_EN (GPIO35) drives N-ch MOSFET → P-ch high-side switch; GPS_RESET (GPIO36) active LOW -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock, GPS_RESET, GPS_EN, &board.periph_power); +class HeltecTrackerGpsDriver : public NmeaGpsDriver { +public: + HeltecTrackerGpsDriver() + : NmeaGpsDriver(Serial1, PIN_GPS_TX, PIN_GPS_RX, GPS_RESET, GPS_EN, + &board.periph_power) {} + + void begin() override { + NmeaGpsDriver::begin(); + sendNmeaSentence("$CFGSYS,h35155*68"); + } +}; + +HeltecTrackerGpsDriver gps_driver; +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); HWTSensorManager sensors = HWTSensorManager(nmea); #ifdef DISPLAY_CLASS @@ -43,18 +56,11 @@ mesh::LocalIdentity radio_new_identity() { } void HWTSensorManager::start_gps() { - if (!gps_active) { - _location->begin(); // Claims periph_power via RefCountedDigitalPin - gps_active = true; - Serial1.println("$CFGSYS,h35155*68"); // Configure GPS for all constellations - } + _location->setGpsOn(true); } void HWTSensorManager::stop_gps() { - if (gps_active) { - gps_active = false; - _location->stop(); // Releases periph_power via RefCountedDigitalPin - } + _location->setGpsOn(false); } bool HWTSensorManager::begin() { @@ -76,7 +82,7 @@ void HWTSensorManager::loop() { _location->loop(); if (millis() > next_gps_update) { - if (gps_active && _location->isValid()) { + if (_location->isEnabled() && _location->isValid()) { node_lat = ((double)_location->getLatitude())/1000000.; node_lon = ((double)_location->getLongitude())/1000000.; node_altitude = ((double)_location->getAltitude()) / 1000.0; @@ -93,7 +99,7 @@ const char* HWTSensorManager::getSettingName(int i) const { } const char* HWTSensorManager::getSettingValue(int i) const { if (i == 0) { - return gps_active ? "1" : "0"; + return _location->isGpsOn() ? "1" : "0"; } return NULL; } diff --git a/variants/heltec_tracker/target.h b/variants/heltec_tracker/target.h index 2931eda8..f6fea637 100644 --- a/variants/heltec_tracker/target.h +++ b/variants/heltec_tracker/target.h @@ -14,7 +14,6 @@ #endif class HWTSensorManager : public SensorManager { - bool gps_active = false; LocationProvider * _location; void start_gps(); diff --git a/variants/heltec_tracker_v2/target.cpp b/variants/heltec_tracker_v2/target.cpp index c48a29f6..12b01bcc 100644 --- a/variants/heltec_tracker_v2/target.cpp +++ b/variants/heltec_tracker_v2/target.cpp @@ -17,7 +17,9 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock, GPS_RESET, GPS_EN, &board.periph_power); + NmeaGpsDriver gps_driver(Serial1, PIN_GPS_TX, PIN_GPS_RX, GPS_RESET, GPS_EN, + &board.periph_power); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; @@ -43,4 +45,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/heltec_v3/target.cpp b/variants/heltec_v3/target.cpp index e6af466a..f2f712eb 100644 --- a/variants/heltec_v3/target.cpp +++ b/variants/heltec_v3/target.cpp @@ -17,7 +17,8 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; @@ -43,4 +44,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/heltec_v4/target.cpp b/variants/heltec_v4/target.cpp index 8483f276..0f87c6b3 100644 --- a/variants/heltec_v4/target.cpp +++ b/variants/heltec_v4/target.cpp @@ -17,7 +17,8 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; @@ -43,4 +44,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/heltec_v4_r8/target.cpp b/variants/heltec_v4_r8/target.cpp index 0b38531e..27e9f468 100644 --- a/variants/heltec_v4_r8/target.cpp +++ b/variants/heltec_v4_r8/target.cpp @@ -17,7 +17,9 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock, GPS_RESET, GPS_EN, &board.periph_power); + NmeaGpsDriver gps_driver(Serial1, PIN_GPS_TX, PIN_GPS_RX, GPS_RESET, GPS_EN, + &board.periph_power); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; diff --git a/variants/keepteen_lt1/target.cpp b/variants/keepteen_lt1/target.cpp index f1879ac5..dd7c6741 100644 --- a/variants/keepteen_lt1/target.cpp +++ b/variants/keepteen_lt1/target.cpp @@ -12,7 +12,8 @@ VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; @@ -34,4 +35,3 @@ mesh::LocalIdentity radio_new_identity() { return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/lilygo_t_impulse_plus/target.cpp b/variants/lilygo_t_impulse_plus/target.cpp index 87b3dbfe..b6b0c82c 100644 --- a/variants/lilygo_t_impulse_plus/target.cpp +++ b/variants/lilygo_t_impulse_plus/target.cpp @@ -13,7 +13,8 @@ VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #ifdef ENV_INCLUDE_GPS -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors = EnvironmentSensorManager(); @@ -34,4 +35,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/lilygo_tbeam_1w/target.cpp b/variants/lilygo_tbeam_1w/target.cpp index 6755df09..db11747f 100644 --- a/variants/lilygo_tbeam_1w/target.cpp +++ b/variants/lilygo_tbeam_1w/target.cpp @@ -19,7 +19,8 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; @@ -47,4 +48,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); } - diff --git a/variants/lilygo_tbeam_SX1262/target.cpp b/variants/lilygo_tbeam_SX1262/target.cpp index 6e5292a7..b97dda41 100644 --- a/variants/lilygo_tbeam_SX1262/target.cpp +++ b/variants/lilygo_tbeam_SX1262/target.cpp @@ -17,7 +17,8 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; @@ -38,4 +39,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/lilygo_tbeam_SX1276/target.cpp b/variants/lilygo_tbeam_SX1276/target.cpp index 30f23dab..be7c8287 100644 --- a/variants/lilygo_tbeam_SX1276/target.cpp +++ b/variants/lilygo_tbeam_SX1276/target.cpp @@ -17,7 +17,8 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; diff --git a/variants/lilygo_tbeam_supreme_SX1262/target.cpp b/variants/lilygo_tbeam_supreme_SX1262/target.cpp index a0b765bf..86b9116c 100644 --- a/variants/lilygo_tbeam_supreme_SX1262/target.cpp +++ b/variants/lilygo_tbeam_supreme_SX1262/target.cpp @@ -19,7 +19,8 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; @@ -35,4 +36,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/lilygo_tdeck/target.cpp b/variants/lilygo_tdeck/target.cpp index d20c56a3..4084a73b 100644 --- a/variants/lilygo_tdeck/target.cpp +++ b/variants/lilygo_tdeck/target.cpp @@ -14,7 +14,8 @@ WRAPPER_CLASS radio_driver(radio, board); ESP32RTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); -MicroNMEALocationProvider gps(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider gps(gps_driver, &rtc_clock); EnvironmentSensorManager sensors(gps); #ifdef DISPLAY_CLASS diff --git a/variants/lilygo_techo_card/target.cpp b/variants/lilygo_techo_card/target.cpp index 8bbe0363..ea74bd0c 100644 --- a/variants/lilygo_techo_card/target.cpp +++ b/variants/lilygo_techo_card/target.cpp @@ -13,7 +13,8 @@ VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #ifdef ENV_INCLUDE_GPS -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors = EnvironmentSensorManager(); diff --git a/variants/lilygo_techo_lite/target.cpp b/variants/lilygo_techo_lite/target.cpp index 95a62fcd..6d199fff 100644 --- a/variants/lilygo_techo_lite/target.cpp +++ b/variants/lilygo_techo_lite/target.cpp @@ -13,7 +13,8 @@ VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #ifdef ENV_INCLUDE_GPS -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors = EnvironmentSensorManager(); @@ -35,4 +36,3 @@ mesh::LocalIdentity radio_new_identity() { return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/meshadventurer/target.cpp b/variants/meshadventurer/target.cpp index 8795a4cb..faf2e30e 100644 --- a/variants/meshadventurer/target.cpp +++ b/variants/meshadventurer/target.cpp @@ -11,7 +11,8 @@ WRAPPER_CLASS radio_driver(radio, board); ESP32RTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); MASensorManager sensors = MASensorManager(nmea); #ifdef DISPLAY_CLASS @@ -37,17 +38,13 @@ mesh::LocalIdentity radio_new_identity() { void MASensorManager::start_gps() { - if(!gps_active) { - MESH_DEBUG_PRINTLN("starting GPS"); - gps_active = true; - } + MESH_DEBUG_PRINTLN("starting GPS"); + _location->setGpsOn(true); } void MASensorManager::stop_gps() { - if(gps_active) { - MESH_DEBUG_PRINTLN("stopping GPS"); - gps_active = false; - } + MESH_DEBUG_PRINTLN("stopping GPS"); + _location->setGpsOn(false); } bool MASensorManager::begin() { @@ -67,7 +64,7 @@ bool MASensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& te void MASensorManager::loop() { static long next_gps_update = 0; _location->loop(); - if(millis() > next_gps_update && gps_active) { + if(millis() > next_gps_update && _location->isEnabled()) { if(_location->isValid()) { node_lat = ((double)_location->getLatitude()) / 1000000.; node_lon = ((double)_location->getLongitude()) / 1000000.; @@ -85,7 +82,7 @@ const char* MASensorManager::getSettingName(int i) const { } const char* MASensorManager::getSettingValue(int i) const { if(i == 0) { - return gps_active ? "1" : "0"; + return _location->isGpsOn() ? "1" : "0"; } return NULL; } diff --git a/variants/meshadventurer/target.h b/variants/meshadventurer/target.h index 0749565d..9581d422 100644 --- a/variants/meshadventurer/target.h +++ b/variants/meshadventurer/target.h @@ -15,8 +15,7 @@ #endif class MASensorManager : public SensorManager { - bool gps_active = false; - LocationProvider * _location; + LocationProvider* _location; void start_gps(); void stop_gps(); diff --git a/variants/meshnology_w12/target.cpp b/variants/meshnology_w12/target.cpp index bf04ba36..a3d6089f 100644 --- a/variants/meshnology_w12/target.cpp +++ b/variants/meshnology_w12/target.cpp @@ -17,7 +17,8 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; @@ -70,4 +71,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/meshtracker_x1/target.cpp b/variants/meshtracker_x1/target.cpp index 5b90d660..2dee0ebd 100644 --- a/variants/meshtracker_x1/target.cpp +++ b/variants/meshtracker_x1/target.cpp @@ -1,7 +1,7 @@ #include #include "target.h" +#include #include -#include MeshTrackerX1Board board; @@ -10,7 +10,8 @@ RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BU WRAPPER_CLASS radio_driver(radio, board); VolatileRTCClock rtc_clock; -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +AirohaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); MeshTrackerX1SensorManager sensors = MeshTrackerX1SensorManager(nmea); #ifdef DISPLAY_CLASS @@ -27,28 +28,16 @@ mesh::LocalIdentity radio_new_identity() { } void MeshTrackerX1SensorManager::start_gps() { - gps_active = true; - digitalWrite(GPS_EN, HIGH); - delay(10); - digitalWrite(GPS_RTC_INT, HIGH); - delay(5); - digitalWrite(GPS_RTC_INT, LOW); + _nmea->setGpsOn(true); } void MeshTrackerX1SensorManager::stop_gps() { - gps_active = false; - digitalWrite(GPS_VRTC_EN, HIGH); // keep GPS RTC alive for faster fix on wake - digitalWrite(GPS_RTC_INT, LOW); // make sure this is LOW so we can pulse it to wake - airohaEnterSleep(_nmea); - digitalWrite(GPS_EN, LOW); + _nmea->setGpsOn(false); } bool MeshTrackerX1SensorManager::begin() { // init GPS - Serial1.begin(GPS_BAUD_RATE); - digitalWrite(GPS_RESET, HIGH); - delay(10); - digitalWrite(GPS_RESET, LOW); + gps_driver.initialize(); // init SPA06-003 barometer baro_ok = spa06.begin(SPA06_003_DEFAULT_ADDR, &Wire) || spa06.begin(0x76, &Wire); @@ -81,7 +70,7 @@ void MeshTrackerX1SensorManager::loop() { _nmea->loop(); if (millis() > next_gps_update) { - if (gps_active && _nmea->isValid()) { + if (_nmea->isEnabled() && _nmea->isValid()) { node_lat = ((double)_nmea->getLatitude())/1000000.; node_lon = ((double)_nmea->getLongitude())/1000000.; node_altitude = ((double)_nmea->getAltitude()) / 1000.0; @@ -97,7 +86,7 @@ const char* MeshTrackerX1SensorManager::getSettingName(int i) const { } const char* MeshTrackerX1SensorManager::getSettingValue(int i) const { if (i == 0) { - return gps_active ? "1" : "0"; + return _nmea->isGpsOn() ? "1" : "0"; } return NULL; } diff --git a/variants/meshtracker_x1/target.h b/variants/meshtracker_x1/target.h index 9741efb6..e678e29c 100644 --- a/variants/meshtracker_x1/target.h +++ b/variants/meshtracker_x1/target.h @@ -14,13 +14,11 @@ #endif class MeshTrackerX1SensorManager: public SensorManager { - bool gps_active = false; bool baro_ok = false; Adafruit_SPA06_003 spa06; LocationProvider * _nmea; void start_gps(); - void sleep_gps(); void stop_gps(); public: MeshTrackerX1SensorManager(LocationProvider &nmea): _nmea(&nmea) { } diff --git a/variants/minewsemi_me25ls01/target.cpp b/variants/minewsemi_me25ls01/target.cpp index 41fc71a7..8d9a6e07 100644 --- a/variants/minewsemi_me25ls01/target.cpp +++ b/variants/minewsemi_me25ls01/target.cpp @@ -10,7 +10,8 @@ VolatileRTCClock rtc_clock; extern EnvironmentSensorManager sensors; #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; diff --git a/variants/muzi_base/target.cpp b/variants/muzi_base/target.cpp index 5bd846c9..b65bafa1 100644 --- a/variants/muzi_base/target.cpp +++ b/variants/muzi_base/target.cpp @@ -13,7 +13,8 @@ VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); MuziBaseSensorManager sensors = MuziBaseSensorManager(nmea); #else MuziBaseSensorManager sensors; @@ -38,13 +39,7 @@ static void muziPowerOff() { bool MuziBaseSensorManager::begin() { pwr_btn.begin(); - bool ok = EnvironmentSensorManager::begin(); -#if ENV_INCLUDE_GPS - pinMode(PIN_GPS_SWITCH, INPUT); - _last_gps_sw = digitalRead(PIN_GPS_SWITCH); // initial gps state from the switch - if (_last_gps_sw == HIGH) start_gps(); else stop_gps(); -#endif - return ok; + return EnvironmentSensorManager::begin(); } void MuziBaseSensorManager::loop() { @@ -55,17 +50,6 @@ void MuziBaseSensorManager::loop() { _pwr_last_click = t; if (++_pwr_clicks >= USER_BTN_POWER_OFF_CLICKS) muziPowerOff(); } -#if ENV_INCLUDE_GPS - unsigned long now = millis(); - if (now > _next_sw_check) { // check the mode switch ~once a sec - _next_sw_check = now + 1000; - int sw = digitalRead(PIN_GPS_SWITCH); - if (sw != _last_gps_sw) { - _last_gps_sw = sw; - if (sw == HIGH) start_gps(); else stop_gps(); - } - } -#endif EnvironmentSensorManager::loop(); } diff --git a/variants/muzi_base/target.h b/variants/muzi_base/target.h index 45cd595f..3f7dcbf5 100644 --- a/variants/muzi_base/target.h +++ b/variants/muzi_base/target.h @@ -38,14 +38,10 @@ extern AutoDiscoverRTCClock rtc_clock; // muzi sensor manager, polled from main loop via sensors.loop(): // - user button: USER_BTN_POWER_OFF_CLICKS quick presses -> power off (all builds) -// - gps mode switch on/off (superIO only, see thinknode_m1) +// gps mode is now handled by GpsDriver on superIO class MuziBaseSensorManager : public EnvironmentSensorManager { unsigned long _pwr_last_click = 0; uint8_t _pwr_clicks = 0; -#if ENV_INCLUDE_GPS - int _last_gps_sw = -1; - unsigned long _next_sw_check = 0; -#endif public: #if ENV_INCLUDE_GPS MuziBaseSensorManager(LocationProvider& location) : EnvironmentSensorManager(location) {} diff --git a/variants/muzi_base/variant.h b/variants/muzi_base/variant.h index b007c1f8..0c1245a8 100644 --- a/variants/muzi_base/variant.h +++ b/variants/muzi_base/variant.h @@ -162,6 +162,8 @@ static const uint8_t AREF = (PIN_AREF); // not used #define SWITCH_MODE1 (32+9) // P1.09 #define SWITCH_MODE2 (12) // P0.12 #define PIN_GPS_SWITCH SWITCH_MODE2 +#define GPS_SWITCH_IS_HARDWARE (0) +#define GPS_SWITCH_TURNS_GPS_ON (1) //////////////////////////////////////////////////////////////////////////////// // Buzzer diff --git a/variants/muziworks_r1_neo/target.cpp b/variants/muziworks_r1_neo/target.cpp index f667a282..3081de2c 100644 --- a/variants/muziworks_r1_neo/target.cpp +++ b/variants/muziworks_r1_neo/target.cpp @@ -20,7 +20,8 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; diff --git a/variants/nano_g2_ultra/NanoG2UltraGpsDriver.h b/variants/nano_g2_ultra/NanoG2UltraGpsDriver.h new file mode 100644 index 00000000..3772084b --- /dev/null +++ b/variants/nano_g2_ultra/NanoG2UltraGpsDriver.h @@ -0,0 +1,31 @@ +#pragma once + +#include + +class NanoG2UltraGpsDriver : public NmeaGpsDriver { +public: + NanoG2UltraGpsDriver(HardwareSerial& serial) : NmeaGpsDriver(serial) { + pinMode(PIN_GPS_STANDBY, OUTPUT); + digitalWrite(PIN_GPS_STANDBY, LOW); + } + + void begin() override { + if (isEnabled()) return; + + digitalWrite(PIN_GPS_STANDBY, HIGH); + NmeaGpsDriver::begin(); + delay(1000); + } + + void stop() override { + NmeaGpsDriver::stop(); + digitalWrite(PIN_GPS_STANDBY, LOW); + } + + bool detectAtBoot() { + begin(); + const bool wasActive = detect(0); + if (!wasActive) stop(); + return wasActive; + } +}; diff --git a/variants/nano_g2_ultra/target.cpp b/variants/nano_g2_ultra/target.cpp index 69a2772c..ee258f2d 100644 --- a/variants/nano_g2_ultra/target.cpp +++ b/variants/nano_g2_ultra/target.cpp @@ -3,6 +3,7 @@ #include #include #include +#include "NanoG2UltraGpsDriver.h" NanoG2Ultra board; @@ -12,8 +13,10 @@ WRAPPER_CLASS radio_driver(radio, board); VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); -NanoG2UltraSensorManager sensors = NanoG2UltraSensorManager(nmea); +// NanoG2UltraGpsDriver now handles the special init needed for NanoG2 +NanoG2UltraGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); +NanoG2UltraSensorManager sensors = NanoG2UltraSensorManager(nmea, gps_driver); #ifdef DISPLAY_CLASS DISPLAY_CLASS display; @@ -27,35 +30,19 @@ bool radio_init() { void NanoG2UltraSensorManager::start_gps() { MESH_DEBUG_PRINTLN("Starting GPS"); - if (!gps_active) { - digitalWrite(PIN_GPS_STANDBY, HIGH); // Wake GPS from standby - Serial1.setPins(PIN_GPS_TX, PIN_GPS_RX); - Serial1.begin(9600); - MESH_DEBUG_PRINTLN("Waiting for gps to power up"); - delay(1000); - gps_active = true; - } - _location->begin(); + _location->setGpsOn(true); } void NanoG2UltraSensorManager::stop_gps() { MESH_DEBUG_PRINTLN("Stopping GPS"); - if (gps_active) { - digitalWrite(PIN_GPS_STANDBY, LOW); // sleep GPS - gps_active = false; - } - _location->stop(); + _location->setGpsOn(false); } bool NanoG2UltraSensorManager::begin() { - digitalWrite(PIN_GPS_STANDBY, HIGH); // Wake GPS from standby - Serial1.setPins(PIN_GPS_TX, PIN_GPS_RX); - Serial1.begin(9600); MESH_DEBUG_PRINTLN("Checking GPS switch state"); - delay(1000); // Check initial switch state to determine if GPS should be active - if (Serial1.available() > 0) { + if (_gps_driver->detectAtBoot()) { MESH_DEBUG_PRINTLN("GPS was on at boot, GPS enabled"); start_gps(); } else { @@ -75,14 +62,14 @@ bool NanoG2UltraSensorManager::querySensors(uint8_t requester_permissions, Cayen void NanoG2UltraSensorManager::loop() { static long next_gps_update = 0; - if (!gps_active) { + if (!_location->isEnabled()) { return; // GPS is not active, skip further processing } _location->loop(); if (millis() > next_gps_update) { - if (_location->isValid()) { + if (_location->isEnabled() && _location->isValid()) { node_lat = ((double)_location->getLatitude()) / 1000000.; node_lon = ((double)_location->getLongitude()) / 1000000.; node_altitude = ((double)_location->getAltitude()) / 1000.0; @@ -105,7 +92,7 @@ const char *NanoG2UltraSensorManager::getSettingName(int i) const { const char *NanoG2UltraSensorManager::getSettingValue(int i) const { if (i == 0) { - return gps_active ? "1" : "0"; + return _location->isGpsOn() ? "1" : "0"; } return NULL; } diff --git a/variants/nano_g2_ultra/target.h b/variants/nano_g2_ultra/target.h index 5c6ebee1..d540b9ec 100644 --- a/variants/nano_g2_ultra/target.h +++ b/variants/nano_g2_ultra/target.h @@ -14,15 +14,18 @@ #endif #include +class NanoG2UltraGpsDriver; + class NanoG2UltraSensorManager : public SensorManager { - bool gps_active = false; LocationProvider *_location; + NanoG2UltraGpsDriver *_gps_driver; void start_gps(); void stop_gps(); public: - NanoG2UltraSensorManager(LocationProvider &location) : _location(&location) {} + NanoG2UltraSensorManager(LocationProvider &location, NanoG2UltraGpsDriver &gps_driver) + : _location(&location), _gps_driver(&gps_driver) {} bool begin() override; bool querySensors(uint8_t requester_permissions, CayenneLPP &telemetry) override; void loop() override; @@ -44,4 +47,3 @@ extern MomentaryButton user_btn; bool radio_init(); mesh::LocalIdentity radio_new_identity(); - diff --git a/variants/promicro/target.cpp b/variants/promicro/target.cpp index b5a74c90..5758d9cd 100644 --- a/variants/promicro/target.cpp +++ b/variants/promicro/target.cpp @@ -12,7 +12,8 @@ VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; @@ -34,4 +35,3 @@ mesh::LocalIdentity radio_new_identity() { return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/rak3112/target.cpp b/variants/rak3112/target.cpp index 9df1bf8f..9b1d11a0 100644 --- a/variants/rak3112/target.cpp +++ b/variants/rak3112/target.cpp @@ -17,7 +17,8 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; @@ -43,4 +44,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/rak_wismesh_tag/target.cpp b/variants/rak_wismesh_tag/target.cpp index cdf81f4f..3e80ae63 100644 --- a/variants/rak_wismesh_tag/target.cpp +++ b/variants/rak_wismesh_tag/target.cpp @@ -22,7 +22,8 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; @@ -37,4 +38,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/sensecap_indicator-espnow/target.cpp b/variants/sensecap_indicator-espnow/target.cpp index c84d1883..4895447c 100644 --- a/variants/sensecap_indicator-espnow/target.cpp +++ b/variants/sensecap_indicator-espnow/target.cpp @@ -8,7 +8,8 @@ ESPNOWRadio radio_driver; ESP32RTCClock rtc_clock; #if defined(ENV_INCLUDE_GPS) -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, (mesh::RTCClock*)&rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, (mesh::RTCClock*)&rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors = EnvironmentSensorManager(); diff --git a/variants/sensecap_solar/target.cpp b/variants/sensecap_solar/target.cpp index 46641517..11586ecd 100644 --- a/variants/sensecap_solar/target.cpp +++ b/variants/sensecap_solar/target.cpp @@ -13,7 +13,8 @@ WRAPPER_CLASS radio_driver(radio, board); VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #ifdef ENV_INCLUDE_GPS -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors = EnvironmentSensorManager(); @@ -29,4 +30,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/station_g2/target.cpp b/variants/station_g2/target.cpp index 8018c20e..f5365eb5 100644 --- a/variants/station_g2/target.cpp +++ b/variants/station_g2/target.cpp @@ -17,7 +17,8 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; @@ -44,4 +45,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/station_g3_esp32/target.cpp b/variants/station_g3_esp32/target.cpp index d3de074b..189169d2 100644 --- a/variants/station_g3_esp32/target.cpp +++ b/variants/station_g3_esp32/target.cpp @@ -17,7 +17,8 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; diff --git a/variants/t1000-e/target.cpp b/variants/t1000-e/target.cpp index 0d7710b3..b96d01f8 100644 --- a/variants/t1000-e/target.cpp +++ b/variants/t1000-e/target.cpp @@ -82,13 +82,11 @@ mesh::LocalIdentity radio_new_identity() { } void T1000SensorManager::start_gps() { - gps_active = true; - _nmea->begin(); + _nmea->setGpsOn(true); } void T1000SensorManager::stop_gps() { - gps_active = false; - _nmea->stop(); + _nmea->setGpsOn(false); } @@ -115,7 +113,7 @@ void T1000SensorManager::loop() { _nmea->loop(); if (millis() > next_gps_update) { - if (gps_active && _nmea->isValid()) { + if (_nmea->isEnabled() && _nmea->isValid()) { node_lat = ((double)_nmea->getLatitude())/1000000.; node_lon = ((double)_nmea->getLongitude())/1000000.; node_altitude = ((double)_nmea->getAltitude()) / 1000.0; @@ -131,7 +129,7 @@ const char* T1000SensorManager::getSettingName(int i) const { } const char* T1000SensorManager::getSettingValue(int i) const { if (i == 0) { - return gps_active ? "1" : "0"; + return _nmea->isGpsOn() ? "1" : "0"; } return NULL; } diff --git a/variants/t1000-e/target.h b/variants/t1000-e/target.h index 4a1a2bb7..68508bcb 100644 --- a/variants/t1000-e/target.h +++ b/variants/t1000-e/target.h @@ -15,7 +15,6 @@ class AirohaGpsDriver; class T1000SensorManager: public SensorManager { - bool gps_active = false; LocationProvider * _nmea; AirohaGpsDriver* _gps_driver; diff --git a/variants/thinknode_m3/target.cpp b/variants/thinknode_m3/target.cpp index 1e6c2ad1..bb5746ca 100644 --- a/variants/thinknode_m3/target.cpp +++ b/variants/thinknode_m3/target.cpp @@ -11,7 +11,8 @@ WRAPPER_CLASS radio_driver(radio, board); VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #ifdef ENV_INCLUDE_GPS -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors = EnvironmentSensorManager(); diff --git a/variants/thinknode_m4/target.cpp b/variants/thinknode_m4/target.cpp index 7da3f400..59a898f5 100644 --- a/variants/thinknode_m4/target.cpp +++ b/variants/thinknode_m4/target.cpp @@ -11,7 +11,8 @@ WRAPPER_CLASS radio_driver(radio, board); VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #ifdef ENV_INCLUDE_GPS -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors = EnvironmentSensorManager(); diff --git a/variants/thinknode_m6/target.cpp b/variants/thinknode_m6/target.cpp index de167194..061f7ad4 100644 --- a/variants/thinknode_m6/target.cpp +++ b/variants/thinknode_m6/target.cpp @@ -12,7 +12,8 @@ WRAPPER_CLASS radio_driver(radio, board); VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #ifdef ENV_INCLUDE_GPS -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors = EnvironmentSensorManager(); @@ -32,4 +33,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/thinknode_m7/target.cpp b/variants/thinknode_m7/target.cpp index f4b898d8..8e7e8a05 100644 --- a/variants/thinknode_m7/target.cpp +++ b/variants/thinknode_m7/target.cpp @@ -12,7 +12,8 @@ ESP32RTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #ifdef ENV_INCLUDE_GPS -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors = EnvironmentSensorManager(); @@ -81,4 +82,3 @@ mesh::LocalIdentity radio_new_identity() { return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/thinknode_m8/target.cpp b/variants/thinknode_m8/target.cpp index 69f3cc52..04496d3f 100644 --- a/variants/thinknode_m8/target.cpp +++ b/variants/thinknode_m8/target.cpp @@ -13,7 +13,8 @@ WRAPPER_CLASS radio_driver(radio, board); VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #ifdef ENV_INCLUDE_GPS -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors = EnvironmentSensorManager(); diff --git a/variants/thinknode_m9/target.cpp b/variants/thinknode_m9/target.cpp index 3cbd7d75..990f0388 100644 --- a/variants/thinknode_m9/target.cpp +++ b/variants/thinknode_m9/target.cpp @@ -11,7 +11,8 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #ifdef ENV_INCLUDE_GPS #include -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors = EnvironmentSensorManager(); diff --git a/variants/wio-tracker-l1-1w/target.cpp b/variants/wio-tracker-l1-1w/target.cpp index 7a573258..cd4b8dac 100644 --- a/variants/wio-tracker-l1-1w/target.cpp +++ b/variants/wio-tracker-l1-1w/target.cpp @@ -13,7 +13,8 @@ VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #ifdef ENV_INCLUDE_GPS -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors = EnvironmentSensorManager(); @@ -37,4 +38,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/wio-tracker-l1/target.cpp b/variants/wio-tracker-l1/target.cpp index 7a573258..cd4b8dac 100644 --- a/variants/wio-tracker-l1/target.cpp +++ b/variants/wio-tracker-l1/target.cpp @@ -13,7 +13,8 @@ VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #ifdef ENV_INCLUDE_GPS -MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); +NmeaGpsDriver gps_driver(Serial1); +MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors = EnvironmentSensorManager(); @@ -37,4 +38,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } - diff --git a/variants/xiao_c3/target.cpp b/variants/xiao_c3/target.cpp index 7f30dc8b..d3bbdf1c 100644 --- a/variants/xiao_c3/target.cpp +++ b/variants/xiao_c3/target.cpp @@ -17,7 +17,8 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); + NmeaGpsDriver gps_driver(Serial1); + MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else EnvironmentSensorManager sensors; @@ -39,4 +40,3 @@ mesh::LocalIdentity radio_new_identity() { RadioNoiseListener rng(radio); return mesh::LocalIdentity(&rng); // create new random identity } - From e58be9ab11b8c7a22b08c19dbb78d7bc895a1cef Mon Sep 17 00:00:00 2001 From: Florent Date: Mon, 2 Nov 2026 12:45:05 -0400 Subject: [PATCH 18/53] gps: cache the last fix in LocationProvider (isValid vs hasValidFix), fix and status commands --- docs/cli_commands.md | 37 +++++++++++-- docs/companion_cli.md | 52 +++++++++++++++++++ src/helpers/CommonGpsPrefs.cpp | 48 ++++++++++++++--- src/helpers/sensors/LocationProvider.h | 21 +++++++- .../sensors/MicroNMEALocationProvider.h | 35 +++++++++---- src/helpers/sensors/RakLocationProvider.h | 48 ++++++++++++----- 6 files changed, 208 insertions(+), 33 deletions(-) diff --git a/docs/cli_commands.md b/docs/cli_commands.md index 9fde4d43..eb8c6b20 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md @@ -999,9 +999,16 @@ region save --- ### GPS (When GPS support is compiled in) -#### View or change GPS state +#### View GPS state **Usage:** - `gps` + +**Note:** Reports the requested state, receiver activity, last-fix status (last fix gives number of sats and how long ago was the fix), and mode. In periodic mode, `waiting` means the receiver is off between acquisitions; the last fix remains available. `off(hw)` means a hardware GPS switch is off. + +--- + +#### Change GPS state +**Usage:** - `gps ` **Parameters:** @@ -1009,9 +1016,25 @@ region save **Default:** `off` -**Note:** Output format: -- `off` when the GPS hardware is disabled -- `on, {active|deactivated}, {fix|no fix}, {sat count} sats` when the GPS hardware is enabled +--- + +#### View or change the GPS acquisition period +**Usage:** +- `gps period` +- `gps period ` + +**Parameters:** +- `seconds`: `0` for continuous tracking, or `1`-`86400` for periodic acquisition + +**Note:** A periodic acquisition stops after receiving a valid fix, then resumes after the configured period. + +--- + +#### View the last GPS fix +**Usage:** +- `gps fix` + +**Note:** Returns the last valid latitude, longitude, altitude, and elapsed time since the fix. Returns `no fix` until a valid fix is received. --- @@ -1021,6 +1044,12 @@ region save --- +#### View GPS diagnostics +**Usage:** +- `gps diag` + +--- + #### Set this node's location based on the GPS coordinates **Usage:** - `gps setloc` diff --git a/docs/companion_cli.md b/docs/companion_cli.md index 31a05584..24843269 100644 --- a/docs/companion_cli.md +++ b/docs/companion_cli.md @@ -7,6 +7,7 @@ Since version 1.18 meshcore companion radio firmware has a CLI interface, which - [Configuration](#configuration) - [System](#system) - [Radio](#radio) + - [GPS](#gps) - [BLE](#ble) - [Wifi](#wifi) - [UI](#ui) @@ -119,6 +120,57 @@ Radio parameters are common to all roles and handled by `CommonRadioPrefs`. --- +### GPS + +#### View GPS state +**Usage:** +- `gps` + +**Note:** Reports the requested state, receiver activity, last-fix status (last fix gives number of sats and how long ago was the fix), and mode. In periodic mode, `waiting` means the receiver is off between acquisitions; the last fix remains available. `off(hw)` means a hardware GPS switch is off. + +--- + +#### Change GPS state +**Usage:** +- `gps ` + +**Parameters:** +- `state`: `on`|`off` + +--- + +#### View or change the GPS acquisition period +**Usage:** +- `gps period` +- `gps period ` + +**Parameters:** +- `seconds`: `0` for continuous tracking, or `1`-`86400` for periodic acquisition + +**Note:** A periodic acquisition stops after receiving a valid fix, then resumes after the configured period. + +--- + +#### View the last GPS fix +**Usage:** +- `gps fix` + +**Note:** Returns the last valid latitude, longitude, altitude, and elapsed time since the fix. Returns `no fix` until a valid fix is received. + +--- + +#### Sync this node's clock with GPS time +**Usage:** +- `gps sync` + +--- + +#### View GPS diagnostics +**Usage:** +- `gps diag` + +--- + #### View or change this node's rx boosted gain mode (SX12xx and LR1110) **Usage:** - `get radio.rxgain` diff --git a/src/helpers/CommonGpsPrefs.cpp b/src/helpers/CommonGpsPrefs.cpp index fe29c0c0..248ae540 100644 --- a/src/helpers/CommonGpsPrefs.cpp +++ b/src/helpers/CommonGpsPrefs.cpp @@ -37,13 +37,49 @@ bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors, } if (location == nullptr) { strcpy(reply, "gps provider not found"); - } else if (location->isEnabled()) { - sprintf(reply, "on, %s, %s, %d sats, mode: %s", - isGpsEnabled() ? "active" : "deactivated", - location->isValid() ? "fix" : "no fix", - location->satellitesCount(), mode); + } else if (location->isGpsOn()) { + const char* activity = "active"; + if (!location->isEnabled()) { + if (location->hasHardwareEnableState() && !location->isHardwareEnabled()) { + activity = "off(hw)"; + } else if (getGpsPeriod() > 0) { + activity = "waiting"; + } else { + activity = "inactive"; + } + } + char fix[32]; + if (location->hasValidFix()) { + snprintf(fix, sizeof(fix), "%ld sats %lus ago", + location->satellitesCount(), (unsigned long)location->getLastFixAge()); + } else { + strcpy(fix, "no fix"); + } + snprintf(reply, 160, "on, %s, %s, mode: %s", activity, fix, mode); + } + return true; + } + if (strcmp(command, "gps fix") == 0) { + LocationProvider* location = sensors.getLocationProvider(); + if (location == nullptr) { + strcpy(reply, "gps provider not found"); + } else if (!location->hasValidFix()) { + strcpy(reply, "no fix"); } else { - snprintf(reply, 160, "off, mode: %s", mode); + const long latitude = location->getLatitude(); + const long longitude = location->getLongitude(); + const uint32_t fixAge = location->getLastFixAge(); + char age[24]; + if (fixAge == UINT32_MAX) { + strcpy(age, "age unavailable"); + } else { + snprintf(age, sizeof(age), "%lus ago", (unsigned long)fixAge); + } + snprintf(reply, 160, "%s%ld.%06ld, %s%ld.%06ld, %ldm, %s", + latitude < 0 ? "-" : "", labs(latitude) / 1000000L, + labs(latitude) % 1000000L, + longitude < 0 ? "-" : "", labs(longitude) / 1000000L, + labs(longitude) % 1000000L, location->getAltitude() / 1000L, age); } return true; } diff --git a/src/helpers/sensors/LocationProvider.h b/src/helpers/sensors/LocationProvider.h index 93cecf5b..dc316e1d 100644 --- a/src/helpers/sensors/LocationProvider.h +++ b/src/helpers/sensors/LocationProvider.h @@ -6,9 +6,22 @@ class LocationProvider { +public: + // Last valid fix, always filled in one go so readers get a consistent snapshot. + struct LastFix { + bool valid = false; + long latitude = 0; + long longitude = 0; + long altitude = 0; + long satellites = 0; + long timestamp = 0; + unsigned long acquiredAt = 0; // millis() when the fix was stored + }; + protected: bool _time_sync_needed = true; bool _gpsOn = false; + LastFix _lastFix; public: virtual ~LocationProvider() = default; @@ -19,7 +32,9 @@ public: virtual long getLongitude() = 0; virtual long getAltitude() = 0; virtual long satellitesCount() = 0; - virtual bool isValid() = 0; + virtual bool isValid() = 0; // current fix is valid + virtual bool hasValidFix() { return getLastFix().valid || isValid(); } // a valid fix was obtained at some point + virtual LastFix getLastFix() const { return _lastFix; } // snapshot of the last valid fix virtual long getTimestamp() = 0; virtual void sendSentence(const char * sentence); virtual bool waitFor(const char* prefix, uint32_t timeout_ms) { return false; } @@ -28,6 +43,10 @@ public: virtual bool keepEnabledAfterDetection() const { return false; } virtual void setGpsPeriod(uint32_t seconds) { } virtual uint32_t getGpsPeriod() const { return 0; } + virtual uint32_t getLastFixAge() const { // in seconds, UINT32_MAX if never + const LastFix f = getLastFix(); + return f.valid ? (uint32_t)(millis() - f.acquiredAt) / 1000UL : UINT32_MAX; + } virtual bool hasHardwareEnableState() const { return false; } virtual bool isHardwareEnabled() const { return true; } virtual bool isGpsOn() const { return _gpsOn; } diff --git a/src/helpers/sensors/MicroNMEALocationProvider.h b/src/helpers/sensors/MicroNMEALocationProvider.h index 2e9ff9cc..cceffd8c 100644 --- a/src/helpers/sensors/MicroNMEALocationProvider.h +++ b/src/helpers/sensors/MicroNMEALocationProvider.h @@ -32,10 +32,8 @@ class MicroNMEALocationProvider : public LocationProvider { uint32_t _stop_calls = 0; unsigned long _last_uart_ms = 0; unsigned long _last_nmea_ms = 0; - unsigned long _last_fix_ms = 0; bool _uart_seen = false; bool _nmea_seen = false; - bool _fix_seen = false; static uint32_t ageMs(unsigned long timestamp) { return (uint32_t)(millis() - timestamp); @@ -64,6 +62,18 @@ class MicroNMEALocationProvider : public LocationProvider { _nextAcquisition = millis() + _gpsPeriodSeconds * 1000UL; } + void saveFix() { + LastFix fix; + fix.latitude = nmea.getLatitude(); + fix.longitude = nmea.getLongitude(); + nmea.getAltitude(fix.altitude); + fix.satellites = nmea.getNumSatellites(); + fix.timestamp = getTimestamp(); + fix.acquiredAt = millis(); + fix.valid = true; + _lastFix = fix; + } + void updateTime() { if (!isValid()) time_valid = 0; @@ -161,17 +171,25 @@ public: LocationProvider::syncTime(); } - long getLatitude() override { return nmea.getLatitude(); } - long getLongitude() override { return nmea.getLongitude(); } + long getLatitude() override { + return nmea.isValid() ? nmea.getLatitude() : _lastFix.latitude; + } + long getLongitude() override { + return nmea.isValid() ? nmea.getLongitude() : _lastFix.longitude; + } long getAltitude() override { + if (!nmea.isValid()) return _lastFix.altitude; long alt = 0; nmea.getAltitude(alt); return alt; } - long satellitesCount() override { return nmea.getNumSatellites(); } + long satellitesCount() override { + return nmea.isValid() ? nmea.getNumSatellites() : _lastFix.satellites; + } bool isValid() override { return nmea.isValid(); } long getTimestamp() override { + if (!nmea.isValid()) return _lastFix.timestamp; DateTime dt(nmea.getYear(), nmea.getMonth(), nmea.getDay(), nmea.getHour(), nmea.getMinute(), nmea.getSecond()); return dt.unixtime(); @@ -228,10 +246,9 @@ public: _nmea_seen = true; const char* messageId = nmea.getMessageID(); - if (parsed && isValid() && + if (parsed && nmea.isValid() && (strcmp(messageId, "GGA") == 0 || strcmp(messageId, "RMC") == 0)) { - _last_fix_ms = millis(); - _fix_seen = true; + saveFix(); if (_acquiringPeriodically) finishPeriodicAcquisition(); } } else { @@ -256,7 +273,7 @@ public: char fix_age[11]; formatAge(uart_age, sizeof(uart_age), _uart_seen, _last_uart_ms); formatAge(nmea_age, sizeof(nmea_age), _nmea_seen, _last_nmea_ms); - formatAge(fix_age, sizeof(fix_age), _fix_seen, _last_fix_ms); + formatAge(fix_age, sizeof(fix_age), _lastFix.valid, _lastFix.acquiredAt); snprintf(out, out_size, "en:%u ub:%lu ua:%s ok:%lu bad:%lu na:%s sat:%ld fix:%u fa:%s bc:%lu sc:%lu", diff --git a/src/helpers/sensors/RakLocationProvider.h b/src/helpers/sensors/RakLocationProvider.h index c909b7c4..db0b957b 100644 --- a/src/helpers/sensors/RakLocationProvider.h +++ b/src/helpers/sensors/RakLocationProvider.h @@ -11,6 +11,18 @@ class RakLocationProvider : public LocationProvider { unsigned long _acquisitionStarted = 0; bool _acquiringPeriodically = false; + void saveRak12500Fix() { + LastFix fix; + fix.latitude = _driver.getLatitude(); + fix.longitude = _driver.getLongitude(); + fix.altitude = _driver.getAltitude(); + fix.satellites = _driver.getSatellites(); + fix.timestamp = _driver.getTimestamp(); + fix.acquiredAt = millis(); + fix.valid = true; + _lastFix = fix; + } + public: RakLocationProvider(RakGpsDriver& driver, mesh::RTCClock* clock = nullptr) : _driver(driver), _nmea(driver.nmeaDriver(), clock) {} @@ -21,27 +33,31 @@ public: } long getLatitude() override { - return _driver.usesRak12500() ? _driver.getLatitude() : _nmea.getLatitude(); + return _driver.usesRak12500() ? _lastFix.latitude : _nmea.getLatitude(); } long getLongitude() override { - return _driver.usesRak12500() ? _driver.getLongitude() : _nmea.getLongitude(); + return _driver.usesRak12500() ? _lastFix.longitude : _nmea.getLongitude(); } long getAltitude() override { - return _driver.usesRak12500() ? _driver.getAltitude() : _nmea.getAltitude(); + return _driver.usesRak12500() ? _lastFix.altitude : _nmea.getAltitude(); } long satellitesCount() override { - return _driver.usesRak12500() ? _driver.getSatellites() : _nmea.satellitesCount(); + return _driver.usesRak12500() ? _lastFix.satellites : _nmea.satellitesCount(); } bool isValid() override { return _driver.usesRak12500() ? _driver.hasFix() : _nmea.isValid(); } + LastFix getLastFix() const override { + return _driver.usesRak12500() ? _lastFix : _nmea.getLastFix(); + } + long getTimestamp() override { - return _driver.usesRak12500() ? _driver.getTimestamp() : _nmea.getTimestamp(); + return _driver.usesRak12500() ? _lastFix.timestamp : _nmea.getTimestamp(); } void sendSentence(const char* sentence) override { @@ -104,18 +120,24 @@ public: void loop() override { if (_driver.usesRak12500()) { - if (_gpsOn && _gpsPeriodSeconds > 0) { - if (!_acquiringPeriodically && - (long)(millis() - _nextAcquisition) >= 0) { - _driver.begin(); - _acquiringPeriodically = true; - _acquisitionStarted = millis(); + if (_gpsOn) { + if (_gpsPeriodSeconds > 0) { + if (!_acquiringPeriodically && + (long)(millis() - _nextAcquisition) >= 0) { + _driver.begin(); + _acquiringPeriodically = true; + _acquisitionStarted = millis(); + } } - if (_acquiringPeriodically && isValid()) { + + const bool hasCurrentFix = _driver.hasFix(); + if (hasCurrentFix) saveRak12500Fix(); + + if (_gpsPeriodSeconds > 0 && _acquiringPeriodically && hasCurrentFix) { _driver.stop(); _acquiringPeriodically = false; _nextAcquisition = millis() + _gpsPeriodSeconds * 1000UL; - } else if (_acquiringPeriodically && + } else if (_gpsPeriodSeconds > 0 && _acquiringPeriodically && (uint32_t)(millis() - _acquisitionStarted) >= GPS_PERIODIC_ACQUISITION_TIMEOUT_MS) { _driver.stop(); From e9d197cf795d8414841e5f7586cc57b0b94dd16e Mon Sep 17 00:00:00 2001 From: Florent Date: Tue, 3 Nov 2026 15:52:59 -0400 Subject: [PATCH 19/53] gps: periodic acquisition rules (timeout, sats and settle time), isEnabled renamed isActive --- src/helpers/CommonGpsPrefs.cpp | 4 ++- src/helpers/sensors/AirohaGpsDriver.h | 2 +- .../sensors/EnvironmentSensorManager.cpp | 4 +-- src/helpers/sensors/GpsDriver.h | 2 +- src/helpers/sensors/LocationProvider.h | 4 +-- .../sensors/MicroNMEALocationProvider.h | 33 +++++++++++++++---- src/helpers/sensors/NmeaGpsDriver.h | 2 +- src/helpers/sensors/RakGpsDriver.h | 4 +-- src/helpers/sensors/RakLocationProvider.h | 11 +++++-- 9 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/helpers/CommonGpsPrefs.cpp b/src/helpers/CommonGpsPrefs.cpp index 248ae540..e08878a6 100644 --- a/src/helpers/CommonGpsPrefs.cpp +++ b/src/helpers/CommonGpsPrefs.cpp @@ -39,7 +39,7 @@ bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors, strcpy(reply, "gps provider not found"); } else if (location->isGpsOn()) { const char* activity = "active"; - if (!location->isEnabled()) { + if (!location->isActive()) { if (location->hasHardwareEnableState() && !location->isHardwareEnabled()) { activity = "off(hw)"; } else if (getGpsPeriod() > 0) { @@ -56,6 +56,8 @@ bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors, strcpy(fix, "no fix"); } snprintf(reply, 160, "on, %s, %s, mode: %s", activity, fix, mode); + } else { + snprintf(reply, 160, "off, mode: %s", mode); } return true; } diff --git a/src/helpers/sensors/AirohaGpsDriver.h b/src/helpers/sensors/AirohaGpsDriver.h index 36d5eb6b..cd94d419 100644 --- a/src/helpers/sensors/AirohaGpsDriver.h +++ b/src/helpers/sensors/AirohaGpsDriver.h @@ -77,7 +77,7 @@ public: NmeaGpsDriver::stop(); } - bool isEnabled() const override { + bool isActive() const override { return digitalRead(GPS_EN) == HIGH; } }; diff --git a/src/helpers/sensors/EnvironmentSensorManager.cpp b/src/helpers/sensors/EnvironmentSensorManager.cpp index 2cf8b66b..ee942df1 100644 --- a/src/helpers/sensors/EnvironmentSensorManager.cpp +++ b/src/helpers/sensors/EnvironmentSensorManager.cpp @@ -631,7 +631,7 @@ bool EnvironmentSensorManager::querySensors(uint8_t requester_permissions, Cayen next_available_channel = TELEM_CHANNEL_SELF + 1; #if ENV_INCLUDE_GPS - if (requester_permissions & TELEM_PERM_LOCATION && _location->isEnabled()) { + if (requester_permissions & TELEM_PERM_LOCATION && _location->isGpsOn()) { telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude); } #endif @@ -748,7 +748,7 @@ void EnvironmentSensorManager::loop() { _location->loop(); if ((long)(millis() - next_gps_update) > 0) { - if (_location->isEnabled() && _location->isValid()) { + if (_location->isGpsOn() && _location->hasValidFix()) { node_lat = ((double)_location->getLatitude())/1000000.; node_lon = ((double)_location->getLongitude())/1000000.; MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon); diff --git a/src/helpers/sensors/GpsDriver.h b/src/helpers/sensors/GpsDriver.h index b8cf4a5d..b17a6b27 100644 --- a/src/helpers/sensors/GpsDriver.h +++ b/src/helpers/sensors/GpsDriver.h @@ -9,7 +9,7 @@ public: virtual void begin() = 0; virtual void reset() = 0; virtual void stop() = 0; - virtual bool isEnabled() const = 0; + virtual bool isActive() const = 0; virtual bool hasHardwareEnableState() const { return false; } virtual bool isHardwareEnabled() const { return true; } // Returns true when the switch explicitly enables GPS in software. diff --git a/src/helpers/sensors/LocationProvider.h b/src/helpers/sensors/LocationProvider.h index dc316e1d..5bb6ec6a 100644 --- a/src/helpers/sensors/LocationProvider.h +++ b/src/helpers/sensors/LocationProvider.h @@ -63,14 +63,14 @@ public: virtual void begin() = 0; virtual void stop() = 0; virtual void loop() = 0; - virtual bool isEnabled() = 0; + virtual bool isActive() = 0; // Format compact diagnostics in the caller-provided buffer. Providers that // have more information (for example UART counters) can override this. virtual void formatDiagnostics(char* out, size_t out_size) { if (out_size == 0) return; snprintf(out, out_size, "en:%u sat:%ld fix:%u", - isEnabled() ? 1U : 0U, + isActive() ? 1U : 0U, satellitesCount(), isValid() ? 1U : 0U); } diff --git a/src/helpers/sensors/MicroNMEALocationProvider.h b/src/helpers/sensors/MicroNMEALocationProvider.h index cceffd8c..b07f486b 100644 --- a/src/helpers/sensors/MicroNMEALocationProvider.h +++ b/src/helpers/sensors/MicroNMEALocationProvider.h @@ -7,7 +7,16 @@ #include #ifndef GPS_PERIODIC_ACQUISITION_TIMEOUT_MS - #define GPS_PERIODIC_ACQUISITION_TIMEOUT_MS 120000UL + #define GPS_PERIODIC_ACQUISITION_TIMEOUT_MS 300000UL +#endif + +// in periodic mode, keep reading after the first valid fix until enough satellites +// are used or the settle time has elapsed +#ifndef GPS_MIN_SATS_FOR_FIX + #define GPS_MIN_SATS_FOR_FIX 5 +#endif +#ifndef GPS_FIX_SETTLE_MS + #define GPS_FIX_SETTLE_MS 10000UL #endif class MicroNMEALocationProvider : public LocationProvider { @@ -22,6 +31,7 @@ class MicroNMEALocationProvider : public LocationProvider { uint32_t _gpsPeriodSeconds = 0; unsigned long _nextAcquisition = 0; unsigned long _acquisitionStarted = 0; + unsigned long _firstFixInWindow = 0; bool _acquiringPeriodically = false; static const unsigned long TIME_SYNC_INTERVAL = 1800000; @@ -54,6 +64,7 @@ class MicroNMEALocationProvider : public LocationProvider { _driver->reset(); _acquiringPeriodically = true; _acquisitionStarted = millis(); + _firstFixInWindow = 0; } void finishPeriodicAcquisition() { @@ -122,11 +133,12 @@ public: void begin() override { _begin_calls++; _driver->begin(); - if (_gpsPeriodSeconds > 0 && _driver->isEnabled()) { + if (_gpsPeriodSeconds > 0 && _driver->isActive()) { nmea.clear(); time_valid = 0; _acquiringPeriodically = true; _acquisitionStarted = millis(); + _firstFixInWindow = 0; } } @@ -140,8 +152,8 @@ public: _driver->stop(); } - bool isEnabled() override { - return _driver->isEnabled(); + bool isActive() override { + return _driver->isActive(); } bool hasHardwareEnableState() const override { @@ -249,7 +261,13 @@ public: if (parsed && nmea.isValid() && (strcmp(messageId, "GGA") == 0 || strcmp(messageId, "RMC") == 0)) { saveFix(); - if (_acquiringPeriodically) finishPeriodicAcquisition(); + if (_acquiringPeriodically) { + if (_firstFixInWindow == 0) _firstFixInWindow = millis(); + if (nmea.getNumSatellites() >= GPS_MIN_SATS_FOR_FIX || + (uint32_t)(millis() - _firstFixInWindow) >= GPS_FIX_SETTLE_MS) { + finishPeriodicAcquisition(); + } + } } } else { _nmea_checksum_bad++; @@ -257,7 +275,8 @@ public: } } - if (_acquiringPeriodically && + // no timeout until a first fix has been obtained (cold start can be long) + if (_acquiringPeriodically && _lastFix.valid && (uint32_t)(millis() - _acquisitionStarted) >= GPS_PERIODIC_ACQUISITION_TIMEOUT_MS) { finishPeriodicAcquisition(); } @@ -277,7 +296,7 @@ public: snprintf(out, out_size, "en:%u ub:%lu ua:%s ok:%lu bad:%lu na:%s sat:%ld fix:%u fa:%s bc:%lu sc:%lu", - isEnabled() ? 1U : 0U, + isActive() ? 1U : 0U, (unsigned long)_uart_bytes, uart_age, (unsigned long)_nmea_checksum_ok, diff --git a/src/helpers/sensors/NmeaGpsDriver.h b/src/helpers/sensors/NmeaGpsDriver.h index f9ea5124..abecb817 100644 --- a/src/helpers/sensors/NmeaGpsDriver.h +++ b/src/helpers/sensors/NmeaGpsDriver.h @@ -183,7 +183,7 @@ public: stopHardware(); } - bool isEnabled() const override { + bool isActive() const override { return _started && isHardwareEnabled(); } diff --git a/src/helpers/sensors/RakGpsDriver.h b/src/helpers/sensors/RakGpsDriver.h index fb4b772b..b3754852 100644 --- a/src/helpers/sensors/RakGpsDriver.h +++ b/src/helpers/sensors/RakGpsDriver.h @@ -77,8 +77,8 @@ public: _serialDriver.stop(); } - bool isEnabled() const override { - return _usingRak12500 ? true : _serialDriver.isEnabled(); + bool isActive() const override { + return _usingRak12500 ? true : _serialDriver.isActive(); } bool detect(uint32_t) override { diff --git a/src/helpers/sensors/RakLocationProvider.h b/src/helpers/sensors/RakLocationProvider.h index db0b957b..57ceb5e9 100644 --- a/src/helpers/sensors/RakLocationProvider.h +++ b/src/helpers/sensors/RakLocationProvider.h @@ -137,7 +137,7 @@ public: _driver.stop(); _acquiringPeriodically = false; _nextAcquisition = millis() + _gpsPeriodSeconds * 1000UL; - } else if (_gpsPeriodSeconds > 0 && _acquiringPeriodically && + } else if (_gpsPeriodSeconds > 0 && _acquiringPeriodically && _lastFix.valid && (uint32_t)(millis() - _acquisitionStarted) >= GPS_PERIODIC_ACQUISITION_TIMEOUT_MS) { _driver.stop(); @@ -150,8 +150,8 @@ public: } } - bool isEnabled() override { - return _driver.isEnabled(); + bool isActive() override { + return _driver.isActive(); } void setGpsPeriod(uint32_t seconds) override { @@ -163,4 +163,9 @@ public: uint32_t getGpsPeriod() const override { return _gpsPeriodSeconds; } + + void setGpsOn(bool on) override { + if (_driver.usesRak12500()) LocationProvider::setGpsOn(on); + else { _gpsOn = on; _nmea.setGpsOn(on); } + } }; From a0c5854b591ccef21061009c05932aa334722978 Mon Sep 17 00:00:00 2001 From: Florent Date: Tue, 3 Nov 2026 21:10:01 -0400 Subject: [PATCH 20/53] rak_gps: simplify the rak driver, which now only handles i2c, for nmea use nmea driver directly --- .../{RakGpsDriver.h => RakI2cGpsDriver.h} | 50 +++++--------- src/helpers/sensors/RakLocationProvider.h | 67 ++++++++++++------- variants/gat562_30s_mesh_kit/target.cpp | 2 +- variants/rak3401/target.cpp | 2 +- variants/rak4631/target.cpp | 2 +- 5 files changed, 61 insertions(+), 62 deletions(-) rename src/helpers/sensors/{RakGpsDriver.h => RakI2cGpsDriver.h} (65%) diff --git a/src/helpers/sensors/RakGpsDriver.h b/src/helpers/sensors/RakI2cGpsDriver.h similarity index 65% rename from src/helpers/sensors/RakGpsDriver.h rename to src/helpers/sensors/RakI2cGpsDriver.h index b3754852..ac76e069 100644 --- a/src/helpers/sensors/RakGpsDriver.h +++ b/src/helpers/sensors/RakI2cGpsDriver.h @@ -1,16 +1,16 @@ #pragma once -#include "NmeaGpsDriver.h" +#include "GpsDriver.h" #include -class RakGpsDriver : public GpsDriver { - NmeaGpsDriver _serialDriver; +// Driver for the RAK12500 (u-blox, I2C). Serial NMEA modules are handled by NmeaGpsDriver. +class RakI2cGpsDriver : public GpsDriver { TwoWire& _wire; SFE_UBLOX_GNSS _gnss; uint8_t _powerPin = 0; - bool _usingRak12500 = false; bool _detected = false; + bool _started = false; bool probeRak12500(uint8_t powerPin) { #if defined(ETHERNET_ENABLED) && defined(RAK_BOARD) @@ -37,7 +37,6 @@ class RakGpsDriver : public GpsDriver { _gnss.setMeasurementRate(1000); _gnss.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); _powerPin = powerPin; - _usingRak12500 = true; return true; } @@ -48,62 +47,47 @@ class RakGpsDriver : public GpsDriver { } public: - RakGpsDriver(HardwareSerial& serial = Serial1, TwoWire& wire = Wire) - : _serialDriver(serial), _wire(wire) {} + RakI2cGpsDriver(TwoWire& wire = Wire) : _wire(wire) {} void begin() override { - if (_usingRak12500) { + if (!_detected) return; #ifndef RAK_3401 - pinMode(_powerPin, OUTPUT); - digitalWrite(_powerPin, HIGH); + pinMode(_powerPin, OUTPUT); + digitalWrite(_powerPin, HIGH); #endif - return; - } - _serialDriver.begin(); + _started = true; } - void reset() override { - if (!_usingRak12500) _serialDriver.reset(); - } + void reset() override {} void stop() override { - if (_usingRak12500) { + if (_detected) { #ifndef RAK_3401 pinMode(_powerPin, OUTPUT); digitalWrite(_powerPin, LOW); #endif - return; } - _serialDriver.stop(); + _started = false; } bool isActive() const override { - return _usingRak12500 ? true : _serialDriver.isActive(); + return _started; } + // Probes the I2C module on the possible power pins; the module is left powered. bool detect(uint32_t) override { if (_detected) return true; - _serialDriver.begin(); if (probeRak12500(WB_IO2) || probeRak12500(WB_IO4) || probeRak12500(WB_IO5)) { _detected = true; - return true; + _started = true; } - - if (_serialDriver.detect(0)) { - _detected = true; - return true; - } - - _serialDriver.stop(); - return false; + return _detected; } - bool usesRak12500() const { return _usingRak12500; } bool isDetected() const { return _detected; } - NmeaGpsDriver& nmeaDriver() { return _serialDriver; } - bool hasFix() { return _usingRak12500 && _gnss.getGnssFixOk(8); } + bool hasFix() { return _started && _gnss.getGnssFixOk(8); } long getLatitude() { return _gnss.getLatitude(2) / 10; } long getLongitude() { return _gnss.getLongitude(2) / 10; } long getAltitude() { return _gnss.getAltitude(2); } diff --git a/src/helpers/sensors/RakLocationProvider.h b/src/helpers/sensors/RakLocationProvider.h index 57ceb5e9..d2c755e6 100644 --- a/src/helpers/sensors/RakLocationProvider.h +++ b/src/helpers/sensors/RakLocationProvider.h @@ -1,11 +1,15 @@ #pragma once #include "MicroNMEALocationProvider.h" -#include "RakGpsDriver.h" +#include "NmeaGpsDriver.h" +#include "RakI2cGpsDriver.h" class RakLocationProvider : public LocationProvider { - RakGpsDriver& _driver; + RakI2cGpsDriver& _driver; // RAK12500 (I2C) + NmeaGpsDriver _nmeaDriver; // serial NMEA module MicroNMEALocationProvider _nmea; + bool _detected = false; + bool _useI2c = false; uint32_t _gpsPeriodSeconds = 0; unsigned long _nextAcquisition = 0; unsigned long _acquisitionStarted = 0; @@ -24,73 +28,74 @@ class RakLocationProvider : public LocationProvider { } public: - RakLocationProvider(RakGpsDriver& driver, mesh::RTCClock* clock = nullptr) - : _driver(driver), _nmea(driver.nmeaDriver(), clock) {} + RakLocationProvider(RakI2cGpsDriver& driver, mesh::RTCClock* clock = nullptr, + HardwareSerial& serial = Serial1) + : _driver(driver), _nmeaDriver(serial), _nmea(_nmeaDriver, clock) {} void syncTime() override { LocationProvider::syncTime(); - if (!_driver.usesRak12500()) _nmea.syncTime(); + if (!_useI2c) _nmea.syncTime(); } long getLatitude() override { - return _driver.usesRak12500() ? _lastFix.latitude : _nmea.getLatitude(); + return _useI2c ? _lastFix.latitude : _nmea.getLatitude(); } long getLongitude() override { - return _driver.usesRak12500() ? _lastFix.longitude : _nmea.getLongitude(); + return _useI2c ? _lastFix.longitude : _nmea.getLongitude(); } long getAltitude() override { - return _driver.usesRak12500() ? _lastFix.altitude : _nmea.getAltitude(); + return _useI2c ? _lastFix.altitude : _nmea.getAltitude(); } long satellitesCount() override { - return _driver.usesRak12500() ? _lastFix.satellites : _nmea.satellitesCount(); + return _useI2c ? _lastFix.satellites : _nmea.satellitesCount(); } bool isValid() override { - return _driver.usesRak12500() ? _driver.hasFix() : _nmea.isValid(); + return _useI2c ? _driver.hasFix() : _nmea.isValid(); } LastFix getLastFix() const override { - return _driver.usesRak12500() ? _lastFix : _nmea.getLastFix(); + return _useI2c ? _lastFix : _nmea.getLastFix(); } long getTimestamp() override { - return _driver.usesRak12500() ? _lastFix.timestamp : _nmea.getTimestamp(); + return _useI2c ? _lastFix.timestamp : _nmea.getTimestamp(); } void sendSentence(const char* sentence) override { - if (!_driver.usesRak12500()) _nmea.sendSentence(sentence); + if (!_useI2c) _nmea.sendSentence(sentence); } bool waitFor(const char* prefix, uint32_t timeout_ms) override { - return _driver.usesRak12500() ? false : _nmea.waitFor(prefix, timeout_ms); + return _useI2c ? false : _nmea.waitFor(prefix, timeout_ms); } void drain() override { - if (!_driver.usesRak12500()) _nmea.drain(); + if (!_useI2c) _nmea.drain(); } void reset() override { - _driver.reset(); + if (!_useI2c) _nmea.reset(); } void begin() override { - if (_driver.isDetected() && _driver.usesRak12500()) { + if (_detected && _useI2c) { _driver.begin(); _acquiringPeriodically = _gpsPeriodSeconds > 0; _acquisitionStarted = millis(); - } else if (_driver.isDetected()) { + } else if (_detected) { _nmea.begin(); } else { - _driver.begin(); + _nmeaDriver.begin(); } } void stop() override { _acquiringPeriodically = false; - if (_driver.usesRak12500()) { + if (_useI2c) { _driver.stop(); } else { _nmea.stop(); @@ -98,9 +103,19 @@ public: } bool detect(uint32_t timeout_ms) override { - if (!_driver.detect(timeout_ms)) return false; + if (_detected) return true; - if (_driver.usesRak12500()) { + // the serial module is started first, then the I2C module is probed + _nmeaDriver.begin(); + if (_driver.detect(timeout_ms)) { + _useI2c = true; + } else if (!_nmeaDriver.detect(0)) { + _nmeaDriver.stop(); + return false; + } + _detected = true; + + if (_useI2c) { _acquiringPeriodically = _gpsPeriodSeconds > 0; _acquisitionStarted = millis(); } else { @@ -119,7 +134,7 @@ public: } void loop() override { - if (_driver.usesRak12500()) { + if (_useI2c) { if (_gpsOn) { if (_gpsPeriodSeconds > 0) { if (!_acquiringPeriodically && @@ -151,13 +166,13 @@ public: } bool isActive() override { - return _driver.isActive(); + return _useI2c ? _driver.isActive() : _nmea.isActive(); } void setGpsPeriod(uint32_t seconds) override { _gpsPeriodSeconds = seconds; _nextAcquisition = millis(); - if (!_driver.usesRak12500()) _nmea.setGpsPeriod(seconds); + if (!_useI2c) _nmea.setGpsPeriod(seconds); } uint32_t getGpsPeriod() const override { @@ -165,7 +180,7 @@ public: } void setGpsOn(bool on) override { - if (_driver.usesRak12500()) LocationProvider::setGpsOn(on); + if (_useI2c) LocationProvider::setGpsOn(on); else { _gpsOn = on; _nmea.setGpsOn(on); } } }; diff --git a/variants/gat562_30s_mesh_kit/target.cpp b/variants/gat562_30s_mesh_kit/target.cpp index ba81a690..96e38a79 100644 --- a/variants/gat562_30s_mesh_kit/target.cpp +++ b/variants/gat562_30s_mesh_kit/target.cpp @@ -27,7 +27,7 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - RakGpsDriver gps_driver; + RakI2cGpsDriver gps_driver; RakLocationProvider location(gps_driver, &rtc_clock); EnvironmentSensorManager sensors(location); #else diff --git a/variants/rak3401/target.cpp b/variants/rak3401/target.cpp index 26501977..58c0e1d7 100644 --- a/variants/rak3401/target.cpp +++ b/variants/rak3401/target.cpp @@ -26,7 +26,7 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - RakGpsDriver gps_driver; + RakI2cGpsDriver gps_driver; RakLocationProvider location(gps_driver, &rtc_clock); EnvironmentSensorManager sensors(location); #else diff --git a/variants/rak4631/target.cpp b/variants/rak4631/target.cpp index 06ccd23d..158de2b5 100644 --- a/variants/rak4631/target.cpp +++ b/variants/rak4631/target.cpp @@ -26,7 +26,7 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - RakGpsDriver gps_driver; + RakI2cGpsDriver gps_driver; RakLocationProvider location(gps_driver, &rtc_clock); EnvironmentSensorManager sensors(location); #else From 53037037507c64c72b02c74d75a85a2219166f0b Mon Sep 17 00:00:00 2001 From: Florent Date: Tue, 3 Nov 2026 21:29:21 -0400 Subject: [PATCH 21/53] gps: adapt ui-new and the sensor managers --- examples/companion_radio/ui-new/UITask.cpp | 51 ++++++++++++++++--- variants/heltec_mesh_solar/target.cpp | 2 +- variants/heltec_tracker/target.cpp | 2 +- variants/meshadventurer/target.cpp | 4 +- variants/meshtracker_x1/target.cpp | 2 +- variants/nano_g2_ultra/NanoG2UltraGpsDriver.h | 2 +- variants/nano_g2_ultra/target.cpp | 4 +- variants/t1000-e/target.cpp | 2 +- variants/thinknode_m1/target.cpp | 2 +- 9 files changed, 53 insertions(+), 18 deletions(-) diff --git a/examples/companion_radio/ui-new/UITask.cpp b/examples/companion_radio/ui-new/UITask.cpp index a1400669..6b6af33e 100644 --- a/examples/companion_radio/ui-new/UITask.cpp +++ b/examples/companion_radio/ui-new/UITask.cpp @@ -86,6 +86,30 @@ public: } }; +#if ENV_INCLUDE_GPS == 1 +// exact period with the largest unit that divides it: "30s", "5m", "2h" +static void formatGpsPeriod(char* buf, size_t size, uint32_t seconds) { + if (seconds % 3600 == 0) { + snprintf(buf, size, "%luh", (unsigned long)(seconds / 3600)); + } else if (seconds % 60 == 0) { + snprintf(buf, size, "%lum", (unsigned long)(seconds / 60)); + } else { + snprintf(buf, size, "%lus", (unsigned long)seconds); + } +} + +// age of the last fix, rounded down to a single unit: "45s", "12m", "3h" +static void formatFixAge(char* buf, size_t size, uint32_t seconds) { + if (seconds < 60) { + snprintf(buf, size, "%lus", (unsigned long)seconds); + } else if (seconds < 3600) { + snprintf(buf, size, "%lum", (unsigned long)(seconds / 60)); + } else { + snprintf(buf, size, "%luh", (unsigned long)(seconds / 3600)); + } +} +#endif + class HomeScreen : public UIScreen { enum HomePage { FIRST, @@ -393,14 +417,26 @@ public: int y = 18; bool gps_state = _task->getGPSState(); bool hw_gps_state = nmea != NULL && nmea->isHardwareEnabled(); - if (nmea != NULL && nmea->hasHardwareEnableState()) { - if (gps_state != hw_gps_state) { - strcpy(buf, gps_state ? "gps off(hw)" : "gps off(sw)"); - } else { - strcpy(buf, gps_state ? "gps on" : "gps off"); + uint32_t period = nmea != NULL ? nmea->getGpsPeriod() : 0; + char fix_buf[12] = ""; + if (nmea != NULL) { + uint32_t fix_age = nmea->getLastFixAge(); + if (fix_age == UINT32_MAX) { + strcpy(fix_buf, "no fix"); + } else { + formatFixAge(fix_buf, sizeof(fix_buf), fix_age); + } } + if (nmea != NULL && nmea->hasHardwareEnableState() && gps_state != hw_gps_state) { + strcpy(buf, gps_state ? "gps off(hw)" : "gps off(sw)"); + } else if (!gps_state) { + strcpy(buf, "gps off"); + } else if (period == 0) { + strcpy(buf, "gps on"); } else { - strcpy(buf, gps_state ? "gps on" : "gps off"); + char per[12]; + formatGpsPeriod(per, sizeof(per), period); + snprintf(buf, sizeof(buf), "%s (%s)", nmea != NULL && nmea->isActive() ? "active" : "sleep", per); } display.setColor(UIColor::primary_txt); display.drawTextLeftAlign(0, y, buf); @@ -410,8 +446,7 @@ public: display.drawTextLeftAlign(0, y, "Can't access GPS"); } else { display.setColor(UIColor::primary_txt); - strcpy(buf, nmea->isValid()?"fix":"no fix"); - display.drawTextRightAlign(display.width()-1, y, buf); + display.drawTextRightAlign(display.width()-1, y, fix_buf); y = y + 12; display.setColor(UIColor::secondary_txt); display.drawTextLeftAlign(0, y, "sat"); diff --git a/variants/heltec_mesh_solar/target.cpp b/variants/heltec_mesh_solar/target.cpp index 9468f8fc..9e7fb04d 100644 --- a/variants/heltec_mesh_solar/target.cpp +++ b/variants/heltec_mesh_solar/target.cpp @@ -66,7 +66,7 @@ void SolarSensorManager::loop() { _location->loop(); if (millis() > next_gps_update) { - if (_location->isValid()) { + if (_location->isGpsOn() && _location->hasValidFix()) { node_lat = ((double)_location->getLatitude())/1000000.; node_lon = ((double)_location->getLongitude())/1000000.; node_altitude = ((double)_location->getAltitude()) / 1000.0; diff --git a/variants/heltec_tracker/target.cpp b/variants/heltec_tracker/target.cpp index 5903f659..041cb2b8 100644 --- a/variants/heltec_tracker/target.cpp +++ b/variants/heltec_tracker/target.cpp @@ -82,7 +82,7 @@ void HWTSensorManager::loop() { _location->loop(); if (millis() > next_gps_update) { - if (_location->isEnabled() && _location->isValid()) { + if (_location->isGpsOn() && _location->hasValidFix()) { node_lat = ((double)_location->getLatitude())/1000000.; node_lon = ((double)_location->getLongitude())/1000000.; node_altitude = ((double)_location->getAltitude()) / 1000.0; diff --git a/variants/meshadventurer/target.cpp b/variants/meshadventurer/target.cpp index faf2e30e..827e23b7 100644 --- a/variants/meshadventurer/target.cpp +++ b/variants/meshadventurer/target.cpp @@ -64,8 +64,8 @@ bool MASensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& te void MASensorManager::loop() { static long next_gps_update = 0; _location->loop(); - if(millis() > next_gps_update && _location->isEnabled()) { - if(_location->isValid()) { + if(millis() > next_gps_update && _location->isGpsOn()) { + if(_location->hasValidFix()) { node_lat = ((double)_location->getLatitude()) / 1000000.; node_lon = ((double)_location->getLongitude()) / 1000000.; node_altitude = ((double)_location->getAltitude()) / 1000.0; diff --git a/variants/meshtracker_x1/target.cpp b/variants/meshtracker_x1/target.cpp index 2dee0ebd..84e0870e 100644 --- a/variants/meshtracker_x1/target.cpp +++ b/variants/meshtracker_x1/target.cpp @@ -70,7 +70,7 @@ void MeshTrackerX1SensorManager::loop() { _nmea->loop(); if (millis() > next_gps_update) { - if (_nmea->isEnabled() && _nmea->isValid()) { + if (_nmea->isGpsOn() && _nmea->hasValidFix()) { node_lat = ((double)_nmea->getLatitude())/1000000.; node_lon = ((double)_nmea->getLongitude())/1000000.; node_altitude = ((double)_nmea->getAltitude()) / 1000.0; diff --git a/variants/nano_g2_ultra/NanoG2UltraGpsDriver.h b/variants/nano_g2_ultra/NanoG2UltraGpsDriver.h index 3772084b..7e3bd109 100644 --- a/variants/nano_g2_ultra/NanoG2UltraGpsDriver.h +++ b/variants/nano_g2_ultra/NanoG2UltraGpsDriver.h @@ -10,7 +10,7 @@ public: } void begin() override { - if (isEnabled()) return; + if (isActive()) return; digitalWrite(PIN_GPS_STANDBY, HIGH); NmeaGpsDriver::begin(); diff --git a/variants/nano_g2_ultra/target.cpp b/variants/nano_g2_ultra/target.cpp index ee258f2d..fedc48e7 100644 --- a/variants/nano_g2_ultra/target.cpp +++ b/variants/nano_g2_ultra/target.cpp @@ -62,14 +62,14 @@ bool NanoG2UltraSensorManager::querySensors(uint8_t requester_permissions, Cayen void NanoG2UltraSensorManager::loop() { static long next_gps_update = 0; - if (!_location->isEnabled()) { + if (!_location->isGpsOn()) { return; // GPS is not active, skip further processing } _location->loop(); if (millis() > next_gps_update) { - if (_location->isEnabled() && _location->isValid()) { + if (_location->isGpsOn() && _location->hasValidFix()) { node_lat = ((double)_location->getLatitude()) / 1000000.; node_lon = ((double)_location->getLongitude()) / 1000000.; node_altitude = ((double)_location->getAltitude()) / 1000.0; diff --git a/variants/t1000-e/target.cpp b/variants/t1000-e/target.cpp index b96d01f8..609c32bc 100644 --- a/variants/t1000-e/target.cpp +++ b/variants/t1000-e/target.cpp @@ -113,7 +113,7 @@ void T1000SensorManager::loop() { _nmea->loop(); if (millis() > next_gps_update) { - if (_nmea->isEnabled() && _nmea->isValid()) { + if (_nmea->isGpsOn() && _nmea->hasValidFix()) { node_lat = ((double)_nmea->getLatitude())/1000000.; node_lon = ((double)_nmea->getLongitude())/1000000.; node_altitude = ((double)_nmea->getAltitude()) / 1000.0; diff --git a/variants/thinknode_m1/target.cpp b/variants/thinknode_m1/target.cpp index 78f5a88e..4183c514 100644 --- a/variants/thinknode_m1/target.cpp +++ b/variants/thinknode_m1/target.cpp @@ -58,7 +58,7 @@ void ThinkNodeM1SensorManager::loop() { _location->loop(); if (millis() > next_gps_update) { - if (_location->isEnabled() && _location->isValid()) { + if (_location->isGpsOn() && _location->hasValidFix()) { node_lat = ((double)_location->getLatitude())/1000000.; node_lon = ((double)_location->getLongitude())/1000000.; node_altitude = ((double)_location->getAltitude()) / 1000.0; From 47959b8960701209bf5012f77c43444eb93f9821 Mon Sep 17 00:00:00 2001 From: Florent Date: Wed, 4 Nov 2026 09:02:38 -0400 Subject: [PATCH 22/53] gps: fix_stats command --- docs/cli_commands.md | 8 +++++ docs/companion_cli.md | 8 +++++ src/helpers/CommonGpsPrefs.cpp | 32 +++++++++++++++-- src/helpers/sensors/LocationProvider.h | 20 +++++++++++ .../sensors/MicroNMEALocationProvider.h | 35 ++++++++++++++----- src/helpers/sensors/RakLocationProvider.h | 13 ++++++- 6 files changed, 105 insertions(+), 11 deletions(-) diff --git a/docs/cli_commands.md b/docs/cli_commands.md index eb8c6b20..d7b8d5d0 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md @@ -1044,6 +1044,14 @@ region save --- +#### View GPS time to first fix statistics +**Usage:** +- `gps fix_stats` + +**Output:** `ttf:/ ok: miss:`: last and maximum time between a start of the GPS and its first valid fix, then the last count of consecutive starts with a fix (`ok`) or without one (`miss`). + +--- + #### View GPS diagnostics **Usage:** - `gps diag` diff --git a/docs/companion_cli.md b/docs/companion_cli.md index 24843269..0b3a27df 100644 --- a/docs/companion_cli.md +++ b/docs/companion_cli.md @@ -165,6 +165,14 @@ Radio parameters are common to all roles and handled by `CommonRadioPrefs`. --- +#### View GPS time to first fix statistics +**Usage:** +- `gps fix_stats` + +**Output:** `ttf:/ ok: miss:`: last and maximum time between a start of the GPS and its first valid fix, then the last count of consecutive starts with a fix (`ok`) or without one (`miss`). + +--- + #### View GPS diagnostics **Usage:** - `gps diag` diff --git a/src/helpers/CommonGpsPrefs.cpp b/src/helpers/CommonGpsPrefs.cpp index e08878a6..c4936d4f 100644 --- a/src/helpers/CommonGpsPrefs.cpp +++ b/src/helpers/CommonGpsPrefs.cpp @@ -25,6 +25,17 @@ bool CommonGpsPrefs::apply(SensorManager& sensors) const { return true; } +// single unit, rounded down: "45s", "12m", "3h" +static void formatDuration(char* buf, size_t size, uint32_t seconds) { + if (seconds < 60) { + snprintf(buf, size, "%lus", (unsigned long)seconds); + } else if (seconds < 3600) { + snprintf(buf, size, "%lum", (unsigned long)(seconds / 60)); + } else { + snprintf(buf, size, "%luh", (unsigned long)(seconds / 3600)); + } +} + bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors, char* reply) { if (strcmp(command, "gps") == 0) { LocationProvider* location = sensors.getLocationProvider(); @@ -50,8 +61,9 @@ bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors, } char fix[32]; if (location->hasValidFix()) { - snprintf(fix, sizeof(fix), "%ld sats %lus ago", - location->satellitesCount(), (unsigned long)location->getLastFixAge()); + char age[12]; + formatDuration(age, sizeof(age), location->getLastFixAge()); + snprintf(fix, sizeof(fix), "%ld sats %s ago", location->satellitesCount(), age); } else { strcpy(fix, "no fix"); } @@ -134,6 +146,22 @@ bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors, } return true; } + if (strcmp(command, "gps fix_stats") == 0) { + LocationProvider* location = sensors.getLocationProvider(); + if (location == nullptr) { + strcpy(reply, "gps provider not found"); + } else { + const LocationProvider::FixStats s = location->getFixStats(); + char last[12] = "-", max[12] = "-"; + if (s.maxTtffMs > 0) { + formatDuration(last, sizeof(last), (s.lastTtffMs + 500) / 1000); + formatDuration(max, sizeof(max), (s.maxTtffMs + 500) / 1000); + } + snprintf(reply, 160, "ttf:%s/%s ok:%lu miss:%lu", last, max, + (unsigned long)s.consecutiveFixes, (unsigned long)s.consecutiveMisses); + } + return true; + } if (strcmp(command, "gps diag") == 0) { LocationProvider* location = sensors.getLocationProvider(); if (location == nullptr) { diff --git a/src/helpers/sensors/LocationProvider.h b/src/helpers/sensors/LocationProvider.h index 5bb6ec6a..3d7abd7a 100644 --- a/src/helpers/sensors/LocationProvider.h +++ b/src/helpers/sensors/LocationProvider.h @@ -18,10 +18,29 @@ public: unsigned long acquiredAt = 0; // millis() when the fix was stored }; + struct FixStats { + uint32_t lastTtffMs = 0; //time to first fix in ms + uint32_t maxTtffMs = 0; + uint32_t consecutiveFixes = 0; + uint32_t consecutiveMisses = 0; + }; + protected: bool _time_sync_needed = true; bool _gpsOn = false; LastFix _lastFix; + FixStats _fixStats; + + void fixStatsFix(uint32_t ttffMs) { + _fixStats.lastTtffMs = ttffMs; + if (ttffMs > _fixStats.maxTtffMs) _fixStats.maxTtffMs = ttffMs; + _fixStats.consecutiveFixes++; + _fixStats.consecutiveMisses = 0; + } + void fixStatsMiss() { + _fixStats.consecutiveMisses++; + _fixStats.consecutiveFixes = 0; + } public: virtual ~LocationProvider() = default; @@ -35,6 +54,7 @@ public: virtual bool isValid() = 0; // current fix is valid virtual bool hasValidFix() { return getLastFix().valid || isValid(); } // a valid fix was obtained at some point virtual LastFix getLastFix() const { return _lastFix; } // snapshot of the last valid fix + virtual FixStats getFixStats() const { return _fixStats; } virtual long getTimestamp() = 0; virtual void sendSentence(const char * sentence); virtual bool waitFor(const char* prefix, uint32_t timeout_ms) { return false; } diff --git a/src/helpers/sensors/MicroNMEALocationProvider.h b/src/helpers/sensors/MicroNMEALocationProvider.h index b07f486b..3c2adc6e 100644 --- a/src/helpers/sensors/MicroNMEALocationProvider.h +++ b/src/helpers/sensors/MicroNMEALocationProvider.h @@ -68,21 +68,30 @@ class MicroNMEALocationProvider : public LocationProvider { } void finishPeriodicAcquisition() { + if (_firstFixInWindow == 0) fixStatsMiss(); _driver->stop(); _acquiringPeriodically = false; _nextAcquisition = millis() + _gpsPeriodSeconds * 1000UL; } - void saveFix() { + bool saveFix() { + // only store a fix when the snapshot is coherent (sats > 0). + // Returns true if the fix was stored. + if (nmea.getNumSatellites() == 0) return false; + LastFix fix; fix.latitude = nmea.getLatitude(); fix.longitude = nmea.getLongitude(); - nmea.getAltitude(fix.altitude); + // altitude comes from GGA only: keep the previous one if not available + if (!nmea.getAltitude(fix.altitude)) { + fix.altitude = _lastFix.valid ? _lastFix.altitude : 0; + } fix.satellites = nmea.getNumSatellites(); fix.timestamp = getTimestamp(); fix.acquiredAt = millis(); fix.valid = true; _lastFix = fix; + return true; } void updateTime() { @@ -132,7 +141,12 @@ public: void begin() override { _begin_calls++; + const bool wasActive = _driver->isActive(); _driver->begin(); + if (!wasActive) { + _acquisitionStarted = millis(); + _firstFixInWindow = 0; + } if (_gpsPeriodSeconds > 0 && _driver->isActive()) { nmea.clear(); time_valid = 0; @@ -183,20 +197,22 @@ public: LocationProvider::syncTime(); } + // Position, altitude and satellite count describe the last saved fix + // Before the first saved fix, fall back to the live parser state. long getLatitude() override { - return nmea.isValid() ? nmea.getLatitude() : _lastFix.latitude; + return _lastFix.valid ? _lastFix.latitude : nmea.getLatitude(); } long getLongitude() override { - return nmea.isValid() ? nmea.getLongitude() : _lastFix.longitude; + return _lastFix.valid ? _lastFix.longitude : nmea.getLongitude(); } long getAltitude() override { - if (!nmea.isValid()) return _lastFix.altitude; + if (_lastFix.valid) return _lastFix.altitude; long alt = 0; nmea.getAltitude(alt); return alt; } long satellitesCount() override { - return nmea.isValid() ? nmea.getNumSatellites() : _lastFix.satellites; + return _lastFix.valid ? _lastFix.satellites : nmea.getNumSatellites(); } bool isValid() override { return nmea.isValid(); } @@ -260,9 +276,12 @@ public: const char* messageId = nmea.getMessageID(); if (parsed && nmea.isValid() && (strcmp(messageId, "GGA") == 0 || strcmp(messageId, "RMC") == 0)) { - saveFix(); + if (!saveFix()) continue; // incoherent snapshot + if (_firstFixInWindow == 0) { + _firstFixInWindow = millis(); + fixStatsFix((uint32_t)(_firstFixInWindow - _acquisitionStarted)); + } if (_acquiringPeriodically) { - if (_firstFixInWindow == 0) _firstFixInWindow = millis(); if (nmea.getNumSatellites() >= GPS_MIN_SATS_FOR_FIX || (uint32_t)(millis() - _firstFixInWindow) >= GPS_FIX_SETTLE_MS) { finishPeriodicAcquisition(); diff --git a/src/helpers/sensors/RakLocationProvider.h b/src/helpers/sensors/RakLocationProvider.h index d2c755e6..7deddf43 100644 --- a/src/helpers/sensors/RakLocationProvider.h +++ b/src/helpers/sensors/RakLocationProvider.h @@ -57,6 +57,10 @@ public: return _useI2c ? _driver.hasFix() : _nmea.isValid(); } + FixStats getFixStats() const override { + return _useI2c ? _fixStats : _nmea.getFixStats(); + } + LastFix getLastFix() const override { return _useI2c ? _lastFix : _nmea.getLastFix(); } @@ -146,7 +150,13 @@ public: } const bool hasCurrentFix = _driver.hasFix(); - if (hasCurrentFix) saveRak12500Fix(); + if (hasCurrentFix) { + // first fix since the start + if (!_lastFix.valid || (long)(_lastFix.acquiredAt - _acquisitionStarted) < 0) { + fixStatsFix((uint32_t)(millis() - _acquisitionStarted)); + } + saveRak12500Fix(); + } if (_gpsPeriodSeconds > 0 && _acquiringPeriodically && hasCurrentFix) { _driver.stop(); @@ -155,6 +165,7 @@ public: } else if (_gpsPeriodSeconds > 0 && _acquiringPeriodically && _lastFix.valid && (uint32_t)(millis() - _acquisitionStarted) >= GPS_PERIODIC_ACQUISITION_TIMEOUT_MS) { + fixStatsMiss(); _driver.stop(); _acquiringPeriodically = false; _nextAcquisition = millis() + _gpsPeriodSeconds * 1000UL; From bd21096ce0282851958dcef6d4422d56094df373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=ADckolas=20Goline?= Date: Wed, 4 Nov 2026 13:36:33 -0300 Subject: [PATCH 23/53] muzi_base: fix GPS UART pins and re-enable ATGM336H NMEA output The refactor no longer calls Serial1.setPins() on nRF52, so Serial1 uses PIN_SERIAL1_RX/TX directly. Those were swapped (the GPS transmits on P0.20), so no data was received. Fix them and define PIN_GPS_TX/RX by the gps module's side, as other variants do. The ATGM336H keeps its NMEA output config across power cycles, and other firmware may leave every sentence disabled. Re-enable GGA + RMC after each power-up. --- variants/muzi_base/target.cpp | 12 ++++++++++++ variants/muzi_base/target.h | 4 ++++ variants/muzi_base/variant.h | 8 ++++---- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/variants/muzi_base/target.cpp b/variants/muzi_base/target.cpp index b65bafa1..6653ada1 100644 --- a/variants/muzi_base/target.cpp +++ b/variants/muzi_base/target.cpp @@ -50,6 +50,18 @@ void MuziBaseSensorManager::loop() { _pwr_last_click = t; if (++_pwr_clicks >= USER_BTN_POWER_OFF_CLICKS) muziPowerOff(); } +#if ENV_INCLUDE_GPS + // the ATGM336H keeps its nmea output config across power cycles (backup + // supply), and other firmware may have turned every sentence off. re-enable + // GGA + RMC after each power-up, once the module has booted. + bool gps_on = _location->isActive(); + if (gps_on && !_gps_was_active) _gps_cfg_at = millis() + 1000; + _gps_was_active = gps_on; + if (_gps_cfg_at && gps_on && (long)(millis() - _gps_cfg_at) > 0) { + _gps_cfg_at = 0; + _location->sendSentence("$PCAS03,1,0,0,0,1,0,0,0,0,0,,,0,0"); + } +#endif EnvironmentSensorManager::loop(); } diff --git a/variants/muzi_base/target.h b/variants/muzi_base/target.h index 3f7dcbf5..0c143e1b 100644 --- a/variants/muzi_base/target.h +++ b/variants/muzi_base/target.h @@ -42,6 +42,10 @@ extern AutoDiscoverRTCClock rtc_clock; class MuziBaseSensorManager : public EnvironmentSensorManager { unsigned long _pwr_last_click = 0; uint8_t _pwr_clicks = 0; +#if ENV_INCLUDE_GPS + unsigned long _gps_cfg_at = 0; + bool _gps_was_active = false; +#endif public: #if ENV_INCLUDE_GPS MuziBaseSensorManager(LocationProvider& location) : EnvironmentSensorManager(location) {} diff --git a/variants/muzi_base/variant.h b/variants/muzi_base/variant.h index 0c1245a8..d42804f6 100644 --- a/variants/muzi_base/variant.h +++ b/variants/muzi_base/variant.h @@ -54,8 +54,8 @@ static const uint8_t AREF = (PIN_AREF); // not used //////////////////////////////////////////////////////////////////////////////// // UART pin definition -#define PIN_SERIAL1_RX (19) // P0.19 used for GPS RX -#define PIN_SERIAL1_TX (20) // P0.20 used for GPS TX +#define PIN_SERIAL1_RX (20) // P0.20, data from the gps +#define PIN_SERIAL1_TX (19) // P0.19, data to the gps //////////////////////////////////////////////////////////////////////////////// // I2C pin definition @@ -153,8 +153,8 @@ static const uint8_t AREF = (PIN_AREF); // not used //////////////////////////////////////////////////////////////////////////////// // GPS #define HAS_GPS 1 -#define PIN_GPS_RX PIN_SERIAL1_RX -#define PIN_GPS_TX PIN_SERIAL1_TX +#define PIN_GPS_TX PIN_SERIAL1_RX // gps module's TX pin +#define PIN_GPS_RX PIN_SERIAL1_TX // gps module's RX pin #define PIN_GPS_EN (32+1) // P1.01 PWR_IO2 on schematic. gps power, driven by the sensor manager. // superIO 3-position mode switch (pins from the meshtastic muzi_base variant). From a134188c62a7f7bdfaa1bd429fc2a2dad44ebe75 Mon Sep 17 00:00:00 2001 From: Florent Date: Wed, 4 Nov 2026 16:30:44 -0400 Subject: [PATCH 24/53] gps: serial pin and configuration fixes (nrf52, meshadventurer, tracker_v1, heltec_tracker) --- src/helpers/sensors/AirohaGpsDriver.h | 4 +- src/helpers/sensors/AirohaSleep.h | 19 ------- .../sensors/MicroNMEALocationProvider.h | 10 +--- src/helpers/sensors/NmeaGpsDriver.h | 55 +++++++++---------- src/helpers/sensors/RakLocationProvider.h | 2 +- variants/heltec_tracker/platformio.ini | 4 +- variants/heltec_tracker/target.cpp | 14 +++-- variants/meshadventurer/platformio.ini | 4 +- variants/meshadventurer/target.cpp | 4 +- variants/nano_g2_ultra/NanoG2UltraGpsDriver.h | 2 +- variants/t1000-e/variant.h | 2 + 11 files changed, 48 insertions(+), 72 deletions(-) delete mode 100644 src/helpers/sensors/AirohaSleep.h diff --git a/src/helpers/sensors/AirohaGpsDriver.h b/src/helpers/sensors/AirohaGpsDriver.h index cd94d419..9ba8a55d 100644 --- a/src/helpers/sensors/AirohaGpsDriver.h +++ b/src/helpers/sensors/AirohaGpsDriver.h @@ -46,8 +46,8 @@ class AirohaGpsDriver : public NmeaGpsDriver { } public: - AirohaGpsDriver(HardwareSerial& serial) - : NmeaGpsDriver(serial, -1, -1, -1, -1, nullptr, HIGH, LOW, 115200) {} + AirohaGpsDriver(UART_TYPE& serial) + : NmeaGpsDriver(serial, -1, -1, -1, -1, nullptr, HIGH, LOW, GPS_BAUD_RATE) {} void initialize() { NmeaGpsDriver::begin(); diff --git a/src/helpers/sensors/AirohaSleep.h b/src/helpers/sensors/AirohaSleep.h deleted file mode 100644 index 6637d0fb..00000000 --- a/src/helpers/sensors/AirohaSleep.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once -#include - -// Board must keep the GPS RTC backup pin powered and can cut GPS power once we return. -// Returns true if the command was acked within 3 attempts, otherwise returns false. -static inline bool airohaEnterSleep(LocationProvider* nmea) { - for (uint8_t attempt = 0; attempt < 3; attempt++) { - nmea->drain(); - nmea->sendSentence("$PAIR650,0"); - if (nmea->waitFor("$PAIR001,650,0", 50)) { // wait for the command received signal - #ifdef GPS_NMEA_DEBUG - Serial.printf("Airoha RTC Backup sleep command accepted by GPS after %u attempts\r\n", attempt + 1); - #endif - nmea->waitFor("$PAIR650,0", 50); // give the GPS 50ms grace to signal it is ready for sleep - return true; - } - } - return false; -} \ No newline at end of file diff --git a/src/helpers/sensors/MicroNMEALocationProvider.h b/src/helpers/sensors/MicroNMEALocationProvider.h index 3c2adc6e..db279acb 100644 --- a/src/helpers/sensors/MicroNMEALocationProvider.h +++ b/src/helpers/sensors/MicroNMEALocationProvider.h @@ -119,15 +119,7 @@ public: // Temporary migration adapter for targets and downstream boards. New code // should inject a GpsDriver; this constructor will be removed once targets // have been migrated. - MicroNMEALocationProvider(HardwareSerial& serial, mesh::RTCClock* clock = nullptr, - int pin_reset = GPS_RESET, int pin_en = GPS_EN, - RefCountedDigitalPin* peripher_power = nullptr) - : nmea(_nmeaBuffer, sizeof(_nmeaBuffer)), _clock(clock), - _driver(new NmeaGpsDriver(serial, PIN_GPS_TX, PIN_GPS_RX, pin_reset, pin_en, - peripher_power)), - _ownsDriver(true) {} - - MicroNMEALocationProvider(Stream& serial, mesh::RTCClock* clock = nullptr, + MicroNMEALocationProvider(UART_TYPE& serial, mesh::RTCClock* clock = nullptr, int pin_reset = GPS_RESET, int pin_en = GPS_EN, RefCountedDigitalPin* peripher_power = nullptr) : nmea(_nmeaBuffer, sizeof(_nmeaBuffer)), _clock(clock), diff --git a/src/helpers/sensors/NmeaGpsDriver.h b/src/helpers/sensors/NmeaGpsDriver.h index abecb817..25acff45 100644 --- a/src/helpers/sensors/NmeaGpsDriver.h +++ b/src/helpers/sensors/NmeaGpsDriver.h @@ -5,6 +5,14 @@ #include #include +#ifndef UART_TYPE +#ifdef NRF52_PLATFORM + #define UART_TYPE Uart +#else + #define UART_TYPE HardwareSerial +#endif +#endif + #ifndef PIN_GPS_TX #define PIN_GPS_TX (-1) #endif @@ -46,7 +54,11 @@ #endif #ifndef GPS_BAUD_RATE - #define GPS_BAUD_RATE 9600 + #ifdef GPS_BAUDRATE + #define GPS_BAUD_RATE GPS_BAUDRATE + #else + #define GPS_BAUD_RATE 9600 + #endif #endif #ifndef PIN_GPS_SWITCH @@ -66,8 +78,8 @@ #endif class NmeaGpsDriver : public GpsDriver { - Stream* _stream = nullptr; - HardwareSerial* _hardwareSerial = nullptr; + UART_TYPE* _hardwareSerial = nullptr; + RefCountedDigitalPin* _peripheralPower = nullptr; int _txPin = -1; int _rxPin = -1; @@ -87,12 +99,12 @@ class NmeaGpsDriver : public GpsDriver { #endif void startHardware() { - if (_stream == nullptr || _started) return; + if (_hardwareSerial == nullptr || _started) return; if (_hardwareSerial != nullptr) { -#ifdef ESP32_PLATFORM - _hardwareSerial->setPins(_txPin, _rxPin); -#endif + if (_txPin != -1 && _rxPin != -1) { + _hardwareSerial->setPins(_txPin, _rxPin); + } _hardwareSerial->begin(_baudRate); } @@ -116,14 +128,14 @@ class NmeaGpsDriver : public GpsDriver { public: NmeaGpsDriver() = default; - NmeaGpsDriver(Stream& stream, int txPin = PIN_GPS_TX, int rxPin = PIN_GPS_RX, + NmeaGpsDriver(UART_TYPE& serial, int txPin = PIN_GPS_TX, int rxPin = PIN_GPS_RX, int resetPin = GPS_RESET, int enablePin = GPS_EN, RefCountedDigitalPin* peripheralPower = nullptr, int enableActive = GPS_EN_ACTIVE, int resetActive = GPS_RESET_ACTIVE, uint32_t baudRate = GPS_BAUD_RATE, int hardwareEnablePin = PIN_GPS_SWITCH, int hardwareEnableActive = GPS_SWITCH_ACTIVE) - : _stream(&stream), _peripheralPower(peripheralPower), _txPin(txPin), _rxPin(rxPin), + : _hardwareSerial(&serial), _peripheralPower(peripheralPower), _txPin(txPin), _rxPin(rxPin), _resetPin(resetPin), _enablePin(enablePin), _enableActive(enableActive), _resetActive(resetActive), _baudRate(baudRate), _hardwareEnablePin(hardwareEnablePin), _hardwareEnableActive(hardwareEnableActive) { @@ -140,19 +152,6 @@ public: } } - NmeaGpsDriver(HardwareSerial& serial, int txPin = PIN_GPS_TX, int rxPin = PIN_GPS_RX, - int resetPin = GPS_RESET, int enablePin = GPS_EN, - RefCountedDigitalPin* peripheralPower = nullptr, - int enableActive = GPS_EN_ACTIVE, int resetActive = GPS_RESET_ACTIVE, - uint32_t baudRate = GPS_BAUD_RATE, - int hardwareEnablePin = PIN_GPS_SWITCH, - int hardwareEnableActive = GPS_SWITCH_ACTIVE) - : NmeaGpsDriver(static_cast(serial), txPin, rxPin, resetPin, enablePin, - peripheralPower, enableActive, resetActive, baudRate, - hardwareEnablePin, hardwareEnableActive) { - _hardwareSerial = &serial; - } - void begin() override { #if !GPS_SWITCH_IS_HARDWARE _requestedOn = true; @@ -232,11 +231,11 @@ public: } int available() { - return _stream == nullptr ? 0 : _stream->available(); + return _hardwareSerial == nullptr ? 0 : _hardwareSerial->available(); } int read() { - return _stream == nullptr ? -1 : _stream->read(); + return _hardwareSerial == nullptr ? -1 : _hardwareSerial->read(); } void drain() { @@ -244,19 +243,19 @@ public: } void sendNmeaSentence(const char* sentence) { - if (_stream == nullptr || sentence == nullptr || sentence[0] != '$') return; + if (_hardwareSerial == nullptr || sentence == nullptr || sentence[0] != '$') return; uint8_t checksum = 0; const char* p = sentence + 1; while (*p != '\0' && *p != '*') checksum ^= (uint8_t)*p++; - _stream->print(sentence); + _hardwareSerial->print(sentence); if (*p == '\0') { char checksumSuffix[6]; snprintf(checksumSuffix, sizeof(checksumSuffix), "*%02X\r\n", checksum); - _stream->print(checksumSuffix); + _hardwareSerial->print(checksumSuffix); } else { - _stream->print("\r\n"); + _hardwareSerial->print("\r\n"); } } }; diff --git a/src/helpers/sensors/RakLocationProvider.h b/src/helpers/sensors/RakLocationProvider.h index 7deddf43..094e70ec 100644 --- a/src/helpers/sensors/RakLocationProvider.h +++ b/src/helpers/sensors/RakLocationProvider.h @@ -29,7 +29,7 @@ class RakLocationProvider : public LocationProvider { public: RakLocationProvider(RakI2cGpsDriver& driver, mesh::RTCClock* clock = nullptr, - HardwareSerial& serial = Serial1) + UART_TYPE& serial = Serial1) : _driver(driver), _nmeaDriver(serial), _nmea(_nmeaDriver, clock) {} void syncTime() override { diff --git a/variants/heltec_tracker/platformio.ini b/variants/heltec_tracker/platformio.ini index 75d1cadf..3affd8ae 100644 --- a/variants/heltec_tracker/platformio.ini +++ b/variants/heltec_tracker/platformio.ini @@ -31,8 +31,8 @@ build_flags = -D USE_PIN_TFT=1 -D PIN_VEXT_EN=3 ; Vext is connected to VDD which is also connected to OLED & GPS -D PIN_TFT_LEDA_CTL=21 ; LEDK (switches on/off via mosfet to create the ground) - -D PIN_GPS_RX=33 - -D PIN_GPS_TX=34 + -D PIN_GPS_TX=33 ; GPS module TX -> MCU RX (GPIO33) + -D PIN_GPS_RX=34 ; GPS module RX <- MCU TX (GPIO34) -D PIN_GPS_EN=35 ; N-ch MOSFET Q2 drives P-ch high-side switch → active HIGH (default) -D PIN_GPS_RESET=36 -D PIN_GPS_RESET_ACTIVE=LOW diff --git a/variants/heltec_tracker/target.cpp b/variants/heltec_tracker/target.cpp index 041cb2b8..67f98306 100644 --- a/variants/heltec_tracker/target.cpp +++ b/variants/heltec_tracker/target.cpp @@ -23,9 +23,14 @@ public: : NmeaGpsDriver(Serial1, PIN_GPS_TX, PIN_GPS_RX, GPS_RESET, GPS_EN, &board.periph_power) {} - void begin() override { - NmeaGpsDriver::begin(); - sendNmeaSentence("$CFGSYS,h35155*68"); + void reset() override { + NmeaGpsDriver::reset(); + // Send configuration after reset + if (isActive()) { + delay(250); + sendNmeaSentence("$CFGSYS,h35155*68"); + // TODO: remove active wait (250ms is a lot) + } } }; @@ -64,8 +69,7 @@ void HWTSensorManager::stop_gps() { } bool HWTSensorManager::begin() { - // init GPS port - Serial1.begin(115200, SERIAL_8N1, PIN_GPS_RX, PIN_GPS_TX); + // the GPS UART (pins, baud rate) is now configured by gps_driver when the GPS is started return true; } diff --git a/variants/meshadventurer/platformio.ini b/variants/meshadventurer/platformio.ini index 4af0f7f7..5e35251c 100644 --- a/variants/meshadventurer/platformio.ini +++ b/variants/meshadventurer/platformio.ini @@ -23,8 +23,8 @@ build_flags = -D SX126X_DIO2_AS_RF_SWITCH=false -D SX126X_DIO3_TCXO_VOLTAGE=1.8 -D SX126X_RX_BOOSTED_GAIN=1 - -D PIN_GPS_RX=12 - -D PIN_GPS_TX=15 + -D PIN_GPS_TX=12 ; GPS module TX -> MCU RX (GPIO12) + -D PIN_GPS_RX=15 ; GPS module RX <- MCU TX (GPIO15) -D DISPLAY_CLASS=SSD1306Display build_src_filter = ${esp32_base.build_src_filter} +<../variants/meshadventurer> diff --git a/variants/meshadventurer/target.cpp b/variants/meshadventurer/target.cpp index 827e23b7..1ccfe151 100644 --- a/variants/meshadventurer/target.cpp +++ b/variants/meshadventurer/target.cpp @@ -48,9 +48,7 @@ void MASensorManager::stop_gps() { } bool MASensorManager::begin() { - Serial1.setPins(PIN_GPS_RX, PIN_GPS_TX); - Serial1.begin(9600); - delay(500); + // the GPS UART (pins, baud rate) is now configured by gps_driver when the GPS is started return true; } diff --git a/variants/nano_g2_ultra/NanoG2UltraGpsDriver.h b/variants/nano_g2_ultra/NanoG2UltraGpsDriver.h index 7e3bd109..76270d32 100644 --- a/variants/nano_g2_ultra/NanoG2UltraGpsDriver.h +++ b/variants/nano_g2_ultra/NanoG2UltraGpsDriver.h @@ -4,7 +4,7 @@ class NanoG2UltraGpsDriver : public NmeaGpsDriver { public: - NanoG2UltraGpsDriver(HardwareSerial& serial) : NmeaGpsDriver(serial) { + NanoG2UltraGpsDriver(UART_TYPE& serial) : NmeaGpsDriver(serial) { pinMode(PIN_GPS_STANDBY, OUTPUT); digitalWrite(PIN_GPS_STANDBY, LOW); } diff --git a/variants/t1000-e/variant.h b/variants/t1000-e/variant.h index bd509073..17a5538c 100644 --- a/variants/t1000-e/variant.h +++ b/variants/t1000-e/variant.h @@ -117,6 +117,8 @@ #define GPS_RTC_INT (15) // P0.15 #define GPS_RESETB (46) // P1.14 +#define GPS_BAUDRATE 115200 + //////////////////////////////////////////////////////////////////////////////// // Temp+Lux Sensor From d41aee832e4252ebe2ab87899ee55f1217b798fe Mon Sep 17 00:00:00 2001 From: Florent Date: Wed, 4 Nov 2026 20:34:28 -0400 Subject: [PATCH 25/53] gps: guard the clock sync against stalled and too old fixes --- src/helpers/sensors/MicroNMEALocationProvider.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/helpers/sensors/MicroNMEALocationProvider.h b/src/helpers/sensors/MicroNMEALocationProvider.h index db279acb..dddcce19 100644 --- a/src/helpers/sensors/MicroNMEALocationProvider.h +++ b/src/helpers/sensors/MicroNMEALocationProvider.h @@ -70,6 +70,8 @@ class MicroNMEALocationProvider : public LocationProvider { void finishPeriodicAcquisition() { if (_firstFixInWindow == 0) fixStatsMiss(); _driver->stop(); + nmea.clear(); // the parser keeps the last time as valid otherwise + time_valid = 0; _acquiringPeriodically = false; _nextAcquisition = millis() + _gpsPeriodSeconds * 1000UL; } @@ -95,7 +97,7 @@ class MicroNMEALocationProvider : public LocationProvider { } void updateTime() { - if (!isValid()) time_valid = 0; + if (!isValid() || (millis() - _last_nmea_ms) > 2000) time_valid = 0; // no stale time if ((long)(millis() - next_check) <= 0) return; next_check = millis() + 1000; @@ -156,6 +158,8 @@ public: _stop_calls++; _acquiringPeriodically = false; _driver->stop(); + nmea.clear(); + time_valid = 0; } bool isActive() override { @@ -274,7 +278,9 @@ public: fixStatsFix((uint32_t)(_firstFixInWindow - _acquisitionStarted)); } if (_acquiringPeriodically) { - if (nmea.getNumSatellites() >= GPS_MIN_SATS_FOR_FIX || + // stay on until a pending time sync is done (or the settle time is over) + const bool synced = _clock == nullptr || !_time_sync_needed; + if ((nmea.getNumSatellites() >= GPS_MIN_SATS_FOR_FIX && synced) || (uint32_t)(millis() - _firstFixInWindow) >= GPS_FIX_SETTLE_MS) { finishPeriodicAcquisition(); } From 539b55b7724fd56ccf1880fd9081b9151e0155d5 Mon Sep 17 00:00:00 2001 From: Florent Date: Thu, 5 Nov 2026 10:16:28 -0400 Subject: [PATCH 26/53] gps: move detection into LocationProvider, remove the legacy constructor --- .../sensors/EnvironmentSensorManager.cpp | 23 ++----------------- src/helpers/sensors/LocationProvider.h | 16 +++++++++++++ .../sensors/MicroNMEALocationProvider.h | 16 ------------- 3 files changed, 18 insertions(+), 37 deletions(-) diff --git a/src/helpers/sensors/EnvironmentSensorManager.cpp b/src/helpers/sensors/EnvironmentSensorManager.cpp index ee942df1..eafa11a3 100644 --- a/src/helpers/sensors/EnvironmentSensorManager.cpp +++ b/src/helpers/sensors/EnvironmentSensorManager.cpp @@ -696,31 +696,12 @@ bool EnvironmentSensorManager::setSettingValue(const char* name, const char* val #if ENV_INCLUDE_GPS void EnvironmentSensorManager::initBasicGPS() { - start_gps(); - #ifndef PIN_GPS_EN MESH_DEBUG_PRINTLN("No GPS wake/reset pin found for this board. Continuing on..."); #endif -#ifdef ENV_SKIP_GPS_DETECT - gps_detected = true; -#else - gps_detected = _location->detect(5000); -#endif - - if (gps_detected) { - MESH_DEBUG_PRINTLN("GPS detected"); - #if defined(PERSISTANT_GPS) - return; - #else - if (_location->keepEnabledAfterDetection()) { - return; - } - #endif - } else { - MESH_DEBUG_PRINTLN("No GPS detected"); - } - stop_gps(); + gps_detected = _location->autoDetect(); + MESH_DEBUG_PRINTLN("%s", gps_detected ? "GPS detected" : "No GPS detected"); } void EnvironmentSensorManager::start_gps() { diff --git a/src/helpers/sensors/LocationProvider.h b/src/helpers/sensors/LocationProvider.h index 3d7abd7a..ad2bba1d 100644 --- a/src/helpers/sensors/LocationProvider.h +++ b/src/helpers/sensors/LocationProvider.h @@ -61,6 +61,22 @@ public: virtual void drain() { } virtual bool detect(uint32_t timeout_ms) { return false; } virtual bool keepEnabledAfterDetection() const { return false; } + // Power on, detect, then leave the GPS on or off as the board wants. + // Returns true if a GPS is present. + virtual bool autoDetect() { + setGpsOn(true); + #ifdef ENV_SKIP_GPS_DETECT + bool found = true; + #else + bool found = detect(5000); + #endif + #ifdef PERSISTANT_GPS + if (found) return true; + #endif + if (found && keepEnabledAfterDetection()) return true; + setGpsOn(false); + return found; + } virtual void setGpsPeriod(uint32_t seconds) { } virtual uint32_t getGpsPeriod() const { return 0; } virtual uint32_t getLastFixAge() const { // in seconds, UINT32_MAX if never diff --git a/src/helpers/sensors/MicroNMEALocationProvider.h b/src/helpers/sensors/MicroNMEALocationProvider.h index dddcce19..ea87ad19 100644 --- a/src/helpers/sensors/MicroNMEALocationProvider.h +++ b/src/helpers/sensors/MicroNMEALocationProvider.h @@ -24,7 +24,6 @@ class MicroNMEALocationProvider : public LocationProvider { MicroNMEA nmea; mesh::RTCClock* _clock; NmeaGpsDriver* _driver; - bool _ownsDriver = false; unsigned long next_check = 0; long time_valid = 0; unsigned long _last_time_sync = 0; @@ -118,21 +117,6 @@ public: MicroNMEALocationProvider(NmeaGpsDriver& driver, mesh::RTCClock* clock = nullptr) : nmea(_nmeaBuffer, sizeof(_nmeaBuffer)), _clock(clock), _driver(&driver) {} - // Temporary migration adapter for targets and downstream boards. New code - // should inject a GpsDriver; this constructor will be removed once targets - // have been migrated. - MicroNMEALocationProvider(UART_TYPE& serial, mesh::RTCClock* clock = nullptr, - int pin_reset = GPS_RESET, int pin_en = GPS_EN, - RefCountedDigitalPin* peripher_power = nullptr) - : nmea(_nmeaBuffer, sizeof(_nmeaBuffer)), _clock(clock), - _driver(new NmeaGpsDriver(serial, PIN_GPS_TX, PIN_GPS_RX, pin_reset, pin_en, - peripher_power)), - _ownsDriver(true) {} - - ~MicroNMEALocationProvider() override { - if (_ownsDriver) delete _driver; - } - void begin() override { _begin_calls++; const bool wasActive = _driver->isActive(); From 93d165dd8cdc72301870ec1e8a83fd64f69ef899 Mon Sep 17 00:00:00 2001 From: Florent Date: Sat, 7 Nov 2026 09:05:11 -0400 Subject: [PATCH 27/53] gps: once command --- src/helpers/CommonGpsPrefs.cpp | 27 ++++++++++++++++++ src/helpers/sensors/LocationProvider.h | 1 + .../sensors/MicroNMEALocationProvider.h | 28 +++++++++++++++++++ src/helpers/sensors/RakLocationProvider.h | 8 ++++++ 4 files changed, 64 insertions(+) diff --git a/src/helpers/CommonGpsPrefs.cpp b/src/helpers/CommonGpsPrefs.cpp index c4936d4f..926efe71 100644 --- a/src/helpers/CommonGpsPrefs.cpp +++ b/src/helpers/CommonGpsPrefs.cpp @@ -97,6 +97,33 @@ bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors, } return true; } + if (memcmp(command, "gps once", 8) == 0) { // before "gps on" + LocationProvider* location = sensors.getLocationProvider(); + + if (location == nullptr) { + strcpy(reply, "gps provider not found"); + return true; + } + + if (location->hasHardwareEnableState() && !location->isHardwareEnabled()) { + strcpy(reply, "gps hw switch off"); + return true; + } + + if (strstr(command, " -s") != nullptr) location->syncTime(); + + const char* state = !location->isGpsOn() ? "turning on and acquiring" + : (location->getGpsPeriod() > 0 && !location->isActive()) ? "waking gps" + : "gps already on"; + + if (location->startOnce()) { + snprintf(reply, 160, "ok (%s)", state); + } else { + strcpy(reply, "gps once not supported"); + } + + return true; + } if (memcmp(command, "gps on", 6) == 0) { if (!setGpsEnabled(sensors, true)) { strcpy(reply, "gps toggle not found"); diff --git a/src/helpers/sensors/LocationProvider.h b/src/helpers/sensors/LocationProvider.h index ad2bba1d..b15c0ed2 100644 --- a/src/helpers/sensors/LocationProvider.h +++ b/src/helpers/sensors/LocationProvider.h @@ -86,6 +86,7 @@ public: virtual bool hasHardwareEnableState() const { return false; } virtual bool isHardwareEnabled() const { return true; } virtual bool isGpsOn() const { return _gpsOn; } + virtual bool startOnce() { return false; } virtual void setGpsOn(bool on) { _gpsOn = on; if (on) { diff --git a/src/helpers/sensors/MicroNMEALocationProvider.h b/src/helpers/sensors/MicroNMEALocationProvider.h index ea87ad19..9a6d8006 100644 --- a/src/helpers/sensors/MicroNMEALocationProvider.h +++ b/src/helpers/sensors/MicroNMEALocationProvider.h @@ -32,6 +32,7 @@ class MicroNMEALocationProvider : public LocationProvider { unsigned long _acquisitionStarted = 0; unsigned long _firstFixInWindow = 0; bool _acquiringPeriodically = false; + unsigned long _onceDeadline = 0; static const unsigned long TIME_SYNC_INTERVAL = 1800000; uint32_t _uart_bytes = 0; @@ -119,6 +120,7 @@ public: void begin() override { _begin_calls++; + _onceDeadline = 0; const bool wasActive = _driver->isActive(); _driver->begin(); if (!wasActive) { @@ -140,6 +142,7 @@ public: void stop() override { _stop_calls++; + _onceDeadline = 0; _acquiringPeriodically = false; _driver->stop(); nmea.clear(); @@ -172,6 +175,16 @@ public: return _gpsPeriodSeconds; } + bool startOnce() override { + if (_gpsOn) { // already on, acquire now + _nextAcquisition = millis(); + return true; + } + setGpsOn(true); + _onceDeadline = millis() + GPS_PERIODIC_ACQUISITION_TIMEOUT_MS; + return true; + } + void syncTime() override { nmea.clear(); LocationProvider::syncTime(); @@ -232,6 +245,21 @@ public: void loop() override { if (_driver->pollGpsSwitch()) _gpsOn = true; + if (_onceDeadline != 0) { + if (_firstFixInWindow != 0 && (long)(_onceDeadline - (millis() + GPS_FIX_SETTLE_MS)) > 0) { + _onceDeadline = millis() + GPS_FIX_SETTLE_MS; + } + if ((long)(millis() - _onceDeadline) >= 0) { + if (_driver->isActive()) { + setGpsOn(false); + } else { // do not stop twice + _gpsOn = false; + _onceDeadline = 0; + _acquiringPeriodically = false; + } + } + } + if (_gpsOn && _gpsPeriodSeconds > 0 && !_acquiringPeriodically && (long)(millis() - _nextAcquisition) >= 0) { startPeriodicAcquisition(); diff --git a/src/helpers/sensors/RakLocationProvider.h b/src/helpers/sensors/RakLocationProvider.h index 094e70ec..6ad05125 100644 --- a/src/helpers/sensors/RakLocationProvider.h +++ b/src/helpers/sensors/RakLocationProvider.h @@ -190,6 +190,14 @@ public: return _gpsPeriodSeconds; } + bool isGpsOn() const override { + return _useI2c ? _gpsOn : _nmea.isGpsOn(); + } + + bool startOnce() override { + return _useI2c ? false : _nmea.startOnce(); // not supported on the RAK12500 + } + void setGpsOn(bool on) override { if (_useI2c) LocationProvider::setGpsOn(on); else { _gpsOn = on; _nmea.setGpsOn(on); } From 6b34ae96dfd7df221911705e10077fd82d83269b Mon Sep 17 00:00:00 2001 From: Florent Date: Sat, 7 Nov 2026 13:45:40 -0400 Subject: [PATCH 28/53] gps: Rework the CLI - more consistency with other cli commands - new format for gps fix - gps format accepts postfix (s/m/d/h) - documentation --- docs/cli_commands.md | 27 +++++- docs/companion_cli.md | 27 +++++- src/helpers/CommonCLI.cpp | 12 +-- src/helpers/CommonGpsPrefs.cpp | 89 ++++++++++--------- src/helpers/sensors/LocationProvider.h | 1 + .../sensors/MicroNMEALocationProvider.h | 4 + src/helpers/sensors/RakLocationProvider.h | 4 + 7 files changed, 110 insertions(+), 54 deletions(-) diff --git a/docs/cli_commands.md b/docs/cli_commands.md index d7b8d5d0..44ff3a25 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md @@ -1021,10 +1021,10 @@ region save #### View or change the GPS acquisition period **Usage:** - `gps period` -- `gps period ` +- `gps period ` **Parameters:** -- `seconds`: `0` for continuous tracking, or `1`-`86400` for periodic acquisition +- `value`: `0` for continuous tracking, or a period. The value can be postfixed with `s`, `m`, `h` or `d` to specify the unit (seconds by default). **Note:** A periodic acquisition stops after receiving a valid fix, then resumes after the configured period. @@ -1034,7 +1034,12 @@ region save **Usage:** - `gps fix` -**Note:** Returns the last valid latitude, longitude, altitude, and elapsed time since the fix. Returns `no fix` until a valid fix is received. +**Output:** +- `> Fix (n sat): , `: the GPS currently has a fix. +- `> Last fix ago (n sat)[, acq