displayer.c 41 KB

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