esp_timer_cxx.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <string>
  19. #include "esp_exception.hpp"
  20. #include "esp_timer.h"
  21. namespace idf {
  22. namespace esp_timer {
  23. /**
  24. * @brief Get time since boot
  25. * @return time since \c esp_timer_init() was called (this normally happens early during application startup).
  26. */
  27. static inline std::chrono::microseconds get_time()
  28. {
  29. return std::chrono::microseconds(esp_timer_get_time());
  30. }
  31. /**
  32. * @brief Get the timestamp when the next timeout is expected to occur
  33. * @return Timestamp of the nearest timer event.
  34. * The timebase is the same as for the values returned by \c get_time().
  35. */
  36. static inline std::chrono::microseconds get_next_alarm()
  37. {
  38. return std::chrono::microseconds(esp_timer_get_next_alarm());
  39. }
  40. /**
  41. * @brief
  42. * A timer using the esp_timer component which can be started either as one-shot timer or periodically.
  43. */
  44. class ESPTimer {
  45. public:
  46. /**
  47. * @param timeout_cb The timeout callback.
  48. * @param timer_name The name of the timer (optional). This is for debugging using \c esp_timer_dump().
  49. */
  50. ESPTimer(std::function<void()> timeout_cb, const std::string &timer_name = "ESPTimer");
  51. /**
  52. * Stop the timer if necessary and delete it.
  53. */
  54. ~ESPTimer();
  55. /**
  56. * Default copy constructor is deleted since one instance of esp_timer_handle_t must not be shared.
  57. */
  58. ESPTimer(const ESPTimer&) = delete;
  59. /**
  60. * Default copy assignment is deleted since one instance of esp_timer_handle_t must not be shared.
  61. */
  62. ESPTimer &operator=(const ESPTimer&) = delete;
  63. /**
  64. * @brief Start one-shot timer
  65. *
  66. * Timer should not be running (started) when this function is called.
  67. *
  68. * @param timeout timer timeout, in microseconds relative to the current moment.
  69. *
  70. * @throws ESPException with error ESP_ERR_INVALID_STATE if the timer is already running.
  71. */
  72. inline void start(std::chrono::microseconds timeout)
  73. {
  74. CHECK_THROW(esp_timer_start_once(timer_handle, timeout.count()));
  75. }
  76. /**
  77. * @brief Start periodic timer
  78. *
  79. * Timer should not be running when this function is called. This function will
  80. * start a timer which will trigger every 'period' microseconds.
  81. *
  82. * Timer should not be running (started) when this function is called.
  83. *
  84. * @param timeout timer timeout, in microseconds relative to the current moment.
  85. *
  86. * @throws ESPException with error ESP_ERR_INVALID_STATE if the timer is already running.
  87. */
  88. inline void start_periodic(std::chrono::microseconds period)
  89. {
  90. CHECK_THROW(esp_timer_start_periodic(timer_handle, period.count()));
  91. }
  92. /**
  93. * @brief Stop the previously started timer.
  94. *
  95. * This function stops the timer previously started using \c start() or \c start_periodic().
  96. *
  97. * @throws ESPException with error ESP_ERR_INVALID_STATE if the timer has not been started yet.
  98. */
  99. inline void stop()
  100. {
  101. CHECK_THROW(esp_timer_stop(timer_handle));
  102. }
  103. private:
  104. /**
  105. * Internal callback to hook into esp_timer component.
  106. */
  107. static void esp_timer_cb(void *arg);
  108. /**
  109. * Timer instance of the underlying esp_event component.
  110. */
  111. esp_timer_handle_t timer_handle;
  112. /**
  113. * Callback which will be called once the timer triggers.
  114. */
  115. std::function<void()> timeout_cb;
  116. /**
  117. * Name of the timer, will be passed to the underlying timer framework and is used for debugging.
  118. */
  119. const std::string name;
  120. };
  121. } // esp_timer
  122. } // idf
  123. #endif // __cpp_exceptions