diff --git a/esp32_marauder/BatteryInterface.cpp b/esp32_marauder/BatteryInterface.cpp index f279886..8289988 100644 --- a/esp32_marauder/BatteryInterface.cpp +++ b/esp32_marauder/BatteryInterface.cpp @@ -14,30 +14,79 @@ void BatteryInterface::main(uint32_t currentTime) { if (this->battery_level != new_level) { Serial.println(text00 + (String)new_level); this->battery_level = new_level; + Serial.println("Battery Level: " + (String)this->battery_level); } } } } void BatteryInterface::RunSetup() { + byte error; + byte addr; + Wire.begin(I2C_SDA, I2C_SCL); + + Serial.println("Checking for battery monitors..."); + + for(addr = 1; addr < 127; addr++ ) { + Wire.beginTransmission(addr); + error = Wire.endTransmission(); + + if (error == 0) + { + Serial.print("I2C device found at address 0x"); + + if (addr<16) + Serial.print("0"); + + Serial.println(addr,HEX); + + if (addr == IP5306_ADDR) { + this->has_ip5306 = true; + this->i2c_supported = true; + } + + if (addr == MAX17048_ADDR) { + if (maxlipo.begin()) { + Serial.println("Detected MAX17048"); + this->has_max17048 = true; + this->i2c_supported = true; + } + } + } + } + + /*if (this->maxlipo.begin()) { + Serial.println("Detected MAX17048"); + this->has_max17048 = true; + this->i2c_supported = true; + }*/ + this->initTime = millis(); } int8_t BatteryInterface::getBatteryLevel() { - Wire.beginTransmission(IP5306_ADDR); - Wire.write(0x78); - if (Wire.endTransmission(false) == 0 && - Wire.requestFrom(0x75, 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; + + 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; + } + + + if (this->has_max17048) { + return this->maxlipo.cellPercent(); } - this->i2c_supported = false; - return -1; } diff --git a/esp32_marauder/BatteryInterface.h b/esp32_marauder/BatteryInterface.h index 078c120..bd0d2c4 100644 --- a/esp32_marauder/BatteryInterface.h +++ b/esp32_marauder/BatteryInterface.h @@ -6,21 +6,24 @@ #include #include "configs.h" +#include "Adafruit_MAX1704X.h" #include -#define I2C_SDA 33 -#define I2C_SCL 22 #define IP5306_ADDR 0x75 +#define MAX17048_ADDR 0x36 class BatteryInterface { private: uint32_t initTime = 0; + Adafruit_MAX17048 maxlipo; public: int8_t battery_level = 0; int8_t old_level = 0; bool i2c_supported = false; + bool has_max17048 = false; + bool has_ip5306 = false; BatteryInterface(); diff --git a/esp32_marauder/MenuFunctions.cpp b/esp32_marauder/MenuFunctions.cpp index fbc8aad..fd03474 100644 --- a/esp32_marauder/MenuFunctions.cpp +++ b/esp32_marauder/MenuFunctions.cpp @@ -1002,7 +1002,10 @@ void MenuFunctions::battery(bool initial) if ((battery_obj.battery_level != battery_obj.old_level) || (initial)) { battery_obj.old_level = battery_obj.battery_level; display_obj.tft.fillRect(204, 0, SCREEN_WIDTH, STATUS_BAR_WIDTH, STATUSBAR_COLOR); - display_obj.tft.setCursor(0, 1); + } + + display_obj.tft.setCursor(0, 1); + /*if (!this->disable_touch) { display_obj.tft.drawXBitmap(186, 0, menu_icons[STATUS_BAT], @@ -1010,8 +1013,8 @@ void MenuFunctions::battery(bool initial) 16, STATUSBAR_COLOR, the_color); - display_obj.tft.drawString((String)battery_obj.battery_level + "%", 204, 0, 2); - } + }*/ + display_obj.tft.drawString((String)battery_obj.battery_level + "%", 204, 0, 2); } #endif } @@ -1099,14 +1102,15 @@ void MenuFunctions::updateStatusBar() } // Draw battery info - //MenuFunctions::battery(false); - display_obj.tft.fillRect(190, 0, SCREEN_WIDTH, STATUS_BAR_WIDTH, STATUSBAR_COLOR); + MenuFunctions::battery(false); + display_obj.tft.fillRect(186, 0, 16, STATUS_BAR_WIDTH, STATUSBAR_COLOR); + #ifdef HAS_ILI9341 #ifdef HAS_BUTTONS if (this->disable_touch) { display_obj.tft.setCursor(0, 1); - display_obj.tft.drawXBitmap(190, + display_obj.tft.drawXBitmap(186, 0, menu_icons[DISABLE_TOUCH], 16, @@ -1207,14 +1211,15 @@ void MenuFunctions::drawStatusBar() #endif - //MenuFunctions::battery2(true); - display_obj.tft.fillRect(190, 0, SCREEN_WIDTH, STATUS_BAR_WIDTH, STATUSBAR_COLOR); + MenuFunctions::battery(true); + display_obj.tft.fillRect(186, 0, 16, STATUS_BAR_WIDTH, STATUSBAR_COLOR); + #ifdef HAS_ILI9341 #ifdef HAS_BUTTONS if (this->disable_touch) { display_obj.tft.setCursor(0, 1); - display_obj.tft.drawXBitmap(190, + display_obj.tft.drawXBitmap(186, 0, menu_icons[DISABLE_TOUCH], 16, diff --git a/esp32_marauder/configs.h b/esp32_marauder/configs.h index d634721..010290c 100644 --- a/esp32_marauder/configs.h +++ b/esp32_marauder/configs.h @@ -21,7 +21,7 @@ //#define MARAUDER_REV_FEATHER //// END BOARD TARGETS - #define MARAUDER_VERSION "v1.1.0" + #define MARAUDER_VERSION "v1.2.0" //// HARDWARE NAMES #ifdef MARAUDER_M5STICKC @@ -1011,6 +1011,36 @@ #endif //// END GPS STUFF + //// BATTERY STUFF + #ifdef HAS_BATTERY + + #ifdef MARAUDER_V4 + #define I2C_SDA 33 + #define I2C_SCL 22 + #endif + + #ifdef MARAUDER_V6 + #define I2C_SDA 33 + #define I2C_SCL 22 + #endif + + #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 + + #endif + //// MARAUDER TITLE STUFF #ifdef MARAUDER_V4 #define MARAUDER_TITLE_BYTES 13578 diff --git a/esp32_marauder/esp32_marauder.ino b/esp32_marauder/esp32_marauder.ino index 64cc7b4..565def6 100644 --- a/esp32_marauder/esp32_marauder.ino +++ b/esp32_marauder/esp32_marauder.ino @@ -182,6 +182,9 @@ void setup() Serial.begin(115200); + while(!Serial) + delay(10); + Serial.println("ESP-IDF version is: " + String(esp_get_idf_version())); #ifdef HAS_SCREEN