led_vu.c 12 KB

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