ClearCore Library
Loading...
Searching...
No Matches
SysTimer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Teknic, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
28#ifndef __SYSTIMER_H__
29#define __SYSTIMER_H__
30
31#include "SysTiming.h"
32
33namespace ClearCore {
34
45class SysTimer {
46
47protected:
48 // Timer state variables
49 uint32_t m_timerStart = 0;
50 uint32_t m_timerDelay = 0;
51
55 SysTimer() {}
56
63 SysTimer(uint32_t delay) {
64 m_timerDelay = delay;
65 }
66
67public:
74 void SetDelay(uint32_t delay) {
75 m_timerDelay = delay;
76 }
77
84 void Start(uint32_t delay) {
85 m_timerDelay = delay;
86 m_timerStart = Now();
87 }
88
92 void Start() {
93 m_timerStart = Now();
94 }
95
106 bool Completed() {
107 return (Now() - m_timerStart) >= m_timerDelay;
108 }
109
120 uint32_t Elapsed() {
121 return Now() - m_timerStart;
122 }
123
124private:
135 virtual uint32_t Now() = 0;
136
137}; // SysTimer
138
149class SysTimerMs : public SysTimer {
150
151public:
156
162 SysTimerMs(uint32_t delay) : SysTimer(delay) {
163 m_timerStart = Now();
164 }
165
166private:
172 uint32_t Now() override {
173 return Milliseconds();
174 }
175
176}; // SysTimerMs
177
188class SysTimerUs : public SysTimer {
189
190public:
195
201 SysTimerUs(uint32_t delay) : SysTimer(delay) {
202 m_timerStart = Now();
203 }
204
205private:
211 uint32_t Now() override {
212 return Microseconds();
213 }
214
215}; // SysTimerUs
216
217} // ClearCore namespace
218
219#endif //__SYSTIMER_H__
ClearCore timing profiling utility functions.
uint32_t Microseconds(void)
Number of microseconds since the ClearCore was initialized.
uint32_t Milliseconds(void)
Number of milliseconds since the ClearCore was initialized.
Abstract stopwatch/timer base (Start/Elapsed/Completed) with selectable time base.
Definition SysTimer.h:45
uint32_t Elapsed()
Check how much time has elapsed since the start of the timer.
Definition SysTimer.h:120
bool Completed()
Check to see if the timer has completed.
Definition SysTimer.h:106
void Start(uint32_t delay)
Starts the timer with a delay argument.
Definition SysTimer.h:84
void Start()
Starts the timer with the default or configured delay.
Definition SysTimer.h:92
void SetDelay(uint32_t delay)
Set the delay of the timer.
Definition SysTimer.h:74
Stopwatch/timer using millisecond time base (recommended for most timeouts).
Definition SysTimer.h:149
SysTimerMs(uint32_t delay)
Construct and specify the timer's delay.
Definition SysTimer.h:162
SysTimerMs()
Default constructor.
Definition SysTimer.h:155
Stopwatch/timer using microsecond time base (short, high-resolution timing).
Definition SysTimer.h:188
SysTimerUs(uint32_t delay)
Construct and specify the timer's delay.
Definition SysTimer.h:201
SysTimerUs()
Default constructor.
Definition SysTimer.h:194
Namespace to encompass the ClearCore board API.
Definition AdcManager.h:36