displayer.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. /*
  2. * (c) Philippe G. 2019, philippe_44@outlook.com
  3. *
  4. * This software is released under the MIT License.
  5. * https://opensource.org/licenses/MIT
  6. *
  7. */
  8. #include <ctype.h>
  9. #include <math.h>
  10. #include "esp_dsp.h"
  11. #include "squeezelite.h"
  12. #include "slimproto.h"
  13. #include "display.h"
  14. #include "gds.h"
  15. #include "gds_text.h"
  16. #include "gds_draw.h"
  17. #include "gds_image.h"
  18. #pragma pack(push, 1)
  19. struct grfb_packet {
  20. char opcode[4];
  21. s16_t brightness;
  22. };
  23. struct grfe_packet {
  24. char opcode[4];
  25. u16_t offset;
  26. u8_t transition;
  27. u8_t param;
  28. };
  29. struct grfs_packet {
  30. char opcode[4];
  31. u8_t screen;
  32. u8_t direction; // 1=left, 2=right
  33. u32_t pause; // in ms
  34. u32_t speed; // in ms
  35. u16_t by; // # of pixel of scroll step
  36. u16_t mode; // 0=continuous, 1=once and stop, 2=once and end
  37. u16_t width; // total width of animation
  38. u16_t offset; // offset if multiple packets are sent
  39. };
  40. struct grfg_packet {
  41. char opcode[4];
  42. u16_t screen;
  43. u16_t width; // # of pixels of scrollable
  44. };
  45. struct grfa_packet {
  46. char opcode[4];
  47. u32_t length;
  48. u16_t x;
  49. u16_t y;
  50. u32_t offset;
  51. };
  52. struct visu_packet {
  53. char opcode[4];
  54. u8_t which;
  55. u8_t count;
  56. union {
  57. struct {
  58. u32_t width;
  59. union {
  60. struct {
  61. u32_t bars;
  62. u32_t spectrum_scale;
  63. };
  64. u32_t style;
  65. };
  66. } full;
  67. struct {
  68. u32_t width;
  69. u32_t height;
  70. s32_t col;
  71. s32_t row;
  72. u32_t border;
  73. u32_t bars;
  74. u32_t spectrum_scale;
  75. };
  76. struct {
  77. u32_t mono;
  78. u32_t bandwidth;
  79. u32_t preemph;
  80. struct {
  81. u32_t pos;
  82. u32_t width;
  83. u32_t orient;
  84. u32_t bar_width;
  85. u32_t bar_space;
  86. u32_t clipping;
  87. u32_t bar_intens;
  88. u32_t bar_cap_intens;
  89. } channels[2];
  90. };
  91. struct {
  92. u32_t mono;
  93. u32_t style;
  94. struct {
  95. u32_t pos;
  96. u32_t width;
  97. } channels[2];
  98. } classical_vu;
  99. };
  100. };
  101. struct ANIC_header {
  102. char opcode[4];
  103. u32_t length;
  104. u8_t mode;
  105. };
  106. struct dmxt_packet {
  107. char opcode[4];
  108. u16_t x;
  109. u16_t length;
  110. };
  111. #pragma pack(pop)
  112. static struct {
  113. TaskHandle_t task;
  114. int wake;
  115. bool owned;
  116. struct {
  117. SemaphoreHandle_t mutex;
  118. int width, height;
  119. bool dirty;
  120. };
  121. } displayer = { .dirty = true, .owned = true };
  122. static uint32_t *grayMap;
  123. #define LONG_WAKE (10*1000)
  124. #define SB_HEIGHT 32
  125. // lenght are number of frames, i.e. 2 channels of 16 bits
  126. #define FFT_LEN_BIT 7
  127. #define FFT_LEN (1 << FFT_LEN_BIT)
  128. #define RMS_LEN_BIT 6
  129. #define RMS_LEN (1 << RMS_LEN_BIT)
  130. #define VU_WIDTH 160
  131. #define VU_HEIGHT SB_HEIGHT
  132. #define VU_COUNT 48
  133. #define ARROW_WIDTH 11
  134. #define DISPLAY_BW 20000
  135. static struct scroller_s {
  136. // copy of grfs content
  137. u8_t screen;
  138. u32_t pause;
  139. u16_t mode;
  140. s16_t by;
  141. // scroller management & sharing between grfg and scrolling task
  142. bool active, first, overflow;
  143. int scrolled;
  144. int speed, wake;
  145. struct {
  146. u8_t *frame;
  147. u32_t width;
  148. u32_t max, size;
  149. } scroll;
  150. struct {
  151. u8_t *frame;
  152. u32_t width;
  153. } back;
  154. u8_t *frame;
  155. u32_t width;
  156. } scroller;
  157. static struct {
  158. u8_t *data;
  159. u32_t size;
  160. u16_t x, y;
  161. bool enable, full;
  162. } artwork;
  163. #define MAX_BARS 32
  164. #define VISU_ESP32 0x10
  165. static EXT_RAM_ATTR struct {
  166. int bar_gap, bar_width, bar_border;
  167. bool rotate;
  168. struct bar_s {
  169. int current, max;
  170. int limit;
  171. } bars[MAX_BARS];
  172. float spectrum_scale;
  173. int n, col, row, height, width, border, style, max;
  174. enum { VISU_BLANK, VISU_VUMETER = 0x01, VISU_SPECTRUM = 0x02, VISU_WAVEFORM } mode;
  175. struct {
  176. u8_t *frame;
  177. int width;
  178. bool active;
  179. } back;
  180. } visu;
  181. static EXT_RAM_ATTR struct {
  182. float fft[FFT_LEN*2], samples[FFT_LEN*2], hanning[FFT_LEN];
  183. int levels[2];
  184. } meters;
  185. static EXT_RAM_ATTR struct {
  186. int mode;
  187. int max;
  188. u16_t config;
  189. struct bar_s bars[MAX_BARS] ;
  190. } led_visu;
  191. static EXT_RAM_ATTR uint8_t vu_bitmap[VU_WIDTH * VU_HEIGHT];
  192. extern const uint8_t vu_base[] asm("_binary_vu_s_data_start");
  193. extern const struct {
  194. uint8_t offset;
  195. uint8_t data[VU_HEIGHT * ARROW_WIDTH];
  196. } vu_arrow[VU_COUNT] asm("_binary_arrow_data_start");
  197. #define ANIM_NONE 0x00
  198. #define ANIM_TRANSITION 0x01 // A transition animation has finished
  199. #define ANIM_SCROLL_ONCE 0x02
  200. #define ANIM_SCREEN_1 0x04
  201. #define ANIM_SCREEN_2 0x08
  202. #define SCROLL_STACK_SIZE (3*1024)
  203. #define LINELEN 40
  204. static log_level loglevel = lINFO;
  205. static bool (*slimp_handler_chain)(u8_t *data, int len);
  206. static void (*notify_chain)(in_addr_t ip, u16_t hport, u16_t cport);
  207. static bool (*display_bus_chain)(void *from, enum display_bus_cmd_e cmd);
  208. #define max(a,b) (((a) > (b)) ? (a) : (b))
  209. static void server(in_addr_t ip, u16_t hport, u16_t cport);
  210. static void sendSETD(u16_t width, u16_t height, u16_t led_config);
  211. static void sendANIC(u8_t code);
  212. static bool handler(u8_t *data, int len);
  213. static bool display_bus_handler(void *from, enum display_bus_cmd_e cmd);
  214. static void vfdc_handler( u8_t *_data, int bytes_read);
  215. static void grfe_handler( u8_t *data, int len);
  216. static void grfb_handler(u8_t *data, int len);
  217. static void grfs_handler(u8_t *data, int len);
  218. static void grfg_handler(u8_t *data, int len);
  219. static void grfa_handler(u8_t *data, int len);
  220. static void visu_handler(u8_t *data, int len);
  221. static void dmxt_handler(u8_t *data, int len);
  222. static void displayer_task(void* arg);
  223. void *led_display;
  224. /* scrolling undocumented information
  225. grfs
  226. B: screen number
  227. B:1 = left, 2 = right,
  228. Q: scroll pause once done (ms)
  229. Q: scroll speed (ms)
  230. W: # of pixels to scroll each time
  231. W: 0 = continue scrolling after pause, 1 = scroll to scrollend and then stop, 2 = scroll to scrollend and then end animation (causing new update)
  232. W: width of total scroll area in pixels
  233. grfd
  234. W: screen number
  235. W: width of scrollable area in pixels
  236. anic ( two versions, don't know what to chose)
  237. B: flag
  238. ANIM_TRANSITION (0x01) - transition animation has finished (previous use of ANIC)
  239. ANIM_SCREEN_1 (0x04) - end of first scroll on screen 1
  240. ANIM_SCREEN_2 (0x08) - end of first scroll on screen 2
  241. ANIM_SCROLL_ONCE (0x02) | ANIM_SCREEN_1 (0x04) - end of scroll once on screen 1
  242. ANIM_SCROLL_ONCE (0x02) | ANIM_SCREEN_2 (0x08) - end of scroll once on screen 2
  243. - or -
  244. ANIM_TRANSITION 0x01 # A transition animation has finished
  245. ANIM_SCROLL_ONCE 0x02 # A scrollonce has finished
  246. ANIM_SCREEN_1 0x04 # For scrollonce only, screen 1 was scrolling
  247. ANIM_SCREEN_2 0x08 # For scrollonce only, screen 2 was scrolling
  248. */
  249. /* classical visu not our specific version)
  250. Parameters for the spectrum analyzer:
  251. 0 - Channels: stereo == 0, mono == 1
  252. 1 - Bandwidth: 0..22050Hz == 0, 0..11025Hz == 1
  253. 2 - Preemphasis in dB per KHz
  254. Left channel parameters:
  255. 3 - Position in pixels
  256. 4 - Width in pixels
  257. 5 - orientation: left to right == 0, right to left == 1
  258. 6 - Bar width in pixels
  259. 7 - Bar spacing in pixels
  260. 8 - Clipping: show all subbands == 0, clip higher subbands == 1
  261. 9 - Bar intensity (greyscale): 1-3
  262. 10 - Bar cap intensity (greyscale): 1-3
  263. Right channel parameters (not required for mono):
  264. 11-18 - same as left channel parameters
  265. Parameters for the vumeter:
  266. 0 - Channels: stereo == 0, mono == 1
  267. 1 - Style: digital == 0, analog == 1
  268. Left channel parameters:
  269. 2 - Position in pixels
  270. 3 - Width in pixels
  271. Right channel parameters (not required for mono):
  272. 4-5 - same as left channel parameters
  273. */
  274. /****************************************************************************************
  275. *
  276. */
  277. bool sb_displayer_init(void) {
  278. static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
  279. static EXT_RAM_ATTR StackType_t xStack[SCROLL_STACK_SIZE] __attribute__ ((aligned (4)));
  280. // no display, just make sure we won't have requests
  281. if ((GDS_GetWidth(display) <= 0 || GDS_GetHeight(display) <= 0) && !led_display) {
  282. LOG_INFO("no display or led visualizer for LMS");
  283. return false;
  284. }
  285. if (display) {
  286. // need to force height to 32 maximum
  287. displayer.width = GDS_GetWidth(display);
  288. displayer.height = min(GDS_GetHeight(display), SB_HEIGHT);
  289. // allocate gray-color mapping if needed;
  290. if (GDS_GetMode(display) > GDS_GRAYSCALE) {
  291. grayMap = malloc(256*sizeof(*grayMap));
  292. for (int i = 0; i < 256; i++) grayMap[i] = GDS_GrayMap(display, i);
  293. }
  294. // create visu configuration
  295. visu.bar_gap = 1;
  296. visu.back.frame = calloc(1, (displayer.width * displayer.height) / 8);
  297. // prepare the VU raw data in PSRAM
  298. memcpy(vu_bitmap, vu_base, sizeof(vu_bitmap));
  299. // size scroller (width + current screen)
  300. scroller.scroll.max = (displayer.width * displayer.height / 8) * (15 + 1);
  301. scroller.scroll.frame = malloc(scroller.scroll.max);
  302. scroller.back.frame = malloc(displayer.width * displayer.height / 8);
  303. scroller.frame = malloc(displayer.width * displayer.height / 8);
  304. // chain handlers
  305. display_bus_chain = display_bus;
  306. display_bus = display_bus_handler;
  307. }
  308. if (led_display) {
  309. // PLACEHOLDER to init config
  310. led_visu.mode = VISU_VUMETER;
  311. }
  312. // inform LMS of our screen/led dimensions
  313. sendSETD(GDS_GetWidth(display), GDS_GetHeight(display), led_visu.config);
  314. dsps_fft2r_init_fc32(meters.fft, FFT_LEN);
  315. dsps_wind_hann_f32(meters.hanning, FFT_LEN);
  316. // create displayer management task
  317. displayer.mutex = xSemaphoreCreateMutex();
  318. displayer.task = xTaskCreateStatic( (TaskFunction_t) displayer_task, "sb_displayer", SCROLL_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, &xTaskBuffer);
  319. // chain handlers
  320. slimp_handler_chain = slimp_handler;
  321. slimp_handler = handler;
  322. notify_chain = server_notify;
  323. server_notify = server;
  324. return display != NULL;
  325. }
  326. /****************************************************************************************
  327. * Receive display bus commands
  328. */
  329. static bool display_bus_handler(void *from, enum display_bus_cmd_e cmd) {
  330. // don't answer to own requests
  331. if (from == &displayer) return false ;
  332. LOG_INFO("Display bus command %d", cmd);
  333. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  334. switch (cmd) {
  335. case DISPLAY_BUS_TAKE:
  336. displayer.owned = false;
  337. break;
  338. case DISPLAY_BUS_GIVE:
  339. displayer.owned = true;
  340. break;
  341. }
  342. xSemaphoreGive(displayer.mutex);
  343. // chain to rest of "bus"
  344. if (display_bus_chain) return (*display_bus_chain)(from, cmd);
  345. else return true;
  346. }
  347. /****************************************************************************************
  348. * Send ANImation Complete
  349. */
  350. static void sendANIC(u8_t code) {
  351. struct ANIC_header pkt_header;
  352. memset(&pkt_header, 0, sizeof(pkt_header));
  353. memcpy(&pkt_header.opcode, "ANIC", 4);
  354. pkt_header.length = htonl(sizeof(pkt_header) - 8);
  355. pkt_header.mode = code;
  356. LOCK_P;
  357. send_packet((uint8_t *) &pkt_header, sizeof(pkt_header));
  358. UNLOCK_P;
  359. }
  360. /****************************************************************************************
  361. * Send SETD for width
  362. */
  363. static void sendSETD(u16_t width, u16_t height, u16_t led_config) {
  364. struct SETD_header pkt_header;
  365. memset(&pkt_header, 0, sizeof(pkt_header));
  366. memcpy(&pkt_header.opcode, "SETD", 4);
  367. pkt_header.id = 0xfe; // id 0xfe is width S:P:Squeezebox2
  368. pkt_header.length = htonl(sizeof(pkt_header) + 6 - 8);
  369. LOG_INFO("sending dimension %ux%u", width, height);
  370. width = htons(width);
  371. height = htons(height);
  372. LOCK_P;
  373. send_packet((uint8_t *) &pkt_header, sizeof(pkt_header));
  374. send_packet((uint8_t *) &width, 2);
  375. send_packet((uint8_t *) &height, 2);
  376. send_packet((uint8_t *) &led_config, 2);
  377. UNLOCK_P;
  378. }
  379. /****************************************************************************************
  380. *
  381. */
  382. static void server(in_addr_t ip, u16_t hport, u16_t cport) {
  383. char msg[32];
  384. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  385. sprintf(msg, "%s:%hu", inet_ntoa(ip), hport);
  386. if (display && displayer.owned) GDS_TextPos(display, GDS_FONT_LINE_1, GDS_TEXT_CENTERED, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, msg);
  387. displayer.dirty = true;
  388. xSemaphoreGive(displayer.mutex);
  389. // inform new LMS server of our capabilities
  390. sendSETD(GDS_GetWidth(display), GDS_GetHeight(display), led_visu.config);
  391. if (notify_chain) (*notify_chain)(ip, hport, cport);
  392. }
  393. /****************************************************************************************
  394. * Process graphic display data
  395. */
  396. static bool handler(u8_t *data, int len){
  397. bool res = true;
  398. if (!strncmp((char*) data, "vfdc", 4)) {
  399. vfdc_handler(data, len);
  400. } else if (!strncmp((char*) data, "grfe", 4)) {
  401. grfe_handler(data, len);
  402. } else if (!strncmp((char*) data, "grfb", 4)) {
  403. grfb_handler(data, len);
  404. } else if (!strncmp((char*) data, "grfs", 4)) {
  405. grfs_handler(data, len);
  406. } else if (!strncmp((char*) data, "grfg", 4)) {
  407. grfg_handler(data, len);
  408. } else if (!strncmp((char*) data, "grfa", 4)) {
  409. grfa_handler(data, len);
  410. } else if (!strncmp((char*) data, "visu", 4)) {
  411. visu_handler(data, len);
  412. } else if (!strncmp((char*) data, "dmxt", 4)) {
  413. dmxt_handler(data, len);
  414. } else {
  415. res = false;
  416. }
  417. // chain protocol handlers (bitwise or is fine)
  418. if (*slimp_handler_chain) res |= (*slimp_handler_chain)(data, len);
  419. return res;
  420. }
  421. /****************************************************************************************
  422. * Change special LCD chars to something more printable on screen
  423. */
  424. static void makeprintable(unsigned char * line) {
  425. for (int n = 0; n < LINELEN; n++) {
  426. switch (line[n]) {
  427. case 11: /* block */
  428. line[n] = '#';
  429. break;;
  430. case 16: /* rightarrow */
  431. line[n] = '>';
  432. break;;
  433. case 22: /* circle */
  434. line[n] = '@';
  435. break;;
  436. case 145: /* note */
  437. line[n] = ' ';
  438. break;;
  439. case 152: /* bell */
  440. line[n] = 'o';
  441. break;
  442. default:
  443. break;
  444. }
  445. }
  446. }
  447. /****************************************************************************************
  448. * Check if char is printable, or a valid symbol
  449. */
  450. static bool charisok(unsigned char c) {
  451. switch (c) {
  452. case 11: /* block */
  453. case 16: /* rightarrow */
  454. case 22: /* circle */
  455. case 145: /* note */
  456. case 152: /* bell */
  457. return true;
  458. break;;
  459. default:
  460. return isprint(c);
  461. }
  462. }
  463. /****************************************************************************************
  464. * Show the display (text mode)
  465. */
  466. static void show_display_buffer(char *ddram) {
  467. char line1[LINELEN+1];
  468. char *line2;
  469. memset(line1, 0, LINELEN+1);
  470. strncpy(line1, ddram, LINELEN+1);
  471. line1[LINELEN] = '\0';
  472. line2 = &(ddram[LINELEN]);
  473. line2[LINELEN] = '\0';
  474. /* Convert special LCD chars */
  475. makeprintable((unsigned char *)line1);
  476. makeprintable((unsigned char *)line2);
  477. LOG_DEBUG("\n\t%.40s\n\t%.40s", line1, line2);
  478. GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_CLEAR, line1);
  479. GDS_TextLine(display, 2, GDS_TEXT_LEFT, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, line2);
  480. }
  481. /****************************************************************************************
  482. * Process display data
  483. */
  484. static void vfdc_handler( u8_t *_data, int bytes_read) {
  485. unsigned short *data = (unsigned short*) _data, *display_data;
  486. char ddram[(LINELEN + 1) * 2];
  487. int n, addr = 0; /* counter */
  488. bytes_read -= 4;
  489. if (bytes_read % 2) bytes_read--; /* even number of bytes */
  490. // if we use Noritake VFD codes, display data starts at 12
  491. display_data = &(data[5]); /* display data starts at byte 10 */
  492. memset(ddram, ' ', LINELEN * 2);
  493. for (n = 0; n < (bytes_read/2); n++) {
  494. unsigned short d; /* data element */
  495. unsigned char t, c;
  496. d = ntohs(display_data[n]);
  497. t = (d & 0x00ff00) >> 8; /* type of display data */
  498. c = (d & 0x0000ff); /* character/command */
  499. switch (t) {
  500. case 0x03: /* character */
  501. if (!charisok(c)) c = ' ';
  502. if (addr <= LINELEN * 2) {
  503. ddram[addr++] = c;
  504. }
  505. break;
  506. case 0x02: /* command */
  507. switch (c) {
  508. case 0x06: /* display clear */
  509. memset(ddram, ' ', LINELEN * 2);
  510. break;
  511. case 0x02: /* cursor home */
  512. addr = 0;
  513. break;
  514. case 0xc0: /* cursor home2 */
  515. addr = LINELEN;
  516. break;
  517. }
  518. }
  519. }
  520. show_display_buffer(ddram);
  521. }
  522. /****************************************************************************************
  523. * Display VU-Meter (lots of hard-coding)
  524. */
  525. void draw_VU(struct GDS_Device * display, int level, int x, int y, int width, bool rotate) {
  526. // VU data is by columns and vertical flip to allow block offset
  527. uint8_t *data = vu_bitmap;
  528. int offset = level > 0 ? vu_arrow[level].offset * VU_HEIGHT : 0;
  529. // place the arrow in base VU
  530. memcpy(data + offset, vu_arrow[level].data, sizeof(vu_arrow[level].data));
  531. // adjust to current display window
  532. if (width > VU_WIDTH) {
  533. if (rotate) y += (width - VU_WIDTH) / 2;
  534. else x += (width - VU_WIDTH) / 2;
  535. width = VU_WIDTH;
  536. } else {
  537. data += (VU_WIDTH - width) / 2 * VU_HEIGHT;
  538. }
  539. if (GDS_GetMode(display) <= GDS_GRAYSCALE) {
  540. // this is 8 bits grayscale
  541. int scale = 8 - GDS_GetDepth(display);
  542. // use "fast" version as we are not beyond screen boundaries
  543. if (rotate) {
  544. for (int r = 0; r < width; r++) {
  545. for (int c = VU_HEIGHT; --c >= 0;) {
  546. GDS_DrawPixelFast(display, c + x, r + y, *data++ >> scale);
  547. }
  548. }
  549. } else {
  550. for (int r = 0; r < width; r++) {
  551. for (int c = 0; c < VU_HEIGHT; c++) {
  552. GDS_DrawPixelFast(display, r + x, c + y, *data++ >> scale);
  553. }
  554. }
  555. }
  556. } else {
  557. // use "fast" version as we are not beyond screen boundaries
  558. if (rotate) {
  559. for (int r = 0; r < width; r++) {
  560. for (int c = VU_HEIGHT; --c >= 0;) {
  561. GDS_DrawPixelFast(display, c + x, r + y, grayMap[*data++]);
  562. }
  563. }
  564. } else {
  565. for (int r = 0; r < width; r++) {
  566. for (int c = 0; c < VU_HEIGHT; c++) {
  567. GDS_DrawPixelFast(display, r + x, c + y, grayMap[*data++]);
  568. }
  569. }
  570. }
  571. }
  572. // restore base VU
  573. memcpy(vu_bitmap + offset, vu_base + offset, sizeof(vu_arrow[level].data));
  574. // need to manually set dirty flag as DrawPixel does not do it
  575. GDS_SetDirty(display);
  576. }
  577. /****************************************************************************************
  578. * Process graphic display data
  579. */
  580. static void grfe_handler( u8_t *data, int len) {
  581. struct grfe_packet *pkt = (struct grfe_packet*) data;
  582. // we don't support transition, simply claim we're done
  583. if (pkt->transition != 'c') {
  584. LOG_INFO("Transition %c requested with offset %hu, param %d", pkt->transition, pkt->offset, pkt->param);
  585. sendANIC(ANIM_TRANSITION);
  586. }
  587. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  588. scroller.active = false;
  589. // full screen artwork or for small screen, full screen visu has priority
  590. if (((visu.mode & VISU_ESP32) && !visu.col && visu.row < displayer.height) || artwork.full) {
  591. xSemaphoreGive(displayer.mutex);
  592. return;
  593. }
  594. // are we in control
  595. if (displayer.owned) {
  596. // draw new frame, it might be less than full screen (small visu)
  597. int width = ((len - sizeof(struct grfe_packet)) * 8) / displayer.height;
  598. // did we have something that might have written on the bottom of a displayer's height + display
  599. if (displayer.dirty || (artwork.enable && width == displayer.width && artwork.y < displayer.height)) {
  600. GDS_Clear(display, GDS_COLOR_BLACK);
  601. displayer.dirty = false;
  602. }
  603. // when doing screensaver, that frame becomes a visu background
  604. if (!(visu.mode & VISU_ESP32)) {
  605. visu.back.width = width;
  606. memset(visu.back.frame, 0, (displayer.width * displayer.height) / 8);
  607. memcpy(visu.back.frame, data + sizeof(struct grfe_packet), (width * displayer.height) / 8);
  608. // this is a bit tricky but basically that checks if frame if full of 0
  609. visu.back.active = *visu.back.frame || memcmp(visu.back.frame, visu.back.frame + 1, width - 1);
  610. }
  611. GDS_DrawBitmapCBR(display, data + sizeof(struct grfe_packet), width, displayer.height, GDS_COLOR_WHITE);
  612. GDS_Update(display);
  613. }
  614. xSemaphoreGive(displayer.mutex);
  615. LOG_DEBUG("grfe frame %u", len);
  616. }
  617. /****************************************************************************************
  618. * Brightness
  619. */
  620. static void grfb_handler(u8_t *data, int len) {
  621. struct grfb_packet *pkt = (struct grfb_packet*) data;
  622. pkt->brightness = htons(pkt->brightness);
  623. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  624. // LMS driver sends 0..5 value, we assume driver is highly log
  625. if (pkt->brightness <= 0) {
  626. GDS_DisplayOff(display);
  627. } else {
  628. GDS_DisplayOn(display);
  629. GDS_SetContrast(display, 255 * powf(pkt->brightness / 5.0f, 3));
  630. }
  631. xSemaphoreGive(displayer.mutex);
  632. LOG_INFO("brightness %hu", pkt->brightness);
  633. }
  634. /****************************************************************************************
  635. * Scroll set
  636. */
  637. static void grfs_handler(u8_t *data, int len) {
  638. struct grfs_packet *pkt = (struct grfs_packet*) data;
  639. int size = len - sizeof(struct grfs_packet);
  640. int offset = htons(pkt->offset);
  641. LOG_DEBUG("grfs s:%u d:%u p:%u sp:%u by:%hu m:%hu w:%hu o:%hu",
  642. (int) pkt->screen,
  643. (int) pkt->direction, // 1=left, 2=right
  644. htonl(pkt->pause), // in ms
  645. htonl(pkt->speed), // in ms
  646. htons(pkt->by), // # of pixel of scroll step
  647. htons(pkt->mode), // 0=continuous, 1=once and stop, 2=once and end
  648. htons(pkt->width), // last column of animation that contains a "full" screen
  649. htons(pkt->offset) // offset if multiple packets are sent
  650. );
  651. // new grfs frame, build scroller info
  652. if (!offset) {
  653. // use the display as a general lock
  654. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  655. // copy & set scroll parameters
  656. scroller.screen = pkt->screen;
  657. scroller.pause = htonl(pkt->pause);
  658. scroller.speed = htonl(pkt->speed);
  659. scroller.mode = htons(pkt->mode);
  660. scroller.scroll.width = htons(pkt->width);
  661. scroller.first = true;
  662. scroller.overflow = false;
  663. // set scroller steps & beginning
  664. if (pkt->direction == 1) {
  665. scroller.scrolled = 0;
  666. scroller.by = htons(pkt->by);
  667. } else {
  668. scroller.scrolled = scroller.scroll.width;
  669. scroller.by = -htons(pkt->by);
  670. }
  671. xSemaphoreGive(displayer.mutex);
  672. }
  673. // copy scroll frame data (no semaphore needed)
  674. if (scroller.scroll.size + size < scroller.scroll.max && !scroller.overflow) {
  675. memcpy(scroller.scroll.frame + offset, data + sizeof(struct grfs_packet), size);
  676. scroller.scroll.size = offset + size;
  677. LOG_INFO("scroller current size %u (w:%u)", scroller.scroll.size, scroller.scroll.width);
  678. } else {
  679. LOG_INFO("scroller too large %u/%u (w:%u)", scroller.scroll.size + size, scroller.scroll.max, scroller.scroll.width);
  680. scroller.scroll.width = scroller.scroll.size / (displayer.height / 8) - scroller.back.width;
  681. scroller.overflow = true;
  682. }
  683. }
  684. /****************************************************************************************
  685. * Scroll background frame update & go
  686. */
  687. static void grfg_handler(u8_t *data, int len) {
  688. struct grfg_packet *pkt = (struct grfg_packet*) data;
  689. LOG_DEBUG("gfrg s:%hu w:%hu (len:%u)", htons(pkt->screen), htons(pkt->width), len);
  690. // full screen artwork or for small screen, visu has priority when full screen
  691. if (((visu.mode & VISU_ESP32) && !visu.col && visu.row < displayer.height) || artwork.full) {
  692. return;
  693. }
  694. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  695. // size of scrollable area (less than background)
  696. scroller.width = htons(pkt->width);
  697. scroller.back.width = ((len - sizeof(struct grfg_packet)) * 8) / displayer.height;
  698. memcpy(scroller.back.frame, data + sizeof(struct grfg_packet), len - sizeof(struct grfg_packet));
  699. // update display asynchronously (frames are organized by columns)
  700. memcpy(scroller.frame, scroller.back.frame, scroller.back.width * displayer.height / 8);
  701. for (int i = 0; i < scroller.width * displayer.height / 8; i++) scroller.frame[i] |= scroller.scroll.frame[scroller.scrolled * displayer.height / 8 + i];
  702. // can only write if we really own display
  703. if (displayer.owned) {
  704. GDS_DrawBitmapCBR(display, scroller.frame, scroller.back.width, displayer.height, GDS_COLOR_WHITE);
  705. GDS_Update(display);
  706. }
  707. // now we can active scrolling, but only if we are not on a small screen
  708. if (!visu.mode || visu.col || visu.row >= displayer.height) scroller.active = true;
  709. // if we just got a content update, let the scroller manage the screen
  710. LOG_DEBUG("resuming scrolling task");
  711. xSemaphoreGive(displayer.mutex);
  712. // resume task once we have background, not in grfs
  713. vTaskResume(displayer.task);
  714. }
  715. /****************************************************************************************
  716. * Artwork
  717. */
  718. static void grfa_handler(u8_t *data, int len) {
  719. struct grfa_packet *pkt = (struct grfa_packet*) data;
  720. int size = len - sizeof(struct grfa_packet);
  721. int offset = htonl(pkt->offset);
  722. int length = htonl(pkt->length);
  723. // when using full screen visualizer on small screen there is a brief overlay
  724. artwork.enable = (length != 0);
  725. // just a config or an actual artwork
  726. if (length < 32) {
  727. if (artwork.enable) {
  728. // this is just to specify artwork coordinates
  729. artwork.x = htons(pkt->x);
  730. artwork.y = htons(pkt->y);
  731. } else if (artwork.size) GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
  732. artwork.full = artwork.enable && artwork.x == 0 && artwork.y == 0;
  733. LOG_DEBUG("gfra en:%u x:%hu, y:%hu", artwork.enable, artwork.x, artwork.y);
  734. // done in any case
  735. return;
  736. }
  737. // new grfa artwork, allocate memory
  738. if (!offset) {
  739. // same trick to clean current/previous window
  740. if (artwork.size) {
  741. GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
  742. artwork.size = 0;
  743. }
  744. // now use new parameters
  745. artwork.x = htons(pkt->x);
  746. artwork.y = htons(pkt->y);
  747. artwork.full = artwork.enable && artwork.x == 0 && artwork.y == 0;
  748. if (artwork.data) free(artwork.data);
  749. artwork.data = malloc(length);
  750. }
  751. // copy artwork data
  752. memcpy(artwork.data + offset, data + sizeof(struct grfa_packet), size);
  753. artwork.size += size;
  754. if (artwork.size == length) {
  755. GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
  756. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  757. GDS_DrawJPEG(display, artwork.data, artwork.x, artwork.y, artwork.y < displayer.height ? (GDS_IMAGE_RIGHT | GDS_IMAGE_TOP) : GDS_IMAGE_CENTER);
  758. xSemaphoreGive(displayer.mutex);
  759. free(artwork.data);
  760. artwork.data = NULL;
  761. }
  762. LOG_DEBUG("gfra l:%u x:%hu, y:%hu, o:%u s:%u", length, artwork.x, artwork.y, offset, size);
  763. }
  764. /****************************************************************************************
  765. * Fit spectrum into N bands and convert to dB
  766. */
  767. void spectrum_scale(int n, struct bar_s *bars, int max, float *samples) {
  768. float rate = visu_export.rate;
  769. // now arrange the result with the number of bar and sampling rate (don't want DC)
  770. for (int i = 0, j = 1; i < n && j < (FFT_LEN / 2); i++) {
  771. float power, count;
  772. // find the next point in FFT (this is real signal, so only half matters)
  773. for (count = 0, power = 0; j * visu_export.rate < bars[i].limit * FFT_LEN && j < FFT_LEN / 2; j++, count += 1) {
  774. power += samples[2*j] * samples[2*j] + samples[2*j+1] * samples[2*j+1];
  775. }
  776. // due to sample rate, we have reached the end of the available spectrum
  777. if (j >= (FFT_LEN / 2)) {
  778. // normalize accumulated data
  779. if (count) power /= count * 2.;
  780. } else if (count) {
  781. // how much of what remains do we need to add
  782. float ratio = j - (bars[i].limit * FFT_LEN) / rate;
  783. power += (samples[2*j] * samples[2*j] + samples[2*j+1] * samples[2*j+1]) * ratio;
  784. // normalize accumulated data
  785. power /= (count + ratio) * 2;
  786. } else {
  787. // no data for that band (sampling rate too high), just assume same as previous one
  788. power = (samples[2*j] * samples[2*j] + samples[2*j+1] * samples[2*j+1]) / 2.;
  789. }
  790. // convert to dB and bars, same back-off
  791. bars[i].current = max * (0.01667f*10*(log10f(0.0000001f + power) - log10f(FFT_LEN*(visu_export.gain == FIXED_ONE ? 256 : 2))) - 0.2543f);
  792. if (bars[i].current > max) bars[i].current = max;
  793. else if (bars[i].current < 0) bars[i].current = 0;
  794. }
  795. }
  796. /****************************************************************************************
  797. * Fit levels to max and convert to dB
  798. */
  799. void vu_scale(struct bar_s *bars, int max, int *levels) {
  800. // convert to dB (1 bit remaining for getting X²/N, 60dB dynamic starting from 0dBFS = 3 bits back-off)
  801. for (int i = 2; --i >= 0;) {
  802. bars[i].current = max * (0.01667f*10*log10f(0.0000001f + (levels[i] >> (visu_export.gain == FIXED_ONE ? 8 : 1))) - 0.2543f);
  803. if (bars[i].current > max) bars[i].current = max;
  804. else if (bars[i].current < 0) bars[i].current = 0;
  805. }
  806. }
  807. /****************************************************************************************
  808. * visu draw
  809. */
  810. void visu_draw(void) {
  811. // don't refresh screen if all max are 0 (we were are somewhat idle)
  812. int clear = 0;
  813. for (int i = visu.n; --i >= 0;) clear = max(clear, visu.bars[i].max);
  814. if (clear) GDS_ClearExt(display, false, false, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  815. // draw background if we are in screensaver mode
  816. if (!(visu.mode & VISU_ESP32) && visu.back.active) {
  817. GDS_DrawBitmapCBR(display, visu.back.frame, visu.back.width, displayer.height, GDS_COLOR_WHITE);
  818. }
  819. if ((visu.mode & ~VISU_ESP32) != VISU_VUMETER || !visu.style) {
  820. // there is much more optimization to be done here, like not redrawing bars unless needed
  821. for (int i = visu.n; --i >= 0;) {
  822. // update maximum
  823. if (visu.bars[i].current > visu.bars[i].max) visu.bars[i].max = visu.bars[i].current;
  824. else if (visu.bars[i].max) visu.bars[i].max--;
  825. else if (!clear) continue;
  826. if (visu.rotate) {
  827. int x1 = visu.col;
  828. int y1 = visu.row + visu.border + visu.bar_border + i*(visu.bar_width + visu.bar_gap);
  829. for (int j = 0; j <= visu.bars[i].current; j += 2)
  830. GDS_DrawLine(display, x1 + j, y1, x1 + j, y1 + visu.bar_width - 1, GDS_COLOR_WHITE);
  831. if (visu.bars[i].max > 2) {
  832. GDS_DrawLine(display, x1 + visu.bars[i].max, y1, x1 + visu.bars[i].max, y1 + visu.bar_width - 1, GDS_COLOR_WHITE);
  833. if (visu.bars[i].max < visu.max - 1) GDS_DrawLine(display, x1 + visu.bars[i].max + 1, y1, x1 + visu.bars[i].max + 1, y1 + visu.bar_width - 1, GDS_COLOR_WHITE);
  834. }
  835. } else {
  836. int x1 = visu.col + visu.border + visu.bar_border + i*(visu.bar_width + visu.bar_gap);
  837. int y1 = visu.row + visu.height - 1;
  838. for (int j = 0; j <= visu.bars[i].current; j += 2)
  839. GDS_DrawLine(display, x1, y1 - j, x1 + visu.bar_width - 1, y1 - j, GDS_COLOR_WHITE);
  840. if (visu.bars[i].max > 2) {
  841. GDS_DrawLine(display, x1, y1 - visu.bars[i].max, x1 + visu.bar_width - 1, y1 - visu.bars[i].max, GDS_COLOR_WHITE);
  842. if (visu.bars[i].max < visu.max - 1) GDS_DrawLine(display, x1, y1 - visu.bars[i].max + 1, x1 + visu.bar_width - 1, y1 - visu.bars[i].max + 1, GDS_COLOR_WHITE);
  843. }
  844. }
  845. }
  846. } else if (displayer.width / 2 >= 3 * VU_WIDTH / 4) {
  847. if (visu.rotate) {
  848. draw_VU(display, visu.bars[0].current, 0, visu.row, visu.height / 2, visu.rotate);
  849. draw_VU(display, visu.bars[1].current, 0, visu.row + visu.height / 2, visu.height / 2, visu.rotate);
  850. } else {
  851. draw_VU(display, visu.bars[0].current, 0, visu.row, visu.width / 2, visu.rotate);
  852. draw_VU(display, visu.bars[1].current, visu.width / 2, visu.row, visu.width / 2, visu.rotate);
  853. }
  854. } else {
  855. int level = (visu.bars[0].current + visu.bars[1].current) / 2;
  856. draw_VU(display, level, 0, visu.row, visu.rotate ? visu.height : visu.width, visu.rotate);
  857. }
  858. }
  859. /****************************************************************************************
  860. * Update displayer
  861. */
  862. static void displayer_update(void) {
  863. // no update when artwork is full screen and no led_strip (but no need to protect against not owning the display as we are playing
  864. if ((artwork.full && !led_visu.mode) || pthread_mutex_trylock(&visu_export.mutex)) {
  865. return;
  866. }
  867. int mode = (visu.mode & ~VISU_ESP32) | led_visu.mode;
  868. // not enough frames
  869. if (visu_export.level < (mode & VISU_SPECTRUM ? FFT_LEN : RMS_LEN) && visu_export.running) {
  870. pthread_mutex_unlock(&visu_export.mutex);
  871. return;
  872. }
  873. // reset all levels no matter what
  874. meters.levels[0] = meters.levels[1] = 0;
  875. memset(meters.samples, 0, sizeof(meters.samples));
  876. if (visu_export.running) {
  877. // calculate data for VU-meter
  878. if (mode & VISU_VUMETER) {
  879. s16_t *iptr = (s16_t*) visu_export.buffer + (BYTES_PER_FRAME / 4) - 1;
  880. int *left = &meters.levels[0], *right = &meters.levels[1];
  881. // calculate sum(L²+R²), try to not overflow at the expense of some precision
  882. for (int i = RMS_LEN; --i >= 0;) {
  883. *left += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
  884. iptr += BYTES_PER_FRAME / 4;
  885. *right += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
  886. iptr += BYTES_PER_FRAME / 4;
  887. }
  888. }
  889. // calculate data for spectrum
  890. if (mode & VISU_SPECTRUM) {
  891. s16_t *iptr = (s16_t*) visu_export.buffer + (BYTES_PER_FRAME / 4) - 1;
  892. // on xtensa/esp32 the floating point FFT takes 1/2 cycles of the fixed point
  893. for (int i = 0 ; i < FFT_LEN ; i++) {
  894. // don't normalize here, but we are due INT16_MAX and FFT_LEN / 2 / 2
  895. meters.samples[i * 2 + 0] = (float) (*iptr + *(iptr+BYTES_PER_FRAME/4)) * meters.hanning[i];
  896. meters.samples[i * 2 + 1] = 0;
  897. iptr += 2 * BYTES_PER_FRAME / 4;
  898. }
  899. // actual FFT that might be less cycle than all the crap below
  900. dsps_fft2r_fc32_ae32(meters.samples, FFT_LEN);
  901. dsps_bit_rev_fc32_ansi(meters.samples, FFT_LEN);
  902. }
  903. }
  904. // we took what we want, we can release the buffer
  905. visu_export.level = 0;
  906. pthread_mutex_unlock(&visu_export.mutex);
  907. // actualize the display
  908. if (visu.mode && !artwork.full) {
  909. if (visu.mode & VISU_SPECTRUM) spectrum_scale(visu.n, visu.bars, visu.max, meters.samples);
  910. else for (int i = 2; --i >= 0;) vu_scale(visu.bars, visu.max, meters.levels);
  911. visu_draw();
  912. }
  913. // actualize led_vu
  914. if (led_visu.mode) {
  915. // PLACEHOLDER to handle led_display. you need potentially scaling of spectrum (X and Y)
  916. // and scaling of levels (Y) and then call the
  917. }
  918. }
  919. /****************************************************************************************
  920. * Calculate spectrum spread
  921. */
  922. static void spectrum_limits(int min, int n, int pos) {
  923. if (n / 2) {
  924. int step = ((DISPLAY_BW - min) * visu.spectrum_scale) / (n/2);
  925. visu.bars[pos].limit = min + step;
  926. for (int i = 1; i < n/2; i++) visu.bars[pos+i].limit = visu.bars[pos+i-1].limit + step;
  927. spectrum_limits(visu.bars[pos + n/2 - 1].limit, n - n/2, pos + n/2);
  928. } else {
  929. visu.bars[pos].limit = DISPLAY_BW;
  930. }
  931. }
  932. /****************************************************************************************
  933. * Fit visu
  934. */
  935. static void visu_fit(int bars, int width, int height) {
  936. // try to adapt to what we have
  937. if ((visu.mode & ~VISU_ESP32) == VISU_SPECTRUM) {
  938. visu.n = bars ? bars : MAX_BARS;
  939. visu.max = height - 1;
  940. if (visu.spectrum_scale <= 0 || visu.spectrum_scale > 0.5) visu.spectrum_scale = 0.5;
  941. spectrum_limits(0, visu.n, 0);
  942. } else {
  943. visu.n = 2;
  944. visu.max = (visu.style ? VU_COUNT : height) - 1;
  945. }
  946. do {
  947. visu.bar_width = (width - visu.border - visu.bar_gap * (visu.n - 1)) / visu.n;
  948. if (visu.bar_width > 0) break;
  949. } while (--visu.n);
  950. visu.bar_border = (width - visu.border - (visu.bar_width + visu.bar_gap) * visu.n + visu.bar_gap) / 2;
  951. }
  952. /****************************************************************************************
  953. * Visu packet handler
  954. */
  955. static void visu_handler( u8_t *data, int len) {
  956. struct visu_packet *pkt = (struct visu_packet*) data;
  957. int bars = 0;
  958. LOG_DEBUG("visu %u with %u parameters", pkt->which, pkt->count);
  959. /*
  960. If width is specified, then respect all coordinates, otherwise we try to
  961. use the bottom part of the display and if it is a small display, we overwrite
  962. text
  963. */
  964. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  965. visu.mode = pkt->which;
  966. // little trick to clean the taller screens when switching visu
  967. if (visu.row >= displayer.height) GDS_ClearExt(display, false, true, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  968. if (visu.mode) {
  969. // these will be overidden if necessary
  970. visu.col = visu.border = 0;
  971. visu.rotate = false;
  972. // what type of visu
  973. if (visu.mode & VISU_ESP32) {
  974. if (pkt->count >= 4) {
  975. // more than 4 parameters, this is small visu, then go were we are told to
  976. pkt->height = htonl(pkt->height);
  977. pkt->row = htonl(pkt->row);
  978. pkt->col = htonl(pkt->col);
  979. visu.style = 0;
  980. visu.width = htonl(pkt->width);
  981. visu.height = pkt->height ? pkt->height : displayer.height;
  982. visu.col = pkt->col < 0 ? displayer.width + pkt->col : pkt->col;
  983. visu.row = pkt->row < 0 ? GDS_GetHeight(display) + pkt->row : pkt->row;
  984. visu.border = htonl(pkt->border);
  985. bars = htonl(pkt->bars);
  986. visu.spectrum_scale = htonl(pkt->spectrum_scale) / 100.;
  987. } else {
  988. // full screen visu, try to optimize orientation/shape
  989. visu.width = htonl(pkt->full.width);
  990. visu.height = GDS_GetHeight(display);
  991. // do we have enough height to play with layout
  992. if (GDS_GetHeight(display) > displayer.height) {
  993. // by default, use up to the bottom of the display
  994. visu.height -= displayer.height;
  995. visu.row = displayer.height;
  996. if (artwork.enable && artwork.y) {
  997. // server sets width to artwork X offset to tell us to rotate
  998. if (visu.width != artwork.x) {
  999. visu.height = artwork.y - displayer.height;
  1000. if (visu.height <= 0) {
  1001. visu.height = displayer.height;
  1002. LOG_WARN("No room left for visualizer, disable it or increase artwork offset %d", artwork.y);
  1003. }
  1004. } else visu.rotate = true;
  1005. }
  1006. } else visu.row = 0;
  1007. // is this spectrum or analogue/digital
  1008. if ((visu.mode & ~VISU_ESP32) == VISU_SPECTRUM) {
  1009. bars = htonl(pkt->full.bars);
  1010. visu.spectrum_scale = htonl(pkt->full.spectrum_scale) / 100.;
  1011. } else {
  1012. // select analogue/digital style
  1013. visu.style = htonl(pkt->full.style);
  1014. }
  1015. }
  1016. } else {
  1017. // classical (screensaver) mode, don't try to optimize screen usage & force some params
  1018. visu.row = 0;
  1019. visu.height = GDS_GetHeight(display);
  1020. visu.width = displayer.width;
  1021. visu.spectrum_scale = 0.25;
  1022. if (visu.mode == VISU_SPECTRUM) {
  1023. bars = visu.width / (htonl(pkt->channels[0].bar_width) + htonl(pkt->channels[0].bar_space));
  1024. } else {
  1025. visu.style = htonl(pkt->classical_vu.style);
  1026. if (visu.style) visu.row = visu.height - VU_HEIGHT;
  1027. }
  1028. }
  1029. if (bars > MAX_BARS) bars = MAX_BARS;
  1030. // for rotate, swap width & height
  1031. if (visu.rotate) visu_fit(bars, visu.height, visu.width);
  1032. else visu_fit(bars, visu.width, visu.height);
  1033. // give up if not enough space
  1034. if (visu.bar_width < 0) {
  1035. visu.mode = VISU_BLANK;
  1036. LOG_WARN("Not enough room for displaying visu");
  1037. } else {
  1038. // de-activate scroller if we are taking main screen
  1039. if (visu.row < displayer.height) scroller.active = false;
  1040. vTaskResume(displayer.task);
  1041. }
  1042. displayer.wake = 0;
  1043. // reset bars maximum
  1044. for (int i = visu.n; --i >= 0;) visu.bars[i].max = 0;
  1045. GDS_ClearExt(display, false, true, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  1046. LOG_INFO("Visualizer with %u bars of width %d:%d:%d:%d (%w:%u,h:%u,c:%u,r:%u,s:%.02f)", visu.n, visu.bar_border, visu.bar_width, visu.bar_gap, visu.border, visu.width, visu.height, visu.col, visu.row, visu.spectrum_scale);
  1047. } else {
  1048. LOG_INFO("Stopping visualizer");
  1049. }
  1050. xSemaphoreGive(displayer.mutex);
  1051. }
  1052. /****************************************************************************************
  1053. * Dmx style packet handler
  1054. * ToDo: make packet match dmx protocol format
  1055. */
  1056. static void dmxt_handler( u8_t *data, int len) {
  1057. struct dmxt_packet *pkt = (struct dmxt_packet*) data;
  1058. uint16_t offset = htons(pkt->x);
  1059. uint16_t length = htons(pkt->length);
  1060. LOG_INFO("dmx packet len:%u offset:%u", length, offset);
  1061. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  1062. // PLACEHOLDER
  1063. //led_vu_data(data + sizeof(struct dmxt_packet), offset, length);
  1064. xSemaphoreGive(displayer.mutex);
  1065. }
  1066. /****************************************************************************************
  1067. * Scroll task
  1068. * - with the addition of the visualizer, it's a bit a 2-headed beast not easy to
  1069. * maintain, so som better separation between the visu and scroll is probably needed
  1070. */
  1071. static void displayer_task(void *args) {
  1072. int sleep;
  1073. while (1) {
  1074. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  1075. // suspend ourselves if nothing to do, grfg or visu will wake us up
  1076. if (!scroller.active && !visu.mode && !led_visu.mode) {
  1077. xSemaphoreGive(displayer.mutex);
  1078. vTaskSuspend(NULL);
  1079. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  1080. scroller.wake = displayer.wake = 0;
  1081. }
  1082. // go for long sleep when either item is disabled
  1083. if (!visu.mode && !led_visu.mode) displayer.wake = LONG_WAKE;
  1084. if (!scroller.active) scroller.wake = LONG_WAKE;
  1085. // scroll required amount of columns (within the window)
  1086. if (scroller.active && scroller.wake <= 0) {
  1087. // by default go for the long sleep, will change below if required
  1088. scroller.wake = LONG_WAKE;
  1089. // do we have more to scroll (scroll.width is the last column from which we have a full zone)
  1090. if (scroller.by > 0 ? (scroller.scrolled <= scroller.scroll.width) : (scroller.scrolled >= 0)) {
  1091. memcpy(scroller.frame, scroller.back.frame, scroller.back.width * displayer.height / 8);
  1092. for (int i = 0; i < scroller.width * displayer.height / 8; i++) scroller.frame[i] |= scroller.scroll.frame[scroller.scrolled * displayer.height / 8 + i];
  1093. scroller.scrolled += scroller.by;
  1094. if (displayer.owned) GDS_DrawBitmapCBR(display, scroller.frame, scroller.width, displayer.height, GDS_COLOR_WHITE);
  1095. // short sleep & don't need background update
  1096. scroller.wake = scroller.speed;
  1097. } else if (scroller.first || !scroller.mode) {
  1098. // at least one round done
  1099. scroller.first = false;
  1100. // see if we need to pause or if we are done
  1101. if (scroller.mode) {
  1102. sendANIC(ANIM_SCROLL_ONCE | ANIM_SCREEN_1);
  1103. LOG_INFO("scroll-once terminated");
  1104. } else {
  1105. scroller.wake = scroller.pause;
  1106. LOG_DEBUG("scroll cycle done, pausing for %u (ms)", scroller.pause);
  1107. }
  1108. // need to reset pointers for next scroll
  1109. scroller.scrolled = scroller.by < 0 ? scroller.scroll.width : 0;
  1110. }
  1111. }
  1112. // update visu if active
  1113. if ((visu.mode || led_visu.mode) && displayer.wake <= 0 && displayer.owned) {
  1114. displayer_update();
  1115. displayer.wake = 100;
  1116. }
  1117. // need to make sure we own display
  1118. if (display && displayer.owned) GDS_Update(display);
  1119. else if (!led_display) displayer.wake = LONG_WAKE;
  1120. // release semaphore and sleep what's needed
  1121. xSemaphoreGive(displayer.mutex);
  1122. sleep = min(displayer.wake, scroller.wake);
  1123. vTaskDelay(sleep / portTICK_PERIOD_MS);
  1124. scroller.wake -= sleep;
  1125. displayer.wake -= sleep;
  1126. }
  1127. }