Fixes and new apps (#30)
- M5 Unit Modules library + M5 Unit Test app - Minor fixes for TodoList, TwoEleven, Snake, Brainfuck and Breakout - Fixed SerialConsole to use the uart controller as it was broken in one of the many updates - Fixed keyboard input in TwoEleven, Breakout, Magic8Ball and Snake - Bluetooth Media Keys app, supports pressing physical keys to trigger the corresponding buttonmatrix button - Epub Reader app
This commit is contained in:
@@ -6,21 +6,20 @@
|
||||
#include <vector>
|
||||
#include <lvgl.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>
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/drivers/uart_controller.h>
|
||||
|
||||
class ConnectView final : public View {
|
||||
|
||||
public:
|
||||
|
||||
typedef std::function<void(std::unique_ptr<Uart>)> OnConnectedFunction;
|
||||
std::vector<std::string> uartNames;
|
||||
typedef std::function<void(Device*)> OnConnectedFunction;
|
||||
std::vector<Device*> uartDevices;
|
||||
Preferences preferences = Preferences("SerialConsole");
|
||||
LvglLock lvglLock;
|
||||
|
||||
@@ -30,13 +29,11 @@ private:
|
||||
lv_obj_t* busDropdown = nullptr;
|
||||
lv_obj_t* speedTextarea = nullptr;
|
||||
|
||||
std::string join(const std::vector<std::string>& list) {
|
||||
std::string buildDeviceOptions() {
|
||||
std::string output;
|
||||
for (int i = list.size() - 1; i >= 0; i--) {
|
||||
output.append(list[i].c_str());
|
||||
if (i < list.size() - 1) {
|
||||
output.append(",");
|
||||
}
|
||||
for (size_t i = 0; i < uartDevices.size(); i++) {
|
||||
if (i > 0) output.append("\n");
|
||||
output.append(uartDevices[i]->name);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
@@ -54,36 +51,37 @@ private:
|
||||
|
||||
const char* alert_dialog_labels[] = { "OK" };
|
||||
|
||||
auto selected_uart_index = lv_dropdown_get_selected(busDropdown);
|
||||
if (selected_uart_index >= uartNames.size()) {
|
||||
uint32_t selected_index = lv_dropdown_get_selected(busDropdown);
|
||||
if (selected_index >= uartDevices.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;
|
||||
}
|
||||
Device* dev = uartDevices[selected_index];
|
||||
|
||||
if (!uart->setBaudRate(speed)) {
|
||||
uart->stop();
|
||||
UartConfig cfg = {
|
||||
(uint32_t)speed,
|
||||
UART_CONTROLLER_DATA_8_BITS,
|
||||
UART_CONTROLLER_PARITY_DISABLE,
|
||||
UART_CONTROLLER_STOP_BITS_1,
|
||||
};
|
||||
if (uart_controller_set_config(dev, &cfg) != ERROR_NONE) {
|
||||
tt_app_alertdialog_start("Error", "Failed to set baud rate", alert_dialog_labels, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
onConnected(std::move(uart));
|
||||
if (uart_controller_open(dev) != ERROR_NONE) {
|
||||
tt_app_alertdialog_start("Error", "Failed to open UART", alert_dialog_labels, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
onConnected(dev);
|
||||
}
|
||||
|
||||
static void onConnectCallback(lv_event_t* event) {
|
||||
@@ -104,7 +102,12 @@ public:
|
||||
explicit ConnectView(OnConnectedFunction onConnected) : onConnected(std::move(onConnected)) {}
|
||||
|
||||
void onStart(lv_obj_t* parent) {
|
||||
uartNames = Uart::getNames();
|
||||
// Enumerate UART controller devices
|
||||
uartDevices.clear();
|
||||
device_for_each_of_type(&UART_CONTROLLER_TYPE, &uartDevices, [](Device* d, void* ctx) -> bool {
|
||||
static_cast<std::vector<Device*>*>(ctx)->push_back(d);
|
||||
return true;
|
||||
});
|
||||
|
||||
auto* wrapper = lv_obj_create(parent);
|
||||
lv_obj_set_flex_flow(wrapper, LV_FLEX_FLOW_COLUMN);
|
||||
@@ -118,15 +121,15 @@ public:
|
||||
|
||||
busDropdown = lv_dropdown_create(bus_wrapper);
|
||||
|
||||
auto bus_options = join(uartNames);
|
||||
lv_dropdown_set_options(busDropdown, bus_options.c_str());
|
||||
auto bus_options = buildDeviceOptions();
|
||||
lv_dropdown_set_options(busDropdown, bus_options.empty() ? "none" : 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);
|
||||
if (bus_index >= 0 && (size_t)bus_index < uartDevices.size()) {
|
||||
lv_dropdown_set_selected(busDropdown, (uint32_t)bus_index);
|
||||
}
|
||||
|
||||
auto* bus_label = lv_label_create(bus_wrapper);
|
||||
|
||||
@@ -6,13 +6,14 @@
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <lvgl.h>
|
||||
#include <memory>
|
||||
|
||||
#include <tt_lvgl.h>
|
||||
|
||||
#include <Tactility/RecursiveMutex.h>
|
||||
#include <Tactility/Thread.h>
|
||||
#include <TactilityCpp/LvglLock.h>
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/drivers/uart_controller.h>
|
||||
|
||||
constexpr size_t receiveBufferSize = 512;
|
||||
constexpr size_t renderBufferSize = receiveBufferSize + 2; // Leave space for newline at split and null terminator at the end
|
||||
@@ -24,7 +25,7 @@ class ConsoleView final : public View {
|
||||
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;
|
||||
Device* uartDev = nullptr;
|
||||
std::unique_ptr<tt::Thread> uartThread _Nullable = nullptr;
|
||||
bool uartThreadInterrupted = false;
|
||||
std::unique_ptr<tt::Thread> viewThread _Nullable = nullptr;
|
||||
@@ -91,18 +92,16 @@ class ConsoleView final : public View {
|
||||
}
|
||||
|
||||
int32_t uartThreadMain() {
|
||||
char byte;
|
||||
|
||||
while (!isUartThreadInterrupted()) {
|
||||
assert(uart != nullptr);
|
||||
bool success = uart->readByte(&byte, tt::kernel::millisToTicks(50));
|
||||
uint8_t byte;
|
||||
error_t err = uart_controller_read_byte(uartDev, &byte, tt::kernel::millisToTicks(50));
|
||||
|
||||
// Thread might've been interrupted in the meanwhile
|
||||
if (isUartThreadInterrupted()) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (success) {
|
||||
if (err == ERROR_NONE) {
|
||||
mutex.lock();
|
||||
receiveBuffer[receiveBufferPosition++] = byte;
|
||||
if (receiveBufferPosition == receiveBufferSize) {
|
||||
@@ -110,7 +109,6 @@ class ConsoleView final : public View {
|
||||
}
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -145,10 +143,17 @@ class ConsoleView final : public View {
|
||||
std::string input_text = lv_textarea_get_text(inputTextarea);
|
||||
std::string to_send;
|
||||
to_send.append(input_text + terminatorString);
|
||||
Device* localUart = uartDev;
|
||||
mutex.unlock();
|
||||
|
||||
if (uart != nullptr) {
|
||||
if (!uart->writeBytes(to_send.c_str(), to_send.length(), 100 / portTICK_PERIOD_MS)) {
|
||||
if (localUart != nullptr) {
|
||||
error_t err = uart_controller_write_bytes(
|
||||
localUart,
|
||||
reinterpret_cast<const uint8_t*>(to_send.c_str()),
|
||||
to_send.length(),
|
||||
tt::kernel::millisToTicks(100)
|
||||
);
|
||||
if (err != ERROR_NONE) {
|
||||
ESP_LOGE(TAG, "Failed to send \"%s\"", input_text.c_str());
|
||||
}
|
||||
}
|
||||
@@ -158,13 +163,16 @@ class ConsoleView final : public View {
|
||||
|
||||
public:
|
||||
|
||||
void startLogic(std::unique_ptr<Uart> newUart) {
|
||||
void startLogic(Device* dev) {
|
||||
assert(dev != nullptr);
|
||||
if (dev == nullptr) return;
|
||||
|
||||
memset(receiveBuffer, 0, receiveBufferSize);
|
||||
|
||||
assert(uartThread == nullptr);
|
||||
assert(uart == nullptr);
|
||||
assert(uartDev == nullptr);
|
||||
|
||||
uart = std::move(newUart);
|
||||
uartDev = dev;
|
||||
|
||||
uartThreadInterrupted = false;
|
||||
uartThread = std::make_unique<tt::Thread>(
|
||||
@@ -206,7 +214,6 @@ public:
|
||||
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");
|
||||
@@ -261,17 +268,17 @@ public:
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
if (uart != nullptr && uart->isStarted()) {
|
||||
uart->stop();
|
||||
uart = nullptr;
|
||||
if (uartDev != nullptr) {
|
||||
uart_controller_close(uartDev);
|
||||
uartDev = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void onStart(lv_obj_t* parent, std::unique_ptr<Uart> newUart) {
|
||||
void onStart(lv_obj_t* parent, Device* dev) {
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
startLogic(std::move(newUart));
|
||||
startLogic(dev);
|
||||
startViews(parent);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,10 +11,14 @@ void SerialConsole::stopActiveView() {
|
||||
}
|
||||
}
|
||||
|
||||
void SerialConsole::showConsoleView(std::unique_ptr<Uart> uart) {
|
||||
void SerialConsole::showConsoleView(Device* dev) {
|
||||
if (dev == nullptr) {
|
||||
ESP_LOGE(TAG, "showConsoleView: null device");
|
||||
return;
|
||||
}
|
||||
stopActiveView();
|
||||
activeView = &consoleView;
|
||||
consoleView.onStart(wrapperWidget, std::move(uart));
|
||||
consoleView.onStart(wrapperWidget, dev);
|
||||
lv_obj_remove_flag(disconnectButton, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,14 +9,14 @@ 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));
|
||||
ConnectView connectView = ConnectView([this](Device* dev){
|
||||
showConsoleView(dev);
|
||||
});
|
||||
ConsoleView consoleView;
|
||||
View* activeView = nullptr;
|
||||
|
||||
void stopActiveView();
|
||||
void showConsoleView(std::unique_ptr<Uart> uart);
|
||||
void showConsoleView(Device* dev);
|
||||
void showConnectView();
|
||||
void onDisconnect();
|
||||
static void onDisconnectPressed(lv_event_t* event);
|
||||
|
||||
@@ -5,6 +5,6 @@ sdk=0.7.0-dev
|
||||
platforms=esp32,esp32s3,esp32c6,esp32p4
|
||||
[app]
|
||||
id=one.tactility.serialconsole
|
||||
versionName=0.3.0
|
||||
versionCode=3
|
||||
versionName=0.4.0
|
||||
versionCode=4
|
||||
name=Serial Console
|
||||
|
||||
Reference in New Issue
Block a user