display.c 28 KB

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