Fixed everything

This commit is contained in:
CommanderRedYT
2022-03-04 21:30:13 +01:00
parent a222d68264
commit e40253c70f
3 changed files with 20 additions and 23 deletions

View File

@ -140,50 +140,47 @@ std::string getBatteryDebugString()
return "No Battery"; return "No Battery";
} }
float getMinBatVoltage(BatteryCellType cellType) { float getMinBatCellVoltage(BatteryCellType cellType) {
float minimumVoltage = std::numeric_limits<float>::max();
switch (cellType) switch (cellType)
{ {
case BatteryCellType::_22P: case BatteryCellType::_22P:
{ {
const float expected_ah = BAT_MIN_AH_22P;
BAT_CURVE_22P(GET_MINIMUM_BAT_VOLTAGE); BAT_CURVE_22P(GET_MINIMUM_BAT_VOLTAGE);
break; break;
} }
case BatteryCellType::HG2: case BatteryCellType::HG2:
{ {
const float expected_ah = BAT_MIN_AH_HG2;
BAT_CURVE_HG2(GET_MINIMUM_BAT_VOLTAGE); BAT_CURVE_HG2(GET_MINIMUM_BAT_VOLTAGE);
break; break;
} }
case BatteryCellType::MH1: case BatteryCellType::MH1:
{ {
const float expected_ah = BAT_MIN_AH_MH1;
BAT_CURVE_MH1(GET_MINIMUM_BAT_VOLTAGE); BAT_CURVE_MH1(GET_MINIMUM_BAT_VOLTAGE);
break; break;
} }
case BatteryCellType::VTC5: case BatteryCellType::VTC5:
{ {
const float expected_ah = BAT_MIN_AH_VTC5;
BAT_CURVE_VTC5(GET_MINIMUM_BAT_VOLTAGE); BAT_CURVE_VTC5(GET_MINIMUM_BAT_VOLTAGE);
break; break;
} }
case BatteryCellType::BAK_25R: case BatteryCellType::BAK_25R:
{ {
const float expected_ah = BAT_MIN_AH_BAK_25R;
BAT_CURVE_25R(GET_MINIMUM_BAT_VOLTAGE); BAT_CURVE_25R(GET_MINIMUM_BAT_VOLTAGE);
break; break;
} }
case BatteryCellType::HE4: case BatteryCellType::HE4:
{ {
const float expected_ah = BAT_MIN_AH_HE4;
BAT_CURVE_HE4(GET_MINIMUM_BAT_VOLTAGE); BAT_CURVE_HE4(GET_MINIMUM_BAT_VOLTAGE);
break; break;
} }
default:
return 0.f;
} }
return 0.f; return minimumVoltage;
} }
float getMaxBatVoltage(BatteryCellType cellType) float getMaxBatCellVoltage(BatteryCellType cellType)
{ {
switch (cellType) switch (cellType)
{ {

View File

@ -25,12 +25,12 @@ if (cellVoltage >= lowerVoltage && cellVoltage <= higherVoltage) \
return higherVoltage; return higherVoltage;
#define GET_MINIMUM_BAT_VOLTAGE(higherVoltage,lowerVoltage,fromAh,toAh) \ #define GET_MINIMUM_BAT_VOLTAGE(higherVoltage,lowerVoltage,fromAh,toAh) \
if (expected_ah >= toAh) \ if (lowerVoltage < minimumVoltage) \
return lowerVoltage * configs.battery.cellsSeries.value; minimumVoltage = lowerVoltage;
#define GET_MAXIMUM_BAT_VOLTAGE(higherVoltage,lowerVoltage,fromAh,toAh) \ #define GET_MAXIMUM_BAT_VOLTAGE(higherVoltage,lowerVoltage,fromAh,toAh) \
if (fromAh == 0) \ if (fromAh == 0) \
return higherVoltage * configs.battery.cellsSeries.value; return higherVoltage;
// All curves here have to follow the same order (highest-voltage first) // All curves here have to follow the same order (highest-voltage first)
@ -141,8 +141,8 @@ float getRemainingWattHours();
float getBatteryWattHours(); float getBatteryWattHours();
float getMinBatVoltage(BatteryCellType cellType); float getMinBatCellVoltage(BatteryCellType cellType);
float getMaxBatVoltage(BatteryCellType cellType); float getMaxBatCellVoltage(BatteryCellType cellType);
std::string getBatteryPercentageString(); std::string getBatteryPercentageString();

View File

@ -21,20 +21,20 @@ void BatteryGraphDisplay::initScreen()
{ {
Base::initScreen(); Base::initScreen();
const auto points = count_curve_points(configs.battery.cellType.value); 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() - 1;
const auto max_height = espgui::tft.height() - TOP_OFFSET; const auto max_width = espgui::tft.width() - 4;
const auto max_width = espgui::tft.width();
const uint16_t part = max_width / points; 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++) { for (uint8_t i = 0; points >= i; i++) {
// draw lines between point->minVoltage and point->maxVoltage from left to right // 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) if (const auto point = get_point_n_voltages(configs.battery.cellType.value, points - i); point)
{ {
// TODO: Fix float_map to not be broken const int x1 = 2 + part * (points - i + 1);
// Current output: float_map(335, 2.55, 4.5, 0, 280) = 47736 const int y1 = float_map(point->minVoltage / 100.f, min_voltage, max_voltage, max_height, TOP_OFFSET);
const int x1 = part * i; const int x2 = 2 + part * (points - i);
const int y1 = float_map(point->minVoltage, 2.55, 4.5, 0, max_height); const int y2 = float_map(point->maxVoltage / 100.f, min_voltage, max_voltage, max_height, TOP_OFFSET);
const int x2 = part * (i + 1);
const int y2 = float_map(point->maxVoltage, 2.55, 4.5, 0, max_height);
espgui::tft.drawLine(x1, y1, x2, y2, TFT_WHITE); 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); ESP_LOGI(TAG, "espgui::tft.drawLine(%d, %d, %d, %d, TFT_WHITE);", x1, y1, x2, y2);
} }