diff --git a/esp32_marauder/BatteryInterface.cpp b/esp32_marauder/BatteryInterface.cpp index 93c87a7..bbf607f 100644 --- a/esp32_marauder/BatteryInterface.cpp +++ b/esp32_marauder/BatteryInterface.cpp @@ -1,7 +1,10 @@ + +// #ifdef HAS_BATTERY + #include "BatteryInterface.h" #include "lang_var.h" BatteryInterface::BatteryInterface() { - + } void BatteryInterface::main(uint32_t currentTime) { @@ -27,96 +30,119 @@ void BatteryInterface::RunSetup() { #ifdef HAS_BATTERY #ifdef BATTERY_ADC_PIN - analogReadResolution(12); - pinMode(BATTERY_ADC_PIN, INPUT); - this->has_adc_battery = true; - this->i2c_supported = true; - Serial.println(F("Battery: ADC mode")); - #elif !defined(HAS_AXP2101) - Wire.begin(I2C_SDA, I2C_SCL); + analogReadResolution(12); + pinMode(BATTERY_ADC_PIN, INPUT); + this->has_adc_battery = true; + // this->i2c_supported = true; + Serial.println(F("Battery: ADC mode")); - Wire.beginTransmission(IP5306_ADDR); - error = Wire.endTransmission(); + #elif defined(HAS_AXP2101) && defined(I2C_SDA) + bool result = this->power.begin(Wire, AXP2101_SLAVE_ADDRESS, I2C_SDA, I2C_SCL); + + if (!result) + return; + + Serial.println(F("Detected AXP2101")); - if (error == 0) { - Serial.println(F("Detected IP5306")); - this->has_ip5306 = true; this->i2c_supported = true; - } + this->has_axp2101 = true; - Wire.beginTransmission(MAX17048_ADDR); - error = Wire.endTransmission(); + #elif defined(I2C_SDA) // other i2c (shared) - if (error == 0) { - if (maxlipo.begin()) { - Serial.println(F("Detected MAX17048")); - this->has_max17048 = true; - this->i2c_supported = true; - } - } - #else - bool result = this->power.begin(Wire, AXP2101_SLAVE_ADDRESS, I2C_SDA, I2C_SCL); + Wire.begin(I2C_SDA, I2C_SCL); - if (!result) - return; + #ifdef HAS_IP5306 + Wire.beginTransmission(IP5306_ADDR); + error = Wire.endTransmission(); - Serial.println(F("Detected AXP2101")); + if (error == 0) { + Serial.println(F("Detected IP5306")); + this->has_ip5306 = true; + this->i2c_supported = true; + } + #endif - this->i2c_supported = true; - this->has_axp2101 = true; - #endif + #ifdef HAS_AXP192 + axp192_obj.begin(); + #endif + + + #ifdef HAS_MAX1704X + Wire.beginTransmission(MAX17048_ADDR); + error = Wire.endTransmission(); + + if (error == 0) { + if (maxlipo.begin()) { + Serial.println(F("Detected MAX17048")); + this->has_max17048 = true; + this->i2c_supported = true; + } + } + #endif + + #endif // other i2c this->initTime = millis(); - #endif + #endif // HAS_BATTERY } int8_t BatteryInterface::getBatteryLevel() { - #ifdef BATTERY_ADC_PIN - if (this->has_adc_battery) { - int voltage_mv = analogReadMilliVolts(BATTERY_ADC_PIN) * 2; // voltage divider ratio 2:1 - if (voltage_mv <= 3300) return 0; - if (voltage_mv >= 4150) return 100; - return (int8_t)(((voltage_mv - 3300) * 100) / 850); - } - #endif + #ifdef HAS_BATTERY - if (this->has_ip5306) { - Wire.beginTransmission(IP5306_ADDR); - Wire.write(0x78); - if (Wire.endTransmission(false) == 0 && - Wire.requestFrom(IP5306_ADDR, 1)) { - this->i2c_supported = true; - switch (Wire.read() & 0xF0) { - case 0xE0: return 25; - case 0xC0: return 50; - case 0x80: return 75; - case 0x00: return 100; - default: return 0; + #ifdef BATTERY_ADC_PIN + if (this->has_adc_battery) { + int voltage_mv = analogReadMilliVolts(BATTERY_ADC_PIN) * 2; // voltage divider ratio 2:1 + if (voltage_mv <= 3300) return 0; + if (voltage_mv >= 4150) return 100; + return (int8_t)(((voltage_mv - 3300) * 100) / 850); } - } - this->i2c_supported = false; - return -1; - } + #endif + + #ifdef HAS_IP5306 + if (this->has_ip5306) { + Wire.beginTransmission(IP5306_ADDR); + Wire.write(0x78); + if (Wire.endTransmission(false) == 0 && + Wire.requestFrom(IP5306_ADDR, 1)) { + this->i2c_supported = true; + switch (Wire.read() & 0xF0) { + case 0xE0: return 25; + case 0xC0: return 50; + case 0x80: return 75; + case 0x00: return 100; + default: return 0; + } + } + this->i2c_supported = false; + return -1; + } + #endif - if (this->has_max17048) { - float percent = this->maxlipo.cellPercent(); + #ifdef HAS_MAX1704X + if (this->has_max17048) { + float percent = this->maxlipo.cellPercent(); - // Sometimes we dumb - if (percent >= 100) - return 100; - else if (percent <= 0) - return 0; - else - return percent; - } + // Sometimes we dumb + if (percent >= 100) + return 100; + else if (percent <= 0) + return 0; + else + return percent; + } + #endif - #ifdef HAS_AXP2101 - if (this->has_axp2101) { - return this->power.getBatteryPercent(); - } - #endif + #ifdef HAS_AXP2101 + if (this->has_axp2101) { + return this->power.getBatteryPercent(); + } + #endif + + #endif // HAS_BATTERY return -1; } + +// #endif // HAS_BATTERY diff --git a/esp32_marauder/BatteryInterface.h b/esp32_marauder/BatteryInterface.h index 0d684c2..fabd4cb 100644 --- a/esp32_marauder/BatteryInterface.h +++ b/esp32_marauder/BatteryInterface.h @@ -1,19 +1,31 @@ #pragma once +// #ifdef HAS_BATTERY + #ifndef BatteryInterface_h #define BatteryInterface_h #include #include "configs.h" -#include "Adafruit_MAX1704X.h" + +#ifdef HAS_MAX1704X + #include "Adafruit_MAX1704X.h" +#endif #ifdef HAS_AXP2101 #define XPOWERS_CHIP_AXP2101 #include "XPowersLib.h" #endif -#include +#ifdef HAS_AXP192 + #include "AXP192.h" + AXP192 axp192_obj; +#endif + +#ifndef BATTERY_ADC_PIN // not 12c + #include +#endif #define IP5306_ADDR 0x75 #define MAX17048_ADDR 0x36 @@ -21,7 +33,10 @@ class BatteryInterface { private: uint32_t initTime = 0; - Adafruit_MAX17048 maxlipo; + + #ifdef HAS_MAX1704X + Adafruit_MAX17048 maxlipo; + #endif #ifdef HAS_AXP2101 XPowersPMU power; @@ -43,4 +58,7 @@ class BatteryInterface { int8_t getBatteryLevel(); }; -#endif +#endif // ifndef BatteryInterface_h + +// #endif // HAS_BATTERY + diff --git a/esp32_marauder/Display.cpp b/esp32_marauder/Display.cpp index fd243a0..0f14cb3 100644 --- a/esp32_marauder/Display.cpp +++ b/esp32_marauder/Display.cpp @@ -590,12 +590,18 @@ void Display::displayBuffer(bool do_clear) } } -void Display::showCenterText(String text, int y, bool small_pp) +void Display::showCenterText(const char* text, int y, bool small_pp) { + if (!text) + text = ""; + + size_t len = strlen(text); + if (!small_pp) - tft.setCursor((SCREEN_WIDTH - (text.length() * (6 * BANNER_TEXT_SIZE))) / 2, y); + tft.setCursor((SCREEN_WIDTH - (len * (6 * BANNER_TEXT_SIZE))) / 2, y); else - tft.setCursor((SCREEN_WIDTH - (text.length() * (6))) / 2, y); + tft.setCursor((SCREEN_WIDTH - (len * 6)) / 2, y); + tft.println(text); } @@ -619,7 +625,7 @@ void Display::buildBanner(String msg, int xpos) this->tft.setFreeFont(NULL); // Font 4 selected this->tft.setTextSize(BANNER_TEXT_SIZE); // Font size scaling is x1 this->tft.setTextColor(TFT_WHITE, TFT_BLACK); // Black text, no background colour - this->showCenterText(msg, banner_y); + this->showCenterText(msg.c_str(), banner_y); } #endif diff --git a/esp32_marauder/Display.h b/esp32_marauder/Display.h index bd01f25..f9eda4b 100644 --- a/esp32_marauder/Display.h +++ b/esp32_marauder/Display.h @@ -141,7 +141,7 @@ class Display void getTouchWhileFunction(bool pressed); void init(); void RunSetup(); - void showCenterText(String text, int y, bool small_pp = false); + void showCenterText(const char* text, int y, bool small_pp = false); void touchToExit(); void twoPartDisplay(String center_text); void updateBanner(String msg); diff --git a/esp32_marauder/MenuFunctions.h b/esp32_marauder/MenuFunctions.h index a0fd55e..c614333 100644 --- a/esp32_marauder/MenuFunctions.h +++ b/esp32_marauder/MenuFunctions.h @@ -43,7 +43,9 @@ extern WiFiScan wifi_scan_obj; extern SDInterface sd_obj; +// #ifdef HAS_BATTERY extern BatteryInterface battery_obj; +// #endif extern Settings settings_obj; #define FLASH_BUTTON 0 diff --git a/esp32_marauder/WiFiScan.cpp b/esp32_marauder/WiFiScan.cpp index 4ab3de2..87abb98 100644 --- a/esp32_marauder/WiFiScan.cpp +++ b/esp32_marauder/WiFiScan.cpp @@ -2112,8 +2112,10 @@ void WiFiScan::displayTargetFilter() { display_obj.tft.setTextColor(TFT_WHITE, TFT_BLACK); for (int i = 0; i < access_points->size(); i++) { AccessPoint access_point = access_points->get(i); - if (access_point.selected) - display_obj.showCenterText("CH: " + (String)access_point.channel + " " + access_point.essid, display_obj.tft.getCursorY(), true); + if (access_point.selected) { + String msg_str = "CH: " + (String)access_point.channel + " " + access_point.essid; + display_obj.showCenterText(msg_str.c_str(), display_obj.tft.getCursorY(), true); + } } } else { display_obj.tft.setTextColor(TFT_RED, TFT_BLACK); @@ -3945,7 +3947,7 @@ void WiFiScan::RunInfo() { #endif Serial.println(text_table4[34]); } - #endif + #endif // HAS_BATTERY if (this->wifi_connected) showNetworkInfo(); @@ -9849,8 +9851,8 @@ void WiFiScan::displayTransmitRate() { displayString2.concat(" "); #ifdef HAS_SCREEN display_obj.tft.setTextColor(TFT_GREEN, TFT_BLACK); - display_obj.showCenterText(displayString2, TFT_HEIGHT / 2); - display_obj.showCenterText(displayString, TFT_HEIGHT / 2); + display_obj.showCenterText(displayString2.c_str(), TFT_HEIGHT / 2); + display_obj.showCenterText(displayString.c_str(), TFT_HEIGHT / 2); #endif } @@ -10121,8 +10123,8 @@ void WiFiScan::main(uint32_t currentTime) displayString2.concat(" "); #ifdef HAS_SCREEN display_obj.tft.setTextColor(TFT_GREEN, TFT_BLACK); - display_obj.showCenterText(displayString2, TFT_HEIGHT / 2); - display_obj.showCenterText(displayString, TFT_HEIGHT / 2); + display_obj.showCenterText(displayString2.c_str(), TFT_HEIGHT / 2); + display_obj.showCenterText(displayString.c_str(), TFT_HEIGHT / 2); #endif } diff --git a/esp32_marauder/configs.h b/esp32_marauder/configs.h index 1327b21..97a6908 100644 --- a/esp32_marauder/configs.h +++ b/esp32_marauder/configs.h @@ -124,6 +124,11 @@ //#define FLIPPER_ZERO_HAT #define HAS_MINI_KB #define HAS_BATTERY + #if defined(MARAUDER_M5STICKC) + #define HAS_AXP192 + #else + #define HAS_TP4057 + #endif #define HAS_BT #define HAS_BUTTONS //#define HAS_NEOPIXEL_LED @@ -237,6 +242,7 @@ #define HAS_TOUCH //#define FLIPPER_ZERO_HAT #define HAS_BATTERY + #define HAS_IP5306 #define HAS_BT //#define HAS_BUTTONS #define HAS_NEOPIXEL_LED @@ -256,6 +262,7 @@ #define HAS_TOUCH //#define FLIPPER_ZERO_HAT #define HAS_BATTERY + #define HAS_IP5306 #define HAS_BT #define HAS_BT_REMOTE #define HAS_BUTTONS @@ -554,7 +561,7 @@ //// POWER MANAGEMENT #ifdef HAS_PWR_MGMT - #if defined(MARAUDER_M5STICKC) || defined(MARAUDER_M5STICKCP2) + #if defined(HAS_AXP192) #include "AXP192.h" #endif @@ -2640,72 +2647,88 @@ //// BATTERY STUFF #ifdef HAS_BATTERY - #ifdef MARAUDER_V4 + #if defined(MARAUDER_M5STICKC) || defined(MARAUDER_M5STICKCP2) #define I2C_SDA 33 #define I2C_SCL 22 - #endif - #ifdef MARAUDER_V6 + #elif defined(MARAUDER_V4) || defined(MARAUDER_V6) || defined(MARAUDER_V6_1) || defined(MARAUDER_KIT) #define I2C_SDA 33 #define I2C_SCL 22 - #endif + #define HAS_IP5306 - #ifdef MARAUDER_V6_1 - #define I2C_SDA 33 - #define I2C_SCL 22 - #endif - - #ifdef MARAUDER_M5STICKC - #define I2C_SDA 33 - #define I2C_SCL 22 - #endif - - #ifdef MARAUDER_KIT - #define I2C_SDA 33 - #define I2C_SCL 22 - #endif - - #ifdef MARAUDER_MINI + #elif defined(MARAUDER_MINI) #define I2C_SDA 33 #define I2C_SCL 26 - #endif - #ifdef MARAUDER_V7 + #elif defined(MARAUDER_V7) #define I2C_SDA 33 #define I2C_SCL 16 - #endif + #define HAS_IP5306 - #ifdef MARAUDER_V7_1 + #elif defined(MARAUDER_V7_1) #define I2C_SDA 33 #define I2C_SCL 27 - #endif - #ifdef MARAUDER_CYD_MICRO + #elif defined(MARAUDER_CYD_MICRO) #define I2C_SDA 22 #define I2C_SCL 27 - #endif - #ifdef MARAUDER_CYD_2USB + #elif defined(MARAUDER_CYD_2USB) #define I2C_SDA 22 #define I2C_SCL 27 - #endif - #ifdef MARAUDER_CYD_3_5_INCH + #elif defined(MARAUDER_CYD_3_5_INCH) #define I2C_SDA 32 #define I2C_SCL 25 - #endif - #ifdef MARAUDER_CYD_GUITION + #elif defined(MARAUDER_CYD_GUITION) #define I2C_SDA 22 #define I2C_SCL 21 - #endif - #ifdef MARAUDER_V8 + #elif defined(MARAUDER_V8) #define I2C_SCL 4 #define I2C_SDA 5 + + #elif defined(MARAUDER_REV_FEATHER) + #define I2C_SCL 4 + #define I2C_SDA 3 + #define HAS_MAX1704X + #undef HAS_AXP2101 + #undef HAS_IP5306 #endif - #endif + // If we know what we have, we can delete what we're not using + #ifdef BATTERY_ADC_PIN + #undef HAS_AXP2101 + #undef HAS_IP5306 + #undef HAS_MAX1704X + #undef HAS_AXP192 + + // No driver for this LiPo charger + #elif defined(HAS_TP4057) + #undef HAS_AXP2101 + #undef HAS_IP5306 + #undef HAS_MAX1704X + #undef HAS_AXP192 + + #elif defined(HAS_AXP192) + #undef HAS_AXP2101 + #undef HAS_IP5306 + #undef HAS_MAX1704X + + #elif defined(HAS_AXP2101) + #undef HAS_IP5306 + #undef HAS_MAX1704X + + + #else // punt + // #define HAS_AXP2101 + #define HAS_IP5306 + #define HAS_MAX1704X + #define HAS_AXP192 + #endif + + #endif // HAS_BATTERY //// MARAUDER TITLE STUFF #ifdef MARAUDER_V4 diff --git a/esp32_marauder/esp32_marauder.ino b/esp32_marauder/esp32_marauder.ino index a465117..2fd84d9 100644 --- a/esp32_marauder/esp32_marauder.ino +++ b/esp32_marauder/esp32_marauder.ino @@ -1,6 +1,6 @@ /* FLASH SETTINGS Board: LOLIN D32 -Flash Frequency: 80MHz + Frequency: 80MHz Partition Scheme: Minimal SPIFFS https://www.online-utility.org/image/convert/to/XBM */ @@ -92,10 +92,6 @@ CommandLine cli_obj; SDInterface sd_obj; #endif -#ifdef MARAUDER_M5STICKC - AXP192 axp192_obj; -#endif - #ifdef HAS_FLIPPER_LED flipperLED flipper_led; #elif defined(XIAO_ESP32_S3) @@ -252,10 +248,6 @@ void setup() delay(100); #endif - #ifdef defined(MARAUDER_M5STICKC) && !defined(MARAUDER_M5STICKCP2) - axp192_obj.begin(); - #endif - #if defined(MARAUDER_M5STICKCP2) // Prevent StickCP2 from turning off when disconnect USB cable pinMode(POWER_HOLD_PIN, OUTPUT); digitalWrite(POWER_HOLD_PIN, HIGH);