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
@@ -1,7 +1,7 @@
#pragma once
#include <concepts>
#include <tt_app.h>
#include <type_traits>
class App {
public:
@@ -0,0 +1,51 @@
#pragma once
#include <tt_lock.h>
class ScopedLock;
/** Represents a lock/mutex */
class Lock {
public:
virtual ~Lock() = default;
virtual bool lock(TickType timeout) const = 0;
bool lock() const { return lock(TT_MAX_TICKS); }
virtual bool unlock() const = 0;
ScopedLock asScopedLock() const;
};
/**
* Represents a lockable instance that is scoped to a specific lifecycle.
* Once the ScopedLock is destroyed, unlock() is called automatically.
*
* In other words:
* You have to lock() this object manually, but unlock() happens automatically on destruction.
*/
class ScopedLock final : public Lock {
const Lock& lockable;
public:
using Lock::lock;
explicit ScopedLock(const Lock& lockable) : lockable(lockable) {}
~ScopedLock() override {
lockable.unlock(); // We don't care whether it succeeded or not
}
bool lock(TickType timeout) const override {
return lockable.lock(timeout);
}
bool unlock() const override {
return lockable.unlock();
}
};
@@ -0,0 +1,22 @@
#pragma once
#include <TactilityCpp/Lock.h>
#include <tt_lvgl.h>
class LvglLock final : public Lock {
public:
bool lock(TickType timeout = TT_MAX_TICKS) const override {
tt_lvgl_lock(timeout);
return true;
}
bool unlock() const override {
tt_lvgl_unlock();
return true;
}
};
@@ -0,0 +1,27 @@
#pragma once
#include <tt_lock.h>
#include <TactilityCpp/Lock.h>
class Mutex final : public Lock {
LockHandle handle;
public:
explicit Mutex(TtMutexType type) : handle(tt_lock_alloc_mutex(type)) {};
~Mutex() override {
tt_lock_free(handle);
}
bool lock(TickType timeout = TT_MAX_TICKS) const override {
return tt_lock_acquire(handle, timeout);
}
bool unlock() const override {
return tt_lock_release(handle);
}
};
@@ -0,0 +1,98 @@
#pragma once
#include <tt_preferences.h>
#include <cstdint>
#include <cstring>
#include <memory>
#include <string>
class Preferences {
PreferencesHandle handle = nullptr;
public:
// Open preferences with the given identifier/namespace
explicit Preferences(const char* identifier) : handle(tt_preferences_alloc(identifier)) {}
// Non-copyable (owns a handle)
Preferences(const Preferences&) = delete;
Preferences& operator=(const Preferences&) = delete;
// Movable
Preferences(Preferences&& other) noexcept : handle(other.handle) {
other.handle = nullptr;
}
Preferences& operator=(Preferences&& other) noexcept {
if (this != &other) {
if (handle) {
tt_preferences_free(handle);
}
handle = other.handle;
other.handle = nullptr;
}
return *this;
}
~Preferences() {
if (handle) {
tt_preferences_free(handle);
}
}
bool optBool(const char* key, bool& out) const {
return tt_preferences_opt_bool(handle, key, &out);
}
bool getBool(const char* key, bool defaultValue = false) const {
bool value = defaultValue;
(void) tt_preferences_opt_bool(handle, key, &value);
return value;
}
void putBool(const char* key, bool value) const {
tt_preferences_put_bool(handle, key, value);
}
bool optInt32(const char* key, int32_t& out) const {
return tt_preferences_opt_int32(handle, key, &out);
}
int32_t getInt32(const char* key, int32_t defaultValue = 0) const {
int32_t value = defaultValue;
(void) tt_preferences_opt_int32(handle, key, &value);
return value;
}
void putInt32(const char* key, int32_t value) const {
tt_preferences_put_int32(handle, key, value);
}
bool optString(const char* key, char* out, uint32_t outSize) const {
return tt_preferences_opt_string(handle, key, out, outSize);
}
template <size_t N>
bool optString(const char* key, char (&out)[N]) const {
return tt_preferences_opt_string(handle, key, out, static_cast<uint32_t>(N));
}
std::string getString(const char* key, size_t maxSize, const std::string& defaultValue = std::string()) const {
if (maxSize == 0) return defaultValue;
std::string buf;
buf.resize(maxSize);
if (tt_preferences_opt_string(handle, key, buf.data(), static_cast<uint32_t>(maxSize))) {
buf.resize(std::strlen(buf.c_str()));
return buf;
}
return defaultValue;
}
void putString(const char* key, const char* value) const {
tt_preferences_put_string(handle, key, value);
}
void putString(const char* key, const std::string& value) const {
tt_preferences_put_string(handle, key, value.c_str());
}
};
@@ -0,0 +1,65 @@
#pragma once
#include <tt_thread.h>
class Thread {
ThreadHandle handle;
public:
Thread() : handle(tt_thread_alloc()) {}
Thread(
const char* name,
uint32_t stackSize,
ThreadCallback callback,
void* _Nullable callbackContext
) : handle(tt_thread_alloc_ext(name, stackSize, callback, callbackContext)) {}
~Thread() {
tt_thread_free(handle);
}
void start() const {
tt_thread_start(handle);
}
bool join(TickType timeout = TT_MAX_TICKS) const {
return tt_thread_join(handle, timeout);
}
void setName(const char* name) const {
tt_thread_set_name(handle, name);
}
void setStackSize(size_t stackSize) const {
tt_thread_set_stack_size(handle, stackSize);
}
void setAffinity(int affinity) const {
tt_thread_set_affinity(handle, affinity);
}
void setCallback(ThreadCallback callback, void* _Nullable callbackContext) const {
tt_thread_set_callback(handle, callback, callbackContext);
}
void setPriority(ThreadPriority priority) const {
tt_thread_set_priority(handle, priority);
}
void setStateCallback(ThreadStateCallback callback, void* _Nullable callbackContext) const {
tt_thread_set_state_callback(handle, callback, callbackContext);
}
ThreadState getState() const {
return tt_thread_get_state(handle);
}
ThreadId getId() const {
return tt_thread_get_id(handle);
}
int32_t getReturnCode() const {
return tt_thread_get_return_code(handle);
}
};
@@ -0,0 +1,74 @@
#pragma once
#include <tt_hal_uart.h>
#include <Str.h>
#include <vector>
class Uart {
UartHandle handle;
public:
explicit Uart(UartHandle handle) : handle(handle) {}
~Uart() {
tt_hal_uart_free(handle);
}
static std::unique_ptr<Uart> open(size_t index) {
auto handle = tt_hal_uart_alloc(index);
return std::make_unique<Uart>(handle);
}
static std::vector<Str> getNames() {
std::vector<Str> names;
size_t count = tt_hal_uart_get_count();
for (size_t i = 0; i < count; i++) {
char buffer[64];
if (tt_hal_uart_get_name(i, buffer, sizeof(buffer))) {
names.push_back(Str(buffer));
}
}
return names;
}
bool start() const {
return tt_hal_uart_start(handle);
}
bool isStarted() const {
return tt_hal_uart_is_started(handle);
}
bool stop() const {
return tt_hal_uart_stop(handle);
}
size_t readBytes(char* buffer, size_t bufferSize, TickType timeout) const {
return tt_hal_uart_read_bytes(handle, buffer, bufferSize, timeout);
}
bool readByte(char* output, TickType timeout) const {
return tt_hal_uart_read_bytes(handle, output, 1, timeout);
}
size_t writeBytes(const char* buffer, size_t bufferSize, TickType timeout) const {
return tt_hal_uart_write_bytes(handle, buffer, bufferSize, timeout);
}
size_t available() const {
return tt_hal_uart_available(handle);
}
bool setBaudRate(size_t baud_rate) const {
return tt_hal_uart_set_baud_rate(handle, baud_rate);
}
uint32_t getBaudRate() const {
return tt_hal_uart_get_baud_rate(handle);
}
void flushInput() const {
tt_hal_uart_flush_input(handle);
}
};