display.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. if (display_bus_chain) return (*display_bus_chain)(from, cmd);
  237. else return true;
  238. }
  239. /****************************************************************************************
  240. * Send message to server (ANIC at that time)
  241. */
  242. static void send_server(void) {
  243. /*
  244. This complication is needed as we cannot send direclty to LMS, because
  245. send_packet is not thread safe. So must subscribe to slimproto busy loop
  246. end send from there
  247. */
  248. if (ANIC_resp != ANIM_NONE) {
  249. struct ANIC_header pkt_header;
  250. memset(&pkt_header, 0, sizeof(pkt_header));
  251. memcpy(&pkt_header.opcode, "ANIC", 4);
  252. pkt_header.length = htonl(sizeof(pkt_header) - 8);
  253. pkt_header.mode = ANIC_resp;
  254. send_packet((u8_t *)&pkt_header, sizeof(pkt_header));
  255. ANIC_resp = ANIM_NONE;
  256. }
  257. if (SETD_width) {
  258. struct SETD_header pkt_header;
  259. LOG_INFO("sending width %u", SETD_width);
  260. memset(&pkt_header, 0, sizeof(pkt_header));
  261. memcpy(&pkt_header.opcode, "SETD", 4);
  262. pkt_header.id = 0xfe; // id 0xfe is width S:P:Squeezebox2
  263. pkt_header.length = htonl(sizeof(pkt_header) + 2 - 8);
  264. send_packet((u8_t *)&pkt_header, sizeof(pkt_header));
  265. send_packet(&SETD_width, 2);
  266. SETD_width = 0;
  267. }
  268. if (slimp_loop_chain) (*slimp_loop_chain)();
  269. }
  270. /****************************************************************************************
  271. *
  272. */
  273. static void server(in_addr_t ip, u16_t hport, u16_t cport) {
  274. char msg[32];
  275. sprintf(msg, "%s:%hu", inet_ntoa(ip), hport);
  276. if (displayer.owned) display->text(DISPLAY_FONT_DEFAULT, DISPLAY_CENTERED, DISPLAY_CLEAR | DISPLAY_UPDATE, msg);
  277. SETD_width = display->width;
  278. displayer.dirty = true;
  279. if (notify_chain) (*notify_chain)(ip, hport, cport);
  280. }
  281. /****************************************************************************************
  282. * Process graphic display data
  283. */
  284. static bool handler(u8_t *data, int len){
  285. bool res = true;
  286. if (!strncmp((char*) data, "vfdc", 4)) {
  287. vfdc_handler(data, len);
  288. } else if (!strncmp((char*) data, "grfe", 4)) {
  289. grfe_handler(data, len);
  290. } else if (!strncmp((char*) data, "grfb", 4)) {
  291. grfb_handler(data, len);
  292. } else if (!strncmp((char*) data, "grfs", 4)) {
  293. grfs_handler(data, len);
  294. } else if (!strncmp((char*) data, "grfg", 4)) {
  295. grfg_handler(data, len);
  296. } else if (!strncmp((char*) data, "visu", 4)) {
  297. visu_handler(data, len);
  298. } else {
  299. res = false;
  300. }
  301. // chain protocol handlers (bitwise or is fine)
  302. if (*slimp_handler_chain) res |= (*slimp_handler_chain)(data, len);
  303. return res;
  304. }
  305. /****************************************************************************************
  306. * Change special LCD chars to something more printable on screen
  307. */
  308. static void makeprintable(unsigned char * line) {
  309. for (int n = 0; n < LINELEN; n++) {
  310. switch (line[n]) {
  311. case 11: /* block */
  312. line[n] = '#';
  313. break;;
  314. case 16: /* rightarrow */
  315. line[n] = '>';
  316. break;;
  317. case 22: /* circle */
  318. line[n] = '@';
  319. break;;
  320. case 145: /* note */
  321. line[n] = ' ';
  322. break;;
  323. case 152: /* bell */
  324. line[n] = 'o';
  325. break;
  326. default:
  327. break;
  328. }
  329. }
  330. }
  331. /****************************************************************************************
  332. * Check if char is printable, or a valid symbol
  333. */
  334. static bool charisok(unsigned char c) {
  335. switch (c) {
  336. case 11: /* block */
  337. case 16: /* rightarrow */
  338. case 22: /* circle */
  339. case 145: /* note */
  340. case 152: /* bell */
  341. return true;
  342. break;;
  343. default:
  344. return isprint(c);
  345. }
  346. }
  347. /****************************************************************************************
  348. * Show the display (text mode)
  349. */
  350. static void show_display_buffer(char *ddram) {
  351. char line1[LINELEN+1];
  352. char *line2;
  353. memset(line1, 0, LINELEN+1);
  354. strncpy(line1, ddram, LINELEN);
  355. line2 = &(ddram[LINELEN]);
  356. line2[LINELEN] = '\0';
  357. /* Convert special LCD chars */
  358. makeprintable((unsigned char *)line1);
  359. makeprintable((unsigned char *)line2);
  360. LOG_DEBUG("\n\t%.40s\n\t%.40s", line1, line2);
  361. display->line(1, DISPLAY_LEFT, DISPLAY_CLEAR, line1);
  362. display->line(2, DISPLAY_LEFT, DISPLAY_CLEAR | DISPLAY_UPDATE, line2);
  363. }
  364. /****************************************************************************************
  365. * Process display data
  366. */
  367. static void vfdc_handler( u8_t *_data, int bytes_read) {
  368. unsigned short *data = (unsigned short*) _data, *display_data;
  369. char ddram[(LINELEN + 1) * 2];
  370. int n, addr = 0; /* counter */
  371. bytes_read -= 4;
  372. if (bytes_read % 2) bytes_read--; /* even number of bytes */
  373. // if we use Noritake VFD codes, display data starts at 12
  374. display_data = &(data[5]); /* display data starts at byte 10 */
  375. memset(ddram, ' ', LINELEN * 2);
  376. for (n = 0; n < (bytes_read/2); n++) {
  377. unsigned short d; /* data element */
  378. unsigned char t, c;
  379. d = ntohs(display_data[n]);
  380. t = (d & 0x00ff00) >> 8; /* type of display data */
  381. c = (d & 0x0000ff); /* character/command */
  382. switch (t) {
  383. case 0x03: /* character */
  384. if (!charisok(c)) c = ' ';
  385. if (addr <= LINELEN * 2) {
  386. ddram[addr++] = c;
  387. }
  388. break;
  389. case 0x02: /* command */
  390. switch (c) {
  391. case 0x06: /* display clear */
  392. memset(ddram, ' ', LINELEN * 2);
  393. break;
  394. case 0x02: /* cursor home */
  395. addr = 0;
  396. break;
  397. case 0xc0: /* cursor home2 */
  398. addr = LINELEN;
  399. break;
  400. }
  401. }
  402. }
  403. show_display_buffer(ddram);
  404. }
  405. /****************************************************************************************
  406. * Process graphic display data
  407. */
  408. static void grfe_handler( u8_t *data, int len) {
  409. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  410. scroller.active = false;
  411. // we are not in control or we are displaying visu on a small screen, do not do screen update
  412. if (visu.mode && !visu.col && visu.row < SB_HEIGHT) {
  413. xSemaphoreGive(displayer.mutex);
  414. return;
  415. }
  416. if (displayer.owned) {
  417. // did we have something that might have write on the bottom of a SB_HEIGHT+ display
  418. if (displayer.dirty) {
  419. display->clear(true);
  420. displayer.dirty = false;
  421. }
  422. // draw new frame
  423. display->draw_cbr(data + sizeof(struct grfe_packet), displayer.width, displayer.height);
  424. display->update();
  425. }
  426. xSemaphoreGive(displayer.mutex);
  427. LOG_DEBUG("grfe frame %u", len);
  428. }
  429. /****************************************************************************************
  430. * Brightness
  431. */
  432. static void grfb_handler(u8_t *data, int len) {
  433. struct grfb_packet *pkt = (struct grfb_packet*) data;
  434. pkt->brightness = htons(pkt->brightness);
  435. LOG_INFO("brightness %hu", pkt->brightness);
  436. if (pkt->brightness < 0) {
  437. display->on(false);
  438. } else {
  439. display->on(true);
  440. display->brightness(pkt->brightness);
  441. }
  442. }
  443. /****************************************************************************************
  444. * Scroll set
  445. */
  446. static void grfs_handler(u8_t *data, int len) {
  447. struct grfs_packet *pkt = (struct grfs_packet*) data;
  448. int size = len - sizeof(struct grfs_packet);
  449. int offset = htons(pkt->offset);
  450. LOG_DEBUG("gfrs s:%u d:%u p:%u sp:%u by:%hu m:%hu w:%hu o:%hu",
  451. (int) pkt->screen,
  452. (int) pkt->direction, // 1=left, 2=right
  453. htonl(pkt->pause), // in ms
  454. htonl(pkt->speed), // in ms
  455. htons(pkt->by), // # of pixel of scroll step
  456. htons(pkt->mode), // 0=continuous, 1=once and stop, 2=once and end
  457. htons(pkt->width), // last column of animation that contains a "full" screen
  458. htons(pkt->offset) // offset if multiple packets are sent
  459. );
  460. // new grfs frame, build scroller info
  461. if (!offset) {
  462. // use the display as a general lock
  463. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  464. // copy & set scroll parameters
  465. scroller.screen = pkt->screen;
  466. scroller.pause = htonl(pkt->pause);
  467. scroller.speed = htonl(pkt->speed);
  468. scroller.mode = htons(pkt->mode);
  469. scroller.scroll.width = htons(pkt->width);
  470. scroller.first = true;
  471. // background excludes space taken by visu (if any)
  472. scroller.back.width = displayer.width - ((visu.mode && visu.row < SB_HEIGHT) ? visu.width : 0);
  473. // set scroller steps & beginning
  474. if (pkt->direction == 1) {
  475. scroller.scrolled = 0;
  476. scroller.by = htons(pkt->by);
  477. } else {
  478. scroller.scrolled = scroller.scroll.width;
  479. scroller.by = -htons(pkt->by);
  480. }
  481. xSemaphoreGive(displayer.mutex);
  482. }
  483. // copy scroll frame data (no semaphore needed)
  484. if (scroller.scroll.size + size < scroller.scroll.max) {
  485. memcpy(scroller.scroll.frame + offset, data + sizeof(struct grfs_packet), size);
  486. scroller.scroll.size = offset + size;
  487. LOG_INFO("scroller current size %u", scroller.scroll.size);
  488. } else {
  489. LOG_INFO("scroller too larger %u/%u", scroller.scroll.size + size, scroller.scroll.max);
  490. }
  491. }
  492. /****************************************************************************************
  493. * Scroll background frame update & go
  494. */
  495. static void grfg_handler(u8_t *data, int len) {
  496. struct grfg_packet *pkt = (struct grfg_packet*) data;
  497. LOG_DEBUG("gfrg s:%hu w:%hu (len:%u)", htons(pkt->screen), htons(pkt->width), len);
  498. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  499. // size of scrollable area (less than background)
  500. scroller.width = htons(pkt->width);
  501. memcpy(scroller.back.frame, data + sizeof(struct grfg_packet), len - sizeof(struct grfg_packet));
  502. // update display asynchronously (frames are oganized by columns)
  503. memcpy(scroller.frame, scroller.back.frame, scroller.back.width * displayer.height / 8);
  504. for (int i = 0; i < scroller.width * displayer.height / 8; i++) scroller.frame[i] |= scroller.scroll.frame[scroller.scrolled * displayer.height / 8 + i];
  505. // can only write if we really own display
  506. if (displayer.owned) {
  507. display->draw_cbr(scroller.frame, scroller.back.width, displayer.height);
  508. display->update();
  509. }
  510. // now we can active scrolling, but only if we are not on a small screen
  511. if (!visu.mode || visu.col || visu.row >= SB_HEIGHT) scroller.active = true;
  512. // if we just got a content update, let the scroller manage the screen
  513. LOG_DEBUG("resuming scrolling task");
  514. xSemaphoreGive(displayer.mutex);
  515. // resume task once we have background, not in grfs
  516. vTaskResume(displayer.task);
  517. }
  518. /****************************************************************************************
  519. * Update visualization bars
  520. */
  521. static void visu_update(void) {
  522. // no need to protect against no woning the display as we are playing
  523. if (pthread_mutex_trylock(&visu_export.mutex)) return;
  524. // not enough samples
  525. if (visu_export.level < (visu.mode == VISU_VUMETER ? RMS_LEN : FFT_LEN) * 2 && visu_export.running) {
  526. pthread_mutex_unlock(&visu_export.mutex);
  527. return;
  528. }
  529. // reset bars for all cases first
  530. for (int i = visu.n; --i >= 0;) visu.bars[i].current = 0;
  531. if (visu_export.running && visu_export.running) {
  532. if (visu.mode == VISU_VUMETER) {
  533. s16_t *iptr = visu_export.buffer;
  534. // calculate sum(L²+R²), try to not overflow at the expense of some precision
  535. for (int i = RMS_LEN; --i >= 0;) {
  536. visu.bars[0].current += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
  537. iptr++;
  538. visu.bars[1].current += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
  539. iptr++;
  540. }
  541. // convert to dB (1 bit remaining for getting X²/N, 60dB dynamic starting from 0dBFS = 3 bits back-off)
  542. for (int i = visu.n; --i >= 0;) {
  543. visu.bars[i].current = 32 * (0.01667f*10*log10f(0.0000001f + (visu.bars[i].current >> 1)) - 0.2543f);
  544. if (visu.bars[i].current > 31) visu.bars[i].current = 31;
  545. else if (visu.bars[i].current < 0) visu.bars[i].current = 0;
  546. }
  547. } else {
  548. // on xtensa/esp32 the floating point FFT takes 1/2 cycles of the fixed point
  549. for (int i = 0 ; i < FFT_LEN ; i++) {
  550. // don't normalize here, but we are due INT16_MAX and FFT_LEN / 2 / 2
  551. visu.samples[i * 2 + 0] = (float) (visu_export.buffer[2*i] + visu_export.buffer[2*i + 1]) * visu.hanning[i];
  552. visu.samples[i * 2 + 1] = 0;
  553. }
  554. // actual FFT that might be less cycle than all the crap below
  555. dsps_fft2r_fc32_ae32(visu.samples, FFT_LEN);
  556. dsps_bit_rev_fc32_ansi(visu.samples, FFT_LEN);
  557. float rate = visu_export.rate;
  558. // now arrange the result with the number of bar and sampling rate (don't want DC)
  559. for (int i = 0, j = 1; i < visu.n && j < (FFT_LEN / 2); i++) {
  560. float power, count;
  561. // find the next point in FFT (this is real signal, so only half matters)
  562. for (count = 0, power = 0; j * visu_export.rate < visu.bars[i].limit * FFT_LEN && j < FFT_LEN / 2; j++, count += 1) {
  563. power += visu.samples[2*j] * visu.samples[2*j] + visu.samples[2*j+1] * visu.samples[2*j+1];
  564. }
  565. // due to sample rate, we have reached the end of the available spectrum
  566. if (j >= (FFT_LEN / 2)) {
  567. // normalize accumulated data
  568. if (count) power /= count * 2.;
  569. } else if (count) {
  570. // how much of what remains do we need to add
  571. float ratio = j - (visu.bars[i].limit * FFT_LEN) / rate;
  572. power += (visu.samples[2*j] * visu.samples[2*j] + visu.samples[2*j+1] * visu.samples[2*j+1]) * ratio;
  573. // normalize accumulated data
  574. power /= (count + ratio) * 2;
  575. } else {
  576. // no data for that band (sampling rate too high), just assume same as previous one
  577. power = (visu.samples[2*j] * visu.samples[2*j] + visu.samples[2*j+1] * visu.samples[2*j+1]) / 2.;
  578. }
  579. // convert to dB and bars, same back-off
  580. if (power) visu.bars[i].current = 32 * (0.01667f*10*(log10f(power) - log10f(FFT_LEN/2*2)) - 0.2543f);
  581. if (visu.bars[i].current > 31) visu.bars[i].current = 31;
  582. else if (visu.bars[i].current < 0) visu.bars[i].current = 0;
  583. }
  584. }
  585. }
  586. // we took what we want, we can release the buffer
  587. visu_export.level = 0;
  588. pthread_mutex_unlock(&visu_export.mutex);
  589. display->clear(false, false, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  590. for (int i = visu.n; --i >= 0;) {
  591. int x1 = visu.col + visu.border + visu.bar_border + i*(visu.bar_width + visu.bar_gap);
  592. int y1 = visu.row + visu.height - 1;
  593. if (visu.bars[i].current > visu.bars[i].max) visu.bars[i].max = visu.bars[i].current;
  594. else if (visu.bars[i].max) visu.bars[i].max--;
  595. for (int j = 0; j <= visu.bars[i].current; j += 2)
  596. display->draw_line( x1, y1 - j, x1 + visu.bar_width - 1, y1 - j);
  597. if (visu.bars[i].max > 2) {
  598. display->draw_line( x1, y1 - visu.bars[i].max, x1 + visu.bar_width - 1, y1 - visu.bars[i].max);
  599. display->draw_line( x1, y1 - visu.bars[i].max + 1, x1 + visu.bar_width - 1, y1 - visu.bars[i].max + 1);
  600. }
  601. }
  602. }
  603. /****************************************************************************************
  604. * Visu packet handler
  605. */
  606. void spectrum_limits(int min, int n, int pos) {
  607. if (n / 2) {
  608. int i;
  609. float step = (DISPLAY_BW - min) * visu.spectrum_scale / (n/2);
  610. visu.bars[pos].limit = min + step;
  611. for (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. }