Mistake on this page?
Report an issue in GitHub or email us
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 
23 namespace 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  */
60 class TimeoutBase : public TickerBase {
61 public:
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)
74 protected:
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 
93 class Timeout : public TimeoutBase {
94 public:
95  Timeout();
96 
97  /** Clock to use with attach_absolute, guaranteeing running only while attached or manually locked */
99 
100  /** @copydoc TimeoutBase::scheduled_time() */
101  HighResClock::time_point scheduled_time() const
102  {
103  /* Massage from virtual TickerDataClock::time_point used internally to true HighResClock::time_point */
104  return HighResClock::time_point{TimeoutBase::scheduled_time().time_since_epoch()};
105  }
106 
107  /** Attach a function to be called by the Timeout, specifying the absolute time
108  *
109  * @param func pointer to the function to be called
110  * @param abs_time the absolute time for the call, referenced to HighResClock
111  *
112  * @note setting @a abs_time to a time in the past means the event will be scheduled immediately
113  * resulting in an instant call to the function.
114  */
115  template <class F>
116  void attach_absolute(F &&func, HighResClock::time_point abs_time)
117  {
118  /* Massage from true HighResClock::time_point to virtual TickerDataClock::time_point used internally */
119  TimeoutBase::attach_absolute(std::forward<F>(func), TickerDataClock::time_point{abs_time.time_since_epoch()});
120  }
121 };
122 
123 /** @}*/
124 
125 } // namespace mbed
126 
127 #endif
A Timeout is used to call a function at a point in the future.
Definition: Timeout.h:60
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:116
std::chrono::microseconds remaining_time() const
Return time remaining until callback.
Definition: Timeout.h:68
HighResClock::time_point scheduled_time() const
Definition: Timeout.h:101
A C++11 Clock representing the HAL us_ticker.
Definition: HighResClock.h:49
Definition: ATHandler.h:46
A Ticker is used to call a function at a recurring interval.
Definition: Ticker.h:73
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.