Basic button calibration implementation

This commit is contained in:
2021-12-30 06:51:54 +01:00
parent e8a4a9f4b8
commit 97c12b103c
2 changed files with 99 additions and 1 deletions

View File

@ -1,18 +1,32 @@
#include "buttoncalibratedisplay.h"
// esp-idf includes
#include <esp_log.h>
namespace {
constexpr const char TAG[] = "BUTTON";
} // namespace
std::string ButtonCalibrateDisplay::text() const
{
return "TODO";
return "Button calibrate";
}
void ButtonCalibrateDisplay::start()
{
Base::start();
m_oldMode = currentMode;
currentMode = &m_mode;
m_lastButton = std::nullopt;
m_status = WaitingLeft;
}
void ButtonCalibrateDisplay::initScreen()
{
Base::initScreen();
m_label.start();
}
void ButtonCalibrateDisplay::update()
@ -23,21 +37,96 @@ void ButtonCalibrateDisplay::update()
void ButtonCalibrateDisplay::redraw()
{
Base::redraw();
switch (m_status)
{
case WaitingLeft:
if (m_lastButton)
m_label.redraw("Press LEFT again");
else
m_label.redraw("Press LEFT");
break;
case WaitingRight:
if (m_lastButton)
m_label.redraw("Press RIGHT again");
else
m_label.redraw("Press RIGHT");
break;
case WaitingUp:
if (m_lastButton)
m_label.redraw("Press UP again");
else
m_label.redraw("Press UP");
break;
case WaitingDown:
if (m_lastButton)
m_label.redraw("Press DOWN again");
else
m_label.redraw("Press DOWN");
break;
case Finished:
m_label.redraw("Finished");
break;
}
}
void ButtonCalibrateDisplay::stop()
{
Base::stop();
if (currentMode == &m_mode)
{
// to avoid crash after deconstruction
m_mode.stop();
lastMode = nullptr;
currentMode = m_oldMode;
}
}
void ButtonCalibrateDisplay::rawButtonPressed(uint8_t button)
{
//Base::rawButtonPressed(button);
ESP_LOGI(TAG, "button=%hhu", button);
if (!m_lastButton || *m_lastButton != button)
m_lastButton = button;
else
{
switch (m_status)
{
case WaitingLeft:
m_leftButton = button;
m_lastButton = std::nullopt;
m_status = WaitingRight;
break;
case WaitingRight:
m_rightButton = button;
m_lastButton = std::nullopt;
m_status = WaitingUp;
break;
case WaitingUp:
m_upButton = button;
m_lastButton = std::nullopt;
m_status = WaitingDown;
break;
case WaitingDown:
m_downButton = button;
m_lastButton = std::nullopt;
m_status = Finished;
break;
case Finished:
break;
}
}
}
void ButtonCalibrateDisplay::rawButtonReleased(uint8_t button)
{
//Base::rawButtonReleased(button);
ESP_LOGI(TAG, "button=%hhu", button);
}
void ButtonCalibrateDisplay::buttonPressed(espgui::Button button)

View File

@ -2,6 +2,7 @@
// 3rdparty lib includes
#include <displaywithtitle.h>
#include <widgets/label.h>
// local includes
#include "globals.h"
@ -32,4 +33,12 @@ private:
const bool m_bootup{false};
ModeInterface *m_oldMode;
IgnoreInputMode m_mode{0, bobbycar::protocol::ControlType::FieldOrientedControl, bobbycar::protocol::ControlMode::Torque};
std::optional<uint8_t> m_lastButton;
enum { WaitingLeft, WaitingRight, WaitingUp, WaitingDown, Finished } m_status;
espgui::Label m_label{25, 72};
uint8_t m_leftButton, m_rightButton, m_upButton, m_downButton;
};