Compare commits
3 Commits
6bbdf4cb71
...
7638f37db7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7638f37db7 | ||
|
|
5d5d7d3ef8 | ||
|
|
2edda4abf6 |
@ -17,6 +17,7 @@
|
||||
|
||||
#include "threads_primitivethread.hpp"
|
||||
#include "threads_signal.hpp"
|
||||
#include "threads_timer.hpp"
|
||||
|
||||
namespace Threads
|
||||
{
|
||||
@ -36,14 +37,53 @@ public:
|
||||
(*dd)();
|
||||
},
|
||||
this, name, priority, stack_size),
|
||||
msg_queue_(xQueueCreate(queue_size, sizeof(value_type))), signal_(*this)
|
||||
msg_queue_(xQueueCreate(queue_size, sizeof(value_type))), signal_(this)
|
||||
{
|
||||
assert(msg_queue_);
|
||||
}
|
||||
|
||||
virtual ~Actor() {}
|
||||
|
||||
Signallable<value_type>* getSignal()
|
||||
template<typename T>
|
||||
struct Signal : Signallable<T>
|
||||
{
|
||||
using parent = Actor<CRTP, Ts...>;
|
||||
|
||||
static_assert(std::is_convertible_v<T, parent::value_type>, "value_type cannot be constructed from T.");
|
||||
|
||||
explicit Signal(parent* q)
|
||||
: q_(q)
|
||||
{}
|
||||
|
||||
bool emit(const T& data) override
|
||||
{
|
||||
parent::value_type to_send = data;
|
||||
const BaseType_t success = xQueueSend(q_->msg_queue_, &to_send, 0);
|
||||
return success == pdTRUE;
|
||||
}
|
||||
|
||||
std::pair<bool, BaseType_t> emitFromIsr(const T& data) override
|
||||
{
|
||||
parent::value_type to_send = data;
|
||||
BaseType_t was_awoken = pdFALSE;
|
||||
const BaseType_t success = xQueueSendFromISR(q_->msg_queue_, &to_send, &was_awoken);
|
||||
|
||||
return {success == pdTRUE, was_awoken};
|
||||
}
|
||||
|
||||
private:
|
||||
parent* q_;
|
||||
};
|
||||
|
||||
template<typename U>
|
||||
Signal<U> getSignal()
|
||||
{
|
||||
static_assert(std::is_convertible_v<U, value_type>, "value_type cannot be constructed from U.");
|
||||
|
||||
return Signal<U>(this);
|
||||
}
|
||||
|
||||
Signallable<value_type>* getStaticSignal()
|
||||
{
|
||||
return &signal_;
|
||||
}
|
||||
@ -89,33 +129,7 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
friend struct Signal_;
|
||||
|
||||
struct Signal_ : Signallable<value_type>
|
||||
{
|
||||
using parent = Actor<CRTP, Ts...>;
|
||||
parent& q;
|
||||
|
||||
explicit Signal_(parent& q)
|
||||
: q(q)
|
||||
{}
|
||||
|
||||
bool emit(const value_type& data) override
|
||||
{
|
||||
const BaseType_t success = xQueueSend(q.msg_queue_, &data, 0);
|
||||
return success == pdTRUE;
|
||||
}
|
||||
|
||||
std::pair<bool, BaseType_t> emitFromIsr(const value_type& data) override
|
||||
{
|
||||
BaseType_t was_awoken = pdFALSE;
|
||||
const BaseType_t success = xQueueSendFromISR(q.msg_queue_, &data, &was_awoken);
|
||||
|
||||
return {success == pdTRUE, was_awoken};
|
||||
}
|
||||
};
|
||||
|
||||
Signal_ signal_;
|
||||
Signal<value_type> signal_;
|
||||
};
|
||||
|
||||
template<typename CRTP>
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
#ifndef THREADS_INC_THREADS_SIGNAL_HPP_
|
||||
#define THREADS_INC_THREADS_SIGNAL_HPP_
|
||||
|
||||
#include <FreeRTOSConfig.h>
|
||||
#include <freertos_os2.h>
|
||||
|
||||
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
|
||||
@ -27,14 +27,24 @@ struct StaticPointer
|
||||
return *(new (storage) value_type(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
value_type& operator*() const
|
||||
const value_type& operator*() const
|
||||
{
|
||||
return reinterpret_cast<T*>(storage);
|
||||
return *reinterpret_cast<T*>(storage);
|
||||
}
|
||||
|
||||
value_type* operator->() const noexcept
|
||||
value_type& operator*()
|
||||
{
|
||||
return reinterpret_cast<T*>(storage);
|
||||
return *reinterpret_cast<T*>(storage);
|
||||
}
|
||||
|
||||
value_type* operator->() noexcept
|
||||
{
|
||||
return reinterpret_cast<value_type*>(storage);
|
||||
}
|
||||
|
||||
const value_type* operator->() const noexcept
|
||||
{
|
||||
return reinterpret_cast<value_type*>(storage);
|
||||
}
|
||||
|
||||
constexpr explicit operator bool() const
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user