Threads: Timer class
This commit is contained in:
parent
2edda4abf6
commit
5d5d7d3ef8
@ -8,7 +8,7 @@
|
|||||||
#ifndef THREADS_INC_THREADS_SIGNAL_HPP_
|
#ifndef THREADS_INC_THREADS_SIGNAL_HPP_
|
||||||
#define THREADS_INC_THREADS_SIGNAL_HPP_
|
#define THREADS_INC_THREADS_SIGNAL_HPP_
|
||||||
|
|
||||||
#include <FreeRTOSConfig.h>
|
#include <freertos_os2.h>
|
||||||
|
|
||||||
namespace Threads
|
namespace Threads
|
||||||
{
|
{
|
||||||
|
|||||||
54
Threads/Inc/threads_timer.hpp
Normal file
54
Threads/Inc/threads_timer.hpp
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* threads_timer.hpp
|
||||||
|
*
|
||||||
|
* Created on: Jun 23, 2021
|
||||||
|
* Author: erki
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef SKULLC_THREADS_TIMER_HPP_
|
||||||
|
#define SKULLC_THREADS_TIMER_HPP_
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
|
#include "threads_signal.hpp"
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
|
struct tmrTimerControl;
|
||||||
|
typedef struct tmrTimerControl* TimerHandle_t;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace Threads
|
||||||
|
{
|
||||||
|
|
||||||
|
struct TimeoutSignal
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
class Timer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Timer(Signallable<TimeoutSignal>* signal, const char* name, const std::int32_t period_ms, const bool auto_reload = true);
|
||||||
|
~Timer();
|
||||||
|
|
||||||
|
void setAutoReload(const bool auto_reload);
|
||||||
|
void setPeriod(const std::int32_t period_ms);
|
||||||
|
BaseType_t setPeriodFromIsr(const std::int32_t period_ms);
|
||||||
|
|
||||||
|
bool start();
|
||||||
|
bool stop();
|
||||||
|
std::pair<bool, BaseType_t> startFromIsr();
|
||||||
|
std::pair<bool, BaseType_t> stopFromIsr();
|
||||||
|
|
||||||
|
private:
|
||||||
|
TimerHandle_t timer_;
|
||||||
|
Signallable<TimeoutSignal>* signal_;
|
||||||
|
|
||||||
|
static void timeoutHandler_(TimerHandle_t timer);
|
||||||
|
};
|
||||||
|
|
||||||
|
}// namespace Threads
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* SKULLC_THREADS_TIMER_HPP_ */
|
||||||
88
Threads/Src/threads_timer.cpp
Normal file
88
Threads/Src/threads_timer.cpp
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
* threads_timer.cpp
|
||||||
|
*
|
||||||
|
* Created on: Jun 23, 2021
|
||||||
|
* Author: erki
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "threads_timer.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <array>
|
||||||
|
|
||||||
|
#include <freertos_os2.h>
|
||||||
|
#include <timers.h>
|
||||||
|
|
||||||
|
namespace Threads
|
||||||
|
{
|
||||||
|
|
||||||
|
Timer::Timer(Signallable<TimeoutSignal>* signal, const char* name, const std::int32_t period_ms, const bool auto_reload)
|
||||||
|
: timer_(xTimerCreate(name, pdMS_TO_TICKS(period_ms), auto_reload, this, &Timer::timeoutHandler_)), signal_(signal)
|
||||||
|
{
|
||||||
|
assert(timer_);
|
||||||
|
}
|
||||||
|
|
||||||
|
Timer::~Timer()
|
||||||
|
{
|
||||||
|
xTimerDelete(timer_, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Timer::setAutoReload(const bool auto_reload)
|
||||||
|
{
|
||||||
|
vTimerSetReloadMode(timer_, auto_reload ? pdTRUE : pdFALSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Timer::setPeriod(const std::int32_t period_ms)
|
||||||
|
{
|
||||||
|
xTimerChangePeriod(timer_, pdMS_TO_TICKS(period_ms), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
BaseType_t Timer::setPeriodFromIsr(const std::int32_t period_ms)
|
||||||
|
{
|
||||||
|
BaseType_t was_awoken = false;
|
||||||
|
xTimerChangePeriodFromISR(timer_, pdMS_TO_TICKS(period_ms), &was_awoken);
|
||||||
|
|
||||||
|
return was_awoken;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Timer::start()
|
||||||
|
{
|
||||||
|
const BaseType_t success = xTimerStart(timer_, 0);
|
||||||
|
|
||||||
|
return success != pdFAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Timer::stop()
|
||||||
|
{
|
||||||
|
const BaseType_t success = xTimerStop(timer_, 0);
|
||||||
|
|
||||||
|
return success != pdFAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::pair<bool, BaseType_t> Timer::startFromIsr()
|
||||||
|
{
|
||||||
|
BaseType_t was_awoken = false;
|
||||||
|
const BaseType_t success = xTimerStartFromISR(timer_, &was_awoken);
|
||||||
|
|
||||||
|
return {success != pdFAIL, was_awoken};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::pair<bool, BaseType_t> Timer::stopFromIsr()
|
||||||
|
{
|
||||||
|
BaseType_t was_awoken = false;
|
||||||
|
const BaseType_t success = xTimerStopFromISR(timer_, &was_awoken);
|
||||||
|
|
||||||
|
return {success != pdFAIL, was_awoken};
|
||||||
|
}
|
||||||
|
|
||||||
|
void Timer::timeoutHandler_(TimerHandle_t timer)
|
||||||
|
{
|
||||||
|
void* source = pvTimerGetTimerID(timer);
|
||||||
|
|
||||||
|
if (!source)
|
||||||
|
return;
|
||||||
|
|
||||||
|
static_cast<Timer*>(source)->signal_->emit(TimeoutSignal{});
|
||||||
|
}
|
||||||
|
|
||||||
|
}// namespace Threads
|
||||||
Loading…
x
Reference in New Issue
Block a user