Implement Serial Console app & more (#239)
- Implemented new app: Serial Console - `Uart::writeString()`: fixed 2 mutex bugs - `AlertDialog::start()` with default "OK" button added - Created `tt::lvgl::defaultLockTime` for re-use - Removed various usages of deprecated `lvgl::obj_set_style_no_padding()` - Implemented `hal::uart::getNames()` to list all interface names
This commit is contained in:
committed by
GitHub
parent
83a82be901
commit
13d7e84ef3
@@ -12,6 +12,7 @@ namespace tt {
|
||||
bool Preferences::optBool(const std::string& key, bool& out) const {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(namespace_, NVS_READWRITE, &handle) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to open namespace %s", namespace_);
|
||||
return false;
|
||||
} else {
|
||||
uint8_t out_number;
|
||||
@@ -27,6 +28,7 @@ bool Preferences::optBool(const std::string& key, bool& out) const {
|
||||
bool Preferences::optInt32(const std::string& key, int32_t& out) const {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(namespace_, NVS_READWRITE, &handle) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to open namespace %s", namespace_);
|
||||
return false;
|
||||
} else {
|
||||
bool success = nvs_get_i32(handle, key.c_str(), &out) == ESP_OK;
|
||||
@@ -38,6 +40,7 @@ bool Preferences::optInt32(const std::string& key, int32_t& out) const {
|
||||
bool Preferences::optString(const std::string& key, std::string& out) const {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(namespace_, NVS_READWRITE, &handle) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to open namespace %s", namespace_);
|
||||
return false;
|
||||
} else {
|
||||
size_t out_size = 256;
|
||||
@@ -69,11 +72,11 @@ void Preferences::putBool(const std::string& key, bool value) {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(namespace_, NVS_READWRITE, &handle) == ESP_OK) {
|
||||
if (nvs_set_u8(handle, key.c_str(), (uint8_t)value) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "failed to write %s:%s", namespace_, key.c_str());
|
||||
TT_LOG_E(TAG, "Failed to write %s:%s", namespace_, key.c_str());
|
||||
}
|
||||
nvs_close(handle);
|
||||
} else {
|
||||
TT_LOG_E(TAG, "failed to open namespace %s for writing", namespace_);
|
||||
TT_LOG_E(TAG, "Failed to open namespace %s", namespace_);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,11 +84,11 @@ void Preferences::putInt32(const std::string& key, int32_t value) {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(namespace_, NVS_READWRITE, &handle) == ESP_OK) {
|
||||
if (nvs_set_i32(handle, key.c_str(), value) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "failed to write %s:%s", namespace_, key.c_str());
|
||||
TT_LOG_E(TAG, "Failed to write %s:%s", namespace_, key.c_str());
|
||||
}
|
||||
nvs_close(handle);
|
||||
} else {
|
||||
TT_LOG_E(TAG, "failed to open namespace %s for writing", namespace_);
|
||||
TT_LOG_E(TAG, "Failed to open namespace %s", namespace_);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +98,7 @@ void Preferences::putString(const std::string& key, const std::string& text) {
|
||||
nvs_set_str(handle, key.c_str(), text.c_str());
|
||||
nvs_close(handle);
|
||||
} else {
|
||||
TT_LOG_E(TAG, "failed to open namespace %s for writing", namespace_);
|
||||
TT_LOG_E(TAG, "Failed to open namespace %s", namespace_);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -101,7 +101,7 @@ int32_t GpsDevice::threadMain() {
|
||||
|
||||
if (bytes_read > 0U) {
|
||||
|
||||
TT_LOG_I(TAG, "%s", buffer);
|
||||
TT_LOG_D(TAG, "%s", buffer);
|
||||
|
||||
switch (minmea_sentence_id((char*)buffer, false)) {
|
||||
case MINMEA_SENTENCE_RMC:
|
||||
|
||||
@@ -4,12 +4,15 @@
|
||||
#include <Tactility/Mutex.h>
|
||||
|
||||
#include <ranges>
|
||||
#include <cstring>
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include <esp_check.h>
|
||||
#include "Tactility/TactilityHeadless.h"
|
||||
#include "Tactility/hal/uart/UartEsp.h"
|
||||
#include <esp_check.h>
|
||||
#else
|
||||
#include "Tactility/hal/uart/UartPosix.h"
|
||||
#include <dirent.h>
|
||||
#endif
|
||||
|
||||
#define TAG "uart"
|
||||
@@ -39,15 +42,8 @@ bool init(const std::vector<uart::Configuration>& configurations) {
|
||||
}
|
||||
|
||||
bool Uart::writeString(const char* buffer, TickType_t timeout) {
|
||||
while (*buffer != 0) {
|
||||
if (writeBytes(reinterpret_cast<const std::byte*>(buffer), 1, timeout)) {
|
||||
buffer++;
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Failed to write - breaking off");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
auto size = strlen(buffer);
|
||||
writeBytes((std::byte*)buffer, size, timeout);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -105,6 +101,8 @@ size_t Uart::readUntil(std::byte* buffer, size_t bufferSize, uint8_t untilByte,
|
||||
}
|
||||
|
||||
std::unique_ptr<Uart> open(std::string name) {
|
||||
TT_LOG_I(TAG, "Open %s", name.c_str());
|
||||
|
||||
auto result = std::views::filter(uartEntries, [&name](auto& entry) {
|
||||
return entry.configuration.name == name;
|
||||
});
|
||||
@@ -123,10 +121,12 @@ std::unique_ptr<Uart> open(std::string name) {
|
||||
auto uart = create(entry.configuration);
|
||||
assert(uart != nullptr);
|
||||
entry.usageId = uart->getId();
|
||||
TT_LOG_I(TAG, "Opened %lu", entry.usageId);
|
||||
return uart;
|
||||
}
|
||||
|
||||
void close(uint32_t uartId) {
|
||||
TT_LOG_I(TAG, "Close %lu", uartId);
|
||||
auto result = std::views::filter(uartEntries, [&uartId](auto& entry) {
|
||||
return entry.usageId == uartId;
|
||||
});
|
||||
@@ -139,6 +139,32 @@ void close(uint32_t uartId) {
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> getNames() {
|
||||
std::vector<std::string> names;
|
||||
#ifdef ESP_PLATFORM
|
||||
for (auto& config : getConfiguration()->uart) {
|
||||
names.push_back(config.name);
|
||||
}
|
||||
#else
|
||||
DIR* dir = opendir("/dev");
|
||||
if (dir == nullptr) {
|
||||
TT_LOG_E(TAG, "Failed to read /dev");
|
||||
return names;
|
||||
}
|
||||
struct dirent* current_entry;
|
||||
while ((current_entry = readdir(dir)) != nullptr) {
|
||||
auto name = std::string(current_entry->d_name);
|
||||
if (name.starts_with("tty")) {
|
||||
auto path = std::string("/dev/") + name;
|
||||
names.push_back(path);
|
||||
}
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
#endif
|
||||
return names;
|
||||
}
|
||||
|
||||
Uart::Uart() : id(++lastUartId) {}
|
||||
|
||||
Uart::~Uart() {
|
||||
|
||||
@@ -13,11 +13,13 @@
|
||||
namespace tt::hal::uart {
|
||||
|
||||
bool UartEsp::start() {
|
||||
TT_LOG_I(TAG, "[%s] Starting", configuration.name.c_str());
|
||||
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
if (started) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Already started", configuration.port);
|
||||
TT_LOG_E(TAG, "[%s] Starting: Already started", configuration.name.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -30,46 +32,53 @@ bool UartEsp::start() {
|
||||
|
||||
esp_err_t result = uart_param_config(configuration.port, &configuration.config);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Failed to configure: %s", configuration.port, esp_err_to_name(result));
|
||||
TT_LOG_E(TAG, "[%s] Starting: Failed to configure: %s", configuration.name.c_str(), esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (uart_is_driver_installed(configuration.port)) {
|
||||
TT_LOG_W(TAG, "[%s] Driver was still installed. You probably forgot to stop, or another system uses/used the driver.", configuration.name.c_str());
|
||||
uart_driver_delete(configuration.port);
|
||||
}
|
||||
|
||||
result = uart_set_pin(configuration.port, configuration.txPin, configuration.rxPin, configuration.rtsPin, configuration.ctsPin);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Failed set pins: %s", configuration.port, esp_err_to_name(result));
|
||||
TT_LOG_E(TAG, "[%s] Starting: Failed set pins: %s", configuration.name.c_str(), esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
result = uart_driver_install(configuration.port, (int)configuration.rxBufferSize, (int)configuration.txBufferSize, 0, nullptr, intr_alloc_flags);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Failed to install driver: %s", configuration.port, esp_err_to_name(result));
|
||||
TT_LOG_E(TAG, "[%s] Starting: Failed to install driver: %s", configuration.name.c_str(), esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
started = true;
|
||||
|
||||
TT_LOG_I(TAG, "(%d) Started", configuration.port);
|
||||
TT_LOG_I(TAG, "[%s] Started", configuration.name.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UartEsp::stop() {
|
||||
TT_LOG_I(TAG, "[%s] Stopping", configuration.name.c_str());
|
||||
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
if (!started) {
|
||||
TT_LOG_E(TAG, "(%d) Stopping: Not started", configuration.port);
|
||||
TT_LOG_E(TAG, "[%s] Stopping: Not started", configuration.name.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
esp_err_t result = uart_driver_delete(configuration.port);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "(%d) Stopping: Failed to delete driver: %s", configuration.port, esp_err_to_name(result));
|
||||
TT_LOG_E(TAG, "[%s] Stopping: Failed to delete driver: %s", configuration.name.c_str(), esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
started = false;
|
||||
|
||||
TT_LOG_I(TAG, "(%d) Stopped", configuration.port);
|
||||
TT_LOG_I(TAG, "[%s] Stopped", configuration.name.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -97,7 +106,8 @@ bool UartEsp::readByte(std::byte* output, TickType_t timeout) {
|
||||
}
|
||||
|
||||
size_t UartEsp::writeBytes(const std::byte* buffer, size_t bufferSize, TickType_t timeout) {
|
||||
if (!mutex.lock(timeout)) {
|
||||
auto lock = mutex.asScopedLock();
|
||||
if (!lock.lock(timeout)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,13 +19,13 @@ bool UartPosix::start() {
|
||||
lock.lock();
|
||||
|
||||
if (device != nullptr) {
|
||||
TT_LOG_E(TAG, "(%s) Starting: Already started", configuration.name.c_str());
|
||||
TT_LOG_E(TAG, "[%s] Starting: Already started", configuration.name.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
auto file = fopen(configuration.name.c_str(), "w");
|
||||
if (file == nullptr) {
|
||||
TT_LOG_E(TAG, "(%s) failed to open", configuration.name.c_str());
|
||||
TT_LOG_E(TAG, "[%s] Open device failed", configuration.name.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -33,18 +33,16 @@ bool UartPosix::start() {
|
||||
|
||||
struct termios tty;
|
||||
if (tcgetattr(fileno(file), &tty) < 0) {
|
||||
printf("(%s) tcgetattr failed: %s\n", configuration.name.c_str(), strerror(errno));
|
||||
printf("[%s] tcgetattr failed: %s\n", configuration.name.c_str(), strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cfsetospeed(&tty, (speed_t)configuration.baudRate) == -1) {
|
||||
TT_LOG_E(TAG, "(%s) failed to set output speed", configuration.name.c_str());
|
||||
return false;
|
||||
TT_LOG_E(TAG, "[%s] Setting output speed failed", configuration.name.c_str());
|
||||
}
|
||||
|
||||
if (cfsetispeed(&tty, (speed_t)configuration.baudRate) == -1) {
|
||||
TT_LOG_E(TAG, "(%s) failed to set input speed", configuration.name.c_str());
|
||||
return false;
|
||||
TT_LOG_E(TAG, "[%s] Setting input speed failed", configuration.name.c_str());
|
||||
}
|
||||
|
||||
tty.c_cflag |= (CLOCAL | CREAD); /* ignore modem controls */
|
||||
@@ -63,13 +61,13 @@ bool UartPosix::start() {
|
||||
tty.c_cc[VTIME] = 1;
|
||||
|
||||
if (tcsetattr(fileno(file), TCSANOW, &tty) != 0) {
|
||||
printf("(%s) tcsetattr failed: %s\n", configuration.name.c_str(), strerror(errno));
|
||||
printf("[%s] tcsetattr failed: %s\n", configuration.name.c_str(), strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
device = std::move(new_device);
|
||||
|
||||
TT_LOG_I(TAG, "(%s) Started", configuration.name.c_str());
|
||||
TT_LOG_I(TAG, "[%s] Started", configuration.name.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -78,13 +76,13 @@ bool UartPosix::stop() {
|
||||
lock.lock();
|
||||
|
||||
if (device == nullptr) {
|
||||
TT_LOG_E(TAG, "(%s) Stopping: Not started", configuration.name.c_str());
|
||||
TT_LOG_E(TAG, "[%s] Stopping: Not started", configuration.name.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
device = nullptr;
|
||||
|
||||
TT_LOG_I(TAG, "(%s) Stopped", configuration.name.c_str());
|
||||
TT_LOG_I(TAG, "[%s] Stopped", configuration.name.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -141,7 +139,7 @@ void UartPosix::flushInput() {
|
||||
uint32_t UartPosix::getBaudRate() {
|
||||
struct termios tty;
|
||||
if (tcgetattr(fileno(device.get()), &tty) < 0) {
|
||||
printf("(%s) tcgetattr failed: %s\n", configuration.name.c_str(), strerror(errno));
|
||||
printf("[%s] tcgetattr failed: %s\n", configuration.name.c_str(), strerror(errno));
|
||||
return false;
|
||||
} else {
|
||||
return (uint32_t)cfgetispeed(&tty);
|
||||
@@ -156,17 +154,17 @@ bool UartPosix::setBaudRate(uint32_t baudRate, TickType_t timeout) {
|
||||
|
||||
struct termios tty;
|
||||
if (tcgetattr(fileno(device.get()), &tty) < 0) {
|
||||
printf("(%s) tcgetattr failed: %s\n", configuration.name.c_str(), strerror(errno));
|
||||
printf("[%s] tcgetattr failed: %s\n", configuration.name.c_str(), strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cfsetospeed(&tty, (speed_t)configuration.baudRate) == -1) {
|
||||
TT_LOG_E(TAG, "(%s) failed to set output speed", configuration.name.c_str());
|
||||
TT_LOG_E(TAG, "[%s] Failed to set output speed", configuration.name.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cfsetispeed(&tty, (speed_t)configuration.baudRate) == -1) {
|
||||
TT_LOG_E(TAG, "(%s) failed to set input speed", configuration.name.c_str());
|
||||
TT_LOG_E(TAG, "[%s] Failed to set input speed", configuration.name.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user