refactoring2

This commit is contained in:
richonguzman 2023-06-03 22:50:38 -04:00
parent 642f3638c4
commit b0fddf1225
3 changed files with 91 additions and 53 deletions

View File

@ -58,6 +58,10 @@ std::vector<String> loadedAPRSMessages;
static uint32_t lastDeleteListenedTracker = millis(); static uint32_t lastDeleteListenedTracker = millis();
void setup_gps() {
neo6m_gps.begin(9600, SERIAL_8N1, GPS_TX, GPS_RX);
}
void loadNumMessages() { void loadNumMessages() {
if(!SPIFFS.begin(true)){ if(!SPIFFS.begin(true)){
Serial.println("An Error has occurred while mounting SPIFFS"); 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)); 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() { void loadMessagesFromMemory() {
File fileToRead; File fileToRead;
noMessageWarning = false; noMessageWarning = false;
@ -660,18 +660,7 @@ void checkReceivedMessage(String packetReceived) {
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
#ifdef TTGO_T_Beam_V1_0 powerManagement.setup();
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
delay(500); delay(500);
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "RichonGuzman -> CD2RXU --> LoRa APRS Tracker/Station"); logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "RichonGuzman -> CD2RXU --> LoRa APRS Tracker/Station");
@ -694,13 +683,7 @@ void setup() {
userButton.attachLongPressStart(ButtonLongPress); userButton.attachLongPressStart(ButtonLongPress);
userButton.attachDoubleClick(ButtonDoublePress); userButton.attachDoubleClick(ButtonDoublePress);
#if defined(TTGO_T_Beam_V1_0) powerManagement.lowerCpuFrequency();
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
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "Smart Beacon is: %s", getSmartBeaconState()); logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "Smart Beacon is: %s", getSmartBeaconState());
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "Setup Done!"); logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "Setup Done!");
@ -725,6 +708,9 @@ void loop() {
gps.encode(neo6m_gps.read()); gps.encode(neo6m_gps.read());
} }
powerManagement.obtainBatteryInfo();
powerManagement.handleChargingLed();
bool gps_time_update = gps.time.isUpdated(); bool gps_time_update = gps.time.isUpdated();
bool gps_loc_update = gps.location.isUpdated(); bool gps_loc_update = gps.location.isUpdated();
@ -766,25 +752,6 @@ void loop() {
static bool sendStandingUpdate = false; static bool sendStandingUpdate = false;
int currentSpeed = (int)gps.speed.kmph(); 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) { if (!send_update && gps_loc_update && currentBeacon->smartBeaconState) {
uint32_t lastTx = millis() - lastTxTime; uint32_t lastTx = millis() - lastTxTime;
currentHeading = gps.course.deg(); currentHeading = gps.course.deg();
@ -998,7 +965,9 @@ void loop() {
fifthRowMainMenu = "LAST Rx = " + lastHeardTracker; fifthRowMainMenu = "LAST Rx = " + lastHeardTracker;
if (BatteryIsConnected) { if (powerManagement.getBatteryInfoIsConnected()) {
String batteryVoltage = powerManagement.getBatteryInfoVoltage();
String batteryChargeCurrent = powerManagement.getBatteryInfoCurrent();
if (batteryChargeCurrent.toInt() == 0) { if (batteryChargeCurrent.toInt() == 0) {
sixthRowMainMenu = "Battery Charged " + String(batteryVoltage) + "V"; sixthRowMainMenu = "Battery Charged " + String(batteryVoltage) + "V";
} else if (batteryChargeCurrent.toInt() > 0) { } else if (batteryChargeCurrent.toInt() > 0) {
@ -1007,7 +976,7 @@ void loop() {
sixthRowMainMenu = "Battery " + String(batteryVoltage) + "V " + String(batteryChargeCurrent) + "mA"; sixthRowMainMenu = "Battery " + String(batteryVoltage) + "V " + String(batteryChargeCurrent) + "mA";
} }
} else { } else {
sixthRowMainMenu = "No Battery Connected." ; sixthRowMainMenu = "No Battery Connected" ;
} }
show_display(String(firstRowMainMenu), show_display(String(firstRowMainMenu),
String(secondRowMainMenu), String(secondRowMainMenu),

View File

@ -1,9 +1,7 @@
#include "power_management.h" #include "power_management.h"
#include "logger.h"
// cppcheck-suppress uninitMemberVar extern logging::Logger logger;
PowerManagement::PowerManagement() {
}
// cppcheck-suppress unusedFunction // cppcheck-suppress unusedFunction
bool PowerManagement::begin(TwoWire &port) { bool PowerManagement::begin(TwoWire &port) {
@ -77,10 +75,67 @@ double PowerManagement::getBatteryChargeDischargeCurrent() {
return -1.0 * axp.getBattDischargeCurrent(); return -1.0 * axp.getBattDischargeCurrent();
} }
bool PowerManagement::isBatteryConnect() { bool PowerManagement::isBatteryConnected() {
return axp.isBatteryConnect(); return axp.isBatteryConnect();
} }
bool PowerManagement::isChargeing() { bool PowerManagement::isChargeing() {
return axp.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;
}

View File

@ -6,9 +6,21 @@
class PowerManagement { class PowerManagement {
public: public:
PowerManagement(); PowerManagement() : BatteryIsConnected(false), batteryVoltage(""), batteryChargeDischargeCurrent("") {};
bool begin(TwoWire &port); bool begin(TwoWire &port);
void setup();
void lowerCpuFrequency();
void handleChargingLed();
void obtainBatteryInfo();
String getBatteryInfoVoltage();
String getBatteryInfoCurrent();
bool getBatteryInfoIsConnected();
private:
bool isChargeing();
void activateLoRa(); void activateLoRa();
void deactivateLoRa(); void deactivateLoRa();
@ -27,11 +39,13 @@ public:
double getBatteryVoltage(); double getBatteryVoltage();
double getBatteryChargeDischargeCurrent(); double getBatteryChargeDischargeCurrent();
bool isBatteryConnect(); bool isBatteryConnected();
bool isChargeing();
private:
AXP20X_Class axp; AXP20X_Class axp;
bool BatteryIsConnected;
String batteryVoltage;
String batteryChargeDischargeCurrent;
}; };
#endif #endif