Ticker.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MBED_TICKER_H
  17. #define MBED_TICKER_H
  18. #include "drivers/TimerEvent.h"
  19. #include "platform/Callback.h"
  20. #include "platform/mbed_toolchain.h"
  21. #include "platform/NonCopyable.h"
  22. #include "platform/mbed_power_mgmt.h"
  23. #include "hal/lp_ticker_api.h"
  24. #include "platform/mbed_critical.h"
  25. namespace mbed {
  26. /** \addtogroup drivers */
  27. /** A Ticker is used to call a function at a recurring interval
  28. *
  29. * You can use as many separate Ticker objects as you require.
  30. *
  31. * @note Synchronization level: Interrupt safe
  32. *
  33. * Example:
  34. * @code
  35. * // Toggle the blinking led after 5 seconds
  36. *
  37. * #include "mbed.h"
  38. *
  39. * Ticker timer;
  40. * DigitalOut led1(LED1);
  41. * DigitalOut led2(LED2);
  42. *
  43. * int flip = 0;
  44. *
  45. * void attime() {
  46. * flip = !flip;
  47. * }
  48. *
  49. * int main() {
  50. * timer.attach(&attime, 5);
  51. * while(1) {
  52. * if(flip == 0) {
  53. * led1 = !led1;
  54. * } else {
  55. * led2 = !led2;
  56. * }
  57. * wait(0.2);
  58. * }
  59. * }
  60. * @endcode
  61. * @ingroup drivers
  62. */
  63. class Ticker : public TimerEvent, private NonCopyable<Ticker> {
  64. public:
  65. Ticker() : TimerEvent(), _function(0), _lock_deepsleep(true)
  66. {
  67. }
  68. // When low power ticker is in use, then do not disable deep-sleep.
  69. Ticker(const ticker_data_t *data) : TimerEvent(data), _function(0), _lock_deepsleep(true)
  70. {
  71. #if DEVICE_LPTICKER
  72. _lock_deepsleep = (data != get_lp_ticker_data());
  73. #endif
  74. }
  75. /** Attach a function to be called by the Ticker, specifying the interval in seconds
  76. *
  77. * @param func pointer to the function to be called
  78. * @param t the time between calls in seconds
  79. */
  80. void attach(Callback<void()> func, float t)
  81. {
  82. attach_us(func, t * 1000000.0f);
  83. }
  84. /** Attach a member function to be called by the Ticker, specifying the interval in seconds
  85. *
  86. * @param obj pointer to the object to call the member function on
  87. * @param method pointer to the member function to be called
  88. * @param t the time between calls in seconds
  89. * @deprecated
  90. * The attach function does not support cv-qualifiers. Replaced by
  91. * attach(callback(obj, method), t).
  92. */
  93. template<typename T, typename M>
  94. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  95. "The attach function does not support cv-qualifiers. Replaced by "
  96. "attach(callback(obj, method), t).")
  97. void attach(T *obj, M method, float t)
  98. {
  99. attach(callback(obj, method), t);
  100. }
  101. /** Attach a function to be called by the Ticker, specifying the interval in micro-seconds
  102. *
  103. * @param func pointer to the function to be called
  104. * @param t the time between calls in micro-seconds
  105. *
  106. * @note setting @a t to a value shorter that it takes to process the ticker callback
  107. * will cause the system to hang. Ticker callback will be called constantly with no time
  108. * for threads scheduling.
  109. *
  110. */
  111. void attach_us(Callback<void()> func, us_timestamp_t t)
  112. {
  113. core_util_critical_section_enter();
  114. // lock only for the initial callback setup and this is not low power ticker
  115. if (!_function && _lock_deepsleep) {
  116. sleep_manager_lock_deep_sleep();
  117. }
  118. _function = func;
  119. setup(t);
  120. core_util_critical_section_exit();
  121. }
  122. /** Attach a member function to be called by the Ticker, specifying the interval in micro-seconds
  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 micro-seconds
  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. virtual ~Ticker()
  140. {
  141. detach();
  142. }
  143. /** Detach the function
  144. */
  145. void detach();
  146. protected:
  147. void setup(us_timestamp_t t);
  148. virtual void handler();
  149. protected:
  150. us_timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
  151. Callback<void()> _function; /**< Callback. */
  152. bool _lock_deepsleep; /**< Flag which indicates if deep-sleep should be disabled. */
  153. };
  154. } // namespace mbed
  155. #endif