led_vu.c 11 KB

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