led_vu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Control of LED strip within squeezelite-esp32
  3. *
  4. * (c) Wizmo 2021
  5. *
  6. * Loosely based on code by
  7. * Chuck Rohs 2020, chuck@zethus.ca
  8. *
  9. * This software is released under the MIT License.
  10. * https://opensource.org/licenses/MIT
  11. *
  12. * ToDo:
  13. * Driver does support other led device. Maybe look at supporting in future.
  14. * The VU refresh rate has been decreaced (100->75) to optimize animation of spin dial. Could make
  15. * configurable like text scrolling (or use the same value)
  16. * Look at reserving a status led within the effects. (may require nvs setting for center or end position)
  17. * Artwork function, but not released as very buggy and not really practical
  18. */
  19. #include <ctype.h>
  20. #include <math.h>
  21. #include "esp_log.h"
  22. #include "globdefs.h"
  23. #include "led_strip.h"
  24. #include "platform_config.h"
  25. #include "led_vu.h"
  26. static const char *TAG = "led_vu";
  27. #define LED_VU_STACK_SIZE (3*1024)
  28. #define LED_VU_PEAK_HOLD 6U
  29. #define LED_VU_DEFAULT_GPIO 22
  30. #define LED_VU_DEFAULT_LENGTH 19
  31. #define LED_VU_MAX_LENGTH 255
  32. #define max(a,b) (((a) > (b)) ? (a) : (b))
  33. struct led_strip_t* led_display = NULL;
  34. static EXT_RAM_ATTR struct led_strip_t led_strip_config = {
  35. .rgb_led_type = RGB_LED_TYPE_WS2812,
  36. .gpio = -1,
  37. };
  38. static EXT_RAM_ATTR struct {
  39. int gpio;
  40. int length;
  41. int vu_length;
  42. int vu_start_l;
  43. int vu_start_r;
  44. int vu_odd;
  45. } strip;
  46. static int led_addr(int pos ) {
  47. if (pos < 0) return pos + strip.length;
  48. if (pos >= strip.length) return pos - strip.length;
  49. return pos;
  50. }
  51. /****************************************************************************************
  52. * Initialize the led vu strip if configured.
  53. *
  54. */
  55. void led_vu_init()
  56. {
  57. char* p;
  58. char* config = config_alloc_get_str("led_vu_config", NULL, "N/A");
  59. // Initialize led VU strip
  60. char* drivername = strcasestr(config, "WS2812");
  61. if ((p = strcasestr(config, "length")) != NULL) {
  62. strip.length = atoi(strchr(p, '=') + 1);
  63. } // else 0
  64. if ((p = strcasestr(config, "gpio")) != NULL) {
  65. strip.gpio = atoi(strchr(p, '=') + 1);
  66. } else {
  67. strip.gpio = LED_VU_DEFAULT_GPIO;
  68. }
  69. // check for valid configuration
  70. if (!drivername || !strip.gpio) {
  71. ESP_LOGI(TAG, "led_vu configuration invalid");
  72. goto done;
  73. }
  74. if (strip.length > LED_VU_MAX_LENGTH) strip.length = LED_VU_MAX_LENGTH;
  75. // initialize vu settings
  76. //strip.vu_length = (strip.length % 2) ? strip.length / 2 : (strip.length - 1) / 2;
  77. strip.vu_length = (strip.length - 1) / 2;
  78. strip.vu_start_l = strip.vu_length;
  79. strip.vu_start_r = strip.vu_start_l + 1;
  80. strip.vu_odd = strip.length - 1;
  81. // create driver configuration
  82. led_strip_config.access_semaphore = xSemaphoreCreateBinary();
  83. led_strip_config.led_strip_length = strip.length;
  84. led_strip_config.led_strip_working = heap_caps_malloc(strip.length * sizeof(struct led_color_t), MALLOC_CAP_8BIT);
  85. led_strip_config.led_strip_showing = heap_caps_malloc(strip.length * sizeof(struct led_color_t), MALLOC_CAP_8BIT);
  86. led_strip_config.gpio = strip.gpio;
  87. led_strip_config.rmt_channel = rmt_system_base_channel++;
  88. // initialize driver
  89. bool led_init_ok = led_strip_init(&led_strip_config);
  90. if (led_init_ok) {
  91. led_display = &led_strip_config;
  92. ESP_LOGI(TAG, "led_vu using gpio:%d length:%d on channel:%d", strip.gpio, strip.length, led_strip_config.rmt_channel);
  93. } else {
  94. ESP_LOGE(TAG, "led_vu init failed");
  95. goto done;
  96. }
  97. // reserver max memory for remote management systems
  98. rmt_set_mem_block_num(led_strip_config.rmt_channel, 7);
  99. led_vu_clear(led_display);
  100. done:
  101. free(config);
  102. return;
  103. }
  104. inline bool inRange(double x, double y, double z) {
  105. return (x > y && x < z);
  106. }
  107. /****************************************************************************************
  108. * Returns the led strip length
  109. */
  110. uint16_t led_vu_string_length() {
  111. if (!led_display) return 0;
  112. return (uint16_t)strip.length;
  113. }
  114. /****************************************************************************************
  115. * Turns all LEDs off (Black)
  116. */
  117. void led_vu_clear() {
  118. if (!led_display) return;
  119. led_strip_clear(led_display);
  120. led_strip_show(led_display);
  121. }
  122. /****************************************************************************************
  123. * Sets all LEDs to one color
  124. * r = red (0-255), g = green (0-255), b - blue (0-255)
  125. * note - all colors are adjusted for brightness
  126. */
  127. void led_vu_color_all(uint8_t r, uint8_t g, uint8_t b) {
  128. if (!led_display) return;
  129. struct led_color_t color_on = {.red = r, .green = g, .blue = b};
  130. for (int i = 0 ; i < strip.length ; i ++){
  131. led_strip_set_pixel_color(led_display, i, &color_on);
  132. }
  133. led_strip_show(led_display);
  134. }
  135. /****************************************************************************************
  136. * Sets LEDs based on a data packet consiting of rgb data
  137. * offset - starting LED,
  138. * length - number of leds (3x rgb bytes)
  139. * data - array of rgb values in multiples of 3 bytes
  140. */
  141. void led_vu_data(uint8_t* data, uint16_t offset, uint16_t length) {
  142. if (!led_display) return;
  143. uint8_t* p = (uint8_t*) data;
  144. for (int i = 0; i < length; i++) {
  145. led_strip_set_pixel_rgb(led_display, i+offset, *p, *(p+1), *(p+2));
  146. p+=3;
  147. }
  148. led_strip_show(led_display);
  149. }
  150. /****************************************************************************************
  151. * Progress bar display
  152. * data - array of gain values(0-100)
  153. * offset - starting position
  154. * length - size of array
  155. */
  156. void led_vu_spectrum(uint8_t* data, int bright, int length, int style) {
  157. if (!led_display) return;
  158. uint8_t gain,r,g,b;
  159. int width = strip.length / length;
  160. int pos = 0;
  161. uint8_t* p = (uint8_t*) data;
  162. for (int i=0; i<length; i++) {
  163. gain = *p;
  164. r = gain*gain/bright;
  165. if (!style) {
  166. g = 0;
  167. b = gain;
  168. } else {
  169. g = r;
  170. r = 0;
  171. b = gain * (bright-gain)/bright;
  172. }
  173. for (int j=0; j<width; j++) {
  174. led_strip_set_pixel_rgb(led_display, pos, r, g, b);
  175. pos++;
  176. }
  177. p++;
  178. }
  179. led_strip_show(led_display);
  180. }
  181. /****************************************************************************************
  182. * Progress bar display
  183. * pct - percentage complete (0-100)
  184. */
  185. void led_vu_progress_bar(int pct, int bright) {
  186. if (!led_display) return;
  187. // define colors
  188. struct led_color_t color_on = {.red = bright, .green = 0, .blue = 0};
  189. struct led_color_t color_off = {.red = 0, .green = bright, .blue = 0};
  190. // calcuate led position
  191. int led_lit = strip.length * pct / 100;
  192. // set colors
  193. for (int i = 0; i < strip.length; i++) {
  194. led_strip_set_pixel_color(led_display, i, (i < led_lit) ? &color_off : &color_on);
  195. }
  196. led_strip_show(led_display);
  197. }
  198. /****************************************************************************************
  199. * Spin dial display
  200. * gain - brightness (0-100), rate - color change speed (0-100)
  201. * comet - alternate display mode
  202. */
  203. void led_vu_spin_dial(int gain, int rate, int speed, bool comet)
  204. {
  205. if (!led_display) return;
  206. static int led_pos = 0;
  207. static uint8_t r = 0;
  208. static uint8_t g = 0;
  209. static uint8_t b = 0;
  210. // calculate next color
  211. uint8_t step = rate / 2; // controls color change speed
  212. if (r == 0 && g == 0 && b == 0) {
  213. r = LED_VU_MAX; g = step;
  214. } else if (b == 0) {
  215. g = (g > LED_VU_MAX-step) ? LED_VU_MAX : g + step;
  216. r = (r < step) ? 0 : r - step;
  217. if (r == 0) b = step;
  218. } else if (r == 0) {
  219. b = (b > LED_VU_MAX-step) ? LED_VU_MAX : b + step;
  220. g = (g < step) ? 0 : g- step;
  221. if (g == 0) r = step;
  222. } else {
  223. r = (r > LED_VU_MAX-step) ? LED_VU_MAX : r + step;
  224. b = (b < step) ? 0 : b - step;
  225. if (r == 0) b = step;
  226. }
  227. uint8_t rp = r * gain / LED_VU_MAX;
  228. uint8_t gp = g * gain / LED_VU_MAX;
  229. uint8_t bp = b * gain / LED_VU_MAX;
  230. // set led color
  231. speed++;
  232. if (comet) {
  233. led_strip_clear(led_display);
  234. led_strip_set_pixel_rgb(led_display, led_addr(led_pos-1), rp/2, gp/2, bp/2);
  235. led_strip_set_pixel_rgb(led_display, led_addr(led_pos-2), rp/4, gp/4, bp/4);
  236. led_strip_set_pixel_rgb(led_display, led_addr(led_pos-3), rp/8, gp/8, bp/8);
  237. //led_strip_set_pixel_rgb(led_display, led_addr(led_pos-4), 0, 0, 0);
  238. }
  239. for (int i = 0; i < speed; i++) {
  240. led_strip_set_pixel_rgb(led_display, led_pos, rp, gp, bp);
  241. led_pos = led_addr(++led_pos);
  242. }
  243. led_strip_show(led_display);
  244. }
  245. /****************************************************************************************
  246. * VU meter display
  247. * vu_l - left response (0-100), vu_r - right response (0-100)
  248. * comet - alternate display mode
  249. */
  250. void led_vu_display(int vu_l, int vu_r, int bright, bool comet) {
  251. static int peak_l = 0;
  252. static int peak_r = 0;
  253. static int decay_l = 0;
  254. static int decay_r = 0;
  255. if (!led_display) return;
  256. // scale vu samples to length
  257. vu_l = vu_l * strip.vu_length / bright;
  258. vu_r = vu_r * strip.vu_length / bright;
  259. // calculate hold peaks
  260. if (peak_l > vu_l) {
  261. if (decay_l-- < 0) {
  262. decay_l = LED_VU_PEAK_HOLD;
  263. peak_l--;
  264. }
  265. } else {
  266. peak_l = vu_l;
  267. decay_l = LED_VU_PEAK_HOLD;
  268. }
  269. if (peak_r > vu_r) {
  270. if (decay_r-- < 0) {
  271. decay_r = LED_VU_PEAK_HOLD;
  272. peak_r--;
  273. }
  274. } else {
  275. peak_r = vu_r;
  276. decay_r = LED_VU_PEAK_HOLD;
  277. }
  278. // turn off all leds
  279. led_strip_clear(led_display);
  280. // set the led bar values
  281. uint8_t step = bright / (strip.vu_length-1);
  282. if (step < 1) step = 1; // dor low brightness or larger strips
  283. uint8_t g = bright * 2 / 3; // more red at top
  284. uint8_t r = 0;
  285. int shift = 0;
  286. for (int i = 0; i < strip.vu_length; i++) {
  287. // set left
  288. if (i == peak_l) {
  289. led_strip_set_pixel_rgb(led_display, strip.vu_start_l - i, r, g, bright);
  290. } else if (i <= vu_l) {
  291. shift = vu_l - i;
  292. if (comet)
  293. led_strip_set_pixel_rgb(led_display, strip.vu_start_l - i, r>>shift, g>>shift, 0);
  294. else
  295. led_strip_set_pixel_rgb(led_display, strip.vu_start_l - i, r, g, 0);
  296. }
  297. // set right
  298. if (i == peak_r) {
  299. led_strip_set_pixel_rgb(led_display, strip.vu_start_r + i, r, g, bright);
  300. } else if (i <= vu_r) {
  301. shift = vu_r - i;
  302. if (comet)
  303. led_strip_set_pixel_rgb(led_display, strip.vu_start_r + i, r>>shift, g>>shift, 0);
  304. else
  305. led_strip_set_pixel_rgb(led_display, strip.vu_start_r + i, r, g, 0);
  306. }
  307. // adjust colors (with limit checks)
  308. r = (r > bright-step) ? bright : r + step;
  309. g = (g < step) ? 0 : g - step;
  310. }
  311. led_strip_show(led_display);
  312. }