Add Serial Console app and update other apps for new SDK (#10)

This commit is contained in:
Ken Van Hoeylandt
2025-10-08 23:28:37 +02:00
committed by GitHub
parent d33e7a41df
commit a42f018ddc
29 changed files with 1679 additions and 30 deletions
+17
View File
@@ -0,0 +1,17 @@
file(GLOB_RECURSE SOURCE_FILES
Source/*.c*
# Library source files must be included directly,
# because all regular dependencies get stripped by elf_loader's cmake script
../../../Libraries/Str/Source/*.c**
../../../Libraries/TactilityCpp/Source/*.c**
)
idf_component_register(
SRCS ${SOURCE_FILES}
# Library headers must be included directly,
# because all regular dependencies get stripped by elf_loader's cmake script
INCLUDE_DIRS ../../../Libraries/Str/Include
INCLUDE_DIRS ../../../Libraries/TactilityCpp/Include
REQUIRES TactilitySDK
)
@@ -0,0 +1,171 @@
#pragma once
#include "View.h"
#include <string>
#include <vector>
#include <lvgl.h>
#include <Str.h>
#include <functional>
#include <memory>
#include <tt_app_alertdialog.h>
#include <tt_hal_uart.h>
#include <tt_lvgl.h>
#include <TactilityCpp/LvglLock.h>
#include <TactilityCpp/Uart.h>
#include <TactilityCpp/Preferences.h>
class ConnectView final : public View {
public:
typedef std::function<void(std::unique_ptr<Uart>)> OnConnectedFunction;
std::vector<Str> uartNames;
Preferences preferences = Preferences("SerialConsole");
LvglLock lvglLock;
private:
OnConnectedFunction onConnected;
lv_obj_t* busDropdown = nullptr;
lv_obj_t* speedTextarea = nullptr;
Str join(const std::vector<Str>& list) {
Str output;
for (int i = list.size() - 1; i >= 0; i--) {
output.append(list[i].c_str());
if (i < list.size() - 1) {
output.append(",");
}
}
return output;
}
int32_t getSpeedInput() const {
auto* speed_text = lv_textarea_get_text(speedTextarea);
return atoi(speed_text);
}
void onConnect() {
auto lock = lvglLock.asScopedLock();
if (!lock.lock(TT_LVGL_DEFAULT_LOCK_TIME)) {
return;
}
const char* alert_dialog_labels[] = { "OK" };
auto selected_uart_index = lv_dropdown_get_selected(busDropdown);
if (selected_uart_index >= uartNames.size()) {
tt_app_alertdialog_start("Error", "No UART selected", alert_dialog_labels, 1);
return;
}
auto uart = Uart::open(selected_uart_index);
if (uart == nullptr) {
tt_app_alertdialog_start("Error", "Failed to connect to UART", alert_dialog_labels, 1);
return;
}
int speed = getSpeedInput();
if (speed <= 0) {
tt_app_alertdialog_start("Error", "Invalid speed", alert_dialog_labels, 1);
return;
}
if (!uart->start()) {
tt_app_alertdialog_start("Error", "Failed to initialize", alert_dialog_labels, 1);
return;
}
if (!uart->setBaudRate(speed)) {
uart->stop();
tt_app_alertdialog_start("Error", "Failed to set baud rate", alert_dialog_labels, 1);
return;
}
onConnected(std::move(uart));
}
static void onConnectCallback(lv_event_t* event) {
auto* view = static_cast<ConnectView*>(lv_event_get_user_data(event));
view->onConnect();
}
static lv_obj_t* createRowWrapper(lv_obj_t* parent) {
auto* wrapper = lv_obj_create(parent);
lv_obj_set_size(wrapper, LV_PCT(100), LV_SIZE_CONTENT);
lv_obj_set_style_border_width(wrapper, 0, LV_STATE_DEFAULT);
lv_obj_set_style_pad_all(wrapper, 0, LV_STATE_DEFAULT);
return wrapper;
}
public:
explicit ConnectView(OnConnectedFunction onConnected) : onConnected(std::move(onConnected)) {}
void onStart(lv_obj_t* parent) {
uartNames = Uart::getNames();
auto* wrapper = lv_obj_create(parent);
lv_obj_set_flex_flow(wrapper, LV_FLEX_FLOW_COLUMN);
lv_obj_set_size(wrapper, LV_PCT(100), LV_SIZE_CONTENT);
lv_obj_set_style_border_width(wrapper, 0, LV_STATE_DEFAULT);
lv_obj_set_style_bg_opa(wrapper, 0, LV_STATE_DEFAULT);
// Bus selection
auto* bus_wrapper = createRowWrapper(wrapper);
busDropdown = lv_dropdown_create(bus_wrapper);
auto bus_options = join(uartNames);
lv_dropdown_set_options(busDropdown, bus_options.c_str());
lv_obj_align(busDropdown, LV_ALIGN_RIGHT_MID, 0, 0);
lv_obj_set_width(busDropdown, LV_PCT(50));
int32_t bus_index = 0;
preferences.optInt32("bus", bus_index);
if (bus_index < uartNames.size()) {
lv_dropdown_set_selected(busDropdown, bus_index);
}
auto* bus_label = lv_label_create(bus_wrapper);
lv_obj_align(bus_label, LV_ALIGN_LEFT_MID, 0, 0);
lv_label_set_text(bus_label, "Bus");
// Baud rate selection
auto* baud_wrapper = createRowWrapper(wrapper);
int32_t speed = 115200;
preferences.optInt32("speed", speed);
speedTextarea = lv_textarea_create(baud_wrapper);
lv_textarea_set_text(speedTextarea, std::to_string(speed).c_str());
lv_textarea_set_one_line(speedTextarea, true);
lv_obj_set_width(speedTextarea, LV_PCT(50));
lv_obj_align(speedTextarea, LV_ALIGN_TOP_RIGHT, 0, 0);
auto* baud_rate_label = lv_label_create(baud_wrapper);
lv_obj_align(baud_rate_label, LV_ALIGN_TOP_LEFT, 0, 0);
lv_label_set_text(baud_rate_label, "Baud");
// Connect
auto* connect_wrapper = createRowWrapper(wrapper);
auto* connect_button = lv_button_create(connect_wrapper);
lv_obj_align(connect_button, LV_ALIGN_CENTER, 0, 0);
lv_obj_add_event_cb(connect_button, onConnectCallback, LV_EVENT_SHORT_CLICKED, this);
auto* connect_label = lv_label_create(connect_button);
lv_label_set_text(connect_label, "Connect");
}
void onStop() override {
int speed = getSpeedInput();
if (speed > 0) {
preferences.putInt32("speed", speed);
}
auto bus_index = static_cast<int32_t>(lv_dropdown_get_selected(busDropdown));
preferences.putInt32("bus", bus_index);
}
};
@@ -0,0 +1,299 @@
#pragma once
#include "View.h"
#include "esp_log.h"
#include <Str.h>
#include <sstream>
#include <lvgl.h>
#include <memory>
#include <tt_lvgl.h>
#include <tt_thread.h>
#include <TactilityCpp/Mutex.h>
#include <TactilityCpp/Thread.h>
#include <TactilityCpp/LvglLock.h>
constexpr size_t receiveBufferSize = 512;
constexpr size_t renderBufferSize = receiveBufferSize + 2; // Leave space for newline at split and null terminator at the end
class ConsoleView final : public View {
const char* TAG = "SerialConsole";
lv_obj_t* _Nullable parent = nullptr;
lv_obj_t* _Nullable logTextarea = nullptr;
lv_obj_t* _Nullable inputTextarea = nullptr;
std::shared_ptr<Uart> _Nullable uart = nullptr;
std::shared_ptr<Thread> uartThread _Nullable = nullptr;
bool uartThreadInterrupted = false;
std::shared_ptr<Thread> viewThread _Nullable = nullptr;
bool viewThreadInterrupted = false;
Mutex mutex = Mutex(MutexTypeRecursive);
uint8_t receiveBuffer[receiveBufferSize];
uint8_t renderBuffer[renderBufferSize];
size_t receiveBufferPosition = 0;
Str terminatorString = "\n";
LvglLock lvglLock;
bool isUartThreadInterrupted() const {
auto lock = mutex.asScopedLock();
lock.lock();
return uartThreadInterrupted;
}
bool isViewThreadInterrupted() const {
auto lock = mutex.asScopedLock();
lock.lock();
return viewThreadInterrupted;
}
void updateViews() {
auto scoped_lvgl_lock = lvglLock.asScopedLock();
if (!scoped_lvgl_lock.lock()) {
return;
}
if (parent == nullptr) {
return;
}
// Updating the view is expensive, so we only want to set the text once:
// Gather all the lines in a single buffer
if (mutex.lock()) {
size_t first_part_size = receiveBufferSize - receiveBufferPosition;
memcpy(renderBuffer, receiveBuffer + receiveBufferPosition, first_part_size);
renderBuffer[receiveBufferPosition] = '\n';
if (receiveBufferPosition > 0) {
memcpy(renderBuffer + first_part_size + 1, receiveBuffer, (receiveBufferSize - first_part_size));
renderBuffer[receiveBufferSize - 1] = 0x00;
}
mutex.unlock();
}
tt_lvgl_lock(TT_MAX_TICKS);
lv_textarea_set_text(logTextarea, (const char*)renderBuffer);
tt_lvgl_unlock();
}
int32_t viewThreadMain() {
while (!isViewThreadInterrupted()) {
auto start_time = tt_kernel_get_ticks();
updateViews();
auto end_time = tt_kernel_get_ticks();
auto time_diff = end_time - start_time;
if (time_diff < 500U) {
tt_kernel_delay_ticks((500U - time_diff) / portTICK_PERIOD_MS);
}
}
return 0;
}
static int32_t viewThreadMainStatic(void* context) {
auto* self = static_cast<ConsoleView*>(context);
return self->viewThreadMain();
}
int32_t uartThreadMain() {
char byte;
while (!isUartThreadInterrupted()) {
assert(uart != nullptr);
bool success = uart->readByte(&byte, 50 / portTICK_PERIOD_MS);
// Thread might've been interrupted in the meanwhile
if (isUartThreadInterrupted()) {
break;
}
if (success) {
mutex.lock();
receiveBuffer[receiveBufferPosition++] = byte;
if (receiveBufferPosition == receiveBufferSize) {
receiveBufferPosition = 0;
}
mutex.unlock();
}
}
return 0;
}
static int32_t uartThreadMainStatic(void* view) {
auto* self = static_cast<ConsoleView*>(view);
return self->uartThreadMain();
}
static void onSendClickedCallback(lv_event_t* event) {
auto* view = (ConsoleView*)lv_event_get_user_data(event);
view->onSendClicked();
}
static void onTerminatorDropdownValueChangedCallback(lv_event_t* event) {
auto* view = (ConsoleView*)lv_event_get_user_data(event);
view->onTerminatorDropDownValueChanged(event);
}
void onTerminatorDropDownValueChanged(lv_event_t* event) {
auto* dropdown = static_cast<lv_obj_t*>(lv_event_get_target(event));
mutex.lock();
switch (lv_dropdown_get_selected(dropdown)) {
case 0:
terminatorString = "\n";
break;
case 1:
terminatorString = "\r\n";
break;
}
mutex.unlock();
}
void onSendClicked() {
mutex.lock();
Str input_text = lv_textarea_get_text(inputTextarea);
Str to_send;
to_send.appendf("%s%s", input_text.c_str(), terminatorString.c_str());
mutex.unlock();
if (uart != nullptr) {
if (!uart->writeBytes(to_send.c_str(), to_send.length(), 100 / portTICK_PERIOD_MS)) {
ESP_LOGE(TAG, "Failed to send \"%s\"", input_text.c_str());
}
}
lv_textarea_set_text(inputTextarea, "");
}
public:
void startLogic(std::unique_ptr<Uart> newUart) {
memset(receiveBuffer, 0, receiveBufferSize);
assert(uartThread == nullptr);
assert(uart == nullptr);
uart = std::move(newUart);
uartThreadInterrupted = false;
uartThread = std::make_unique<Thread>(
"SerConsUart",
4096,
uartThreadMainStatic,
this
);
uartThread->setPriority(ThreadPriorityHigh);
uartThread->start();
}
void startViews(lv_obj_t* parent) {
this->parent = parent;
lv_obj_set_style_pad_gap(parent, 2, 0);
logTextarea = lv_textarea_create(parent);
lv_textarea_set_placeholder_text(logTextarea, "Waiting for data...");
lv_obj_set_flex_grow(logTextarea, 1);
lv_obj_set_width(logTextarea, LV_PCT(100));
lv_obj_add_state(logTextarea, LV_STATE_DISABLED);
lv_obj_set_style_margin_ver(logTextarea, 0, 0);
auto* input_wrapper = lv_obj_create(parent);
lv_obj_set_size(input_wrapper, LV_PCT(100), LV_SIZE_CONTENT);
lv_obj_set_style_pad_all(input_wrapper, 0, 0);
lv_obj_set_style_border_width(input_wrapper, 0, 0);
lv_obj_set_width(input_wrapper, LV_PCT(100));
lv_obj_set_flex_flow(input_wrapper, LV_FLEX_FLOW_ROW);
inputTextarea = lv_textarea_create(input_wrapper);
lv_textarea_set_one_line(inputTextarea, true);
lv_textarea_set_placeholder_text(inputTextarea, "Text to send");
lv_obj_set_width(inputTextarea, LV_PCT(100));
lv_obj_set_flex_grow(inputTextarea, 1);
auto* terminator_dropdown = lv_dropdown_create(input_wrapper);
lv_dropdown_set_options(terminator_dropdown, "\\n\n\\r\\n");
lv_obj_set_width(terminator_dropdown, 70);
lv_obj_add_event_cb(terminator_dropdown, onTerminatorDropdownValueChangedCallback, LV_EVENT_VALUE_CHANGED, this);
auto* button = lv_button_create(input_wrapper);
auto* button_label = lv_label_create(button);
lv_label_set_text(button_label, "Send");
lv_obj_add_event_cb(button, onSendClickedCallback, LV_EVENT_SHORT_CLICKED, this);
viewThreadInterrupted = false;
viewThread = std::make_unique<Thread>(
"SerConsView",
4096,
viewThreadMainStatic,
this
);
viewThread->setPriority(ThreadPriorityHigher);
viewThread->start();
}
void stopLogic() {
auto lock = mutex.asScopedLock();
lock.lock();
uartThreadInterrupted = true;
// Detach thread, it will auto-delete when leaving the current scope
auto old_uart_thread = std::move(uartThread);
// Unlock so thread can lock
lock.unlock();
if (old_uart_thread->getState() != ThreadStateStopped) {
// Wait for thread to finish
old_uart_thread->join();
}
}
void stopViews() {
auto lock = mutex.asScopedLock();
lock.lock();
viewThreadInterrupted = true;
// Detach thread, it will auto-delete when leaving the current scope
auto old_view_thread = std::move(viewThread);
// Unlock so thread can lock
lock.unlock();
if (old_view_thread->getState() != ThreadStateStopped) {
// Wait for thread to finish
old_view_thread->join();
}
}
void stopUart() {
auto lock = mutex.asScopedLock();
lock.lock();
if (uart != nullptr && uart->isStarted()) {
uart->stop();
uart = nullptr;
}
}
void onStart(lv_obj_t* parent, std::unique_ptr<Uart> newUart) {
auto lock = mutex.asScopedLock();
lock.lock();
startLogic(std::move(newUart));
startViews(parent);
}
void onStop() override {
stopViews();
stopLogic();
stopUart();
}
};
@@ -0,0 +1,60 @@
#include "SerialConsole.h"
#include <tt_lvgl_toolbar.h>
constexpr auto* TAG = "SerialMonitor";
void SerialConsole::stopActiveView() {
if (activeView != nullptr) {
activeView->onStop();
lv_obj_clean(wrapperWidget);
activeView = nullptr;
}
}
void SerialConsole::showConsoleView(std::unique_ptr<Uart> uart) {
stopActiveView();
activeView = &consoleView;
consoleView.onStart(wrapperWidget, std::move(uart));
lv_obj_remove_flag(disconnectButton, LV_OBJ_FLAG_HIDDEN);
}
void SerialConsole::showConnectView() {
stopActiveView();
activeView = &connectView;
connectView.onStart(wrapperWidget);
lv_obj_add_flag(disconnectButton, LV_OBJ_FLAG_HIDDEN);
}
void SerialConsole::onDisconnect() {
// Changing views (calling ConsoleView::stop()) also disconnects the UART
showConnectView();
}
void SerialConsole::onDisconnectPressed(lv_event_t* event) {
auto* app = static_cast<SerialConsole*>(lv_event_get_user_data(event));
app->onDisconnect();
}
void SerialConsole::onShow(AppHandle appHandle, lv_obj_t* parent) {
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
lv_obj_set_style_pad_row(parent, 0, LV_STATE_DEFAULT);
auto* toolbar = tt_lvgl_toolbar_create_for_app(parent, appHandle);
disconnectButton = tt_lvgl_toolbar_add_image_button_action(toolbar, LV_SYMBOL_POWER, onDisconnectPressed, this);
lv_obj_add_flag(disconnectButton, LV_OBJ_FLAG_HIDDEN);
wrapperWidget = lv_obj_create(parent);
lv_obj_set_width(wrapperWidget, LV_PCT(100));
lv_obj_set_flex_grow(wrapperWidget, 1);
lv_obj_set_flex_flow(wrapperWidget, LV_FLEX_FLOW_COLUMN);
lv_obj_set_style_pad_all(wrapperWidget, 0, LV_STATE_DEFAULT);
lv_obj_set_style_border_width(wrapperWidget, 0, LV_STATE_DEFAULT);
lv_obj_set_style_bg_opa(wrapperWidget, 0, LV_STATE_DEFAULT);
showConnectView();
}
void SerialConsole::onHide(AppHandle context) {
stopActiveView();
}
@@ -0,0 +1,28 @@
#pragma once
#include "ConnectView.h"
#include "ConsoleView.h"
#include <TactilityCpp/App.h>
class SerialConsole final : public App {
lv_obj_t* disconnectButton = nullptr;
lv_obj_t* wrapperWidget = nullptr;
ConnectView connectView = ConnectView([this](auto uart){
showConsoleView(std::move(uart));
});
ConsoleView consoleView;
View* activeView = nullptr;
void stopActiveView();
void showConsoleView(std::unique_ptr<Uart> uart);
void showConnectView();
void onDisconnect();
static void onDisconnectPressed(lv_event_t* event);
public:
void onShow(AppHandle context, lv_obj_t* parent) override;
void onHide(AppHandle context) override;
};
+6
View File
@@ -0,0 +1,6 @@
#pragma once
class View {
public:
virtual void onStop() = 0;
};
+11
View File
@@ -0,0 +1,11 @@
#include "SerialConsole.h"
#include <TactilityCpp/App.h>
extern "C" {
int main(int argc, char* argv[]) {
registerApp<SerialConsole>();
return 0;
}
}