led.c 6.9 KB

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