display.c 27 KB

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