esp_timer_example.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. #include <iostream>
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/task.h"
  17. #include "esp_timer_cxx.hpp"
  18. #include "esp_exception.hpp"
  19. using namespace std;
  20. using namespace idf;
  21. using namespace idf::esp_timer;
  22. extern "C" void app_main(void)
  23. {
  24. try {
  25. cout << "Setting up timer to trigger in 500ms" << endl;
  26. ESPTimer timer([]() { cout << "timeout" << endl; });
  27. timer.start(chrono::microseconds(200 * 1000));
  28. vTaskDelay(550 / portTICK_PERIOD_MS);
  29. cout << "Setting up timer to trigger periodically every 200ms" << endl;
  30. ESPTimer timer2([]() { cout << "periodic timeout" << endl; });
  31. timer2.start_periodic(chrono::microseconds(200 * 1000));
  32. vTaskDelay(1050 / portTICK_PERIOD_MS);
  33. } catch (const ESPException &e) {
  34. cout << "Exception with error: " << e.error << endl;
  35. }
  36. }