Some battery graph fixes

This commit is contained in:
CommanderRedYT
2022-03-04 22:14:41 +01:00
parent bda9707638
commit 81d467e69f
2 changed files with 36 additions and 25 deletions

View File

@ -11,6 +11,7 @@ constexpr const char * const TAG = "BatteryGraphDisplay";
#include "globals.h"
#include "newsettings.h"
#include "utils.h"
#include "actions/bluetoothbeginmasteraction.h"
namespace {
constexpr char TEXT_BATTERY_GRAPH[] = "Battery Level";
@ -20,29 +21,15 @@ namespace {
void BatteryGraphDisplay::initScreen()
{
Base::initScreen();
const auto points = count_curve_points(configs.battery.cellType.value);
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)
{
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);
}
}
drawBatteryCurve();
}
std::string BatteryGraphDisplay::text() const
{
if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage)
{
return fmt::format("{} ({:.1f}%)", TEXT_BATTERY_GRAPH, getBatteryPercentage(*avgVoltage, configs.battery.cellType.value));
}
return TEXT_BATTERY_GRAPH;
}
@ -57,16 +44,17 @@ void BatteryGraphDisplay::redraw()
return;
const auto cellType = configs.battery.cellType.value;
const uint16_t onePercent = tft.width() / 100;
const uint16_t xOffset = onePercent * getBatteryPercentage(*avgVoltage, cellType);
const uint16_t lastXOffset = onePercent * getBatteryPercentage(m_lastBatVoltage, cellType);
const float onePercent = (tft.width() - 4) / 100.f;
const auto percentage = getBatteryPercentage(*avgVoltage, cellType);
const uint16_t xOffset = onePercent * (100 - percentage);
const uint16_t lastXOffset = onePercent * (100 - getBatteryPercentage(m_lastBatVoltage, cellType));
// clear the old one and draw the new one
tft.fillRect(lastXOffset, TOP_OFFSET, onePercent, tft.height() - TOP_OFFSET, TFT_BLACK);
tft.fillRect(xOffset, TOP_OFFSET, onePercent, tft.height() - TOP_OFFSET, TFT_WHITE);
tft.fillRect(lastXOffset + 2, TOP_OFFSET, onePercent, tft.height() - TOP_OFFSET, TFT_BLACK);
tft.fillRect(xOffset + 2, TOP_OFFSET, onePercent, tft.height() - TOP_OFFSET, TFT_WHITE);
m_lastBatVoltage = *avgVoltage;
drawBatteryCurve();
}
// tft.drawLine() code
}
@ -84,3 +72,24 @@ void BatteryGraphDisplay::buttonPressed(espgui::Button button)
default:;
}
}
void BatteryGraphDisplay::drawBatteryCurve()
{
const auto points = count_curve_points(configs.battery.cellType.value);
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);
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)
{
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);
}
}
}

View File

@ -13,6 +13,8 @@ public:
void buttonPressed(espgui::Button button) override;
static void drawBatteryCurve();
private:
float m_lastBatVoltage{0};
};