led.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <math.h>
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/task.h"
  14. #include "freertos/timers.h"
  15. #include "esp_system.h"
  16. #include "esp_log.h"
  17. #include "driver/gpio.h"
  18. #include "driver/ledc.h"
  19. #include "led.h"
  20. #include "globdefs.h"
  21. #include "accessors.h"
  22. #define MAX_LED 8
  23. #define BLOCKTIME 10 // up to portMAX_DELAY
  24. static const char *TAG = "led";
  25. static struct led_s {
  26. gpio_num_t gpio;
  27. bool on;
  28. int onstate;
  29. int ontime, offtime;
  30. int pwm;
  31. int channel;
  32. int pushedon, pushedoff;
  33. bool pushed;
  34. TimerHandle_t timer;
  35. } leds[MAX_LED];
  36. static struct {
  37. int gpio;
  38. int active;
  39. int pwm;
  40. } green = { .gpio = CONFIG_LED_GREEN_GPIO, .active = 0, .pwm = -1 },
  41. red = { .gpio = CONFIG_LED_RED_GPIO, .active = 0, .pwm = -1 };
  42. static int led_max = 2;
  43. /****************************************************************************************
  44. *
  45. */
  46. static void set_level(struct led_s *led, bool on) {
  47. if (led->pwm < 0) gpio_set_level(led->gpio, on ? led->onstate : !led->onstate);
  48. else {
  49. ledc_set_duty(LEDC_HIGH_SPEED_MODE, led->channel, on ? led->pwm : (led->onstate ? 0 : pwm_system.max));
  50. ledc_update_duty(LEDC_HIGH_SPEED_MODE, led->channel);
  51. }
  52. }
  53. /****************************************************************************************
  54. *
  55. */
  56. static void vCallbackFunction( TimerHandle_t xTimer ) {
  57. struct led_s *led = (struct led_s*) pvTimerGetTimerID (xTimer);
  58. if (!led->timer) return;
  59. led->on = !led->on;
  60. ESP_EARLY_LOGD(TAG,"led vCallbackFunction setting gpio %d level %d (pwm:%d)", led->gpio, led->on, led->pwm);
  61. set_level(led, led->on);
  62. // was just on for a while
  63. if (!led->on && led->offtime == -1) return;
  64. // regular blinking
  65. xTimerChangePeriod(xTimer, (led->on ? led->ontime : led->offtime) / portTICK_RATE_MS, BLOCKTIME);
  66. }
  67. /****************************************************************************************
  68. *
  69. */
  70. bool led_blink_core(int idx, int ontime, int offtime, bool pushed) {
  71. if (!leds[idx].gpio || leds[idx].gpio < 0 ) return false;
  72. ESP_LOGD(TAG,"led_blink_core");
  73. if (leds[idx].timer) {
  74. // normal requests waits if a pop is pending
  75. if (!pushed && leds[idx].pushed) {
  76. leds[idx].pushedon = ontime;
  77. leds[idx].pushedoff = offtime;
  78. return true;
  79. }
  80. xTimerStop(leds[idx].timer, BLOCKTIME);
  81. }
  82. // save current state if not already pushed
  83. if (!leds[idx].pushed) {
  84. leds[idx].pushedon = leds[idx].ontime;
  85. leds[idx].pushedoff = leds[idx].offtime;
  86. leds[idx].pushed = pushed;
  87. }
  88. // then set new one
  89. leds[idx].ontime = ontime;
  90. leds[idx].offtime = offtime;
  91. if (ontime == 0) {
  92. ESP_LOGD(TAG,"led %d, setting reverse level", idx);
  93. set_level(leds + idx, false);
  94. } else if (offtime == 0) {
  95. ESP_LOGD(TAG,"led %d, setting level", idx);
  96. set_level(leds + idx, true);
  97. } else {
  98. if (!leds[idx].timer) {
  99. ESP_LOGD(TAG,"led %d, Creating timer", idx);
  100. leds[idx].timer = xTimerCreate("ledTimer", ontime / portTICK_RATE_MS, pdFALSE, (void *)&leds[idx], vCallbackFunction);
  101. }
  102. leds[idx].on = true;
  103. set_level(leds + idx, true);
  104. ESP_LOGD(TAG,"led %d, Setting gpio %d and starting timer", idx, leds[idx].gpio);
  105. if (xTimerStart(leds[idx].timer, BLOCKTIME) == pdFAIL) return false;
  106. }
  107. return true;
  108. }
  109. /****************************************************************************************
  110. *
  111. */
  112. bool led_brightness(int idx, int pwm) {
  113. if (pwm > 100) pwm = 100;
  114. leds[idx].pwm = pwm_system.max * powf(pwm / 100.0, 3);
  115. if (!leds[idx].onstate) leds[idx].pwm = pwm_system.max - leds[idx].pwm;
  116. ledc_set_duty(LEDC_HIGH_SPEED_MODE, leds[idx].channel, leds[idx].pwm);
  117. ledc_update_duty(LEDC_HIGH_SPEED_MODE, leds[idx].channel);
  118. return true;
  119. }
  120. /****************************************************************************************
  121. *
  122. */
  123. bool led_unpush(int idx) {
  124. if (!leds[idx].gpio || leds[idx].gpio<0) return false;
  125. led_blink_core(idx, leds[idx].pushedon, leds[idx].pushedoff, true);
  126. leds[idx].pushed = false;
  127. return true;
  128. }
  129. /****************************************************************************************
  130. *
  131. */
  132. int led_allocate(void) {
  133. if (led_max < MAX_LED) return led_max++;
  134. return -1;
  135. }
  136. /****************************************************************************************
  137. *
  138. */
  139. bool led_config(int idx, gpio_num_t gpio, int onstate, int pwm) {
  140. if (gpio < 0) {
  141. ESP_LOGW(TAG,"LED GPIO not configured");
  142. return false;
  143. }
  144. ESP_LOGD(TAG,"Index %d, GPIO %d, on state %s", idx, gpio, onstate>0?"On":"Off");
  145. if (idx >= MAX_LED) return false;
  146. leds[idx].gpio = gpio;
  147. leds[idx].onstate = onstate;
  148. leds[idx].pwm = -1;
  149. if (pwm < 0) {
  150. gpio_pad_select_gpio(gpio);
  151. gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
  152. } else {
  153. leds[idx].channel = pwm_system.base_channel++;
  154. leds[idx].pwm = pwm_system.max * powf(pwm / 100.0, 3);
  155. if (!onstate) leds[idx].pwm = pwm_system.max - leds[idx].pwm;
  156. ledc_channel_config_t ledc_channel = {
  157. .channel = leds[idx].channel,
  158. .duty = leds[idx].pwm,
  159. .gpio_num = gpio,
  160. .speed_mode = LEDC_HIGH_SPEED_MODE,
  161. .hpoint = 0,
  162. .timer_sel = pwm_system.timer,
  163. };
  164. ledc_channel_config(&ledc_channel);
  165. }
  166. set_level(leds + idx, false);
  167. ESP_LOGD(TAG,"PWM Index %d, GPIO %d, on state %s, pwm %d%%", idx, gpio, onstate > 0 ? "On" : "Off", pwm);
  168. return true;
  169. }
  170. /****************************************************************************************
  171. *
  172. */
  173. void set_led_gpio(int gpio, char *value) {
  174. char *p;
  175. if (strcasestr(value, "green")) {
  176. green.gpio = gpio;
  177. if ((p = strchr(value, ':')) != NULL) green.active = atoi(p + 1);
  178. } else if (strcasestr(value, "red")) {
  179. red.gpio = gpio;
  180. if ((p = strchr(value, ':')) != NULL) red.active = atoi(p + 1);
  181. }
  182. }
  183. void led_svc_init(void) {
  184. #ifdef CONFIG_LED_GREEN_GPIO_LEVEL
  185. green.active = CONFIG_LED_GREEN_GPIO_LEVEL;
  186. #endif
  187. #ifdef CONFIG_LED_RED_GPIO_LEVEL
  188. red.active = CONFIG_LED_RED_GPIO_LEVEL;
  189. #endif
  190. #ifndef CONFIG_LED_LOCKED
  191. parse_set_GPIO(set_led_gpio);
  192. #endif
  193. ESP_LOGI(TAG,"Configuring LEDs green:%d (active:%d %d%%), red:%d (active:%d %d%%)", green.gpio, green.active, green.pwm, red.gpio, red.active, red.pwm);
  194. char *nvs_item = config_alloc_get(NVS_TYPE_STR, "led_brightness"), *p;
  195. if (nvs_item) {
  196. if ((p = strcasestr(nvs_item, "green")) != NULL) green.pwm = atoi(strchr(p, '=') + 1);
  197. if ((p = strcasestr(nvs_item, "red")) != NULL) red.pwm = atoi(strchr(p, '=') + 1);
  198. free(nvs_item);
  199. }
  200. led_config(LED_GREEN, green.gpio, green.active, green.pwm);
  201. led_config(LED_RED, red.gpio, red.active, red.pwm);
  202. }