led_vu.c 11 KB

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