display.c 26 KB

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