display.c 38 KB

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