esp_timer_cxx.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. #ifdef __cpp_exceptions
  15. #include <functional>
  16. #include "esp_timer_cxx.hpp"
  17. #include "esp_exception.hpp"
  18. using namespace std;
  19. namespace idf {
  20. namespace esp_timer {
  21. ESPTimer::ESPTimer(function<void()> timeout_cb, const string &timer_name)
  22. : timeout_cb(timeout_cb), name(timer_name)
  23. {
  24. if (timeout_cb == nullptr) {
  25. throw ESPException(ESP_ERR_INVALID_ARG);
  26. }
  27. esp_timer_create_args_t timer_args = {};
  28. timer_args.callback = esp_timer_cb;
  29. timer_args.arg = this;
  30. timer_args.dispatch_method = ESP_TIMER_TASK;
  31. timer_args.name = name.c_str();
  32. CHECK_THROW(esp_timer_create(&timer_args, &timer_handle));
  33. }
  34. ESPTimer::~ESPTimer()
  35. {
  36. // Ignore potential ESP_ERR_INVALID_STATE here to not throw exception.
  37. esp_timer_stop(timer_handle);
  38. esp_timer_delete(timer_handle);
  39. }
  40. void ESPTimer::esp_timer_cb(void *arg)
  41. {
  42. ESPTimer *timer = static_cast<ESPTimer*>(arg);
  43. timer->timeout_cb();
  44. }
  45. } // esp_timer
  46. } // idf
  47. #endif // __cpp_exceptions