// SPDX-License-Identifier: Apache-2.0 // Copyright (c) 2012-2024 Scott Penrose and WII5 Buoy contributors // // This file is part of WII5 Buoy firmware. // See LICENSE for full terms. /** * @file WII5ModePosition.cpp * @brief Position mode: periodic GPS + Iridium telemetry only (low power). */ /* Position mode - a simple repeatign mode that will capture position data and send it. */ #include #include #include void WII5ModePosition::reset() { step = WII5POSITION_START; wait = 0; } void WII5ModePosition::begin() { } void WII5ModePosition::start() { step = WII5POSITION_STROBE; wait = 0; } void WII5ModePosition::stop() { step = WII5POSITION_END; wait = 0; } void WII5ModePosition::loop() { bool first = (lastStep != step); lastStep = step; switch (step) { case WII5POSITION_START: if (first) { // TODO This seems bad, perhaps only if next is 10 minutes? // First time in, turn off devices we don't need. // Can we turn off Maths. sh3dNodeIO.led1Set( LED_SHORT ); wii5Controller.setSDOff(); wii5Gps.off(); wii5Iridium.stop(); wii5Sparton.stop(); wii5Controller.shared5Off(); console.log(LOG_INFO, F("Position: Waiting for %lu minutes"), wii5Display.minutesUntilNext(wii5Config.getPositionPeriod()) ); } else if ((wii5Gps.sinceOnLast > (WII5TIME_1HOUR * 1000)) && (!wii5Gps.isTimeValid())) { console.log(LOG_ERROR, F("Position: Internal clock / time is not valid, turn on GPS")); step = WII5POSITION_TIME; wait = 0; } else if (wait > (WII5TIME_1DAY * 1000)) { console.log(LOG_ERROR, F("Position: Timeout - over 24 hours, moving on to do a location and data send")); step = WII5POSITION_STROBE; wait = 0; } else { // TODO - reduce this complicated task ? Lots of maths, being run at full speed minutes = (hour() * 60) + minute(); if ( (minutes % wii5Config.getPositionPeriod()) == 0) { console.log(LOG_INFO, F("Position: Starting minutes match period min=%lu, period=%lu"), minutes, wii5Config.getPositionPeriod()); step = WII5POSITION_STROBE; wait = 0; } } // TODO Sleep ? break; case WII5POSITION_STROBE: // TODO - Manage the strobes // console.log(LOG_DEBUG, F("Position: strobe -> gps_start (skipping)")); if (first) { wii5Config.updateRecordCount(); console.log(LOG_DEBUG, F("Position: New recordcount=%lu"), wii5Config.getRecordCount()); } step = WII5POSITION_GPS_START; wait = 0; break; case WII5POSITION_TIME: if (first) { wii5Gps.autoTime(); } else if (wii5Gps.ready()) { step = WII5POSITION_START; wait = 0; } else if (wait > 300000) { step = WII5POSITION_START; wait = 0; } break; case WII5POSITION_GPS_START: // Ask the GPS for accurate data console.log(LOG_DEBUG, F("Position: Starting GPS, Battery, Temperature")); #ifdef WII5_GPS wii5Gps.autoAccurate(); #endif wii5Battery.start(); wii5Weather_18B20.temperatureRead(); step = WII5POSITION_GPS_WAIT; wait = 0; break; case WII5POSITION_GPS_WAIT: if (first) { displayWait = 0; } else if (wii5Gps.ready()) { if (wii5Gps.isError()) { console.log(LOG_ERROR, F("Position: GPS Finished with ERROR")); } wii5Gps.off(); step = WII5POSITION_IRIDIUM_WAIT; wait = 0; } else if (wait > 300000) { console.log(LOG_ERROR, F("Position: GPS Timed out after 300 seconds")); step = WII5POSITION_IRIDIUM_WAIT; wait = 0; } else if (displayWait > 10000) { console.log(LOG_INFO, F("Position: Waiting for GPS Lock - %lu seconds"), (wait / 1000)); displayWait = 0; } break; case WII5POSITION_IRIDIUM_WAIT: if (first) { console.log(LOG_INFO, F("Position: Communications start")); wii5Communications.setSimpleMode(); // Increment updateRecordCount for each save to SD OR send to Iridium wii5Communications.sendBinModeType(wii5Config.getPositionBinaryType(), wii5Config.getRecordCount()); wii5Communications.start(); } else if (!wii5Communications.isRunning()) { console.log(LOG_INFO, F("Position: Communications end")); step = WII5POSITION_END; wait = 0; } // TODO 10 minutes ok else if (wait > 600000) { console.log(LOG_FATAL, F("Position: Communications timeout")); step = WII5POSITION_END; wait = 0; } break; case WII5POSITION_END: if (first) { console.log(LOG_DEBUG, F("Position: End")); } wii5Communications.stop(); wii5Gps.off(); // Stop everything wii5Controller.setSDOff(); wii5Sparton.stop(); wii5Controller.shared5Off(); step = WII5POSITION_START; wait = 0; break; default: console.log(LOG_FATAL, F("POSITION: Default step... step=%d"), step); step = WII5POSITION_START; } } WII5ModePosition wii5ModePosition;