This commit is contained in:
richonguzman 2024-07-24 21:17:56 -04:00
parent 2396fa4948
commit c119f1346f
7 changed files with 96 additions and 198 deletions

View File

@ -97,8 +97,8 @@ APRSPacket lastReceivedPacket;
logging::Logger logger;
//#define DEBUG
bool SleepModeActive = false;
bool gpsSleepActive = true;
extern bool gpsIsActive;
void setup() {
Serial.begin(115200);
@ -127,7 +127,6 @@ void setup() {
WiFi.mode(WIFI_OFF);
logger.log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, "Main", "WiFi controller stopped");
if (!SleepModeActive) {
if (Config.bluetoothType == 0 || Config.bluetoothType == 2) {
BLE_Utils::setup();
} else {
@ -145,9 +144,6 @@ void setup() {
#endif
KEYBOARD_Utils::setup();
}
} else {
SLEEP_Utils::setup();
}
POWER_Utils::lowerCpuFrequency();
logger.log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, "Main", "Smart Beacon is: %s", Utils::getSmartBeaconState());
@ -166,24 +162,6 @@ void loop() {
}
POWER_Utils::batteryManager();
if (SleepModeActive) {
if (wakeUpFlag) {
MSG_Utils::checkReceivedMessage(LoRa_Utils::receiveFromSleep());
wakeUpFlag = false;
}
//SLEEP_Utils::handle_wakeup();
//SLEEP_Utils::processBufferAfterSleep();
SLEEP_Utils::startSleep();
//if (!wakeUpByButton) SLEEP_Utils::startSleep();
//if (wakeUpByButton && (millis() - wakeUpByButtonTime > 10 * 1000)) wakeUpByButton = false;
} else {
//////////////////////////////////////////////////////////////////
// HERE STARTS NORMAL TRACKER CODE LOOP //
//////////////////////////////////////////////////////////////////
STATION_Utils::checkSmartBeaconValue();
if (!Config.simplifiedTrackerMode) {
@ -199,11 +177,6 @@ void loop() {
KEYBOARD_Utils::mouseRead();
#endif
GPS_Utils::getData();
bool gps_time_update = gps.time.isUpdated();
bool gps_loc_update = gps.location.isUpdated();
GPS_Utils::setDateFromData();
MSG_Utils::checkReceivedMessage(LoRa_Utils::receivePacket());
MSG_Utils::processOutputBuffer();
MSG_Utils::clean25SegBuffer();
@ -218,6 +191,12 @@ void loop() {
#endif
}
if (gpsIsActive) {
GPS_Utils::getData();
bool gps_time_update = gps.time.isUpdated();
bool gps_loc_update = gps.location.isUpdated();
GPS_Utils::setDateFromData();
int currentSpeed = (int) gps.speed.kmph();
if (gps_loc_update) {
@ -225,6 +204,7 @@ void loop() {
STATION_Utils::checkTelemetryTx();
}
lastTx = millis() - lastTxTime;
if (!sendUpdate && gps_loc_update && smartBeaconValue) {
GPS_Utils::calculateDistanceTraveled();
if (!sendUpdate) {
@ -241,5 +221,16 @@ void loop() {
MENU_Utils::showOnScreen();
refreshDisplayTime = millis();
}
} else {
if (millis() > lastTxTime + txInterval) {
SLEEP_Utils::gpsWakeUp();
Serial.println(txInterval);
}
if (millis() - refreshDisplayTime >= 1000) {
MENU_Utils::showOnScreen();
refreshDisplayTime = millis();
}
}
// si se activa GPS y no se envia en X tiempo , se duerme...
// crear contador de tiempo
}

View File

@ -4,6 +4,7 @@
#include "station_utils.h"
#include "boards_pinout.h"
#include "power_utils.h"
#include "sleep_utils.h"
#include "gps_utils.h"
#include "display.h"
#include "logger.h"

View File

@ -45,6 +45,7 @@ extern String winlinkAlias;
extern String winlinkAliasComplete;
extern bool winlinkCommentState;
extern int wxModuleType;
extern bool gpsIsActive;
String freqChangeWarning;
uint8_t lowBatteryPercent = 21;
@ -546,8 +547,12 @@ namespace MENU_Utils {
}
if (gps.satellites.value() <= 9) thirdRowMainMenu += " ";
if (gpsIsActive) {
thirdRowMainMenu += String(gps.satellites.value());
thirdRowMainMenu += hdopState;
} else {
thirdRowMainMenu += "--";
}
String fourthRowAlt = String(gps.altitude.meters(),0);
fourthRowAlt.trim();
@ -587,6 +592,9 @@ namespace MENU_Utils {
fourthRowMainMenu += String(MSG_Utils::getNumAPRSMessages());
fourthRowMainMenu += " ***";
}
if (!gpsIsActive) {
fourthRowMainMenu = "*** GPS SLEEPING ***";
}
}
if (showHumanHeading) {

View File

@ -319,11 +319,9 @@ namespace MSG_Utils {
} else { // message without ack Request
sendMessage(addressee, message);
outputMessagesBuffer.erase(outputMessagesBuffer.begin());
if (!SleepModeActive) {
lastTxTime = millis();
}
}
}
if (outputAckRequestBuffer.empty()) {
ackRequestState = false;
} else if (!outputAckRequestBuffer.empty() && (millis() - lastMsgRxTime) >= 4500 && (millis() - lastTxTime) > 3000) {

View File

@ -1,132 +1,32 @@
#include <TinyGPS++.h>
#include "boards_pinout.h"
#include "configuration.h"
#include "station_utils.h"
#include "sleep_utils.h"
#include "power_utils.h"
#include "lora_utils.h"
#include "msg_utils.h"
#include "gps_utils.h"
#include "display.h"
extern Configuration Config;
extern TinyGPSPlus gps;
extern uint32_t lastTxTime;
extern bool sendUpdate;
bool wakeUpFlag = false;
bool wakeUpByButton = false;
uint32_t wakeUpByButtonTime = 0;
bool gpsIsActive = true;
extern bool gpsSleepActive;
namespace SLEEP_Utils {
void processBeaconAfterSleep() {
if (lastTxTime == 0 || ((millis() - lastTxTime) > 5 * 60 * 1000)) { // 5 min non-smartBeacon
POWER_Utils::activateGPS();
display_toggle(false);
sendUpdate = true;
while (sendUpdate) {
MSG_Utils::checkReceivedMessage(LoRa_Utils::receivePacket());
GPS_Utils::getData();
bool gps_loc_update = gps.location.isUpdated();
if (gps_loc_update){
display_toggle(true); // opcional
STATION_Utils::sendBeacon(0);
lastTxTime = millis();
}
}
}
}
void processBufferAfterSleep() {
if (!MSG_Utils::checkOutputBufferEmpty()) {
//POWER_Utils::activateGPS();
//display_toggle(true);
MSG_Utils::processOutputBuffer();
}
}
void handle_wakeup() {
esp_sleep_wakeup_cause_t wakeup_cause = esp_sleep_get_wakeup_cause();
switch (wakeup_cause) {
case ESP_SLEEP_WAKEUP_TIMER:
processBeaconAfterSleep(); //Serial.println("Woken up by timer (Sending Beacon) \n");
break;
case ESP_SLEEP_WAKEUP_EXT1:
//Serial.println("Woken up by EXT1 (GPIO) (Packet Received)\n");
break;
case ESP_SLEEP_WAKEUP_EXT0:
Serial.println("Wakeup caused by external signal using RTC_IO");
wakeUpByButton = true;
wakeUpByButtonTime = millis();
POWER_Utils::activateGPS();
display_toggle(true);
default:
processBeaconAfterSleep(); //Serial.println("Woken up by unknown reason\n");
break;
}
}
void wakeUpLoRaPacketReceived() {
wakeUpFlag = true;
}
void sleep(int seconds) {
esp_sleep_enable_timer_wakeup(300 * 1000000);
//esp_sleep_enable_timer_wakeup(seconds * 1000000); // 1 min = 60sec
delay(100);
void gpsSleep() {
if (gpsSleepActive && gpsIsActive) {
POWER_Utils::deactivateGPS();
delay(100);
#ifdef ADC_CTRL
#ifdef HELTEC_WIRELESS_TRACKER
digitalWrite(ADC_CTRL, LOW);
#endif
#endif
LoRa_Utils::wakeRadio();
//LoRa_Utils::sleepRadio();
delay(100);
//esp_deep_sleep_start();
esp_light_sleep_start();
gpsIsActive = false;
//
Serial.println("GPS SLEEPING");
//
}
// this could be used for smartBeaconTime delta
/*uint32_t getTimeToSleep() { // quizas no?
uint32_t currentCycleTime = millis() - lastTxTime;
uint32_t timeToSleep = 20 * 1000;//Config.nonSmartBeaconRate * 60;
if (timeToSleep - currentCycleTime <= 0) {
return timeToSleep / 1000;
} else {
return (timeToSleep - currentCycleTime) / 1000;
}
}*/
void startSleep() {
#if defined(HELTEC_WIRELESS_TRACKER)
esp_sleep_enable_ext1_wakeup(WAKEUP_RADIO, ESP_EXT1_WAKEUP_ANY_HIGH);
//pinMode(BUTTON_PIN, INPUT_PULLUP); //internal pull down???
//esp_sleep_enable_ext0_wakeup(WAKEUP_BUTTON, 0);
#endif
sleep(20);
}
void setup() {
//if (SleepModeActive) ?????
#ifdef RADIO_WAKEUP_PIN
pinMode(RADIO_WAKEUP_PIN, INPUT);
#if defined(HELTEC_WIRELESS_TRACKER)
attachInterrupt(digitalPinToInterrupt(RADIO_WAKEUP_PIN), wakeUpLoRaPacketReceived, RISING);
LoRa_Utils::wakeRadio();
#else
Serial.println("NO SLEEP MODE AVAILABLE FOR THIS BOARD");
#endif
#else
Serial.println("NO SLEEP MODE AVAILABLE FOR THIS BOARD");
#endif
void gpsWakeUp() {
if (gpsSleepActive && !gpsIsActive) {
POWER_Utils::activateGPS();
gpsIsActive = true;
//
Serial.println("GPS WAKEUP");
//
}
}
}

View File

@ -5,10 +5,8 @@
namespace SLEEP_Utils {
void processBufferAfterSleep();
void handle_wakeup();
void startSleep();
void setup();
void gpsSleep();
void gpsWakeUp();
}

View File

@ -4,6 +4,7 @@
#include "station_utils.h"
#include "configuration.h"
#include "power_utils.h"
#include "sleep_utils.h"
#include "lora_utils.h"
#include "bme_utils.h"
#include "display.h"
@ -36,7 +37,7 @@ extern uint8_t winlinkStatus;
extern bool winlinkCommentState;
extern int wxModuleType;
extern bool SleepModeActive;
extern bool gpsSleepActive;
bool sendStandingUpdate = false;
uint8_t updateCounter = Config.sendCommentAfterXBeacons;
@ -274,13 +275,14 @@ namespace STATION_Utils {
previousHeading = currentHeading;
lastTxDistance = 0.0;
}
if (!SleepModeActive) {
lastTxTime = millis();
}
sendUpdate = false;
#ifdef HAS_TFT
cleanTFT();
#endif
if (gpsSleepActive) {
SLEEP_Utils::gpsSleep();
}
}
void checkTelemetryTx() {