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