SuperTinyKernel™ RTOS 1.05.3
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(0U), m_period(period)
63 {
64 if (start)
65 Restart();
66 }
67
71 uint32_t GetPeriod() const
72 {
73 return m_period;
74 }
75
81 void SetPeriod(uint32_t period)
82 {
83 m_next = (m_next - static_cast<Ticks>(m_period)) + static_cast<Ticks>(period);
84 m_period = period;
85 }
86
91 void Restart()
92 {
94 }
95
106 bool Poll()
107 {
108 STK_ASSERT(m_next > 0);
109
110 Ticks diff = GetTicks() - m_next;
111 if (diff >= 0)
112 {
113 m_next += m_period;
114 return true;
115 }
116
117 return false;
118 }
119
120protected:
122 uint32_t m_period;
123};
124
125} // namespace time
126} // namespace stk
127
128#endif /* STK_TIME_UTIL_H_ */
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:330
Namespace of STK package.
Ticks GetTicks()
Get number of ticks elapsed since kernel start.
Definition stk_helper.h:248
int64_t Ticks
Ticks value.
Definition stk_common.h:150
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.