Merge pull request #283 from bobbycar-graz/error-handling

This commit is contained in:
CommanderRedYT
2022-03-04 22:19:51 +01:00
committed by GitHub
7 changed files with 42 additions and 52 deletions

View File

@ -271,7 +271,6 @@ std::optional<CalibrationPointVoltages> get_point_n_voltages(BatteryCellType cel
count++;
uint8_t count = 0;
CalibrationPointVoltages point;
switch (cellType)
{
case BatteryCellType::_22P:

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};
};

View File

@ -6,12 +6,11 @@
// local includes
#include "actions/dummyaction.h"
#include "actions/switchscreenaction.h"
#include "bobbyerrorhandler.h"
#include "buildserver.h"
#include "displays/menus/otamenu.h"
#include "globals.h"
#include "icons/back.h"
#include "icons/reboot.h"
#include "utils.h"
#include "newsettings.h"
namespace {
@ -124,7 +123,7 @@ void SelectBuildserverBranchMenu::update()
check_descriptor_request();
if (!request_failed.empty())
{
this->buildMenuRequestError(request_failed);
BobbyErrorHandler{}.errorOccured(fmt::format("Error: {}", request_failed));
request_failed = {};
}
}
@ -154,12 +153,3 @@ void SelectBuildserverBranchMenu::back()
{
espgui::switchScreen<OtaMenu>();
}
void SelectBuildserverBranchMenu::buildMenuRequestError(std::string error)
{
using namespace espgui;
auto &item = constructMenuItem<makeComponent<MenuItem, ChangeableText, DefaultFont, StaticColor<TFT_RED>, DummyAction>>();
item.setTitle(error);
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BACK>, SwitchScreenAction<OtaMenu>, StaticMenuItemIcon<&espgui::icons::back>>>();
}

View File

@ -11,7 +11,6 @@ public:
std::string text() const override;
void buildMenuRequestError(std::string error);
void update() override;
void back() override;
};

View File

@ -6,13 +6,12 @@
#include "fmt/core.h"
// local includes
#include "buildserver.h"
#include "utils.h"
#include "actions/dummyaction.h"
#include "actions/switchscreenaction.h"
#include "icons/back.h"
#include "bobbyerrorhandler.h"
#include "buildserver.h"
#include "displays/menus/otamenu.h"
#include "globals.h"
#include "icons/back.h"
#include "newsettings.h"
#define MESSAGE(text) constructMenuItem<makeComponent<MenuItem, StaticText<text>, DefaultFont, StaticColor<TFT_RED>, DummyAction>>()
@ -108,7 +107,7 @@ void SelectBuildMenu::update()
check_descriptor_request();
if (!request_failed.empty())
{
this->buildMenuRequestError(request_failed);
BobbyErrorHandler{}.errorOccured(fmt::format("Error: {}", request_failed));
request_failed = {};
}
}
@ -139,13 +138,6 @@ void SelectBuildMenu::buildMenuFromJson()
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BACK>, SwitchScreenAction<OtaMenu>, StaticMenuItemIcon<&espgui::icons::back>>>();
}
void SelectBuildMenu::buildMenuRequestError(std::string error)
{
auto &item = constructMenuItem<makeComponent<MenuItem, ChangeableText, DefaultFont, StaticColor<TFT_RED>, DummyAction>>();
item.setTitle(error);
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BACK>, SwitchScreenAction<OtaMenu>, StaticMenuItemIcon<&espgui::icons::back>>>();
}
void SelectBuildMenu::back()
{
switchScreen<OtaMenu>();

View File

@ -17,5 +17,4 @@ public:
private:
void buildMenuFromJson();
void buildMenuRequestError(std::string error);
};