displayer.c 41 KB

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