software-timer-same-callback.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <stdio.h>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "driver/gpio.h"
  5. #define LED_RED GPIO_NUM_5
  6. #define LED_GREEN GPIO_NUM_2
  7. #define LED_BLUE GPIO_NUM_4
  8. static TimerHandle_t led1 , led2 ,led3= NULL ;
  9. void TimerLedControl(TimerHandle_t xtimer){
  10. uint32_t led_timer_id = (uint32_t) pvTimerGetTimerID(xtimer) ;
  11. //red led
  12. if (led_timer_id == 0){
  13. gpio_set_level(LED_RED,!gpio_get_level(LED_RED));
  14. }
  15. //green led
  16. if (led_timer_id == 1){
  17. gpio_set_level(LED_GREEN,!gpio_get_level(LED_GREEN));
  18. }
  19. //blue led
  20. if (led_timer_id == 2){
  21. gpio_set_level(LED_BLUE,!gpio_get_level(LED_BLUE));
  22. }
  23. printf("inside timer\n");
  24. }
  25. extern "C" void app_main()
  26. {
  27. //setting GPIO Direction (INPUT , OUTPUT)
  28. gpio_set_direction(LED_RED, GPIO_MODE_OUTPUT);
  29. gpio_set_direction(LED_GREEN, GPIO_MODE_OUTPUT);
  30. gpio_set_direction(LED_BLUE, GPIO_MODE_OUTPUT);
  31. //timer tasks
  32. led1 = xTimerCreate("LED Red", pdMS_TO_TICKS(100),pdTRUE,(void *) 0,TimerLedControl);
  33. led2 = xTimerCreate("LED Green", pdMS_TO_TICKS(100),pdTRUE,(void *) 1,TimerLedControl);
  34. led3 = xTimerCreate("LED Blue", pdMS_TO_TICKS(100),pdTRUE,(void *) 2,TimerLedControl);
  35. xTimerStart(led1,0);
  36. xTimerStart(led2,0);
  37. xTimerStart(led3,0);