led.c 6.9 KB

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