Fang-Robotics-MCB
Fang Robotics Team Codebase
Loading...
Searching...
No Matches
timeout.hpp
Go to the documentation of this file.
1/*****************************************************************************/
2/********** !!! WARNING: CODE GENERATED BY TAPROOT. DO NOT EDIT !!! **********/
3/*****************************************************************************/
4
5/*
6 * Copyright (c) 2020-2021 Advanced Robotics at the University of Washington <robomstr@uw.edu>
7 *
8 * This file is part of Taproot.
9 *
10 * Taproot is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Taproot is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Taproot. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#ifndef TAPROOT_TIMEOUT_HPP_
25#define TAPROOT_TIMEOUT_HPP_
26
27#include <cstdint>
28
29#include "clock.hpp"
30
31namespace tap
32{
33namespace arch
34{
42template <uint32_t (*T)()>
44{
45 template <typename H>
46 friend class PeriodicTimer;
47
48private:
49 bool isRunning;
50 bool isExecuted;
51 uint32_t expireTime;
52
53public:
54 static constexpr auto TimeFunc = T;
55
57 {
58 stop();
59 this->expireTime = 0;
60 }
61
62 explicit Timeout(uint32_t timeout) { restart(timeout); }
63
70 inline void restart(uint32_t timeout)
71 {
72 this->isRunning = true;
73 this->isExecuted = false;
74 this->expireTime = TimeFunc() + timeout;
75 }
76
80 inline void stop()
81 {
82 this->isRunning = false;
83 this->isExecuted = false;
84 }
85
89 inline bool isStopped() const { return !this->isRunning; }
90
95 inline bool isExpired() const { return this->isRunning && TimeFunc() >= this->expireTime; }
96
100 inline uint32_t timeRemaining() const
101 {
102 if (this->isRunning && TimeFunc() < this->expireTime)
103 return this->expireTime - TimeFunc();
104 else
105 return 0;
106 }
107
115 inline bool execute()
116 {
117 if (!isExecuted && isExpired())
118 {
119 isExecuted = true;
120 return true;
121 }
122
123 return false;
124 }
125};
126
129} // namespace arch
130} // namespace tap
131
132#endif // TAPROOT_TIMEOUT_HPP_
Definition periodic_timer.hpp:42
Definition timeout.hpp:44
void restart(uint32_t timeout)
Definition timeout.hpp:70
Timeout()
Definition timeout.hpp:56
Timeout(uint32_t timeout)
Definition timeout.hpp:62
bool isExpired() const
Definition timeout.hpp:95
void stop()
Definition timeout.hpp:80
bool execute()
Definition timeout.hpp:115
static constexpr auto TimeFunc
Definition timeout.hpp:54
bool isStopped() const
Definition timeout.hpp:89
uint32_t timeRemaining() const
Definition timeout.hpp:100
Definition ballistics.cpp:29