From 5d5d7d3ef8725d6b752a3c13a28e322bd9bbfbb7 Mon Sep 17 00:00:00 2001 From: Erki Date: Thu, 24 Jun 2021 13:15:10 +0300 Subject: [PATCH] Threads: Timer class --- Threads/Inc/threads_signal.hpp | 2 +- Threads/Inc/threads_timer.hpp | 54 +++++++++++++++++++++ Threads/Src/threads_timer.cpp | 88 ++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 Threads/Inc/threads_timer.hpp create mode 100644 Threads/Src/threads_timer.cpp diff --git a/Threads/Inc/threads_signal.hpp b/Threads/Inc/threads_signal.hpp index d6dd057..b63033f 100644 --- a/Threads/Inc/threads_signal.hpp +++ b/Threads/Inc/threads_signal.hpp @@ -8,7 +8,7 @@ #ifndef THREADS_INC_THREADS_SIGNAL_HPP_ #define THREADS_INC_THREADS_SIGNAL_HPP_ -#include +#include namespace Threads { diff --git a/Threads/Inc/threads_timer.hpp b/Threads/Inc/threads_timer.hpp new file mode 100644 index 0000000..015d650 --- /dev/null +++ b/Threads/Inc/threads_timer.hpp @@ -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 +#include + +#include "threads_signal.hpp" + +extern "C" { + +struct tmrTimerControl; +typedef struct tmrTimerControl* TimerHandle_t; +} + +namespace Threads +{ + +struct TimeoutSignal +{ +}; + +class Timer +{ +public: + Timer(Signallable* 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 startFromIsr(); + std::pair stopFromIsr(); + +private: + TimerHandle_t timer_; + Signallable* signal_; + + static void timeoutHandler_(TimerHandle_t timer); +}; + +}// namespace Threads + + +#endif /* SKULLC_THREADS_TIMER_HPP_ */ diff --git a/Threads/Src/threads_timer.cpp b/Threads/Src/threads_timer.cpp new file mode 100644 index 0000000..0007450 --- /dev/null +++ b/Threads/Src/threads_timer.cpp @@ -0,0 +1,88 @@ +/* + * threads_timer.cpp + * + * Created on: Jun 23, 2021 + * Author: erki + */ + +#include "threads_timer.hpp" + +#include +#include + +#include +#include + +namespace Threads +{ + +Timer::Timer(Signallable* 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 Timer::startFromIsr() +{ + BaseType_t was_awoken = false; + const BaseType_t success = xTimerStartFromISR(timer_, &was_awoken); + + return {success != pdFAIL, was_awoken}; +} + +std::pair 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(source)->signal_->emit(TimeoutSignal{}); +} + +}// namespace Threads