led.c 6.9 KB

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