Mbed OS Reference
Loading...
Searching...
No Matches
Timeout.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2006-2019 ARM Limited
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#ifndef MBED_TIMEOUT_H
18#define MBED_TIMEOUT_H
19
20#include "drivers/HighResClock.h"
21#include "drivers/Ticker.h"
22
23namespace mbed {
24/**
25 * \defgroup drivers_Timeout Timeout class
26 * \ingroup drivers-public-api-ticker
27 * @{
28 */
29
30/** A Timeout is used to call a function at a point in the future
31 *
32 * You can use as many separate Timeout objects as you require.
33 *
34 * @note Synchronization level: Interrupt safe
35 *
36 * Example:
37 * @code
38 * // Blink until timeout.
39 *
40 * #include "mbed.h"
41 *
42 * Timeout timeout;
43 * DigitalOut led(LED1);
44 *
45 * int on = 1;
46 *
47 * void attimeout() {
48 * on = 0;
49 * }
50 *
51 * int main() {
52 * timeout.attach(&attimeout, 5);
53 * while(on) {
54 * led = !led;
55 * ThisThread::sleep_for(200);
56 * }
57 * }
58 * @endcode
59 */
60class TimeoutBase : public TickerBase {
61public:
62 /** Return time remaining until callback
63 *
64 * @return time remaining until callback is due
65 *
66 * @note if the callback is overdue, or has already run, the returned value will be negative
67 */
68 std::chrono::microseconds remaining_time() const
69 {
70 return scheduled_time() - _ticker_data.now();
71 }
72
73#if !defined(DOXYGEN_ONLY)
74protected:
75 using TickerBase::TickerBase;
76 ~TimeoutBase() = default;
77
78 void handler() final;
79
80 /** Return scheduled callback time
81 *
82 * @return scheduled callback time
83 *
84 * @note if the callback is overdue, or has already run, the returned value will be in the past
85 */
86 TickerDataClock::time_point scheduled_time() const
87 {
88 return get_time_point(event);
89 }
90#endif
91};
92
93class Timeout : public TimeoutBase {
94public:
95 Timeout();
96
97 /** Clock to use with attach_absolute, guaranteeing running only while attached or manually locked */
99
100 /** Return scheduled callback time
101 *
102 * @return scheduled callback time
103 *
104 * @note if the callback is overdue, or has already run, the returned value will be in the past
105 */
106 HighResClock::time_point scheduled_time() const
107 {
108 /* Massage from virtual TickerDataClock::time_point used internally to true HighResClock::time_point */
109 return HighResClock::time_point{TimeoutBase::scheduled_time().time_since_epoch()};
110 }
111
112 /** Attach a function to be called by the Timeout, specifying the absolute time
113 *
114 * @param func pointer to the function to be called
115 * @param abs_time the absolute time for the call, referenced to HighResClock
116 *
117 * @note setting @a abs_time to a time in the past means the event will be scheduled immediately
118 * resulting in an instant call to the function.
119 */
120 template <class F>
121 void attach_absolute(F &&func, HighResClock::time_point abs_time)
122 {
123 /* Massage from true HighResClock::time_point to virtual TickerDataClock::time_point used internally */
124 TimeoutBase::attach_absolute(std::forward<F>(func), TickerDataClock::time_point{abs_time.time_since_epoch()});
125 }
126};
127
128/** @}*/
129
130} // namespace mbed
131
132#endif
A C++11 Clock representing the HAL us_ticker.
Definition: HighResClock.h:49
A Ticker is used to call a function at a recurring interval.
Definition: Ticker.h:73
A partial implementation of a C++11 Clock representing a HAL ticker.
A Timeout is used to call a function at a point in the future.
Definition: Timeout.h:60
std::chrono::microseconds remaining_time() const
Return time remaining until callback.
Definition: Timeout.h:68
void attach_absolute(F &&func, HighResClock::time_point abs_time)
Attach a function to be called by the Timeout, specifying the absolute time.
Definition: Timeout.h:121
HighResClock::time_point scheduled_time() const
Return scheduled callback time.
Definition: Timeout.h:106