esp_timer_cxx.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #ifdef __cpp_exceptions
  16. #include <chrono>
  17. #include <functional>
  18. #include "esp_exception.hpp"
  19. #include "esp_timer.h"
  20. namespace idf {
  21. namespace esp_timer {
  22. /**
  23. * @brief Get time since boot
  24. * @return time since \c esp_timer_init() was called (this normally happens early during application startup).
  25. */
  26. static inline std::chrono::microseconds get_time()
  27. {
  28. return std::chrono::microseconds(esp_timer_get_time());
  29. }
  30. /**
  31. * @brief Get the timestamp when the next timeout is expected to occur
  32. * @return Timestamp of the nearest timer event.
  33. * The timebase is the same as for the values returned by \c get_time().
  34. */
  35. static inline std::chrono::microseconds get_next_alarm()
  36. {
  37. return std::chrono::microseconds(esp_timer_get_next_alarm());
  38. }
  39. /**
  40. * @brief
  41. * A timer using the esp_timer component which can be started either as one-shot timer or periodically.
  42. */
  43. class ESPTimer {
  44. public:
  45. /**
  46. * @param timeout_cb The timeout callback.
  47. * @param timer_name The name of the timer (optional). This is for debugging using \c esp_timer_dump().
  48. */
  49. ESPTimer(std::function<void()> timeout_cb, const std::string &timer_name = "ESPTimer");
  50. /**
  51. * Stop the timer if necessary and delete it.
  52. */
  53. ~ESPTimer();
  54. /**
  55. * Default copy constructor is deleted since one instance of esp_timer_handle_t must not be shared.
  56. */
  57. ESPTimer(const ESPTimer&) = delete;
  58. /**
  59. * Default copy assignment is deleted since one instance of esp_timer_handle_t must not be shared.
  60. */
  61. ESPTimer &operator=(const ESPTimer&) = delete;
  62. /**
  63. * @brief Start one-shot timer
  64. *
  65. * Timer should not be running (started) when this function is called.
  66. *
  67. * @param timeout timer timeout, in microseconds relative to the current moment.
  68. *
  69. * @throws ESPException with error ESP_ERR_INVALID_STATE if the timer is already running.
  70. */
  71. inline void start(std::chrono::microseconds timeout)
  72. {
  73. CHECK_THROW(esp_timer_start_once(timer_handle, timeout.count()));
  74. }
  75. /**
  76. * @brief Start periodic timer
  77. *
  78. * Timer should not be running when this function is called. This function will
  79. * start a timer which will trigger every 'period' microseconds.
  80. *
  81. * Timer should not be running (started) when this function is called.
  82. *
  83. * @param timeout timer timeout, in microseconds relative to the current moment.
  84. *
  85. * @throws ESPException with error ESP_ERR_INVALID_STATE if the timer is already running.
  86. */
  87. inline void start_periodic(std::chrono::microseconds period)
  88. {
  89. CHECK_THROW(esp_timer_start_periodic(timer_handle, period.count()));
  90. }
  91. /**
  92. * @brief Stop the previously started timer.
  93. *
  94. * This function stops the timer previously started using \c start() or \c start_periodic().
  95. *
  96. * @throws ESPException with error ESP_ERR_INVALID_STATE if the timer has not been started yet.
  97. */
  98. inline void stop()
  99. {
  100. CHECK_THROW(esp_timer_stop(timer_handle));
  101. }
  102. private:
  103. /**
  104. * Internal callback to hook into esp_timer component.
  105. */
  106. static void esp_timer_cb(void *arg);
  107. /**
  108. * Timer instance of the underlying esp_event component.
  109. */
  110. esp_timer_handle_t timer_handle;
  111. /**
  112. * Callback which will be called once the timer triggers.
  113. */
  114. std::function<void()> timeout_cb;
  115. /**
  116. * Name of the timer, will be passed to the underlying timer framework and is used for debugging.
  117. */
  118. const std::string name;
  119. };
  120. } // esp_timer
  121. } // idf
  122. #endif // __cpp_exceptions