diff --git a/docs/gps.md b/docs/gps.md new file mode 100644 index 0000000..e69de29 diff --git a/exercises/12_FiveTalk/platformio.ini b/exercises/12_FiveTalk/platformio.ini index 0eff093..84b0b70 100644 --- a/exercises/12_FiveTalk/platformio.ini +++ b/exercises/12_FiveTalk/platformio.ini @@ -28,6 +28,7 @@ build_flags = -D GPS_WAKEUP_PIN=7 -D GPS_1PPS_PIN=6 -D GPS_L76K + -D NODE_SLOT_COUNT=7 -D LORA_CS=10 -D LORA_MOSI=11 -D LORA_SCK=12 @@ -94,4 +95,6 @@ build_flags = -D NODE_LABEL=\"Guy\" -D NODE_SHORT=\"G\" -D NODE_SLOT_INDEX=6 + -D GPS_UBLOX + diff --git a/exercises/12_FiveTalk/src/main.cpp b/exercises/12_FiveTalk/src/main.cpp index 8c612cd..b292aab 100644 --- a/exercises/12_FiveTalk/src/main.cpp +++ b/exercises/12_FiveTalk/src/main.cpp @@ -27,6 +27,10 @@ #define NODE_SHORT "?" #endif +#ifndef NODE_SLOT_COUNT +#define NODE_SLOT_COUNT 7 +#endif + #ifndef NODE_SLOT_INDEX #define NODE_SLOT_INDEX 0 #endif @@ -63,8 +67,8 @@ #define FW_BUILD_UTC "unknown" #endif -#if (NODE_SLOT_INDEX < 0) || (NODE_SLOT_INDEX > 6) -#error "NODE_SLOT_INDEX must be 0..6" +#if (NODE_SLOT_INDEX < 0) || (NODE_SLOT_INDEX >= NODE_SLOT_COUNT) +#error "NODE_SLOT_INDEX must be 0..NODE_SLOT_COUNT-1" #endif static const uint32_t kSerialDelayMs = 1000; @@ -156,6 +160,20 @@ static uint8_t bestSatelliteCount() return (g_gps.satsUsed > g_gps.satsInView) ? g_gps.satsUsed : g_gps.satsInView; } +static uint32_t computeFrameSeconds(uint32_t requiredSeconds) +{ + uint32_t frame = ((requiredSeconds + 4U) / 5U) * 5U; // round up to 5s + while (frame <= 60U && (60U % frame) != 0U) + { + frame += 5U; + } + if (frame == 0U || frame > 60U) + { + frame = 60U; // fallback + } + return frame; +} + static void logf(const char *fmt, ...) { char msg[256]; @@ -1030,7 +1048,7 @@ static bool initRadio() return false; } - logf("Radio ready for %s (%s), slot=%d sec=%d", NODE_LABEL, NODE_SHORT, NODE_SLOT_INDEX, NODE_SLOT_INDEX * 2); + logf("Radio ready for %s (%s), slot=%d/%d (2s each)", NODE_LABEL, NODE_SHORT, NODE_SLOT_INDEX, NODE_SLOT_COUNT); return true; } @@ -1051,8 +1069,15 @@ static void runTxScheduler() if (!getCurrentUtc(now, epoch)) return; - int slotSecond = NODE_SLOT_INDEX * (int)kSlotSeconds; - int secInFrame = now.second % 10; + uint32_t requiredTxSeconds = (uint32_t)NODE_SLOT_COUNT * kSlotSeconds; + uint32_t frameSeconds = computeFrameSeconds(requiredTxSeconds); + uint32_t slotSecond = (uint32_t)NODE_SLOT_INDEX * kSlotSeconds; + if (slotSecond >= frameSeconds) + return; + + uint32_t secInFrame = (uint32_t)now.second % frameSeconds; + if (secInFrame >= requiredTxSeconds) + return; // idle guard interval if (secInFrame != slotSecond) return;