Prototype for tick events

This commit is contained in:
dario 2025-02-16 00:02:20 +01:00
parent aa21c44270
commit 1479e75365
2 changed files with 138 additions and 60 deletions

View File

@ -33,48 +33,64 @@ namespace sta
{
namespace tacos
{
/**
* @brief Initialize system events.
*/
void initSystemEvents();
namespace events
{
enum class Types : uint32_t
{
STARTUP = 0x01,
TICK_100Hz = 0x02,
TOCK_100Hz = 0x03,
TICK_50Hz = 0x04,
TOCK_50Hz = 0x05,
TICK_20Hz = 0x06,
TOCK_20Hz = 0x07,
TICK_10Hz = 0x08,
TOCK_10Hz = 0x09,
TICK_1Hz = 0x0A,
TOCK_1Hz = 0x0B
};
/**
* @brief Initialize system events.
*/
void init();
/**
* @brief Signal system events.
*
* @param flags System event flags
*
* @ingroup STA_RTOS_SysEvent
*/
void signalSystemEvents(uint32_t flags);
/**
* @brief Signal system events.
*
* @param flags System event flags
*
* @ingroup STA_RTOS_SysEvent
*/
void signal(uint32_t flags);
/**
* @brief Wait for system events.
*
* @param flags System event flags
* @param options osFlagsWaitAll or osFlagsWaitAny (osFlagsNoClear always set)
* @param timeout Wait timeout (0 = instant, osWaitForever = infinite)
*
* @ingroup STA_RTOS_SysEvent
*/
void waitForSystemEvents(uint32_t flags, uint32_t options, uint32_t timeout);
/**
* @brief Wait for system events.
*
* @param flags System event flags
* @param options osFlagsWaitAll or osFlagsWaitAny (osFlagsNoClear always set)
* @param timeout Wait timeout (0 = instant, osWaitForever = infinite)
*
* @ingroup STA_RTOS_SysEvent
*/
void wait(uint32_t flags, uint32_t options, uint32_t timeout);
/**
* @brief Signal startup system event.
*
* @ingroup STA_RTOS_SysEvent
*/
void signalStartup();
/**
* @brief Signal startup system event.
*
* @ingroup STA_RTOS_SysEvent
*/
void signalStartupEvent();
/**
* @brief Wait for startup system event.
*
* Blocking while waiting
*
* @ingroup STA_RTOS_SysEvent
*/
void waitForStartupEvent();
/**
* @brief Wait for startup system event.
*
* Blocking while waiting
*
* @ingroup STA_RTOS_SysEvent
*/
void waitForStartup();
} // namespace events
} // namespace tacos
} // namespace sta

View File

@ -1,5 +1,6 @@
#include <sta/tacos/system/events.hpp>
#include <sta/rtos/event.hpp>
#include <sta/rtos/timer.hpp>
#include <sta/debug/assert.hpp>
@ -11,6 +12,11 @@ namespace
{
// Event handle
sta::RtosEvent * systemEvents = nullptr;
uint8_t tickCounter = 0;
// Tick timer handle
sta::RtosTimer * tickTimer = nullptr;
}
@ -18,34 +24,90 @@ namespace sta
{
namespace tacos
{
void initSystemEvents()
namespace events
{
if (systemEvents == nullptr)
void init()
{
systemEvents = new sta::RtosEvent();
if (systemEvents == nullptr)
{
systemEvents = new sta::RtosEvent();
}
if (tickTimer == nullptr)
{
tickTimer = new sta::RtosTimer([]() {
uint32_t flags = 0x00;
if (tickCounter % 2 == 0)
{
flags |= Types::TICK_100Hz;
if ((tickCounter / 2) % 2 == 0) {
flags |= Types::TICK_50Hz;
}
else
{
flags |= Types::TOCK_50Hz;
}
if (tickCounter % 10 == 0 && ((tickCounter / 10) % 2) == 0)
{
flags |= Types::TICK_10Hz;
}
else if (tickCounter % 10 == 0 && (tickCounter / 10) == 1) {
flags |= Types::TOCK_10Hz;
}
if (tickCounter % 100 == 0 && (tickCounter / 100) % 2 == 0) {
flags |= Types::TICK_1Hz;
}
else if (tickCounter % 100 == 0 && (tickCounter / 100) == 1) {
flags |= Types::TOCK_1Hz;
}
}
else
{
flags |= Types::TOCK_100Hz;
if (tickCounter % 5 == 0 && ((tickCounter / 5) % 2) == 0)
{
flags |= Types::TICK_20Hz;
}
else if (tickCounter % 5 == 0 && (tickCounter / 5) == 1) {
flags |= Types::TOCK_20Hz;
}
}
tickCounter = ++tickCounter % 200;
signal(flags);
}, true);
tickTimer->start(10);
}
}
}
void signalSystemEvents(uint32_t flags)
{
STA_ASSERT_MSG(systemEvents != nullptr, "System events not initialized");
systemEvents->set(flags);
}
void signal(uint32_t flags)
{
STA_ASSERT_MSG(systemEvents != nullptr, "System events not initialized");
systemEvents->set(flags);
}
void waitForSystemEvents(uint32_t flags, uint32_t options, uint32_t timeout)
{
STA_ASSERT_MSG(systemEvents != nullptr, "System events not initialized");
systemEvents->peek(flags, timeout);
}
void signalStartupEvent()
{
signalSystemEvents(STA_TACOS_SYSTEM_EVENTS_STARTUP);
}
void wait(uint32_t flags, uint32_t options, uint32_t timeout)
{
STA_ASSERT_MSG(systemEvents != nullptr, "System events not initialized");
systemEvents->peek(flags, timeout);
}
void waitForStartupEvent()
{
waitForSystemEvents(STA_TACOS_SYSTEM_EVENTS_STARTUP, osFlagsWaitAll, osWaitForever);
}
void signalStartup()
{
signalSystemEvents(STA_TACOS_SYSTEM_EVENTS_STARTUP);
}
void waitForStartup()
{
waitForSystemEvents(STA_TACOS_SYSTEM_EVENTS_STARTUP, osFlagsWaitAll, osWaitForever);
}
} // namespace events
} // namespace tacos
} // namespace sta