Files
bobbycar-boardcomputer-firm…/main/debuginputhandler.cpp
T

214 lines
5.9 KiB
C++
Raw Normal View History

2021-12-19 04:23:47 +01:00
#include "debuginputhandler.h"
2021-12-24 01:28:16 +01:00
// system includes
#include <string_view>
// esp-idf includes
#include <driver/uart.h>
#include <esp_log.h>
2021-12-19 04:23:47 +01:00
// 3rdparty lib includes
#include <tftinstance.h>
2021-12-24 01:28:16 +01:00
#include <esp32-hal-gpio.h>
2021-12-30 00:00:31 +01:00
#include <screenmanager.h>
#include <changevaluedisplay.h>
#include <changevaluedisplay_string.h>
2021-12-19 04:23:47 +01:00
// local includes
#include "globals.h"
#include "utils.h"
2021-12-30 00:00:31 +01:00
#include "bobbybuttons.h"
2021-12-19 04:23:47 +01:00
2021-12-24 01:28:16 +01:00
namespace {
constexpr const char * const TAG = "DEBUG";
2021-12-19 04:23:47 +01:00
2021-12-24 01:28:16 +01:00
uint8_t consoleControlCharsReceived{};
bool uart0Initialized{};
void handleNormalChar(char c);
void handleSpecialChar(char c);
2021-12-24 01:28:16 +01:00
} // namespace
2021-12-19 04:23:47 +01:00
void initDebugInput()
{
2021-12-24 01:28:16 +01:00
if (const auto result = uart_set_pin(UART_NUM_0, UART_PIN_NO_CHANGE, 3, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); result != ESP_OK)
{
ESP_LOGE(TAG, "uart_set_pin() failed with %s", esp_err_to_name(result));
}
if (const auto result = uart_driver_install(UART_NUM_0, SOC_UART_FIFO_LEN + 1, 0, 10, nullptr, 0); result != ESP_OK)
ESP_LOGE(TAG, "uart_driver_install() failed with %s", esp_err_to_name(result));
else
uart0Initialized = true;
2021-12-19 04:23:47 +01:00
}
void handleDebugInput()
{
2021-12-24 01:28:16 +01:00
if (!uart0Initialized)
return;
2021-12-19 04:23:47 +01:00
2021-12-24 01:28:16 +01:00
size_t length{};
if (const auto result = uart_get_buffered_data_len(UART_NUM_0, &length); result != ESP_OK)
2021-12-19 04:23:47 +01:00
{
2021-12-24 01:28:16 +01:00
ESP_LOGW(TAG, "uart_get_buffered_data_len() failed with %s", esp_err_to_name(result));
}
else if (length)
{
char data[length];
length = uart_read_bytes(UART_NUM_0, data, length, 0);
2021-12-19 04:23:47 +01:00
2021-12-24 01:28:16 +01:00
for (char c : std::string_view{data, length})
2021-12-19 04:23:47 +01:00
{
2021-12-30 00:00:31 +01:00
if (consoleControlCharsReceived < 2)
2021-12-24 01:28:16 +01:00
{
2021-12-30 00:00:31 +01:00
switch (c)
{
case '\x1b':
if (consoleControlCharsReceived == 0)
consoleControlCharsReceived = 1;
else
consoleControlCharsReceived = 0;
break;
case '\x5b':
if (consoleControlCharsReceived == 1)
consoleControlCharsReceived = 2;
else
consoleControlCharsReceived = 0;
break;
default:
consoleControlCharsReceived = 0;
handleNormalChar(c);
2021-12-30 00:00:31 +01:00
}
2021-12-24 01:28:16 +01:00
}
2021-12-30 00:00:31 +01:00
else
2021-12-24 01:28:16 +01:00
{
2021-12-30 00:00:31 +01:00
consoleControlCharsReceived = 0;
handleSpecialChar(c);
2021-12-24 01:28:16 +01:00
}
2021-12-19 04:23:47 +01:00
}
}
}
namespace {
void handleNormalChar(char c)
{
if (auto display = espgui::currentDisplay ? espgui::currentDisplay->asChangeValueDisplayString() : nullptr)
{
switch (c)
{
case ' ' ... '~':
display->setShownValue(display->shownValue() + c);
break;
case 8:
if (auto val = display->shownValue(); !val.empty())
{
val.pop_back();
display->setShownValue(std::move(val));
}
break;
default:
ESP_LOGW(TAG, "unknown char %hhx %c", c, c);
}
}
else
{
switch (c)
{
case 'i':
case 'I':
espgui::tft.init();
break;
case 'p':
case 'P':
{
const auto firstPower = controllers.front.command.poweroff;
for (Controller &controller : controllers)
controller.command.poweroff = !firstPower;
break;
}
case 'l':
case 'L':
{
const auto firstLed = controllers.front.command.led;
for (Controller &controller : controllers)
controller.command.led = !firstLed;
break;
}
case 'r':
case 'R':
2022-01-03 17:13:45 +01:00
loadProfileSettings();
break;
case 's':
case 'S':
2022-01-03 17:13:45 +01:00
saveProfileSettings();
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
for (Controller &controller : controllers)
controller.command.buzzer.freq = c-'0';
break;
case 'z':
case 'Z':
if (espgui::currentDisplay)
{
espgui::currentDisplay->buttonPressed(espgui::Button(BobbyButton::Left2));
espgui::currentDisplay->buttonReleased(espgui::Button(BobbyButton::Left2));
}
break;
case 'u':
case 'U':
if (espgui::currentDisplay)
{
espgui::currentDisplay->buttonPressed(espgui::Button(BobbyButton::Right2));
espgui::currentDisplay->buttonReleased(espgui::Button(BobbyButton::Right2));
}
break;
}
}
}
void handleSpecialChar(char c)
{
switch (c)
{
case 'A': // Up arrow pressed
if (espgui::currentDisplay)
{
espgui::currentDisplay->buttonPressed(espgui::Button::Up);
espgui::currentDisplay->buttonReleased(espgui::Button::Up);
}
break;
case 'B': // Down arrow pressed
if (espgui::currentDisplay)
{
espgui::currentDisplay->buttonPressed(espgui::Button::Down);
espgui::currentDisplay->buttonReleased(espgui::Button::Down);
}
break;
case 'C': // Right arrow pressed
if (espgui::currentDisplay)
{
espgui::currentDisplay->buttonPressed(espgui::Button::Right);
espgui::currentDisplay->buttonReleased(espgui::Button::Right);
}
break;
case 'D': // Left arrow pressed
if (espgui::currentDisplay)
{
espgui::currentDisplay->buttonPressed(espgui::Button::Left);
espgui::currentDisplay->buttonReleased(espgui::Button::Left);
}
break;
default:
ESP_LOGI(TAG, "unknown control char received: %hhx", c);
}
}
} // namespace