led.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "freertos/timers.h"
  14. #include "esp_system.h"
  15. #include "esp_log.h"
  16. #include "driver/gpio.h"
  17. #include "led.h"
  18. #define MAX_LED 8
  19. #define BLOCKTIME 10 // up to portMAX_DELAY
  20. static struct led_s {
  21. gpio_num_t gpio;
  22. bool on;
  23. int onstate;
  24. int ontime, offtime;
  25. int waiton, waitoff;
  26. TimerHandle_t timer;
  27. } leds[MAX_LED];
  28. static void vCallbackFunction( TimerHandle_t xTimer ) {
  29. struct led_s *led = (struct led_s*) pvTimerGetTimerID (xTimer);
  30. if (!led->timer) return;
  31. led->on = !led->on;
  32. gpio_set_level(led->gpio, led->on ? led->onstate : !led->onstate);
  33. // was just on for a while
  34. if (!led->on && led->offtime == -1) return;
  35. // regular blinking
  36. xTimerChangePeriod(xTimer, (led->on ? led->ontime : led->offtime) / portTICK_RATE_MS, BLOCKTIME);
  37. }
  38. bool led_blink_core(int idx, int ontime, int offtime, bool wait) {
  39. if (!leds[idx].gpio) return false;
  40. if (leds[idx].timer) {
  41. // low priority timers will wait
  42. if (wait && xTimerIsTimerActive(leds[idx].timer)) {
  43. leds[idx].waiton = ontime;
  44. leds[idx].waitoff = offtime;
  45. return true;
  46. }
  47. xTimerStop(leds[idx].timer, BLOCKTIME);
  48. }
  49. leds[idx].ontime = ontime;
  50. leds[idx].offtime = offtime;
  51. if (ontime == 0) {
  52. gpio_set_level(leds[idx].gpio, !leds[idx].onstate);
  53. } else if (offtime == 0) {
  54. gpio_set_level(leds[idx].gpio, leds[idx].onstate);
  55. } else {
  56. if (!leds[idx].timer) leds[idx].timer = xTimerCreate("ledTimer", ontime / portTICK_RATE_MS, pdFALSE, (void *)&leds[idx], vCallbackFunction);
  57. leds[idx].on = true;
  58. gpio_set_level(leds[idx].gpio, leds[idx].onstate);
  59. if (xTimerStart(leds[idx].timer, BLOCKTIME) == pdFAIL) return false;
  60. }
  61. return true;
  62. }
  63. bool led_release(int idx) {
  64. if (!leds[idx].gpio) return false;
  65. if (leds[idx].waiton) {
  66. led_blink_core(idx, leds[idx].waiton, leds[idx].waitoff, false);
  67. leds[idx].waiton = 0;
  68. } else {
  69. gpio_set_level(leds[idx].gpio, !leds[idx].onstate);
  70. }
  71. return true;
  72. }
  73. bool led_config(int idx, gpio_num_t gpio, int onstate) {
  74. if (idx >= MAX_LED) return false;
  75. leds[idx].gpio = gpio;
  76. leds[idx].onstate = onstate;
  77. gpio_pad_select_gpio(gpio);
  78. gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
  79. gpio_set_level(gpio, !onstate);
  80. return true;
  81. }