display.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. /*
  2. * (c) 2004,2006 Richard Titmuss for SlimProtoLib
  3. * (c) Philippe G. 2019, philippe_44@outlook.com
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include <ctype.h>
  20. #include <math.h>
  21. #include "esp_dsp.h"
  22. #include "squeezelite.h"
  23. #include "slimproto.h"
  24. #include "display.h"
  25. #include "gds.h"
  26. #include "gds_text.h"
  27. #include "gds_draw.h"
  28. #include "gds_image.h"
  29. #pragma pack(push, 1)
  30. struct grfb_packet {
  31. char opcode[4];
  32. s16_t brightness;
  33. };
  34. struct grfe_packet {
  35. char opcode[4];
  36. u16_t offset;
  37. u8_t transition;
  38. u8_t param;
  39. };
  40. struct grfs_packet {
  41. char opcode[4];
  42. u8_t screen;
  43. u8_t direction; // 1=left, 2=right
  44. u32_t pause; // in ms
  45. u32_t speed; // in ms
  46. u16_t by; // # of pixel of scroll step
  47. u16_t mode; // 0=continuous, 1=once and stop, 2=once and end
  48. u16_t width; // total width of animation
  49. u16_t offset; // offset if multiple packets are sent
  50. };
  51. struct grfg_packet {
  52. char opcode[4];
  53. u16_t screen;
  54. u16_t width; // # of pixels of scrollable
  55. };
  56. struct grfa_packet {
  57. char opcode[4];
  58. u32_t length;
  59. u16_t x;
  60. u16_t y;
  61. u32_t offset;
  62. };
  63. struct visu_packet {
  64. char opcode[4];
  65. u8_t which;
  66. u8_t count;
  67. union {
  68. struct {
  69. u32_t bars;
  70. u32_t spectrum_scale;
  71. } full;
  72. struct {
  73. u32_t width;
  74. u32_t height;
  75. s32_t col;
  76. s32_t row;
  77. u32_t border;
  78. u32_t bars;
  79. u32_t spectrum_scale;
  80. };
  81. };
  82. };
  83. struct ANIC_header {
  84. char opcode[4];
  85. u32_t length;
  86. u8_t mode;
  87. };
  88. #pragma pack(pop)
  89. static struct {
  90. TaskHandle_t task;
  91. SemaphoreHandle_t mutex;
  92. int width, height;
  93. bool dirty;
  94. bool owned;
  95. } displayer = { .dirty = true, .owned = true };
  96. #define LONG_WAKE (10*1000)
  97. #define SB_HEIGHT 32
  98. // lenght are number of frames, i.e. 2 channels of 16 bits
  99. #define FFT_LEN_BIT 7
  100. #define FFT_LEN (1 << FFT_LEN_BIT)
  101. #define RMS_LEN_BIT 6
  102. #define RMS_LEN (1 << RMS_LEN_BIT)
  103. #define DISPLAY_BW 20000
  104. static struct scroller_s {
  105. // copy of grfs content
  106. u8_t screen;
  107. u32_t pause, speed;
  108. int wake;
  109. u16_t mode;
  110. s16_t by;
  111. // scroller management & sharing between grfg and scrolling task
  112. bool active, first, overflow;
  113. int scrolled;
  114. struct {
  115. u8_t *frame;
  116. u32_t width;
  117. u32_t max, size;
  118. } scroll;
  119. struct {
  120. u8_t *frame;
  121. u32_t width;
  122. } back;
  123. u8_t *frame;
  124. u32_t width;
  125. } scroller;
  126. static struct {
  127. u8_t *data;
  128. u32_t size;
  129. u16_t x, y;
  130. } artwork;
  131. #define MAX_BARS 32
  132. static EXT_RAM_ATTR struct {
  133. int bar_gap, bar_width, bar_border;
  134. struct {
  135. int current, max;
  136. int limit;
  137. } bars[MAX_BARS];
  138. float spectrum_scale;
  139. int n, col, row, height, width, border;
  140. enum { VISU_BLANK, VISU_VUMETER, VISU_SPECTRUM, VISU_WAVEFORM } mode;
  141. int speed, wake;
  142. float fft[FFT_LEN*2], samples[FFT_LEN*2], hanning[FFT_LEN];
  143. } visu;
  144. #define ANIM_NONE 0x00
  145. #define ANIM_TRANSITION 0x01 // A transition animation has finished
  146. #define ANIM_SCROLL_ONCE 0x02
  147. #define ANIM_SCREEN_1 0x04
  148. #define ANIM_SCREEN_2 0x08
  149. static u8_t ANIC_resp = ANIM_NONE;
  150. static uint16_t SETD_width;
  151. #define SCROLL_STACK_SIZE (3*1024)
  152. #define LINELEN 40
  153. static log_level loglevel = lINFO;
  154. static bool (*slimp_handler_chain)(u8_t *data, int len);
  155. static void (*slimp_loop_chain)(void);
  156. static void (*notify_chain)(in_addr_t ip, u16_t hport, u16_t cport);
  157. static bool (*display_bus_chain)(void *from, enum display_bus_cmd_e cmd);
  158. #define max(a,b) (((a) > (b)) ? (a) : (b))
  159. static void server(in_addr_t ip, u16_t hport, u16_t cport);
  160. static void send_server(void);
  161. static bool handler(u8_t *data, int len);
  162. static bool display_bus_handler(void *from, enum display_bus_cmd_e cmd);
  163. static void vfdc_handler( u8_t *_data, int bytes_read);
  164. static void grfe_handler( u8_t *data, int len);
  165. static void grfb_handler(u8_t *data, int len);
  166. static void grfs_handler(u8_t *data, int len);
  167. static void grfg_handler(u8_t *data, int len);
  168. static void grfa_handler(u8_t *data, int len);
  169. static void visu_handler(u8_t *data, int len);
  170. static void displayer_task(void* arg);
  171. /* scrolling undocumented information
  172. grfs
  173. B: screen number
  174. B:1 = left, 2 = right,
  175. Q: scroll pause once done (ms)
  176. Q: scroll speed (ms)
  177. W: # of pixels to scroll each time
  178. W: 0 = continue scrolling after pause, 1 = scroll to scrollend and then stop, 2 = scroll to scrollend and then end animation (causing new update)
  179. W: width of total scroll area in pixels
  180. grfd
  181. W: screen number
  182. W: width of scrollable area in pixels
  183. anic ( two versions, don't know what to chose)
  184. B: flag
  185. ANIM_TRANSITION (0x01) - transition animation has finished (previous use of ANIC)
  186. ANIM_SCREEN_1 (0x04) - end of first scroll on screen 1
  187. ANIM_SCREEN_2 (0x08) - end of first scroll on screen 2
  188. ANIM_SCROLL_ONCE (0x02) | ANIM_SCREEN_1 (0x04) - end of scroll once on screen 1
  189. ANIM_SCROLL_ONCE (0x02) | ANIM_SCREEN_2 (0x08) - end of scroll once on screen 2
  190. - or -
  191. ANIM_TRANSITION 0x01 # A transition animation has finished
  192. ANIM_SCROLL_ONCE 0x02 # A scrollonce has finished
  193. ANIM_SCREEN_1 0x04 # For scrollonce only, screen 1 was scrolling
  194. ANIM_SCREEN_2 0x08 # For scrollonce only, screen 2 was scrolling
  195. */
  196. /****************************************************************************************
  197. *
  198. */
  199. bool sb_display_init(void) {
  200. static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
  201. static EXT_RAM_ATTR StackType_t xStack[SCROLL_STACK_SIZE] __attribute__ ((aligned (4)));
  202. // no display, just make sure we won't have requests
  203. if (!display || GDS_GetWidth(display) <= 0 || GDS_GetHeight(display) <= 0) {
  204. LOG_INFO("no display for LMS");
  205. return false;
  206. }
  207. // need to force height to 32 maximum
  208. displayer.width = GDS_GetWidth(display);
  209. displayer.height = min(GDS_GetHeight(display), SB_HEIGHT);
  210. SETD_width = displayer.width;
  211. // create visu configuration
  212. visu.bar_gap = 1;
  213. visu.speed = 100;
  214. dsps_fft2r_init_fc32(visu.fft, FFT_LEN);
  215. dsps_wind_hann_f32(visu.hanning, FFT_LEN);
  216. // create scroll management task
  217. displayer.mutex = xSemaphoreCreateMutex();
  218. displayer.task = xTaskCreateStatic( (TaskFunction_t) displayer_task, "displayer_thread", SCROLL_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, &xTaskBuffer);
  219. // size scroller (width + current screen)
  220. scroller.scroll.max = (displayer.width * displayer.height / 8) * (15 + 1);
  221. scroller.scroll.frame = malloc(scroller.scroll.max);
  222. scroller.back.frame = malloc(displayer.width * displayer.height / 8);
  223. scroller.frame = malloc(displayer.width * displayer.height / 8);
  224. // chain handlers
  225. slimp_handler_chain = slimp_handler;
  226. slimp_handler = handler;
  227. slimp_loop_chain = slimp_loop;
  228. slimp_loop = send_server;
  229. notify_chain = server_notify;
  230. server_notify = server;
  231. display_bus_chain = display_bus;
  232. display_bus = display_bus_handler;
  233. return true;
  234. }
  235. /****************************************************************************************
  236. * Receive display bus commands
  237. */
  238. static bool display_bus_handler(void *from, enum display_bus_cmd_e cmd) {
  239. // don't answer to own requests
  240. if (from == &displayer) return false ;
  241. LOG_INFO("Display bus command %d", cmd);
  242. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  243. switch (cmd) {
  244. case DISPLAY_BUS_TAKE:
  245. displayer.owned = false;
  246. break;
  247. case DISPLAY_BUS_GIVE:
  248. displayer.owned = true;
  249. break;
  250. }
  251. xSemaphoreGive(displayer.mutex);
  252. // chain to rest of "bus"
  253. if (display_bus_chain) return (*display_bus_chain)(from, cmd);
  254. else return true;
  255. }
  256. /****************************************************************************************
  257. * Send message to server (ANIC at that time)
  258. */
  259. static void send_server(void) {
  260. /*
  261. This complication is needed as we cannot send direclty to LMS, because
  262. send_packet is not thread safe. So must subscribe to slimproto busy loop
  263. end send from there
  264. */
  265. if (ANIC_resp != ANIM_NONE) {
  266. struct ANIC_header pkt_header;
  267. memset(&pkt_header, 0, sizeof(pkt_header));
  268. memcpy(&pkt_header.opcode, "ANIC", 4);
  269. pkt_header.length = htonl(sizeof(pkt_header) - 8);
  270. pkt_header.mode = ANIC_resp;
  271. send_packet((uint8_t *) &pkt_header, sizeof(pkt_header));
  272. ANIC_resp = ANIM_NONE;
  273. }
  274. if (SETD_width) {
  275. struct SETD_header pkt_header;
  276. memset(&pkt_header, 0, sizeof(pkt_header));
  277. memcpy(&pkt_header.opcode, "SETD", 4);
  278. pkt_header.id = 0xfe; // id 0xfe is width S:P:Squeezebox2
  279. pkt_header.length = htonl(sizeof(pkt_header) + 4 - 8);
  280. u16_t height = GDS_GetHeight(display);
  281. LOG_INFO("sending dimension %ux%u", SETD_width, height);
  282. SETD_width = htons(SETD_width);
  283. height = htons(height);
  284. send_packet((uint8_t *) &pkt_header, sizeof(pkt_header));
  285. send_packet((uint8_t *) &SETD_width, 2);
  286. send_packet((uint8_t *) &height, 2);
  287. SETD_width = 0;
  288. }
  289. if (slimp_loop_chain) (*slimp_loop_chain)();
  290. }
  291. /****************************************************************************************
  292. *
  293. */
  294. static void server(in_addr_t ip, u16_t hport, u16_t cport) {
  295. char msg[32];
  296. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  297. sprintf(msg, "%s:%hu", inet_ntoa(ip), hport);
  298. if (displayer.owned) GDS_TextPos(display, GDS_FONT_DEFAULT, GDS_TEXT_CENTERED, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, msg);
  299. SETD_width = displayer.width;
  300. displayer.dirty = true;
  301. xSemaphoreGive(displayer.mutex);
  302. if (notify_chain) (*notify_chain)(ip, hport, cport);
  303. }
  304. /****************************************************************************************
  305. * Process graphic display data
  306. */
  307. static bool handler(u8_t *data, int len){
  308. bool res = true;
  309. if (!strncmp((char*) data, "vfdc", 4)) {
  310. vfdc_handler(data, len);
  311. } else if (!strncmp((char*) data, "grfe", 4)) {
  312. grfe_handler(data, len);
  313. } else if (!strncmp((char*) data, "grfb", 4)) {
  314. grfb_handler(data, len);
  315. } else if (!strncmp((char*) data, "grfs", 4)) {
  316. grfs_handler(data, len);
  317. } else if (!strncmp((char*) data, "grfg", 4)) {
  318. grfg_handler(data, len);
  319. } else if (!strncmp((char*) data, "grfa", 4)) {
  320. grfa_handler(data, len);
  321. } else if (!strncmp((char*) data, "visu", 4)) {
  322. visu_handler(data, len);
  323. } else {
  324. res = false;
  325. }
  326. // chain protocol handlers (bitwise or is fine)
  327. if (*slimp_handler_chain) res |= (*slimp_handler_chain)(data, len);
  328. return res;
  329. }
  330. /****************************************************************************************
  331. * Change special LCD chars to something more printable on screen
  332. */
  333. static void makeprintable(unsigned char * line) {
  334. for (int n = 0; n < LINELEN; n++) {
  335. switch (line[n]) {
  336. case 11: /* block */
  337. line[n] = '#';
  338. break;;
  339. case 16: /* rightarrow */
  340. line[n] = '>';
  341. break;;
  342. case 22: /* circle */
  343. line[n] = '@';
  344. break;;
  345. case 145: /* note */
  346. line[n] = ' ';
  347. break;;
  348. case 152: /* bell */
  349. line[n] = 'o';
  350. break;
  351. default:
  352. break;
  353. }
  354. }
  355. }
  356. /****************************************************************************************
  357. * Check if char is printable, or a valid symbol
  358. */
  359. static bool charisok(unsigned char c) {
  360. switch (c) {
  361. case 11: /* block */
  362. case 16: /* rightarrow */
  363. case 22: /* circle */
  364. case 145: /* note */
  365. case 152: /* bell */
  366. return true;
  367. break;;
  368. default:
  369. return isprint(c);
  370. }
  371. }
  372. /****************************************************************************************
  373. * Show the display (text mode)
  374. */
  375. static void show_display_buffer(char *ddram) {
  376. char line1[LINELEN+1];
  377. char *line2;
  378. memset(line1, 0, LINELEN+1);
  379. strncpy(line1, ddram, LINELEN);
  380. line2 = &(ddram[LINELEN]);
  381. line2[LINELEN] = '\0';
  382. /* Convert special LCD chars */
  383. makeprintable((unsigned char *)line1);
  384. makeprintable((unsigned char *)line2);
  385. LOG_DEBUG("\n\t%.40s\n\t%.40s", line1, line2);
  386. GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_CLEAR, line1);
  387. GDS_TextLine(display, 2, GDS_TEXT_LEFT, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, line2);
  388. }
  389. /****************************************************************************************
  390. * Process display data
  391. */
  392. static void vfdc_handler( u8_t *_data, int bytes_read) {
  393. unsigned short *data = (unsigned short*) _data, *display_data;
  394. char ddram[(LINELEN + 1) * 2];
  395. int n, addr = 0; /* counter */
  396. bytes_read -= 4;
  397. if (bytes_read % 2) bytes_read--; /* even number of bytes */
  398. // if we use Noritake VFD codes, display data starts at 12
  399. display_data = &(data[5]); /* display data starts at byte 10 */
  400. memset(ddram, ' ', LINELEN * 2);
  401. for (n = 0; n < (bytes_read/2); n++) {
  402. unsigned short d; /* data element */
  403. unsigned char t, c;
  404. d = ntohs(display_data[n]);
  405. t = (d & 0x00ff00) >> 8; /* type of display data */
  406. c = (d & 0x0000ff); /* character/command */
  407. switch (t) {
  408. case 0x03: /* character */
  409. if (!charisok(c)) c = ' ';
  410. if (addr <= LINELEN * 2) {
  411. ddram[addr++] = c;
  412. }
  413. break;
  414. case 0x02: /* command */
  415. switch (c) {
  416. case 0x06: /* display clear */
  417. memset(ddram, ' ', LINELEN * 2);
  418. break;
  419. case 0x02: /* cursor home */
  420. addr = 0;
  421. break;
  422. case 0xc0: /* cursor home2 */
  423. addr = LINELEN;
  424. break;
  425. }
  426. }
  427. }
  428. show_display_buffer(ddram);
  429. }
  430. /****************************************************************************************
  431. * Process graphic display data
  432. */
  433. static void grfe_handler( u8_t *data, int len) {
  434. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  435. scroller.active = false;
  436. // we are not in control or we are displaying visu on a small screen, do not do screen update
  437. if (visu.mode && !visu.col && visu.row < SB_HEIGHT) {
  438. xSemaphoreGive(displayer.mutex);
  439. return;
  440. }
  441. if (displayer.owned) {
  442. // did we have something that might have write on the bottom of a SB_HEIGHT+ display
  443. if (displayer.dirty) {
  444. GDS_ClearExt(display, true);
  445. displayer.dirty = false;
  446. }
  447. // draw new frame, it might be less than full screen (small visu)
  448. int width = ((len - sizeof(struct grfe_packet)) * 8) / displayer.height;
  449. GDS_DrawBitmapCBR(display, data + sizeof(struct grfe_packet), width, displayer.height, GDS_COLOR_WHITE);
  450. GDS_Update(display);
  451. }
  452. xSemaphoreGive(displayer.mutex);
  453. LOG_DEBUG("grfe frame %u", len);
  454. }
  455. /****************************************************************************************
  456. * Brightness
  457. */
  458. static void grfb_handler(u8_t *data, int len) {
  459. struct grfb_packet *pkt = (struct grfb_packet*) data;
  460. pkt->brightness = htons(pkt->brightness);
  461. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  462. if (pkt->brightness < 0) {
  463. GDS_DisplayOff(display);
  464. } else {
  465. GDS_DisplayOn(display);
  466. GDS_SetContrast(display, pkt->brightness);
  467. }
  468. xSemaphoreGive(displayer.mutex);
  469. LOG_INFO("brightness %hu", pkt->brightness);
  470. }
  471. /****************************************************************************************
  472. * Scroll set
  473. */
  474. static void grfs_handler(u8_t *data, int len) {
  475. struct grfs_packet *pkt = (struct grfs_packet*) data;
  476. int size = len - sizeof(struct grfs_packet);
  477. int offset = htons(pkt->offset);
  478. LOG_DEBUG("gfrs s:%u d:%u p:%u sp:%u by:%hu m:%hu w:%hu o:%hu",
  479. (int) pkt->screen,
  480. (int) pkt->direction, // 1=left, 2=right
  481. htonl(pkt->pause), // in ms
  482. htonl(pkt->speed), // in ms
  483. htons(pkt->by), // # of pixel of scroll step
  484. htons(pkt->mode), // 0=continuous, 1=once and stop, 2=once and end
  485. htons(pkt->width), // last column of animation that contains a "full" screen
  486. htons(pkt->offset) // offset if multiple packets are sent
  487. );
  488. // new grfs frame, build scroller info
  489. if (!offset) {
  490. // use the display as a general lock
  491. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  492. // copy & set scroll parameters
  493. scroller.screen = pkt->screen;
  494. scroller.pause = htonl(pkt->pause);
  495. scroller.speed = htonl(pkt->speed);
  496. scroller.mode = htons(pkt->mode);
  497. scroller.scroll.width = htons(pkt->width);
  498. scroller.first = true;
  499. scroller.overflow = false;
  500. // background excludes space taken by visu (if any)
  501. scroller.back.width = displayer.width - ((visu.mode && visu.row < SB_HEIGHT) ? visu.width : 0);
  502. // set scroller steps & beginning
  503. if (pkt->direction == 1) {
  504. scroller.scrolled = 0;
  505. scroller.by = htons(pkt->by);
  506. } else {
  507. scroller.scrolled = scroller.scroll.width;
  508. scroller.by = -htons(pkt->by);
  509. }
  510. xSemaphoreGive(displayer.mutex);
  511. }
  512. // copy scroll frame data (no semaphore needed)
  513. if (scroller.scroll.size + size < scroller.scroll.max && !scroller.overflow) {
  514. memcpy(scroller.scroll.frame + offset, data + sizeof(struct grfs_packet), size);
  515. scroller.scroll.size = offset + size;
  516. LOG_INFO("scroller current size %u (w:%u)", scroller.scroll.size, scroller.scroll.width);
  517. } else {
  518. LOG_INFO("scroller too large %u/%u (w:%u)", scroller.scroll.size + size, scroller.scroll.max, scroller.scroll.width);
  519. scroller.scroll.width = scroller.scroll.size / (displayer.height / 8) - scroller.back.width;
  520. scroller.overflow = true;
  521. }
  522. }
  523. /****************************************************************************************
  524. * Scroll background frame update & go
  525. */
  526. static void grfg_handler(u8_t *data, int len) {
  527. struct grfg_packet *pkt = (struct grfg_packet*) data;
  528. LOG_DEBUG("gfrg s:%hu w:%hu (len:%u)", htons(pkt->screen), htons(pkt->width), len);
  529. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  530. // size of scrollable area (less than background)
  531. scroller.width = htons(pkt->width);
  532. memcpy(scroller.back.frame, data + sizeof(struct grfg_packet), len - sizeof(struct grfg_packet));
  533. // update display asynchronously (frames are organized by columns)
  534. memcpy(scroller.frame, scroller.back.frame, scroller.back.width * displayer.height / 8);
  535. for (int i = 0; i < scroller.width * displayer.height / 8; i++) scroller.frame[i] |= scroller.scroll.frame[scroller.scrolled * displayer.height / 8 + i];
  536. // can only write if we really own display
  537. if (displayer.owned) {
  538. GDS_DrawBitmapCBR(display, scroller.frame, scroller.back.width, displayer.height, GDS_COLOR_WHITE);
  539. GDS_Update(display);
  540. }
  541. // now we can active scrolling, but only if we are not on a small screen
  542. if (!visu.mode || visu.col || visu.row >= SB_HEIGHT) scroller.active = true;
  543. // if we just got a content update, let the scroller manage the screen
  544. LOG_DEBUG("resuming scrolling task");
  545. xSemaphoreGive(displayer.mutex);
  546. // resume task once we have background, not in grfs
  547. vTaskResume(displayer.task);
  548. }
  549. /****************************************************************************************
  550. * Artwork
  551. */
  552. static void grfa_handler(u8_t *data, int len) {
  553. struct grfa_packet *pkt = (struct grfa_packet*) data;
  554. int size = len - sizeof(struct grfa_packet);
  555. int offset = htonl(pkt->offset);
  556. int length = htonl(pkt->length);
  557. // new grfa artwork, allocate memory
  558. if (!offset) {
  559. artwork.x = htons(pkt->x);
  560. artwork.y = htons(pkt->y);
  561. if (artwork.data) free(artwork.data);
  562. artwork.data = malloc(length);
  563. artwork.size = 0;
  564. }
  565. // copy artwork data
  566. memcpy(artwork.data + offset, data + sizeof(struct grfa_packet), size);
  567. artwork.size += size;
  568. if (artwork.size == length) {
  569. GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
  570. GDS_DrawJPEG(display, artwork.data, artwork.x, artwork.y, artwork.y < 32 ? (GDS_IMAGE_RIGHT | GDS_IMAGE_TOP) : GDS_IMAGE_CENTER);
  571. free(artwork.data);
  572. artwork.data = NULL;
  573. artwork.size = 0;
  574. }
  575. LOG_INFO("gfra l:%u x:%hu, y:%hu, o:%u s:%u", length, artwork.x, artwork.y, offset, size);
  576. }
  577. /****************************************************************************************
  578. * Update visualization bars
  579. */
  580. static void visu_update(void) {
  581. // no need to protect against no woning the display as we are playing
  582. if (pthread_mutex_trylock(&visu_export.mutex)) return;
  583. // not enough samples
  584. if (visu_export.level < (visu.mode == VISU_VUMETER ? RMS_LEN : FFT_LEN) * 2 && visu_export.running) {
  585. pthread_mutex_unlock(&visu_export.mutex);
  586. return;
  587. }
  588. // reset bars for all cases first
  589. for (int i = visu.n; --i >= 0;) visu.bars[i].current = 0;
  590. if (visu_export.running) {
  591. if (visu.mode == VISU_VUMETER) {
  592. s16_t *iptr = visu_export.buffer;
  593. // calculate sum(L²+R²), try to not overflow at the expense of some precision
  594. for (int i = RMS_LEN; --i >= 0;) {
  595. visu.bars[0].current += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
  596. iptr++;
  597. visu.bars[1].current += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
  598. iptr++;
  599. }
  600. // convert to dB (1 bit remaining for getting X²/N, 60dB dynamic starting from 0dBFS = 3 bits back-off)
  601. for (int i = visu.n; --i >= 0;) {
  602. visu.bars[i].current = 32 * (0.01667f*10*log10f(0.0000001f + (visu.bars[i].current >> 1)) - 0.2543f);
  603. if (visu.bars[i].current > 31) visu.bars[i].current = 31;
  604. else if (visu.bars[i].current < 0) visu.bars[i].current = 0;
  605. }
  606. } else {
  607. // on xtensa/esp32 the floating point FFT takes 1/2 cycles of the fixed point
  608. for (int i = 0 ; i < FFT_LEN ; i++) {
  609. // don't normalize here, but we are due INT16_MAX and FFT_LEN / 2 / 2
  610. visu.samples[i * 2 + 0] = (float) (visu_export.buffer[2*i] + visu_export.buffer[2*i + 1]) * visu.hanning[i];
  611. visu.samples[i * 2 + 1] = 0;
  612. }
  613. // actual FFT that might be less cycle than all the crap below
  614. dsps_fft2r_fc32_ae32(visu.samples, FFT_LEN);
  615. dsps_bit_rev_fc32_ansi(visu.samples, FFT_LEN);
  616. float rate = visu_export.rate;
  617. // now arrange the result with the number of bar and sampling rate (don't want DC)
  618. for (int i = 0, j = 1; i < visu.n && j < (FFT_LEN / 2); i++) {
  619. float power, count;
  620. // find the next point in FFT (this is real signal, so only half matters)
  621. for (count = 0, power = 0; j * visu_export.rate < visu.bars[i].limit * FFT_LEN && j < FFT_LEN / 2; j++, count += 1) {
  622. power += visu.samples[2*j] * visu.samples[2*j] + visu.samples[2*j+1] * visu.samples[2*j+1];
  623. }
  624. // due to sample rate, we have reached the end of the available spectrum
  625. if (j >= (FFT_LEN / 2)) {
  626. // normalize accumulated data
  627. if (count) power /= count * 2.;
  628. } else if (count) {
  629. // how much of what remains do we need to add
  630. float ratio = j - (visu.bars[i].limit * FFT_LEN) / rate;
  631. power += (visu.samples[2*j] * visu.samples[2*j] + visu.samples[2*j+1] * visu.samples[2*j+1]) * ratio;
  632. // normalize accumulated data
  633. power /= (count + ratio) * 2;
  634. } else {
  635. // no data for that band (sampling rate too high), just assume same as previous one
  636. power = (visu.samples[2*j] * visu.samples[2*j] + visu.samples[2*j+1] * visu.samples[2*j+1]) / 2.;
  637. }
  638. // convert to dB and bars, same back-off
  639. if (power) visu.bars[i].current = 32 * (0.01667f*10*(log10f(power) - log10f(FFT_LEN/2*2)) - 0.2543f);
  640. if (visu.bars[i].current > 31) visu.bars[i].current = 31;
  641. else if (visu.bars[i].current < 0) visu.bars[i].current = 0;
  642. }
  643. }
  644. }
  645. // we took what we want, we can release the buffer
  646. visu_export.level = 0;
  647. pthread_mutex_unlock(&visu_export.mutex);
  648. // don't refresh screen if all max are 0 (we were are somewhat idle)
  649. int clear = 0;
  650. for (int i = visu.n; --i >= 0;) clear = max(clear, visu.bars[i].max);
  651. if (clear) GDS_ClearExt(display, false, false, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  652. // there is much more optimization to be done here, like not redrawing bars unless needed
  653. for (int i = visu.n; --i >= 0;) {
  654. int x1 = visu.col + visu.border + visu.bar_border + i*(visu.bar_width + visu.bar_gap);
  655. int y1 = visu.row + visu.height - 1;
  656. if (visu.bars[i].current > visu.bars[i].max) visu.bars[i].max = visu.bars[i].current;
  657. else if (visu.bars[i].max) visu.bars[i].max--;
  658. else if (!clear) continue;
  659. for (int j = 0; j <= visu.bars[i].current; j += 2)
  660. GDS_DrawLine(display, x1, y1 - j, x1 + visu.bar_width - 1, y1 - j, GDS_COLOR_WHITE);
  661. if (visu.bars[i].max > 2) {
  662. GDS_DrawLine(display, x1, y1 - visu.bars[i].max, x1 + visu.bar_width - 1, y1 - visu.bars[i].max, GDS_COLOR_WHITE);
  663. GDS_DrawLine(display, x1, y1 - visu.bars[i].max + 1, x1 + visu.bar_width - 1, y1 - visu.bars[i].max + 1, GDS_COLOR_WHITE);
  664. }
  665. }
  666. }
  667. /****************************************************************************************
  668. * Visu packet handler
  669. */
  670. void spectrum_limits(int min, int n, int pos) {
  671. if (n / 2) {
  672. int step = ((DISPLAY_BW - min) * visu.spectrum_scale) / (n/2);
  673. visu.bars[pos].limit = min + step;
  674. for (int i = 1; i < n/2; i++) visu.bars[pos+i].limit = visu.bars[pos+i-1].limit + step;
  675. spectrum_limits(visu.bars[pos + n/2 - 1].limit, n - n/2, pos + n/2);
  676. } else {
  677. visu.bars[pos].limit = DISPLAY_BW;
  678. }
  679. }
  680. /****************************************************************************************
  681. * Visu packet handler
  682. */
  683. static void visu_handler( u8_t *data, int len) {
  684. struct visu_packet *pkt = (struct visu_packet*) data;
  685. int bars = 0;
  686. LOG_DEBUG("visu %u with %u parameters", pkt->which, pkt->count);
  687. /*
  688. If width is specified, then respect all coordinates, otherwise we try to
  689. use the bottom part of the display and if it is a small display, we overwrite
  690. text
  691. */
  692. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  693. visu.mode = pkt->which;
  694. // little trick to clean the taller screens when switching visu
  695. if (visu.row >= SB_HEIGHT) GDS_ClearExt(display, false, true, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  696. if (visu.mode) {
  697. if (pkt->count >= 4) {
  698. // small visu, then go were we are told to
  699. pkt->height = htonl(pkt->height);
  700. pkt->row = htonl(pkt->row);
  701. pkt->col = htonl(pkt->col);
  702. visu.width = htonl(pkt->width);
  703. visu.height = pkt->height ? pkt->height : SB_HEIGHT;
  704. visu.col = pkt->col < 0 ? displayer.width + pkt->col : pkt->col;
  705. visu.row = pkt->row < 0 ? GDS_GetHeight(display) + pkt->row : pkt->row;
  706. visu.border = htonl(pkt->border);
  707. bars = htonl(pkt->bars);
  708. visu.spectrum_scale = htonl(pkt->spectrum_scale) / 100.;
  709. // might have a race condition with scroller message, so update width in case
  710. if (scroller.active) scroller.back.width = displayer.width - visu.width;
  711. } else {
  712. // full screen visu, try to use bottom screen if available
  713. visu.width = displayer.width;
  714. visu.height = GDS_GetHeight(display) > SB_HEIGHT ? GDS_GetHeight(display) - SB_HEIGHT : GDS_GetHeight(display);
  715. visu.col = visu.border = 0;
  716. visu.row = GDS_GetHeight(display) - visu.height;
  717. bars = htonl(pkt->full.bars);
  718. visu.spectrum_scale = htonl(pkt->full.spectrum_scale) / 100.;
  719. }
  720. // try to adapt to what we have
  721. if (visu.mode == VISU_SPECTRUM) {
  722. visu.n = bars ? bars : MAX_BARS;
  723. if (visu.spectrum_scale <= 0 || visu.spectrum_scale > 0.5) visu.spectrum_scale = 0.5;
  724. spectrum_limits(0, visu.n, 0);
  725. } else {
  726. visu.n = 2;
  727. }
  728. do {
  729. visu.bar_width = (visu.width - visu.border - visu.bar_gap * (visu.n - 1)) / visu.n;
  730. if (visu.bar_width > 0) break;
  731. } while (--visu.n);
  732. visu.bar_border = (visu.width - visu.border - (visu.bar_width + visu.bar_gap) * visu.n + visu.bar_gap) / 2;
  733. // give up if not enough space
  734. if (visu.bar_width < 0) {
  735. visu.mode = VISU_BLANK;
  736. LOG_WARN("Not enough room for displaying visu");
  737. } else {
  738. // de-activate scroller if we are taking main screen
  739. if (visu.row < SB_HEIGHT) scroller.active = false;
  740. vTaskResume(displayer.task);
  741. }
  742. visu.wake = 0;
  743. // reset bars maximum
  744. for (int i = visu.n; --i >= 0;) visu.bars[i].max = 0;
  745. GDS_ClearExt(display, false, true, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  746. 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);
  747. } else {
  748. LOG_INFO("Stopping visualizer");
  749. }
  750. xSemaphoreGive(displayer.mutex);
  751. }
  752. /****************************************************************************************
  753. * Scroll task
  754. * - with the addition of the visualizer, it's a bit a 2-headed beast not easy to
  755. * maintain, so som better separation between the visu and scroll is probably needed
  756. */
  757. static void displayer_task(void *args) {
  758. int sleep;
  759. while (1) {
  760. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  761. // suspend ourselves if nothing to do, grfg or visu will wake us up
  762. if (!scroller.active && !visu.mode) {
  763. xSemaphoreGive(displayer.mutex);
  764. vTaskSuspend(NULL);
  765. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  766. scroller.wake = visu.wake = 0;
  767. }
  768. // go for long sleep when either item is disabled
  769. if (!visu.mode) visu.wake = LONG_WAKE;
  770. if (!scroller.active) scroller.wake = LONG_WAKE;
  771. // scroll required amount of columns (within the window)
  772. if (scroller.active && scroller.wake <= 0) {
  773. // by default go for the long sleep, will change below if required
  774. scroller.wake = LONG_WAKE;
  775. // do we have more to scroll (scroll.width is the last column from which we have a full zone)
  776. if (scroller.by > 0 ? (scroller.scrolled <= scroller.scroll.width) : (scroller.scrolled >= 0)) {
  777. memcpy(scroller.frame, scroller.back.frame, scroller.back.width * displayer.height / 8);
  778. for (int i = 0; i < scroller.width * displayer.height / 8; i++) scroller.frame[i] |= scroller.scroll.frame[scroller.scrolled * displayer.height / 8 + i];
  779. scroller.scrolled += scroller.by;
  780. if (displayer.owned) GDS_DrawBitmapCBR(display, scroller.frame, scroller.width, displayer.height, GDS_COLOR_WHITE);
  781. // short sleep & don't need background update
  782. scroller.wake = scroller.speed;
  783. } else if (scroller.first || !scroller.mode) {
  784. // at least one round done
  785. scroller.first = false;
  786. // see if we need to pause or if we are done
  787. if (scroller.mode) {
  788. // can't call directly send_packet from slimproto as it's not re-entrant
  789. ANIC_resp = ANIM_SCROLL_ONCE | ANIM_SCREEN_1;
  790. LOG_INFO("scroll-once terminated");
  791. } else {
  792. scroller.wake = scroller.pause;
  793. LOG_DEBUG("scroll cycle done, pausing for %u (ms)", scroller.pause);
  794. }
  795. // need to reset pointers for next scroll
  796. scroller.scrolled = scroller.by < 0 ? scroller.scroll.width : 0;
  797. }
  798. }
  799. // update visu if active
  800. if (visu.mode && visu.wake <= 0) {
  801. visu_update();
  802. visu.wake = 100;
  803. }
  804. // need to make sure we own display
  805. if (displayer.owned) GDS_Update(display);
  806. // release semaphore and sleep what's needed
  807. xSemaphoreGive(displayer.mutex);
  808. sleep = min(visu.wake, scroller.wake);
  809. vTaskDelay(sleep / portTICK_PERIOD_MS);
  810. scroller.wake -= sleep;
  811. visu.wake -= sleep;
  812. }
  813. }