led_vu.c 13 KB

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