Mistake on this page?
Report an issue in GitHub or email us
Ticker.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_TICKER_H
18 #define MBED_TICKER_H
19 
20 #include <chrono>
21 #include <mstd_utility>
22 #include "drivers/TickerDataClock.h"
23 #include "drivers/TimerEvent.h"
24 #include "platform/Callback.h"
25 #include "platform/mbed_toolchain.h"
26 #include "platform/NonCopyable.h"
27 #include "hal/lp_ticker_api.h"
28 
29 namespace mbed {
30 
31 /**
32  * \defgroup drivers_Ticker Ticker class
33  * \ingroup drivers-public-api-ticker
34  * @{
35  */
36 
37 /** A Ticker is used to call a function at a recurring interval
38  *
39  * You can use as many separate Ticker objects as you require.
40  *
41  * @note Synchronization level: Interrupt safe
42  *
43  * Example:
44  * @code
45  * // Toggle the blinking LED after 5 seconds
46  *
47  * #include "mbed.h"
48  * using namespace std::chrono;
49  *
50  * Ticker timer;
51  * DigitalOut led1(LED1);
52  * DigitalOut led2(LED2);
53  *
54  * int flip = 0;
55  *
56  * void attime() {
57  * flip = !flip;
58  * }
59  *
60  * int main() {
61  * timer.attach(&attime, 5us);
62  * while(1) {
63  * if(flip == 0) {
64  * led1 = !led1;
65  * } else {
66  * led2 = !led2;
67  * }
68  * ThisThread::sleep_for(200ms);
69  * }
70  * }
71  * @endcode
72  */
73 class TickerBase : public TimerEvent, private NonCopyable<TickerBase> {
74 public:
75  /** Attach a function to be called by the Ticker, specifying the interval in seconds
76  *
77  * The method forwards its arguments to attach_us() rather than copying them which
78  * may not be trivial depending on the callback copied.
79  * The function is forcibly inlined to not use floating-point operations. This is
80  * possible given attach_us() expects an integer value for the callback interval.
81  * @param func pointer to the function to be called
82  * @param t the time between calls in seconds
83  * @deprecated Pass a chrono duration, not a float second count. For example use `10ms` rather than `0.01f`.
84  */
85 #if defined(__ICCARM__)
86  MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not a float second count. For example use `10ms` rather than `0.01f`.")
87  MBED_FORCEINLINE template <typename F>
88 #else
89  template <typename F> MBED_FORCEINLINE
90  MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not a float second count. For example use `10ms` rather than `0.01f`.")
91 #endif
92  void attach(F &&func, float t)
93  {
94  auto float_interval = std::chrono::duration<float>(t);
95  attach(std::forward<F>(func), std::chrono::duration_cast<std::chrono::microseconds>(float_interval));
96  }
97 
98  /** Attach a function to be called by the Ticker, specifying the interval in microseconds
99  *
100  * @param func pointer to the function to be called
101  * @param t the time between calls in micro-seconds
102  *
103  * @note setting @a t to a value shorter than it takes to process the ticker callback
104  * causes the system to hang. Ticker callback is called constantly with no time
105  * for threads scheduling.
106  * @deprecated Pass a chrono duration, not an integer microsecond count. For example use `10ms` rather than `10000`.
107  *
108  */
109  MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer microsecond count. For example use `10ms` rather than `10000`.")
110  void attach_us(Callback<void()> func, us_timestamp_t t);
111 
112  /** Attach a function to be called by the Ticker, specifying the interval in microseconds
113  *
114  * @param func pointer to the function to be called
115  * @param t the time between calls in micro-seconds
116  *
117  * @note setting @a t to a value shorter than it takes to process the ticker callback
118  * causes the system to hang. Ticker callback is called constantly with no time
119  * for threads scheduling.
120  *
121  */
122  void attach(Callback<void()> func, std::chrono::microseconds t);
123 
124  /** Detach the function
125  */
126  void detach();
127 
128 #if !defined(DOXYGEN_ONLY)
129 protected:
130  TickerBase(const ticker_data_t *data);
131  TickerBase(const ticker_data_t *data, bool lock_deepsleep);
132 
133  ~TickerBase()
134  {
135  detach();
136  }
137 
138  /** Attach a function to be called by the Ticker, specifying the absolute call time
139  *
140  * If used, handler must be overridden, as TickerBase::handler would attempt
141  * to reschedule. This is done by `TimeoutBase` (used by `Timeout` and `LowPowerTimeout`).
142  *
143  * @param func pointer to the function to be called
144  * @param abs_time the time for the call
145  *
146  * @note setting @a abs_time to a time in the past means the event will be scheduled immediately
147  * resulting in an instant call to the function.
148  */
149  void attach_absolute(Callback<void()> func, TickerDataClock::time_point abs_time);
150 
151  void handler() override;
152  std::chrono::microseconds _delay{0}; /**< Time delay (in microseconds) for resetting the multishot callback. */
153  Callback<void()> _function; /**< Callback. */
154  bool _lock_deepsleep; /**< Flag which indicates if deep sleep should be disabled. */
155 #endif
156 private:
157  void setup(std::chrono::microseconds t);
158  void setup_absolute(TickerDataClock::time_point t);
159 };
160 
161 class Ticker : public TickerBase {
162 public:
163  Ticker();
164 };
165 /** @}*/
166 
167 } // namespace mbed
168 
169 #endif
MBED_FORCEINLINE void attach(F &&func, float t)
Attach a function to be called by the Ticker, specifying the interval in seconds. ...
Definition: Ticker.h:92
#define MBED_FORCEINLINE
MBED_FORCEINLINE Declare a function that must always be inlined.
void attach_us(Callback< void()> func, us_timestamp_t t)
Attach a function to be called by the Ticker, specifying the interval in microseconds.
void detach()
Detach the function.
uint64_t us_timestamp_t
A us timestamp stored in a 64 bit integer.
Definition: ticker_api.h:39
Ticker&#39;s data structure.
Definition: ticker_api.h:93
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:162
Base abstraction for timer interrupts.
Definition: TimerEvent.h:37
Callback class based on template specialization.
Definition: Callback.h:53
Definition: ATHandler.h:46
A Ticker is used to call a function at a recurring interval.
Definition: Ticker.h:73
#define MBED_DEPRECATED_SINCE(D, M)
MBED_DEPRECATED("message string") Mark a function declaration as deprecated, if it used then a warnin...
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.