Ken Van Hoeylandt 686f7cce83
TactilityCore improvements (#187)
FreeRTOS handles were stored plainly and they were deleted in the destructor of classes.
This meant that if a class were to be copied, the destructor would be called twice on the same handles and lead to double-free.

Seha on Discord suggested to fix this by using `std::unique_ptr` with a custom deletion function.

The changes affect:
- Thread
- Semaphore
- Mutex
- StreamBuffer
- Timer
- MessageQueue
- EventFlag

Thread  changes:
- Removal of the hack with the `Data` struct
- Thread's main body is now just a private static function inside the class.
- The C functions were relocated to static class members

PubSub changes:
- Refactored pubsub into class
- Renamed files to `PubSub` instead of `Pubsub`
- `PubSubSubscription` is now a private inner struct and `PubSub` only exposes `SubscriptionHandle`

Lockable, ScopedLockable, Mutex:
- Added `lock()` method that locks indefinitely
- Remove deprecated `acquire()` and `release()` methods
- Removed `TtWaitForever` in favour of `portMAX_DELAY`
2025-01-25 17:29:11 +01:00

99 lines
2.8 KiB
C++

#pragma once
#include "RtosCompatTimers.h"
#include "Thread.h"
#include <memory>
namespace tt {
class Timer {
public:
typedef void (*Callback)(std::shared_ptr<void> context);
typedef void (*PendingCallback)(void* context, uint32_t arg);
private:
struct TimerHandleDeleter {
void operator()(TimerHandle_t handleToDelete) {
xTimerDelete(handleToDelete, portMAX_DELAY);
}
};
Callback callback;
std::shared_ptr<void> callbackContext;
std::unique_ptr<std::remove_pointer_t<TimerHandle_t>, TimerHandleDeleter> handle;
static void onCallback(TimerHandle_t hTimer);
public:
enum class Type {
Once = 0, ///< One-shot timer.
Periodic = 1 ///< Repeating timer.
};
enum class Priority{
Normal, /**< Lower then other threads */
Elevated, /**< Same as other threads */
};
/**
* @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();
/** Start timer
* @warning This is asynchronous call, real operation will happen as soon as timer service process this request.
* @param[in] interval The interval in ticks
* @return success result
*/
bool start(TickType_t interval);
/** Restart timer with previous timeout value
* @warning This is asynchronous call, real operation will happen as soon as timer service process this request.
* @param[in] interval The interval in ticks
* @return success result
*/
bool restart(TickType_t interval);
/** Stop timer
* @warning This is asynchronous call, real operation will happen as soon as timer service process this request.
* @return success result
*/
bool stop();
/** Is timer running
* @warning This cal may and will return obsolete timer state if timer commands are still in the queue. Please read FreeRTOS timer documentation first.
* @return true when running
*/
bool isRunning();
/** Get timer expire time
* @return expire tick
*/
TickType_t getExpireTime();
/**
* Calls xTimerPendFunctionCall internally.
* @param[in] callback the function to call
* @param[in] callbackContext the first function argument
* @param[in] callbackArg the second function argument
* @param[in] timeout the function timeout (must set to 0 in ISR mode)
* @return true on success
*/
bool setPendingCallback(PendingCallback callback, void* callbackContext, uint32_t callbackArg, TickType_t timeout);
/** Set Timer thread priority
* @param[in] priority The priority
*/
void setThreadPriority(Thread::Priority priority);
};
} // namespace