Files
bobbycar-boardcomputer-firm…/main/displays/batterygraphdisplay.cpp
T

95 lines
3.2 KiB
C++
Raw Normal View History

2022-02-27 23:42:55 +01:00
#include "batterygraphdisplay.h"
2022-02-28 21:59:28 +01:00
constexpr const char * const TAG = "BatteryGraphDisplay";
2022-02-27 23:42:55 +01:00
// 3rdparty lib includes
#include <screenmanager.h>
2022-02-28 01:45:03 +01:00
#include <tftinstance.h>
2022-02-27 23:42:55 +01:00
// local includes
2022-02-28 01:45:03 +01:00
#include "battery.h"
2022-02-27 23:42:55 +01:00
#include "displays/menus/batterymenu.h"
#include "globals.h"
2022-02-28 21:59:28 +01:00
#include "newsettings.h"
#include "utils.h"
2022-03-04 22:14:41 +01:00
#include "actions/bluetoothbeginmasteraction.h"
2022-02-27 23:42:55 +01:00
namespace {
constexpr char TEXT_BATTERY_GRAPH[] = "Battery Level";
2022-02-28 21:59:28 +01:00
constexpr const uint8_t TOP_OFFSET = 40;
2022-02-27 23:42:55 +01:00
} // namespace
2022-02-28 01:08:13 +01:00
void BatteryGraphDisplay::initScreen()
{
2022-02-27 23:42:55 +01:00
Base::initScreen();
2022-03-04 22:14:41 +01:00
drawBatteryCurve();
2022-02-27 23:42:55 +01:00
}
2022-02-28 01:08:13 +01:00
std::string BatteryGraphDisplay::text() const
{
2022-03-04 22:14:41 +01:00
if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage)
{
2022-04-29 22:40:49 +02:00
return fmt::format("{} ({:.1f}%)", TEXT_BATTERY_GRAPH, getBatteryPercentage(*avgVoltage, configs.battery.cellType.value()));
2022-03-04 22:14:41 +01:00
}
2022-02-27 23:42:55 +01:00
return TEXT_BATTERY_GRAPH;
}
2022-02-28 01:08:13 +01:00
void BatteryGraphDisplay::redraw()
{
using namespace espgui;
2022-02-27 23:42:55 +01:00
Base::redraw();
2022-02-28 01:08:13 +01:00
2022-02-28 01:45:03 +01:00
if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage)
{
if (*avgVoltage == m_lastBatVoltage)
return;
2022-04-29 22:40:49 +02:00
const auto cellType = configs.battery.cellType.value();
2022-02-28 01:45:03 +01:00
2022-03-04 22:14:41 +01:00
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));
2022-02-28 01:45:03 +01:00
// clear the old one and draw the new one
2022-03-04 22:14:41 +01:00
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);
2022-02-28 01:45:03 +01:00
m_lastBatVoltage = *avgVoltage;
2022-03-04 22:14:41 +01:00
drawBatteryCurve();
2022-02-28 01:45:03 +01:00
}
2022-02-28 01:08:13 +01:00
// tft.drawLine() code
2022-02-27 23:42:55 +01:00
}
void BatteryGraphDisplay::buttonPressed(espgui::Button button)
{
Base::buttonPressed(button);
switch (button)
{
using espgui::Button;
case Button::Left:
case Button::Right:
espgui::popScreen();
2022-02-27 23:42:55 +01:00
break;
default:;
}
}
2022-03-04 22:14:41 +01:00
void BatteryGraphDisplay::drawBatteryCurve()
{
2022-04-29 22:40:49 +02:00
const auto points = count_curve_points(configs.battery.cellType.value());
2022-03-04 22:14:41 +01:00
const auto max_height = espgui::tft.height() - 1;
const auto max_width = espgui::tft.width() - 4;
const uint16_t part = max_width / points;
2022-04-29 22:40:49 +02:00
const auto min_voltage = getMinBatCellVoltage(configs.battery.cellType.value());
const auto max_voltage = getMaxBatCellVoltage(configs.battery.cellType.value());
2022-03-04 22:14:41 +01:00
for (uint8_t i = 0; points >= i; i++) {
// draw lines between point->minVoltage and point->maxVoltage from left to right
2022-04-29 22:40:49 +02:00
if (const auto point = get_point_n_voltages(configs.battery.cellType.value(), points - i); point)
2022-03-04 22:14:41 +01:00
{
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);
}
}
}