// 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 WII5ModeLowBattery.cpp * @brief Low-battery mode: degraded operation when supply is depleted. */ /* LowBattery mode - a simple repeatign mode that will capture position data and send it. */ #include #include #include void WII5ModeLowBattery::reset() { step = WII5LOWBATTERY_START; wait = 0; } void WII5ModeLowBattery::begin() { } void WII5ModeLowBattery::start() { step = WII5LOWBATTERY_GPS_START; wait = 0; } void WII5ModeLowBattery::stop() { step = WII5LOWBATTERY_END; wait = 0; } void WII5ModeLowBattery::loop() { bool first = (lastStep != step); lastStep = step; switch (step) { case WII5LOWBATTERY_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_TRIPPLE_GAP ); wii5Controller.setSDOff(); wii5Gps.off(); wii5Iridium.stop(); wii5Sparton.stop(); wii5Controller.shared5Off(); console.log(LOG_INFO, F("LowBattery: Waiting for %lu minutes"), wii5Display.minutesUntilNext(40) ); } else if (wait > (WII5TIME_1DAY * 1000)) { console.log(LOG_ERROR, F("LowBattery: Timeout - over 24 hours, moving on to do a location and data send")); step = WII5LOWBATTERY_GPS_START; wait = 0; } else { // TODO - reduce this complicated task ? Lots of maths, being run at full speed minutes = (hour() * 60) + minute(); if ( (minutes % 40) == 0) { console.log(LOG_INFO, F("LowBattery: Starting minutes match period min=%lu, period=%lu"), minutes, 40); step = WII5LOWBATTERY_GPS_START; wait = 0; sh3dNodeIO.led1Set( LED_TRIPPLE_GAP ); } } break; case WII5LOWBATTERY_GPS_START: // Ask the GPS for accurate data console.log(LOG_DEBUG, F("LowBattery: Starting GPS, Battery, Temperature")); #ifdef WII5_GPS wii5Gps.autoAccurate(); #endif wii5Battery.start(); wii5Weather_18B20.temperatureRead(); step = WII5LOWBATTERY_GPS_WAIT; wait = 0; break; case WII5LOWBATTERY_GPS_WAIT: if (first) { displayWait = 0; } else if (wii5Gps.ready()) { if (wii5Gps.isError()) { console.log(LOG_ERROR, F("LowBattery: GPS Finished with ERROR")); } wii5Gps.off(); step = WII5LOWBATTERY_IRIDIUM_WAIT; wait = 0; } else if (wait > 120000) { console.log(LOG_ERROR, F("LowBattery: GPS Timed out after 300 seconds")); step = WII5LOWBATTERY_IRIDIUM_WAIT; wait = 0; } break; case WII5LOWBATTERY_IRIDIUM_WAIT: if (first) { console.log(LOG_INFO, F("LowBattery: Communications start")); wii5Communications.setSimpleMode(); // Increment updateRecordCount for each save to SD OR send to Iridium wii5Communications.sendBinModeType(1, wii5Config.getRecordCount()); wii5Communications.start(); } else if (!wii5Communications.isRunning()) { console.log(LOG_INFO, F("LowBattery: Communications end")); step = WII5LOWBATTERY_END; wait = 0; } // TODO 10 minutes ok else if (wait > 600000) { console.log(LOG_FATAL, F("LowBattery: Communications timeout")); step = WII5LOWBATTERY_END; wait = 0; } break; case WII5LOWBATTERY_END: if (first) { console.log(LOG_DEBUG, F("LowBattery: End")); } wii5Communications.stop(); wii5Gps.off(); // Stop everything wii5Controller.setSDOff(); wii5Sparton.stop(); wii5Controller.shared5Off(); step = WII5LOWBATTERY_START; wait = 0; break; default: console.log(LOG_FATAL, F("LOWBATTERY: Default step... step=%d"), step); step = WII5LOWBATTERY_START; } } WII5ModeLowBattery wii5ModeLowBattery;