diff --git a/src/LoRa_APRS_Tracker.cpp b/src/LoRa_APRS_Tracker.cpp index 9498abf..f43b93b 100644 --- a/src/LoRa_APRS_Tracker.cpp +++ b/src/LoRa_APRS_Tracker.cpp @@ -58,6 +58,10 @@ std::vector loadedAPRSMessages; static uint32_t lastDeleteListenedTracker = millis(); +void setup_gps() { + neo6m_gps.begin(9600, SERIAL_8N1, GPS_TX, GPS_RX); +} + void loadNumMessages() { if(!SPIFFS.begin(true)){ Serial.println("An Error has occurred while mounting SPIFFS"); @@ -83,10 +87,6 @@ void loadNumMessages() { logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "Number of APRS Messages : %s", String(numAPRSMessages)); } -void setup_gps() { - neo6m_gps.begin(9600, SERIAL_8N1, GPS_TX, GPS_RX); -} - void loadMessagesFromMemory() { File fileToRead; noMessageWarning = false; @@ -660,18 +660,7 @@ void checkReceivedMessage(String packetReceived) { void setup() { Serial.begin(115200); - #ifdef TTGO_T_Beam_V1_0 - Wire.begin(SDA, SCL); - if (!powerManagement.begin(Wire)) { - logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "AXP192", "init done!"); - } else { - logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "AXP192", "init failed!"); - } - powerManagement.activateLoRa(); - powerManagement.activateOLED(); - powerManagement.activateGPS(); - powerManagement.activateMeasurement(); - #endif + powerManagement.setup(); delay(500); logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "RichonGuzman -> CD2RXU --> LoRa APRS Tracker/Station"); @@ -694,13 +683,7 @@ void setup() { userButton.attachLongPressStart(ButtonLongPress); userButton.attachDoubleClick(ButtonDoublePress); - #if defined(TTGO_T_Beam_V1_0) - if (setCpuFrequencyMhz(80)) { - logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "CPU frequency set to 80MHz"); - } else { - logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "CPU frequency unchanged"); - } - #endif + powerManagement.lowerCpuFrequency(); logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "Smart Beacon is: %s", getSmartBeaconState()); logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "Setup Done!"); @@ -725,6 +708,9 @@ void loop() { gps.encode(neo6m_gps.read()); } + powerManagement.obtainBatteryInfo(); + powerManagement.handleChargingLed(); + bool gps_time_update = gps.time.isUpdated(); bool gps_loc_update = gps.location.isUpdated(); @@ -766,25 +752,6 @@ void loop() { static bool sendStandingUpdate = false; int currentSpeed = (int)gps.speed.kmph(); - static bool BatteryIsConnected = false; - static String batteryVoltage = ""; - static String batteryChargeCurrent = ""; - #ifdef TTGO_T_Beam_V1_0 - static unsigned int rate_limit_check_battery = 0; - if (!(rate_limit_check_battery++ % 60)) - BatteryIsConnected = powerManagement.isBatteryConnect(); - if (BatteryIsConnected) { - batteryVoltage = String(powerManagement.getBatteryVoltage(), 2); - batteryChargeCurrent = String(powerManagement.getBatteryChargeDischargeCurrent(), 0); - } - #endif - - if (powerManagement.isChargeing()) { - powerManagement.enableChgLed(); - } else { - powerManagement.disableChgLed(); - } - if (!send_update && gps_loc_update && currentBeacon->smartBeaconState) { uint32_t lastTx = millis() - lastTxTime; currentHeading = gps.course.deg(); @@ -998,7 +965,9 @@ void loop() { fifthRowMainMenu = "LAST Rx = " + lastHeardTracker; - if (BatteryIsConnected) { + if (powerManagement.getBatteryInfoIsConnected()) { + String batteryVoltage = powerManagement.getBatteryInfoVoltage(); + String batteryChargeCurrent = powerManagement.getBatteryInfoCurrent(); if (batteryChargeCurrent.toInt() == 0) { sixthRowMainMenu = "Battery Charged " + String(batteryVoltage) + "V"; } else if (batteryChargeCurrent.toInt() > 0) { @@ -1007,7 +976,7 @@ void loop() { sixthRowMainMenu = "Battery " + String(batteryVoltage) + "V " + String(batteryChargeCurrent) + "mA"; } } else { - sixthRowMainMenu = "No Battery Connected." ; + sixthRowMainMenu = "No Battery Connected" ; } show_display(String(firstRowMainMenu), String(secondRowMainMenu), diff --git a/src/power_management.cpp b/src/power_management.cpp index d9527a4..5365bd9 100644 --- a/src/power_management.cpp +++ b/src/power_management.cpp @@ -1,9 +1,7 @@ - #include "power_management.h" +#include "logger.h" -// cppcheck-suppress uninitMemberVar -PowerManagement::PowerManagement() { -} +extern logging::Logger logger; // cppcheck-suppress unusedFunction bool PowerManagement::begin(TwoWire &port) { @@ -77,10 +75,67 @@ double PowerManagement::getBatteryChargeDischargeCurrent() { return -1.0 * axp.getBattDischargeCurrent(); } -bool PowerManagement::isBatteryConnect() { +bool PowerManagement::isBatteryConnected() { return axp.isBatteryConnect(); } bool PowerManagement::isChargeing() { return axp.isChargeing(); } + +void PowerManagement::setup() { +#ifdef TTGO_T_Beam_V1_0 + Wire.begin(SDA, SCL); + if (!begin(Wire)) { + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "AXP192", "init done!"); + } else { + logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "AXP192", "init failed!"); + } + activateLoRa(); + activateOLED(); + activateGPS(); + activateMeasurement(); +#endif +} + +void PowerManagement::lowerCpuFrequency() { + #if defined(TTGO_T_Beam_V1_0) + if (setCpuFrequencyMhz(80)) { + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "CPU frequency set to 80MHz"); + } else { + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "CPU frequency unchanged"); + } + #endif +} + +void PowerManagement::handleChargingLed() { + if (isChargeing()) { + enableChgLed(); + } else { + disableChgLed(); + } +} + +void PowerManagement::obtainBatteryInfo() { +#ifdef TTGO_T_Beam_V1_0 + static unsigned int rate_limit_check_battery = 0; + if (!(rate_limit_check_battery++ % 60)) + BatteryIsConnected = isBatteryConnected(); + if (BatteryIsConnected) { + batteryVoltage = String(getBatteryVoltage(), 2); + batteryChargeDischargeCurrent = String(getBatteryChargeDischargeCurrent(), 0); + } +#endif +} + +String PowerManagement::getBatteryInfoVoltage() { + return batteryVoltage; +} + +String PowerManagement::getBatteryInfoCurrent() { + return batteryChargeDischargeCurrent; +} + +bool PowerManagement::getBatteryInfoIsConnected() { + return BatteryIsConnected; +} \ No newline at end of file diff --git a/src/power_management.h b/src/power_management.h index 73edfbf..ea885fa 100644 --- a/src/power_management.h +++ b/src/power_management.h @@ -6,9 +6,21 @@ class PowerManagement { public: - PowerManagement(); + PowerManagement() : BatteryIsConnected(false), batteryVoltage(""), batteryChargeDischargeCurrent("") {}; bool begin(TwoWire &port); + void setup(); + void lowerCpuFrequency(); + void handleChargingLed(); + + void obtainBatteryInfo(); + String getBatteryInfoVoltage(); + String getBatteryInfoCurrent(); + bool getBatteryInfoIsConnected(); + +private: + bool isChargeing(); + void activateLoRa(); void deactivateLoRa(); @@ -27,11 +39,13 @@ public: double getBatteryVoltage(); double getBatteryChargeDischargeCurrent(); - bool isBatteryConnect(); - bool isChargeing(); + bool isBatteryConnected(); -private: AXP20X_Class axp; + + bool BatteryIsConnected; + String batteryVoltage; + String batteryChargeDischargeCurrent; }; #endif