Prototype for tick events

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

View File

@ -33,48 +33,64 @@ namespace sta
{ {
namespace tacos namespace tacos
{ {
/** namespace events
* @brief Initialize system events. {
*/ enum class Types : uint32_t
void initSystemEvents(); {
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. * @brief Signal system events.
* *
* @param flags System event flags * @param flags System event flags
* *
* @ingroup STA_RTOS_SysEvent * @ingroup STA_RTOS_SysEvent
*/ */
void signalSystemEvents(uint32_t flags); void signal(uint32_t flags);
/** /**
* @brief Wait for system events. * @brief Wait for system events.
* *
* @param flags System event flags * @param flags System event flags
* @param options osFlagsWaitAll or osFlagsWaitAny (osFlagsNoClear always set) * @param options osFlagsWaitAll or osFlagsWaitAny (osFlagsNoClear always set)
* @param timeout Wait timeout (0 = instant, osWaitForever = infinite) * @param timeout Wait timeout (0 = instant, osWaitForever = infinite)
* *
* @ingroup STA_RTOS_SysEvent * @ingroup STA_RTOS_SysEvent
*/ */
void waitForSystemEvents(uint32_t flags, uint32_t options, uint32_t timeout); 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. * @brief Wait for startup system event.
* *
* @ingroup STA_RTOS_SysEvent * Blocking while waiting
*/ *
void signalStartupEvent(); * @ingroup STA_RTOS_SysEvent
*/
/** void waitForStartup();
* @brief Wait for startup system event. } // namespace events
*
* Blocking while waiting
*
* @ingroup STA_RTOS_SysEvent
*/
void waitForStartupEvent();
} // namespace tacos } // namespace tacos
} // namespace sta } // namespace sta

View File

@ -1,5 +1,6 @@
#include <sta/tacos/system/events.hpp> #include <sta/tacos/system/events.hpp>
#include <sta/rtos/event.hpp> #include <sta/rtos/event.hpp>
#include <sta/rtos/timer.hpp>
#include <sta/debug/assert.hpp> #include <sta/debug/assert.hpp>
@ -11,6 +12,11 @@ namespace
{ {
// Event handle // Event handle
sta::RtosEvent * systemEvents = nullptr; sta::RtosEvent * systemEvents = nullptr;
uint8_t tickCounter = 0;
// Tick timer handle
sta::RtosTimer * tickTimer = nullptr;
} }
@ -18,34 +24,90 @@ namespace sta
{ {
namespace tacos 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) void signal(uint32_t flags)
{ {
STA_ASSERT_MSG(systemEvents != nullptr, "System events not initialized"); STA_ASSERT_MSG(systemEvents != nullptr, "System events not initialized");
systemEvents->set(flags); systemEvents->set(flags);
} }
void waitForSystemEvents(uint32_t flags, uint32_t options, uint32_t timeout) void wait(uint32_t flags, uint32_t options, uint32_t timeout)
{ {
STA_ASSERT_MSG(systemEvents != nullptr, "System events not initialized"); STA_ASSERT_MSG(systemEvents != nullptr, "System events not initialized");
systemEvents->peek(flags, timeout); systemEvents->peek(flags, timeout);
} }
void signalStartupEvent()
{
signalSystemEvents(STA_TACOS_SYSTEM_EVENTS_STARTUP);
}
void waitForStartupEvent() void signalStartup()
{ {
waitForSystemEvents(STA_TACOS_SYSTEM_EVENTS_STARTUP, osFlagsWaitAll, osWaitForever); signalSystemEvents(STA_TACOS_SYSTEM_EVENTS_STARTUP);
} }
void waitForStartup()
{
waitForSystemEvents(STA_TACOS_SYSTEM_EVENTS_STARTUP, osFlagsWaitAll, osWaitForever);
}
} // namespace events
} // namespace tacos } // namespace tacos
} // namespace sta } // namespace sta