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(!data->interface->runs_in_deep_sleep)
76  {
77  }
78 
79  /** Attach a function to be called by the Ticker, specifying the interval in seconds
80  *
81  * @param func pointer to the function to be called
82  * @param t the time between calls in seconds
83  */
84  void attach(Callback<void()> func, float t)
85  {
86  attach_us(func, t * 1000000.0f);
87  }
88 
89  /** Attach a member function to be called by the Ticker, specifying the interval in seconds
90  *
91  * @param obj pointer to the object to call the member function on
92  * @param method pointer to the member function to be called
93  * @param t the time between calls in seconds
94  * @deprecated
95  * The attach function does not support cv-qualifiers. Replaced by
96  * attach(callback(obj, method), t).
97  */
98  template<typename T, typename M>
99  MBED_DEPRECATED_SINCE("mbed-os-5.1",
100  "The attach function does not support cv-qualifiers. Replaced by "
101  "attach(callback(obj, method), t).")
102  void attach(T *obj, M method, float t)
103  {
104  attach(callback(obj, method), t);
105  }
106 
107  /** Attach a function to be called by the Ticker, specifying the interval in microseconds
108  *
109  * @param func pointer to the function to be called
110  * @param t the time between calls in micro-seconds
111  *
112  * @note setting @a t to a value shorter than it takes to process the ticker callback
113  * causes the system to hang. Ticker callback is called constantly with no time
114  * for threads scheduling.
115  *
116  */
117  void attach_us(Callback<void()> func, us_timestamp_t t);
118 
119  /** Attach a member function to be called by the Ticker, specifying the interval in microseconds
120  *
121  * @param obj pointer to the object to call the member function on
122  * @param method pointer to the member function to be called
123  * @param t the time between calls in microseconds
124  * @deprecated
125  * The attach_us function does not support cv-qualifiers. Replaced by
126  * attach_us(callback(obj, method), t).
127  */
128  template<typename T, typename M>
129  MBED_DEPRECATED_SINCE("mbed-os-5.1",
130  "The attach_us function does not support cv-qualifiers. Replaced by "
131  "attach_us(callback(obj, method), t).")
132  void attach_us(T *obj, M method, us_timestamp_t t)
133  {
134  attach_us(Callback<void()>(obj, method), t);
135  }
136 
137  virtual ~Ticker()
138  {
139  detach();
140  }
141 
142  /** Detach the function
143  */
144  void detach();
145 
146 #if !defined(DOXYGEN_ONLY)
147 protected:
148  void setup(us_timestamp_t t);
149  virtual void handler();
150 
151 protected:
152  us_timestamp_t _delay; /**< 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 };
157 
158 } // namespace mbed
159 
160 #endif
bool runs_in_deep_sleep
Whether ticker operates in deep sleep.
Definition: ticker_api.h:70
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: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
const ticker_interface_t * interface
Ticker&#39;s interface.
Definition: ticker_api.h:94
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:84
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.