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:
committed by
GitHub
parent
d72852a6e2
commit
3f1bfee3f5
@@ -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();
|
||||
};
|
||||
|
||||
}
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user