display.c 29 KB

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