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 <mstd_utility>
21 #include "drivers/TimerEvent.h"
22 #include "platform/Callback.h"
23 #include "platform/mbed_toolchain.h"
24 #include "platform/NonCopyable.h"
25 #include "hal/lp_ticker_api.h"
26 
27 namespace mbed {
28 /**
29  * \defgroup drivers_Ticker Ticker class
30  * \ingroup drivers-public-api-ticker
31  * @{
32  */
33 
34 /** A Ticker is used to call a function at a recurring interval
35  *
36  * You can use as many separate Ticker objects as you require.
37  *
38  * @note Synchronization level: Interrupt safe
39  *
40  * Example:
41  * @code
42  * // Toggle the blinking LED after 5 seconds
43  *
44  * #include "mbed.h"
45  *
46  * Ticker timer;
47  * DigitalOut led1(LED1);
48  * DigitalOut led2(LED2);
49  *
50  * int flip = 0;
51  *
52  * void attime() {
53  * flip = !flip;
54  * }
55  *
56  * int main() {
57  * timer.attach(&attime, 5);
58  * while(1) {
59  * if(flip == 0) {
60  * led1 = !led1;
61  * } else {
62  * led2 = !led2;
63  * }
64  * wait(0.2);
65  * }
66  * }
67  * @endcode
68  */
69 class Ticker : public TimerEvent, private NonCopyable<Ticker> {
70 
71 public:
72  Ticker();
73 
74  // When low power ticker is in use, then do not disable deep sleep.
75  Ticker(const ticker_data_t *data);
76 
77  /** Attach a function to be called by the Ticker, specifying the interval in seconds
78  *
79  * The method forwards its arguments to attach_us() rather than copying them which
80  * may not be trivial depending on the callback copied.
81  * The function is forcibly inlined to not use floating-point operations. This is
82  * possible given attach_us() expects an integer value for the callback interval.
83  * @param func pointer to the function to be called
84  * @param t the time between calls in seconds
85  */
86 #if defined(__ICCARM__)
87  MBED_FORCEINLINE template <typename F>
88 #else
89  template <typename F> MBED_FORCEINLINE
90 #endif
91  void attach(F &&func, float t)
92  {
93  attach_us(std::forward<F>(func), t * 1000000.0f);
94  }
95 
96  /** Attach a member function to be called by the Ticker, specifying the interval in seconds
97  *
98  * @param obj pointer to the object to call the member function on
99  * @param method pointer to the member function to be called
100  * @param t the time between calls in seconds
101  * @deprecated
102  * The attach function does not support cv-qualifiers. Replaced by
103  * attach(callback(obj, method), t).
104  */
105  template<typename T, typename M>
106  MBED_DEPRECATED_SINCE("mbed-os-5.1",
107  "The attach function does not support cv-qualifiers. Replaced by "
108  "attach(callback(obj, method), t).")
109  void attach(T *obj, M method, float t)
110  {
111  attach(callback(obj, method), t);
112  }
113 
114  /** Attach a function to be called by the Ticker, specifying the interval in microseconds
115  *
116  * @param func pointer to the function to be called
117  * @param t the time between calls in micro-seconds
118  *
119  * @note setting @a t to a value shorter than it takes to process the ticker callback
120  * causes the system to hang. Ticker callback is called constantly with no time
121  * for threads scheduling.
122  *
123  */
124  void attach_us(Callback<void()> func, us_timestamp_t t);
125 
126  /** Attach a member function to be called by the Ticker, specifying the interval in microseconds
127  *
128  * @param obj pointer to the object to call the member function on
129  * @param method pointer to the member function to be called
130  * @param t the time between calls in microseconds
131  * @deprecated
132  * The attach_us function does not support cv-qualifiers. Replaced by
133  * attach_us(callback(obj, method), t).
134  */
135  template<typename T, typename M>
136  MBED_DEPRECATED_SINCE("mbed-os-5.1",
137  "The attach_us function does not support cv-qualifiers. Replaced by "
138  "attach_us(callback(obj, method), t).")
139  void attach_us(T *obj, M method, us_timestamp_t t)
140  {
141  attach_us(Callback<void()>(obj, method), t);
142  }
143 
144  virtual ~Ticker()
145  {
146  detach();
147  }
148 
149  /** Detach the function
150  */
151  void detach();
152 
153 #if !defined(DOXYGEN_ONLY)
154 protected:
155  void setup(us_timestamp_t t);
156  virtual void handler();
157 
158 protected:
159  us_timestamp_t _delay; /**< Time delay (in microseconds) for resetting the multishot callback. */
160  Callback<void()> _function; /**< Callback. */
161  bool _lock_deepsleep; /**< Flag which indicates if deep sleep should be disabled. */
162 #endif
163 };
164 
165 /** @}*/
166 
167 } // namespace mbed
168 
169 #endif
#define MBED_FORCEINLINE
MBED_FORCEINLINE Declare a function that must always be inlined.
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:169
Base abstraction for timer interrupts.
Definition: TimerEvent.h:34
Callback< R(ArgTs...)> callback(R(*func)(ArgTs...)=0)
Create a callback class with type inferred from the arguments.
Definition: Callback.h:709
void detach()
Detach the function.
void attach_us(Callback< void()> func, us_timestamp_t t)
Attach a function to be called by the Ticker, specifying the interval in microseconds.
A Ticker is used to call a function at a recurring interval.
Definition: Ticker.h:69
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:91
Callback class based on template specialization.
Definition: Callback.h:39
#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.