Line |
Branch |
Exec |
Source |
1 |
|
|
// This file is part of INSTINCT, the INS Toolkit for Integrated |
2 |
|
|
// Navigation Concepts and Training by the Institute of Navigation of |
3 |
|
|
// the University of Stuttgart, Germany. |
4 |
|
|
// |
5 |
|
|
// This Source Code Form is subject to the terms of the Mozilla Public |
6 |
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this |
7 |
|
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
8 |
|
|
|
9 |
|
|
/// @file CallbackTimer.hpp |
10 |
|
|
/// @brief Starts a Periodic Timer |
11 |
|
|
/// @author T. Topp (topp@ins.uni-stuttgart.de) |
12 |
|
|
/// @date 2020-07-15 |
13 |
|
|
|
14 |
|
|
#pragma once |
15 |
|
|
|
16 |
|
|
#include <functional> |
17 |
|
|
#include <thread> |
18 |
|
|
#include <atomic> |
19 |
|
|
|
20 |
|
|
/// @brief Manages a thread which calls a specified function at a specified interval |
21 |
|
|
class CallbackTimer |
22 |
|
|
{ |
23 |
|
|
public: |
24 |
|
|
/// @brief Default constructor |
25 |
|
336 |
CallbackTimer() = default; |
26 |
|
|
/// @brief Copy constructor |
27 |
|
|
CallbackTimer(const CallbackTimer&) = delete; |
28 |
|
|
/// @brief Move constructor |
29 |
|
|
CallbackTimer(CallbackTimer&&) = delete; |
30 |
|
|
/// @brief Copy assignment operator |
31 |
|
|
CallbackTimer& operator=(const CallbackTimer&) = delete; |
32 |
|
|
/// @brief Move assignment operator |
33 |
|
|
CallbackTimer& operator=(CallbackTimer&&) = delete; |
34 |
|
|
/// @brief Destructor |
35 |
|
|
~CallbackTimer(); |
36 |
|
|
|
37 |
|
|
/// @brief Stops the Timer |
38 |
|
|
void stop(); |
39 |
|
|
|
40 |
|
|
/// @brief Starts the timer |
41 |
|
|
/// @param[in] interval Interval in [ms] when to trigger the callback |
42 |
|
|
/// @param[in] func Function to call |
43 |
|
|
/// @param[in, out] userData User Data which will be passed to the callback function |
44 |
|
|
void start(int interval, const std::function<void(void*)>& func, void* userData); |
45 |
|
|
|
46 |
|
|
/// @brief Set the Interval of the timer |
47 |
|
|
/// @param[in] interval Interval in [ms] when to trigger the callback |
48 |
|
|
void setInterval(int interval); |
49 |
|
|
|
50 |
|
|
/// @brief Checks if the timer is currently running |
51 |
|
|
/// @return True if the timer is running |
52 |
|
|
[[nodiscard]] bool is_running() const noexcept; |
53 |
|
|
|
54 |
|
|
private: |
55 |
|
|
/// @brief Interval in which the timer is triggered |
56 |
|
|
std::atomic<int> _interval{ 0 }; |
57 |
|
|
/// @brief Flag whether the timer should execute |
58 |
|
|
std::atomic<bool> _execute{ false }; |
59 |
|
|
/// @brief Thread object which triggers the timer |
60 |
|
|
std::thread _thd; |
61 |
|
|
}; |
62 |
|
|
|