2
0

led_vu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 "Config.h"
  25. #include "accessors.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. } strip;
  49. static int led_addr(int pos ) {
  50. if (pos < 0) return pos + strip.length;
  51. if (pos >= strip.length) return pos - strip.length;
  52. return pos;
  53. }
  54. static void battery_svc(float value, int cells) {
  55. battery_status = battery_level_svc();
  56. ESP_LOGI(TAG, "Called for battery service with volt:%f cells:%d status:%d", value, cells, battery_status);
  57. if (battery_handler_chain) battery_handler_chain(value, cells);
  58. }
  59. /****************************************************************************************
  60. * Initialize the led vu strip if configured.
  61. *
  62. */
  63. void led_vu_init()
  64. {
  65. sys_dev_led_strip*config;
  66. if(!SYS_DEV_LEDSTRIP(config)){
  67. ESP_LOGI(TAG,"No LED Strip configuration");
  68. return;
  69. }
  70. if(config->gpio<0){
  71. ESP_LOGI(TAG,"LED Strip has no GPIO configured");
  72. return;
  73. }
  74. ESP_LOGI(TAG, "Initializing led_vu");
  75. // Initialize led VU strip
  76. strip.length = config->length;
  77. strip.gpio = config->gpio;
  78. // check for valid configuration
  79. if (config->strip_type == sys_dev_strip_types_LS_UNKNOWN || config->gpio <0) {
  80. ESP_LOGI(TAG, "led_vu configuration invalid");
  81. return;
  82. }
  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", strip.vu_length, strip.vu_start_l, strip.vu_start_r, strip.vu_status);
  101. // create driver configuration
  102. led_strip_config.rgb_led_type = config->strip_type == sys_dev_strip_types_LS_WS2812?RGB_LED_TYPE_WS2812:RGB_LED_TYPE_MAX;
  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. return;
  117. }
  118. // reserver max memory for remote management systems
  119. rmt_set_mem_block_num(led_strip_config.rmt_channel, 7);
  120. led_vu_clear(led_display);
  121. }
  122. inline bool inRange(double x, double y, double z) {
  123. return (x > y && x < z);
  124. }
  125. /****************************************************************************************
  126. * Returns the led strip length
  127. */
  128. uint16_t led_vu_string_length() {
  129. if (!led_display) return 0;
  130. return (uint16_t)strip.length;
  131. }
  132. /****************************************************************************************
  133. * Turns all LEDs off (Black)
  134. */
  135. void led_vu_clear() {
  136. if (!led_display) return;
  137. led_strip_clear(led_display);
  138. led_strip_show(led_display);
  139. }
  140. /****************************************************************************************
  141. * Sets all LEDs to one color
  142. * r = red (0-255), g = green (0-255), b - blue (0-255)
  143. * note - all colors are adjusted for brightness
  144. */
  145. void led_vu_color_all(uint8_t r, uint8_t g, uint8_t b) {
  146. if (!led_display) return;
  147. struct led_color_t color_on = {.red = r, .green = g, .blue = b};
  148. for (int i = 0 ; i < strip.length ; i ++){
  149. led_strip_set_pixel_color(led_display, i, &color_on);
  150. }
  151. led_strip_show(led_display);
  152. }
  153. /****************************************************************************************
  154. * Sets LEDs based on a data packet consiting of rgb data
  155. * offset - starting LED,
  156. * length - number of leds (3x rgb bytes)
  157. * data - array of rgb values in multiples of 3 bytes
  158. */
  159. void led_vu_data(uint8_t* data, uint16_t offset, uint16_t length) {
  160. if (!led_display) return;
  161. uint8_t* p = (uint8_t*) data;
  162. for (int i = 0; i < length; i++) {
  163. led_strip_set_pixel_rgb(led_display, i+offset, *p, *(p+1), *(p+2));
  164. p+=3;
  165. }
  166. led_strip_show(led_display);
  167. }
  168. /****************************************************************************************
  169. * Progress bar display
  170. * data - array of gain values(0-100)
  171. * offset - starting position
  172. * length - size of array
  173. */
  174. void led_vu_spectrum(uint8_t* data, int bright, int length, int style) {
  175. if (!led_display) return;
  176. uint8_t gain,r,g,b;
  177. int width = strip.length / length;
  178. int pos = 0;
  179. uint8_t* p = (uint8_t*) data;
  180. for (int i=0; i<length; i++) {
  181. gain = *p;
  182. r = gain*gain/bright;
  183. if (!style) {
  184. g = 0;
  185. b = gain;
  186. } else {
  187. g = r;
  188. r = 0;
  189. b = gain * (bright-gain)/bright;
  190. }
  191. for (int j=0; j<width; j++) {
  192. led_strip_set_pixel_rgb(led_display, pos, r, g, b);
  193. pos++;
  194. }
  195. p++;
  196. }
  197. led_strip_show(led_display);
  198. }
  199. /****************************************************************************************
  200. * Progress bar display
  201. * pct - percentage complete (0-100)
  202. */
  203. void led_vu_progress_bar(int pct, int bright) {
  204. if (!led_display) return;
  205. // define colors
  206. struct led_color_t color_on = {.red = bright, .green = 0, .blue = 0};
  207. struct led_color_t color_off = {.red = 0, .green = bright, .blue = 0};
  208. // calcuate led position
  209. int led_lit = strip.length * pct / 100;
  210. // set colors
  211. for (int i = 0; i < strip.length; i++) {
  212. led_strip_set_pixel_color(led_display, i, (i < led_lit) ? &color_off : &color_on);
  213. }
  214. led_strip_show(led_display);
  215. }
  216. /****************************************************************************************
  217. * Spin dial display
  218. * gain - brightness (0-100), rate - color change speed (0-100)
  219. * comet - alternate display mode
  220. */
  221. void led_vu_spin_dial(int gain, int rate, int speed, bool comet)
  222. {
  223. if (!led_display) return;
  224. static int led_pos = 0;
  225. static uint8_t r = 0;
  226. static uint8_t g = 0;
  227. static uint8_t b = 0;
  228. // calculate next color
  229. uint8_t step = rate / 2; // controls color change speed
  230. if (r == 0 && g == 0 && b == 0) {
  231. r = LED_VU_MAX; g = step;
  232. } else if (b == 0) {
  233. g = (g > LED_VU_MAX-step) ? LED_VU_MAX : g + step;
  234. r = (r < step) ? 0 : r - step;
  235. if (r == 0) b = step;
  236. } else if (r == 0) {
  237. b = (b > LED_VU_MAX-step) ? LED_VU_MAX : b + step;
  238. g = (g < step) ? 0 : g- step;
  239. if (g == 0) r = step;
  240. } else {
  241. r = (r > LED_VU_MAX-step) ? LED_VU_MAX : r + step;
  242. b = (b < step) ? 0 : b - step;
  243. if (r == 0) b = step;
  244. }
  245. uint8_t rp = r * gain / LED_VU_MAX;
  246. uint8_t gp = g * gain / LED_VU_MAX;
  247. uint8_t bp = b * gain / LED_VU_MAX;
  248. // set led color
  249. speed++;
  250. if (comet) {
  251. led_strip_clear(led_display);
  252. led_strip_set_pixel_rgb(led_display, led_addr(led_pos-1), rp/2, gp/2, bp/2);
  253. led_strip_set_pixel_rgb(led_display, led_addr(led_pos-2), rp/4, gp/4, bp/4);
  254. led_strip_set_pixel_rgb(led_display, led_addr(led_pos-3), rp/8, gp/8, bp/8);
  255. //led_strip_set_pixel_rgb(led_display, led_addr(led_pos-4), 0, 0, 0);
  256. }
  257. for (int i = 0; i < speed; i++) {
  258. led_strip_set_pixel_rgb(led_display, led_pos, rp, gp, bp);
  259. led_pos = led_addr(++led_pos);
  260. }
  261. led_strip_show(led_display);
  262. }
  263. /****************************************************************************************
  264. * VU meter display
  265. * vu_l - left response (0-100), vu_r - right response (0-100)
  266. * comet - alternate display mode
  267. */
  268. void led_vu_display(int vu_l, int vu_r, int bright, bool comet) {
  269. static int peak_l = 0;
  270. static int peak_r = 0;
  271. static int decay_l = 0;
  272. static int decay_r = 0;
  273. if (!led_display) return;
  274. // single bar
  275. if (strip.vu_start_l == strip.vu_start_r) {
  276. vu_r = (vu_l + vu_r) / 2;
  277. vu_l = 0;
  278. }
  279. // scale vu samples to length
  280. vu_l = vu_l * strip.vu_length / bright;
  281. vu_r = vu_r * strip.vu_length / bright;
  282. // calculate hold peaks
  283. if (peak_l > vu_l) {
  284. if (decay_l-- < 0) {
  285. decay_l = LED_VU_PEAK_HOLD;
  286. peak_l--;
  287. }
  288. } else {
  289. peak_l = vu_l;
  290. decay_l = LED_VU_PEAK_HOLD;
  291. }
  292. if (peak_r > vu_r) {
  293. if (decay_r-- < 0) {
  294. decay_r = LED_VU_PEAK_HOLD;
  295. peak_r--;
  296. }
  297. } else {
  298. peak_r = vu_r;
  299. decay_r = LED_VU_PEAK_HOLD;
  300. }
  301. // turn off all leds
  302. led_strip_clear(led_display);
  303. // set the led bar values
  304. uint8_t step = bright / (strip.vu_length-1);
  305. if (step < 1) step = 1; // dor low brightness or larger strips
  306. uint8_t g = bright * 2 / 3; // more red at top
  307. uint8_t r = 0;
  308. int shift = 0;
  309. for (int i = 0; i < strip.vu_length; i++) {
  310. // set left
  311. if (i == peak_l) {
  312. led_strip_set_pixel_rgb(led_display, strip.vu_start_l - i, r, g, bright);
  313. } else if (i <= vu_l) {
  314. shift = vu_l - i;
  315. if (comet)
  316. led_strip_set_pixel_rgb(led_display, strip.vu_start_l - i, r>>shift, g>>shift, 0);
  317. else
  318. led_strip_set_pixel_rgb(led_display, strip.vu_start_l - i, r, g, 0);
  319. }
  320. // set right
  321. if (i == peak_r) {
  322. led_strip_set_pixel_rgb(led_display, strip.vu_start_r + i, r, g, bright);
  323. } else if (i <= vu_r) {
  324. shift = vu_r - i;
  325. if (comet)
  326. led_strip_set_pixel_rgb(led_display, strip.vu_start_r + i, r>>shift, g>>shift, 0);
  327. else
  328. led_strip_set_pixel_rgb(led_display, strip.vu_start_r + i, r, g, 0);
  329. }
  330. // adjust colors (with limit checks)
  331. r = (r > bright-step) ? bright : r + step;
  332. g = (g < step) ? 0 : g - step;
  333. }
  334. // show battery status
  335. if (battery_status > LED_VU_STATUS_GREEN)
  336. led_strip_set_pixel_rgb(led_display, strip.vu_status, 0, bright, 0);
  337. else if (battery_status > LED_VU_STATUS_RED)
  338. led_strip_set_pixel_rgb(led_display, strip.vu_status, bright/2, bright/2, 0);
  339. else if (battery_status > 0)
  340. led_strip_set_pixel_rgb(led_display, strip.vu_status, bright, 0, 0);
  341. led_strip_show(led_display);
  342. }