SuperTinyKernel™ RTOS 1.06.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk_time_util.h
Go to the documentation of this file.
1/*
2 * SuperTinyKernel(TM) RTOS: Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
10#ifndef STK_TIME_UTIL_H_
11#define STK_TIME_UTIL_H_
12
16
17namespace stk {
18namespace time {
19
51{
52public:
62 PeriodicTrigger(uint32_t period, bool start = false) : m_next(0), m_period(period)
63 {
64 if (start)
65 {
66 Restart();
67 }
68 }
69
73 uint32_t GetPeriod() const
74 {
75 return m_period;
76 }
77
83 void SetPeriod(uint32_t period)
84 {
85 m_next = (m_next - static_cast<Ticks>(m_period)) + static_cast<Ticks>(period);
86 m_period = period;
87 }
88
93 void Restart()
94 {
95 m_next = GetTicks() + static_cast<Ticks>(m_period);
96 }
97
108 bool Poll()
109 {
110 STK_ASSERT(m_next > 0);
111
112 bool triggered = false;
113 const Ticks diff = GetTicks() - m_next;
114
115 if (diff >= 0)
116 {
117 m_next += static_cast<Ticks>(m_period);
118 triggered = true;
119 }
120
121 return triggered;
122 }
123
124protected:
126 uint32_t m_period;
127};
128
129} // namespace time
130} // namespace stk
131
132#endif /* STK_TIME_UTIL_H_ */
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:409
Namespace of STK package.
int64_t Ticks
Ticks value.
Definition stk_common.h:130
static Ticks GetTicks()
Get number of ticks elapsed since kernel start.
Definition stk_helper.h:319
Time-related primitives.
Ticks m_next
Next trigger time in ticks.
uint32_t GetPeriod() const
Get currently configured trigger period.
void Restart()
Reset the trigger and start.
void SetPeriod(uint32_t period)
Change the trigger period while preserving phase.
uint32_t m_period
Trigger period in ticks. Modified only by SetPeriod(). Must be > 0.
bool Poll()
Check whether the scheduled trigger time has been reached.
PeriodicTrigger(uint32_t period, bool start=false)
Construct a PeriodicTrigger.