display.c 29 KB

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