display.c 36 KB

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