diff --git a/main/battery.cpp b/main/battery.cpp index f12d773..2bd8e83 100644 --- a/main/battery.cpp +++ b/main/battery.cpp @@ -140,50 +140,47 @@ std::string getBatteryDebugString() return "No Battery"; } -float getMinBatVoltage(BatteryCellType cellType) { +float getMinBatCellVoltage(BatteryCellType cellType) { + float minimumVoltage = std::numeric_limits::max(); switch (cellType) { case BatteryCellType::_22P: { - const float expected_ah = BAT_MIN_AH_22P; BAT_CURVE_22P(GET_MINIMUM_BAT_VOLTAGE); break; } case BatteryCellType::HG2: { - const float expected_ah = BAT_MIN_AH_HG2; BAT_CURVE_HG2(GET_MINIMUM_BAT_VOLTAGE); break; } case BatteryCellType::MH1: { - const float expected_ah = BAT_MIN_AH_MH1; BAT_CURVE_MH1(GET_MINIMUM_BAT_VOLTAGE); break; } case BatteryCellType::VTC5: { - const float expected_ah = BAT_MIN_AH_VTC5; BAT_CURVE_VTC5(GET_MINIMUM_BAT_VOLTAGE); break; } case BatteryCellType::BAK_25R: { - const float expected_ah = BAT_MIN_AH_BAK_25R; BAT_CURVE_25R(GET_MINIMUM_BAT_VOLTAGE); break; } case BatteryCellType::HE4: { - const float expected_ah = BAT_MIN_AH_HE4; BAT_CURVE_HE4(GET_MINIMUM_BAT_VOLTAGE); break; } + default: + return 0.f; } - return 0.f; + return minimumVoltage; } -float getMaxBatVoltage(BatteryCellType cellType) +float getMaxBatCellVoltage(BatteryCellType cellType) { switch (cellType) { diff --git a/main/battery.h b/main/battery.h index 02846c1..473d8fd 100644 --- a/main/battery.h +++ b/main/battery.h @@ -25,12 +25,12 @@ if (cellVoltage >= lowerVoltage && cellVoltage <= higherVoltage) \ return higherVoltage; #define GET_MINIMUM_BAT_VOLTAGE(higherVoltage,lowerVoltage,fromAh,toAh) \ - if (expected_ah >= toAh) \ - return lowerVoltage * configs.battery.cellsSeries.value; + if (lowerVoltage < minimumVoltage) \ + minimumVoltage = lowerVoltage; #define GET_MAXIMUM_BAT_VOLTAGE(higherVoltage,lowerVoltage,fromAh,toAh) \ if (fromAh == 0) \ - return higherVoltage * configs.battery.cellsSeries.value; + return higherVoltage; // All curves here have to follow the same order (highest-voltage first) @@ -141,8 +141,8 @@ float getRemainingWattHours(); float getBatteryWattHours(); -float getMinBatVoltage(BatteryCellType cellType); -float getMaxBatVoltage(BatteryCellType cellType); +float getMinBatCellVoltage(BatteryCellType cellType); +float getMaxBatCellVoltage(BatteryCellType cellType); std::string getBatteryPercentageString(); diff --git a/main/displays/batterygraphdisplay.cpp b/main/displays/batterygraphdisplay.cpp index dff301e..615ab09 100644 --- a/main/displays/batterygraphdisplay.cpp +++ b/main/displays/batterygraphdisplay.cpp @@ -21,20 +21,20 @@ void BatteryGraphDisplay::initScreen() { Base::initScreen(); const auto points = count_curve_points(configs.battery.cellType.value); - ESP_LOGI(TAG, "Battery graph points: %d, cell type: %s", points, toString(configs.battery.cellType.value).c_str()); - const auto max_height = espgui::tft.height() - TOP_OFFSET; - const auto max_width = espgui::tft.width(); + const auto max_height = espgui::tft.height() - 1; + const auto max_width = espgui::tft.width() - 4; const uint16_t part = max_width / points; + const auto min_voltage = getMinBatCellVoltage(configs.battery.cellType.value); + const auto max_voltage = getMaxBatCellVoltage(configs.battery.cellType.value); + ESP_LOGI(TAG, "min_voltage: %f, max_voltage: %f", min_voltage, max_voltage); for (uint8_t i = 0; points >= i; i++) { // draw lines between point->minVoltage and point->maxVoltage from left to right if (const auto point = get_point_n_voltages(configs.battery.cellType.value, points - i); point) { - // TODO: Fix float_map to not be broken - // Current output: float_map(335, 2.55, 4.5, 0, 280) = 47736 - const int x1 = part * i; - const int y1 = float_map(point->minVoltage, 2.55, 4.5, 0, max_height); - const int x2 = part * (i + 1); - const int y2 = float_map(point->maxVoltage, 2.55, 4.5, 0, max_height); + const int x1 = 2 + part * (points - i + 1); + const int y1 = float_map(point->minVoltage / 100.f, min_voltage, max_voltage, max_height, TOP_OFFSET); + const int x2 = 2 + part * (points - i); + const int y2 = float_map(point->maxVoltage / 100.f, min_voltage, max_voltage, max_height, TOP_OFFSET); espgui::tft.drawLine(x1, y1, x2, y2, TFT_WHITE); ESP_LOGI(TAG, "espgui::tft.drawLine(%d, %d, %d, %d, TFT_WHITE);", x1, y1, x2, y2); }