Files
bobbycar-boardcomputer-firm…/main/displays/lockscreen.cpp

218 lines
5.6 KiB
C++
Raw Normal View History

2021-11-02 23:13:43 +01:00
#include "lockscreen.h"
2022-01-04 00:54:40 +01:00
// system includes
#include <algorithm>
2021-11-02 23:13:43 +01:00
// 3rdparty lib includes
#include <tftinstance.h>
#include <screenmanager.h>
// local includes
#include "globals.h"
#include "utils.h"
#include "displays/menus/mainmenu.h"
2021-12-30 06:36:35 +01:00
#include "displays/potiscalibratedisplay.h"
2021-12-30 00:00:31 +01:00
#include "bobbybuttons.h"
2021-11-02 23:13:43 +01:00
2022-01-04 00:54:40 +01:00
namespace {
2022-04-08 18:19:24 +02:00
bool isValid1stPin(std::array<int8_t, 4> enteredPin)
2022-01-03 03:03:40 +01:00
{
2022-01-04 00:54:40 +01:00
return std::equal(std::cbegin(enteredPin), std::cend(enteredPin),
std::cbegin(configs.lockscreen.pin), std::cend(configs.lockscreen.pin),
[](const int8_t digit, const auto &configuredDigit){
return digit == configuredDigit.value;
});
2022-01-03 03:03:40 +01:00
}
2022-04-08 18:19:24 +02:00
bool isValid2ndPin(std::array<int8_t, 4> enteredPin)
{
return std::equal(std::cbegin(enteredPin), std::cend(enteredPin),
std::cbegin(configs.lockscreen.pin2), std::cend(configs.lockscreen.pin2),
[](const int8_t digit, const auto &configuredDigit){
return digit == configuredDigit.value;
});
}
2022-01-04 00:54:40 +01:00
} // namespace
2022-01-03 03:03:40 +01:00
2021-11-02 23:13:43 +01:00
void Lockscreen::start()
{
Base::start();
2021-11-02 23:13:43 +01:00
m_numbers = {0,0,0,0};
m_currentIndex = 0;
m_pressed = false;
m_rotated = 0;
m_oldMode = currentMode;
currentMode = &m_mode;
2021-11-21 01:37:51 +01:00
isLocked = true;
2022-01-03 03:03:40 +01:00
if (configs.lockscreen.keepLockedAfterReboot.value && !configs.lockscreen.locked.value)
{
2022-01-03 03:03:40 +01:00
configs.write_config(configs.lockscreen.locked, true);
}
2021-11-02 23:13:43 +01:00
}
void Lockscreen::initScreen()
{
Base::initScreen();
2021-11-02 23:13:43 +01:00
espgui::tft.fillScreen(TFT_BLACK);
espgui::tft.setTextFont(4);
espgui::tft.setTextColor(TFT_YELLOW);
2021-12-30 03:17:30 +01:00
espgui::tft.drawString("Lock vehicle", 5, 5);
2021-11-02 23:13:43 +01:00
espgui::tft.fillRect(0, 34, espgui::tft.width(), 3, TFT_WHITE);
espgui::tft.setTextColor(TFT_WHITE);
espgui::tft.drawString("Enter code to unlock:", 0, 50);
espgui::tft.setTextColor(TFT_WHITE, TFT_BLACK);
for(int i = 0; i <= 3; i++)
{
drawRect(i, 3, TFT_WHITE);
drawRect(i, 4, TFT_WHITE);
}
for (auto &label : m_labels)
label.start();
espgui::tft.setTextFont(7);
drawRect(m_currentIndex, 1, TFT_YELLOW);
drawRect(m_currentIndex, 2, TFT_YELLOW);
for(int i = 0; i <= m_currentIndex; i++)
m_labels[i].redraw(std::to_string(m_numbers[i]));
2021-11-02 23:13:43 +01:00
}
void Lockscreen::update()
{
Base::update();
2021-11-02 23:13:43 +01:00
}
void Lockscreen::redraw()
{
Base::redraw();
if (m_pressed || m_back_pressed)
2021-11-02 23:13:43 +01:00
{
drawRect(m_currentIndex, 1, TFT_BLACK);
drawRect(m_currentIndex, 2, TFT_BLACK);
if (!m_back_pressed && ++m_currentIndex>=4)
2021-11-02 23:13:43 +01:00
{
2022-04-08 18:19:24 +02:00
if (isValid1stPin(m_numbers))
2021-11-02 23:13:43 +01:00
{
2021-12-17 22:40:08 +01:00
if (!gas || !brems || *gas > 200.f || *brems > 200.f)
2021-12-30 06:36:35 +01:00
espgui::switchScreen<PotisCalibrateDisplay>(true);
2021-12-17 22:40:08 +01:00
else
espgui::switchScreen<MainMenu>();
2021-11-02 23:13:43 +01:00
#ifdef LOCKSCREEN_PLUGIN
#include LOCKSCREEN_PLUGIN
2022-04-08 18:19:24 +02:00
LOCKSCREEN_PLUGIN_FIXES_1
#endif
return;
}
else if(isValid2ndPin(m_numbers))
{
if (!gas || !brems || *gas > 200.f || *brems > 200.f)
espgui::switchScreen<PotisCalibrateDisplay>(true);
else
espgui::switchScreen<MainMenu>();
#ifdef LOCKSCREEN_PLUGIN_FIXES_2
LOCKSCREEN_PLUGIN_FIXES_2
2021-11-02 23:13:43 +01:00
#endif
return;
}
m_numbers = {0,0,0,0};
m_currentIndex = 0;
std::for_each(std::begin(m_labels) + 1, std::end(m_labels), [](auto &label){ label.redraw("0"); });
}
else if (m_back_pressed && m_currentIndex < 3)
{
drawRect(m_currentIndex+1, 1, TFT_BLACK);
drawRect(m_currentIndex+1, 2, TFT_BLACK);
2021-11-02 23:13:43 +01:00
}
m_labels[m_currentIndex].redraw(std::to_string(m_numbers[m_currentIndex]));
drawRect(m_currentIndex, 1, TFT_YELLOW);
drawRect(m_currentIndex, 2, TFT_YELLOW);
m_pressed = false;
m_back_pressed = false;
2021-11-02 23:13:43 +01:00
}
if (m_rotated)
{
m_numbers[m_currentIndex] -= m_rotated;
if (m_numbers[m_currentIndex] < 0)
m_numbers[m_currentIndex]+=10;
else if (m_numbers[m_currentIndex] > 9)
m_numbers[m_currentIndex]-=10;
m_labels[m_currentIndex].redraw(std::to_string(m_numbers[m_currentIndex]));
m_rotated = 0;
}
}
void Lockscreen::stop()
{
Base::stop();
if (currentMode == &m_mode)
{
// to avoid crash after deconstruction
m_mode.stop();
lastMode = nullptr;
currentMode = m_oldMode;
}
2021-11-21 01:37:51 +01:00
isLocked = false;
2021-12-17 22:40:08 +01:00
if (!(!gas || !brems || *gas > 200.f || *brems > 200.f))
{
2022-01-03 03:03:40 +01:00
if (configs.lockscreen.keepLockedAfterReboot.value && configs.lockscreen.locked.value)
2021-12-17 22:40:08 +01:00
{
2022-01-03 03:03:40 +01:00
configs.write_config(configs.lockscreen.locked, false);
2021-12-17 22:40:08 +01:00
}
}
2021-11-02 23:13:43 +01:00
}
void Lockscreen::buttonPressed(espgui::Button button)
2021-11-02 23:13:43 +01:00
{
2022-01-03 03:03:40 +01:00
if (configs.lockscreen.allowPresetSwitch.value ||
2021-12-30 00:00:31 +01:00
!cpputils::is_in(button, BobbyButton::Profile0, BobbyButton::Profile1, BobbyButton::Profile2, BobbyButton::Profile3))
Base::buttonPressed(button);
2021-11-02 23:13:43 +01:00
switch (button)
{
using espgui::Button;
case Button::Left:
if (m_currentIndex > 0)
m_currentIndex--;
m_back_pressed = true;
break;
case Button::Right:
m_pressed = true;
break;
case Button::Up:
m_rotated--;
break;
case Button::Down:
m_rotated++;
break;
}
2021-11-02 23:13:43 +01:00
}
void Lockscreen::drawRect(int index, int offset, uint32_t color) const
{
espgui::tft.drawRect(m_labels[index].x()-offset, m_labels[index].y()-offset, boxWidth+(offset*2), boxHeight+(offset*2), color);
}