Naze32 clone with Frysky receiver
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Ticker.h 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "TimerEvent.h"
  19. #include "FunctionPointer.h"
  20. namespace mbed {
  21. /** A Ticker is used to call a function at a recurring interval
  22. *
  23. * You can use as many seperate Ticker objects as you require.
  24. *
  25. * Example:
  26. * @code
  27. * // Toggle the blinking led after 5 seconds
  28. *
  29. * #include "mbed.h"
  30. *
  31. * Ticker timer;
  32. * DigitalOut led1(LED1);
  33. * DigitalOut led2(LED2);
  34. *
  35. * int flip = 0;
  36. *
  37. * void attime() {
  38. * flip = !flip;
  39. * }
  40. *
  41. * int main() {
  42. * timer.attach(&attime, 5);
  43. * while(1) {
  44. * if(flip == 0) {
  45. * led1 = !led1;
  46. * } else {
  47. * led2 = !led2;
  48. * }
  49. * wait(0.2);
  50. * }
  51. * }
  52. * @endcode
  53. */
  54. class Ticker : public TimerEvent {
  55. public:
  56. Ticker() : TimerEvent() {
  57. }
  58. Ticker(const ticker_data_t *data) : TimerEvent(data) {
  59. }
  60. /** Attach a function to be called by the Ticker, specifiying the interval in seconds
  61. *
  62. * @param fptr pointer to the function to be called
  63. * @param t the time between calls in seconds
  64. */
  65. void attach(void (*fptr)(void), float t) {
  66. attach_us(fptr, t * 1000000.0f);
  67. }
  68. /** Attach a member function to be called by the Ticker, specifiying the interval in seconds
  69. *
  70. * @param tptr pointer to the object to call the member function on
  71. * @param mptr pointer to the member function to be called
  72. * @param t the time between calls in seconds
  73. */
  74. template<typename T>
  75. void attach(T* tptr, void (T::*mptr)(void), float t) {
  76. attach_us(tptr, mptr, t * 1000000.0f);
  77. }
  78. /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
  79. *
  80. * @param fptr pointer to the function to be called
  81. * @param t the time between calls in micro-seconds
  82. */
  83. void attach_us(void (*fptr)(void), timestamp_t t) {
  84. _function.attach(fptr);
  85. setup(t);
  86. }
  87. /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
  88. *
  89. * @param tptr pointer to the object to call the member function on
  90. * @param mptr pointer to the member function to be called
  91. * @param t the time between calls in micro-seconds
  92. */
  93. template<typename T>
  94. void attach_us(T* tptr, void (T::*mptr)(void), timestamp_t t) {
  95. _function.attach(tptr, mptr);
  96. setup(t);
  97. }
  98. virtual ~Ticker() {
  99. detach();
  100. }
  101. /** Detach the function
  102. */
  103. void detach();
  104. protected:
  105. void setup(timestamp_t t);
  106. virtual void handler();
  107. protected:
  108. timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
  109. FunctionPointer _function; /**< Callback. */
  110. };
  111. } // namespace mbed
  112. #endif