Various improvements (#264)

- Replace C function pointers with C++ `std::function` in `Thread`, `Timer` and `DispatcherThread`
- Rename `SystemEvent`-related functions
- WiFi: fix auto-connect when WiFi disconnects from bad signal
- WiFi: fix auto-connect when WiFi fails to auto-connect
- WiFi: implement disconnect() when tapping connected WiFi ap in WiFi management app
This commit is contained in:
Ken Van Hoeylandt
2025-03-30 21:59:31 +02:00
committed by GitHub
parent d72852a6e2
commit 3f1bfee3f5
37 changed files with 239 additions and 322 deletions
+4 -5
View File
@@ -15,11 +15,10 @@ Dispatcher::~Dispatcher() {
mutex.unlock();
}
void Dispatcher::dispatch(Function function, std::shared_ptr<void> context, TickType_t timeout) {
auto message = std::make_shared<DispatcherMessage>(function, std::move(context));
void Dispatcher::dispatch(Function function, TickType_t timeout) {
// Mutate
if (mutex.lock(timeout)) {
queue.push(std::move(message));
queue.push(std::move(function));
if (queue.size() == BACKPRESSURE_WARNING_COUNT) {
TT_LOG_W(TAG, "Backpressure: You're not consuming fast enough (100 queued)");
}
@@ -46,13 +45,13 @@ uint32_t Dispatcher::consume(TickType_t timeout) {
do {
if (mutex.lock(10)) {
if (!queue.empty()) {
auto item = queue.front();
auto function = queue.front();
queue.pop();
consumed++;
processing = !queue.empty();
// Don't keep lock as callback might be slow
mutex.unlock();
item->function(item->context);
function();
} else {
processing = false;
mutex.unlock();
+8 -11
View File
@@ -2,18 +2,13 @@
namespace tt {
int32_t dispatcherThreadMain(void* context) {
auto* dispatcherThread = (DispatcherThread*)context;
dispatcherThread->_threadMain();
return 0;
}
DispatcherThread::DispatcherThread(const std::string& threadName, size_t threadStackSize) {
thread = std::make_unique<Thread>(
threadName,
threadStackSize,
dispatcherThreadMain,
this
[this]() {
return threadMain();
}
);
}
@@ -23,7 +18,7 @@ DispatcherThread::~DispatcherThread() {
}
}
void DispatcherThread::_threadMain() {
int32_t DispatcherThread::threadMain() {
do {
/**
* If this value is too high (e.g. 1 second) then the dispatcher destroys too slowly when the simulator exits.
@@ -31,10 +26,12 @@ void DispatcherThread::_threadMain() {
*/
dispatcher.consume(100 / portTICK_PERIOD_MS);
} while (!interruptThread);
return 0;
}
void DispatcherThread::dispatch(Dispatcher::Function function, std::shared_ptr<void> context, TickType_t timeout) {
dispatcher.dispatch(function, std::move(context), timeout);
void DispatcherThread::dispatch(Dispatcher::Function function, TickType_t timeout) {
dispatcher.dispatch(function, timeout);
}
void DispatcherThread::start() {
+25 -7
View File
@@ -53,7 +53,7 @@ void Thread::mainBody(void* context) {
TT_LOG_I(TAG, "Starting %s", thread->name.c_str());
assert(thread->state == Thread::State::Starting);
thread->setState(Thread::State::Running);
thread->callbackResult = thread->callback(thread->callbackContext);
thread->callbackResult = thread->mainFunction();
assert(thread->state == Thread::State::Running);
thread->setState(Thread::State::Stopped);
@@ -73,8 +73,21 @@ Thread::Thread(
_Nullable void* callbackContext,
portBASE_TYPE affinity
) :
callback(callback),
callbackContext(callbackContext),
mainFunction([callback, callbackContext]() {
return callback(callbackContext);
}),
name(std::move(name)),
stackSize(stackSize),
affinity(affinity)
{}
Thread::Thread(
std::string name,
configSTACK_DEPTH_TYPE stackSize,
MainFunction function,
portBASE_TYPE affinity
) :
mainFunction(function),
name(std::move(name)),
stackSize(stackSize),
affinity(affinity)
@@ -97,12 +110,17 @@ void Thread::setStackSize(size_t newStackSize) {
stackSize = newStackSize;
}
void Thread::setCallback(Callback newCallback, _Nullable void* newCallbackContext) {
void Thread::setCallback(Callback callback, _Nullable void* callbackContext) {
assert(state == State::Stopped);
callback = newCallback;
callbackContext = newCallbackContext;
mainFunction = [callback, callbackContext]() {
return callback(callbackContext);
};
}
void Thread::setMainFunction(MainFunction function) {
assert(state == State::Stopped);
mainFunction = function;
}
void Thread::setPriority(Priority newPriority) {
assert(state == State::Stopped);
@@ -121,7 +139,7 @@ Thread::State Thread::getState() const {
}
void Thread::start() {
assert(callback);
assert(mainFunction);
assert(state == State::Stopped);
assert(stackSize > 0U && stackSize < (UINT16_MAX * sizeof(StackType_t)));
+2 -6
View File
@@ -4,15 +4,12 @@
#include "Tactility/RtosCompat.h"
#include "Tactility/kernel/Kernel.h"
#include <utility>
namespace tt {
void Timer::onCallback(TimerHandle_t hTimer) {
auto* timer = static_cast<Timer*>(pvTimerGetTimerID(hTimer));
if (timer != nullptr) {
timer->callback(timer->callbackContext);
timer->callback();
}
}
@@ -30,9 +27,8 @@ static inline TimerHandle_t createTimer(Timer::Type type, void* timerId, TimerCa
return xTimerCreate(nullptr, portMAX_DELAY, (BaseType_t)reload, timerId, callback);
}
Timer::Timer(Type type, Callback callback, std::shared_ptr<void> callbackContext) :
Timer::Timer(Type type, Callback callback) :
callback(callback),
callbackContext(std::move(callbackContext)),
handle(createTimer(type, this, onCallback))
{
assert(!kernel::isIsr());