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
+3 -15
View File
@@ -22,23 +22,12 @@ namespace tt {
class Dispatcher {
public:
typedef void (*Function)(std::shared_ptr<void> data);
typedef std::function<void()> Function;
private:
struct DispatcherMessage {
Function function;
std::shared_ptr<void> context; // Can't use unique_ptr with void, so we use shared_ptr
DispatcherMessage(Function function, std::shared_ptr<void> context) :
function(function),
context(std::move(context))
{}
~DispatcherMessage() = default;
};
Mutex mutex;
std::queue<std::shared_ptr<DispatcherMessage>> queue = {};
std::queue<Function> queue = {};
EventFlag eventFlag;
public:
@@ -49,9 +38,8 @@ public:
/**
* Queue a function to be consumed elsewhere.
* @param[in] function the function to execute elsewhere
* @param[in] context the data to pass onto the function
*/
void dispatch(Function function, std::shared_ptr<void> context, TickType_t timeout = portMAX_DELAY);
void dispatch(Function function, TickType_t timeout = portMAX_DELAY);
/**
* Consume 1 or more dispatched function (if any) until the queue is empty.
@@ -11,6 +11,8 @@ class DispatcherThread {
std::unique_ptr<Thread> thread;
bool interruptThread = false;
int32_t threadMain();
public:
explicit DispatcherThread(const std::string& threadName, size_t threadStackSize = 4096);
@@ -19,16 +21,13 @@ public:
/**
* Dispatch a message.
*/
void dispatch(Dispatcher::Function function, std::shared_ptr<void> context, TickType_t timeout = portMAX_DELAY);
void dispatch(Dispatcher::Function function, TickType_t timeout = portMAX_DELAY);
/** Start the thread (blocking). */
void start();
/** Stop the thread (blocking). */
void stop();
/** Internal method */
void _threadMain();
};
}
+26 -6
View File
@@ -1,12 +1,12 @@
#pragma once
#include "CoreDefines.h"
#include <string>
#include <memory>
#include "RtosCompatTask.h"
#include <functional>
#include <memory>
#include <string>
namespace tt {
typedef TaskHandle_t ThreadId;
@@ -38,6 +38,7 @@ public:
* @warning never use osThreadExit in Thread
*/
typedef int32_t (*Callback)(void* context);
typedef std::function<int32_t()> MainFunction;
/** Write to stdout callback
* @param[in] data pointer to data
@@ -57,8 +58,7 @@ private:
TaskHandle_t taskHandle = nullptr;
State state = State::Stopped;
Callback callback = nullptr;
void* callbackContext = nullptr;
MainFunction mainFunction;
int32_t callbackResult = 0;
StateCallback stateCallback = nullptr;
void* stateCallbackContext = nullptr;
@@ -80,6 +80,7 @@ public:
* @param[in] callbackContext
* @param[in] affinity Which CPU core to pin this task to, -1 means unpinned (only works on ESP32)
*/
[[deprecated("Use constructor variant with std::function")]]
Thread(
std::string name,
configSTACK_DEPTH_TYPE stackSize,
@@ -88,6 +89,19 @@ public:
portBASE_TYPE affinity = -1
);
/** Allocate Thread, shortcut version
* @param[in] name the name of the thread
* @param[in] stackSize in bytes
* @param[in] function
* @param[in] affinity Which CPU core to pin this task to, -1 means unpinned (only works on ESP32)
*/
Thread(
std::string name,
configSTACK_DEPTH_TYPE stackSize,
MainFunction function,
portBASE_TYPE affinity = -1
);
~Thread();
/** Set Thread name
@@ -104,8 +118,14 @@ public:
* @param[in] callback ThreadCallback, called upon thread run
* @param[in] callbackContext what to pass to the callback
*/
[[deprecated("use setMainFunction()")]]
void setCallback(Callback callback, _Nullable void* callbackContext = nullptr);
/** Set Thread callback
* @param[in] function called upon thread run
*/
void setMainFunction(MainFunction function);
/** Set Thread priority
* @param[in] priority ThreadPriority value
*/
+5 -4
View File
@@ -2,7 +2,9 @@
#include "RtosCompatTimers.h"
#include "Thread.h"
#include <memory>
#include <functional>
namespace tt {
@@ -10,7 +12,8 @@ class Timer {
public:
typedef void (*Callback)(std::shared_ptr<void> context);
typedef std::function<void()> Callback;
// typedef std::function<void(uint32_t)> PendingCallback;
typedef void (*PendingCallback)(void* context, uint32_t arg);
private:
@@ -22,7 +25,6 @@ private:
};
Callback callback;
std::shared_ptr<void> callbackContext;
std::unique_ptr<std::remove_pointer_t<TimerHandle_t>, TimerHandleDeleter> handle;
static void onCallback(TimerHandle_t hTimer);
@@ -42,9 +44,8 @@ public:
/**
* @param[in] type The timer type
* @param[in] callback The callback function
* @param callbackContext The callback context
*/
Timer(Type type, Callback callback, std::shared_ptr<void> callbackContext = nullptr);
Timer(Type type, Callback callback);
~Timer();
+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());