display.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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. // done in any case
  688. return;
  689. }
  690. // new grfa artwork, allocate memory
  691. if (!offset) {
  692. // same trick to clean current/previous window
  693. if (artwork.size) {
  694. GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
  695. artwork.size = 0;
  696. }
  697. // now use new parameters
  698. artwork.x = htons(pkt->x);
  699. artwork.y = htons(pkt->y);
  700. if (artwork.data) free(artwork.data);
  701. artwork.data = malloc(length);
  702. }
  703. // copy artwork data
  704. memcpy(artwork.data + offset, data + sizeof(struct grfa_packet), size);
  705. artwork.size += size;
  706. if (artwork.size == length) {
  707. GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
  708. GDS_DrawJPEG(display, artwork.data, artwork.x, artwork.y, artwork.y < displayer.height ? (GDS_IMAGE_RIGHT | GDS_IMAGE_TOP) : GDS_IMAGE_CENTER);
  709. free(artwork.data);
  710. artwork.data = NULL;
  711. }
  712. LOG_INFO("gfra l:%u x:%hu, y:%hu, o:%u s:%u", length, artwork.x, artwork.y, offset, size);
  713. }
  714. /****************************************************************************************
  715. * Update visualization bars
  716. */
  717. static void visu_update(void) {
  718. // no update when artwork is full screen (but no need to protect against not owning the display as we are playing
  719. if ((artwork.enable && artwork.x == 0 && artwork.y == 0) || pthread_mutex_trylock(&visu_export.mutex)) {
  720. return;
  721. }
  722. int mode = visu.mode & ~VISU_ESP32;
  723. // not enough samples
  724. if (visu_export.level < (mode == VISU_VUMETER ? RMS_LEN : FFT_LEN) * 2 && visu_export.running) {
  725. pthread_mutex_unlock(&visu_export.mutex);
  726. return;
  727. }
  728. // reset bars for all cases first
  729. for (int i = visu.n; --i >= 0;) visu.bars[i].current = 0;
  730. if (visu_export.running) {
  731. if (mode == VISU_VUMETER) {
  732. s16_t *iptr = visu_export.buffer;
  733. // calculate sum(L²+R²), try to not overflow at the expense of some precision
  734. for (int i = RMS_LEN; --i >= 0;) {
  735. visu.bars[0].current += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
  736. iptr++;
  737. visu.bars[1].current += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
  738. iptr++;
  739. }
  740. // convert to dB (1 bit remaining for getting X²/N, 60dB dynamic starting from 0dBFS = 3 bits back-off)
  741. for (int i = visu.n; --i >= 0;) {
  742. 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);
  743. if (visu.bars[i].current > visu.max) visu.bars[i].current = visu.max;
  744. else if (visu.bars[i].current < 0) visu.bars[i].current = 0;
  745. }
  746. } else {
  747. // on xtensa/esp32 the floating point FFT takes 1/2 cycles of the fixed point
  748. for (int i = 0 ; i < FFT_LEN ; i++) {
  749. // don't normalize here, but we are due INT16_MAX and FFT_LEN / 2 / 2
  750. visu.samples[i * 2 + 0] = (float) (visu_export.buffer[2*i] + visu_export.buffer[2*i + 1]) * visu.hanning[i];
  751. visu.samples[i * 2 + 1] = 0;
  752. }
  753. // actual FFT that might be less cycle than all the crap below
  754. dsps_fft2r_fc32_ae32(visu.samples, FFT_LEN);
  755. dsps_bit_rev_fc32_ansi(visu.samples, FFT_LEN);
  756. float rate = visu_export.rate;
  757. // now arrange the result with the number of bar and sampling rate (don't want DC)
  758. for (int i = 0, j = 1; i < visu.n && j < (FFT_LEN / 2); i++) {
  759. float power, count;
  760. // find the next point in FFT (this is real signal, so only half matters)
  761. for (count = 0, power = 0; j * visu_export.rate < visu.bars[i].limit * FFT_LEN && j < FFT_LEN / 2; j++, count += 1) {
  762. power += visu.samples[2*j] * visu.samples[2*j] + visu.samples[2*j+1] * visu.samples[2*j+1];
  763. }
  764. // due to sample rate, we have reached the end of the available spectrum
  765. if (j >= (FFT_LEN / 2)) {
  766. // normalize accumulated data
  767. if (count) power /= count * 2.;
  768. } else if (count) {
  769. // how much of what remains do we need to add
  770. float ratio = j - (visu.bars[i].limit * FFT_LEN) / rate;
  771. power += (visu.samples[2*j] * visu.samples[2*j] + visu.samples[2*j+1] * visu.samples[2*j+1]) * ratio;
  772. // normalize accumulated data
  773. power /= (count + ratio) * 2;
  774. } else {
  775. // no data for that band (sampling rate too high), just assume same as previous one
  776. power = (visu.samples[2*j] * visu.samples[2*j] + visu.samples[2*j+1] * visu.samples[2*j+1]) / 2.;
  777. }
  778. // convert to dB and bars, same back-off
  779. 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);
  780. if (visu.bars[i].current > visu.max) visu.bars[i].current = visu.max;
  781. else if (visu.bars[i].current < 0) visu.bars[i].current = 0;
  782. }
  783. }
  784. }
  785. // we took what we want, we can release the buffer
  786. visu_export.level = 0;
  787. pthread_mutex_unlock(&visu_export.mutex);
  788. // don't refresh screen if all max are 0 (we were are somewhat idle)
  789. int clear = 0;
  790. for (int i = visu.n; --i >= 0;) clear = max(clear, visu.bars[i].max);
  791. if (clear) GDS_ClearExt(display, false, false, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  792. // draw background if we are in screensaver mode
  793. if (!(visu.mode & VISU_ESP32) && visu.back.active) {
  794. GDS_DrawBitmapCBR(display, visu.back.frame, visu.back.width, displayer.height, GDS_COLOR_WHITE);
  795. }
  796. if (mode != VISU_VUMETER || !visu.style) {
  797. // there is much more optimization to be done here, like not redrawing bars unless needed
  798. for (int i = visu.n; --i >= 0;) {
  799. // update maximum
  800. if (visu.bars[i].current > visu.bars[i].max) visu.bars[i].max = visu.bars[i].current;
  801. else if (visu.bars[i].max) visu.bars[i].max--;
  802. else if (!clear) continue;
  803. if (visu.rotate) {
  804. int x1 = visu.col;
  805. int y1 = visu.row + visu.border + visu.bar_border + i*(visu.bar_width + visu.bar_gap);
  806. for (int j = 0; j <= visu.bars[i].current; j += 2)
  807. GDS_DrawLine(display, x1 + j, y1, x1 + j, y1 + visu.bar_width - 1, GDS_COLOR_WHITE);
  808. if (visu.bars[i].max > 2) {
  809. GDS_DrawLine(display, x1 + visu.bars[i].max, y1, x1 + visu.bars[i].max, y1 + visu.bar_width - 1, GDS_COLOR_WHITE);
  810. 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);
  811. }
  812. } else {
  813. int x1 = visu.col + visu.border + visu.bar_border + i*(visu.bar_width + visu.bar_gap);
  814. int y1 = visu.row + visu.height - 1;
  815. for (int j = 0; j <= visu.bars[i].current; j += 2)
  816. GDS_DrawLine(display, x1, y1 - j, x1 + visu.bar_width - 1, y1 - j, GDS_COLOR_WHITE);
  817. if (visu.bars[i].max > 2) {
  818. GDS_DrawLine(display, x1, y1 - visu.bars[i].max, x1 + visu.bar_width - 1, y1 - visu.bars[i].max, GDS_COLOR_WHITE);
  819. 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);
  820. }
  821. }
  822. }
  823. } else if (displayer.width / 2 > 3 * VU_WIDTH / 4) {
  824. if (visu.rotate) {
  825. draw_VU(display, vu_bitmap, visu.bars[0].current, 0, visu.row, visu.height / 2, visu.rotate);
  826. draw_VU(display, vu_bitmap, visu.bars[1].current, 0, visu.row + visu.height / 2, visu.height / 2, visu.rotate);
  827. } else {
  828. draw_VU(display, vu_bitmap, visu.bars[0].current, 0, visu.row, visu.width / 2, visu.rotate);
  829. draw_VU(display, vu_bitmap, visu.bars[1].current, visu.width / 2, visu.row, visu.width / 2, visu.rotate);
  830. }
  831. } else {
  832. int level = (visu.bars[0].current + visu.bars[1].current) / 2;
  833. draw_VU(display, vu_bitmap, level, 0, visu.row, visu.rotate ? visu.height : visu.width, visu.rotate);
  834. }
  835. }
  836. /****************************************************************************************
  837. * Calculate spectrum spread
  838. */
  839. static void spectrum_limits(int min, int n, int pos) {
  840. if (n / 2) {
  841. int step = ((DISPLAY_BW - min) * visu.spectrum_scale) / (n/2);
  842. visu.bars[pos].limit = min + step;
  843. for (int i = 1; i < n/2; i++) visu.bars[pos+i].limit = visu.bars[pos+i-1].limit + step;
  844. spectrum_limits(visu.bars[pos + n/2 - 1].limit, n - n/2, pos + n/2);
  845. } else {
  846. visu.bars[pos].limit = DISPLAY_BW;
  847. }
  848. }
  849. /****************************************************************************************
  850. * Fit visu
  851. */
  852. static void visu_fit(int bars, int width, int height) {
  853. // try to adapt to what we have
  854. if ((visu.mode & ~VISU_ESP32) == VISU_SPECTRUM) {
  855. visu.n = bars ? bars : MAX_BARS;
  856. visu.max = height - 1;
  857. if (visu.spectrum_scale <= 0 || visu.spectrum_scale > 0.5) visu.spectrum_scale = 0.5;
  858. spectrum_limits(0, visu.n, 0);
  859. } else {
  860. visu.n = 2;
  861. visu.max = (visu.style ? VU_COUNT : height) - 1;
  862. }
  863. do {
  864. visu.bar_width = (width - visu.border - visu.bar_gap * (visu.n - 1)) / visu.n;
  865. if (visu.bar_width > 0) break;
  866. } while (--visu.n);
  867. visu.bar_border = (width - visu.border - (visu.bar_width + visu.bar_gap) * visu.n + visu.bar_gap) / 2;
  868. }
  869. /****************************************************************************************
  870. * Visu packet handler
  871. */
  872. static void visu_handler( u8_t *data, int len) {
  873. struct visu_packet *pkt = (struct visu_packet*) data;
  874. int bars = 0;
  875. LOG_DEBUG("visu %u with %u parameters", pkt->which, pkt->count);
  876. /*
  877. If width is specified, then respect all coordinates, otherwise we try to
  878. use the bottom part of the display and if it is a small display, we overwrite
  879. text
  880. */
  881. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  882. visu.mode = pkt->which;
  883. // little trick to clean the taller screens when switching visu
  884. if (visu.row >= displayer.height) GDS_ClearExt(display, false, true, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  885. if (visu.mode) {
  886. // these will be overidden if necessary
  887. visu.col = visu.border = 0;
  888. visu.rotate = false;
  889. // what type of visu
  890. if (visu.mode & VISU_ESP32) {
  891. if (pkt->count >= 4) {
  892. // more than 4 parameters, this is small visu, then go were we are told to
  893. pkt->height = htonl(pkt->height);
  894. pkt->row = htonl(pkt->row);
  895. pkt->col = htonl(pkt->col);
  896. visu.style = 0;
  897. visu.width = htonl(pkt->width);
  898. visu.height = pkt->height ? pkt->height : displayer.height;
  899. visu.col = pkt->col < 0 ? displayer.width + pkt->col : pkt->col;
  900. visu.row = pkt->row < 0 ? GDS_GetHeight(display) + pkt->row : pkt->row;
  901. visu.border = htonl(pkt->border);
  902. bars = htonl(pkt->bars);
  903. visu.spectrum_scale = htonl(pkt->spectrum_scale) / 100.;
  904. } else {
  905. // full screen visu, try to use bottom screen if available
  906. visu.width = htonl(pkt->full.width);
  907. visu.height = GDS_GetHeight(display) > displayer.height ? GDS_GetHeight(display) - displayer.height : GDS_GetHeight(display);
  908. visu.row = GDS_GetHeight(display) - visu.height;
  909. // try to estimate if we should rotate visu
  910. if (visu.height > displayer.height && visu.height > 2*visu.width) visu.rotate = true;
  911. // is this spectrum or analogue/digital
  912. if ((visu.mode & ~VISU_ESP32) == VISU_SPECTRUM) {
  913. bars = htonl(pkt->full.bars);
  914. visu.spectrum_scale = htonl(pkt->full.spectrum_scale) / 100.;
  915. } else {
  916. // select analogue/digital style
  917. visu.style = htonl(pkt->full.style);
  918. }
  919. }
  920. } else {
  921. // classical (screensaver) mode, don't try to optimize screen usage & force some params
  922. visu.row = 0;
  923. visu.height = GDS_GetHeight(display);
  924. visu.width = displayer.width;
  925. visu.spectrum_scale = 0.25;
  926. if (visu.mode == VISU_SPECTRUM) {
  927. bars = visu.width / (htonl(pkt->channels[0].bar_width) + htonl(pkt->channels[0].bar_space));
  928. } else {
  929. visu.style = htonl(pkt->classical_vu.style);
  930. if (visu.style) visu.row = visu.height - VU_HEIGHT;
  931. }
  932. if (bars > MAX_BARS) bars = MAX_BARS;
  933. }
  934. // for rotate, swap width & height
  935. if (visu.rotate) visu_fit(bars, visu.height, visu.width);
  936. else visu_fit(bars, visu.width, visu.height);
  937. // give up if not enough space
  938. if (visu.bar_width < 0) {
  939. visu.mode = VISU_BLANK;
  940. LOG_WARN("Not enough room for displaying visu");
  941. } else {
  942. // de-activate scroller if we are taking main screen
  943. if (visu.row < displayer.height) scroller.active = false;
  944. vTaskResume(displayer.task);
  945. }
  946. visu.wake = 0;
  947. // reset bars maximum
  948. for (int i = visu.n; --i >= 0;) visu.bars[i].max = 0;
  949. GDS_ClearExt(display, false, true, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  950. 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);
  951. } else {
  952. LOG_INFO("Stopping visualizer");
  953. }
  954. xSemaphoreGive(displayer.mutex);
  955. }
  956. /****************************************************************************************
  957. * Scroll task
  958. * - with the addition of the visualizer, it's a bit a 2-headed beast not easy to
  959. * maintain, so som better separation between the visu and scroll is probably needed
  960. */
  961. static void displayer_task(void *args) {
  962. int sleep;
  963. while (1) {
  964. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  965. // suspend ourselves if nothing to do, grfg or visu will wake us up
  966. if (!scroller.active && !visu.mode) {
  967. xSemaphoreGive(displayer.mutex);
  968. vTaskSuspend(NULL);
  969. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  970. scroller.wake = visu.wake = 0;
  971. }
  972. // go for long sleep when either item is disabled
  973. if (!visu.mode) visu.wake = LONG_WAKE;
  974. if (!scroller.active) scroller.wake = LONG_WAKE;
  975. // scroll required amount of columns (within the window)
  976. if (scroller.active && scroller.wake <= 0) {
  977. // by default go for the long sleep, will change below if required
  978. scroller.wake = LONG_WAKE;
  979. // do we have more to scroll (scroll.width is the last column from which we have a full zone)
  980. if (scroller.by > 0 ? (scroller.scrolled <= scroller.scroll.width) : (scroller.scrolled >= 0)) {
  981. memcpy(scroller.frame, scroller.back.frame, scroller.back.width * displayer.height / 8);
  982. for (int i = 0; i < scroller.width * displayer.height / 8; i++) scroller.frame[i] |= scroller.scroll.frame[scroller.scrolled * displayer.height / 8 + i];
  983. scroller.scrolled += scroller.by;
  984. if (displayer.owned) GDS_DrawBitmapCBR(display, scroller.frame, scroller.width, displayer.height, GDS_COLOR_WHITE);
  985. // short sleep & don't need background update
  986. scroller.wake = scroller.speed;
  987. } else if (scroller.first || !scroller.mode) {
  988. // at least one round done
  989. scroller.first = false;
  990. // see if we need to pause or if we are done
  991. if (scroller.mode) {
  992. sendANIC(ANIM_SCROLL_ONCE | ANIM_SCREEN_1);
  993. LOG_INFO("scroll-once terminated");
  994. } else {
  995. scroller.wake = scroller.pause;
  996. LOG_DEBUG("scroll cycle done, pausing for %u (ms)", scroller.pause);
  997. }
  998. // need to reset pointers for next scroll
  999. scroller.scrolled = scroller.by < 0 ? scroller.scroll.width : 0;
  1000. }
  1001. }
  1002. // update visu if active
  1003. if (visu.mode && visu.wake <= 0) {
  1004. visu_update();
  1005. visu.wake = 100;
  1006. }
  1007. // need to make sure we own display
  1008. if (displayer.owned) GDS_Update(display);
  1009. // release semaphore and sleep what's needed
  1010. xSemaphoreGive(displayer.mutex);
  1011. sleep = min(visu.wake, scroller.wake);
  1012. vTaskDelay(sleep / portTICK_PERIOD_MS);
  1013. scroller.wake -= sleep;
  1014. visu.wake -= sleep;
  1015. }
  1016. }