display.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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. #include "gds_image.h"
  29. #pragma pack(push, 1)
  30. struct grfb_packet {
  31. char opcode[4];
  32. s16_t brightness;
  33. };
  34. struct grfe_packet {
  35. char opcode[4];
  36. u16_t offset;
  37. u8_t transition;
  38. u8_t param;
  39. };
  40. struct grfs_packet {
  41. char opcode[4];
  42. u8_t screen;
  43. u8_t direction; // 1=left, 2=right
  44. u32_t pause; // in ms
  45. u32_t speed; // in ms
  46. u16_t by; // # of pixel of scroll step
  47. u16_t mode; // 0=continuous, 1=once and stop, 2=once and end
  48. u16_t width; // total width of animation
  49. u16_t offset; // offset if multiple packets are sent
  50. };
  51. struct grfg_packet {
  52. char opcode[4];
  53. u16_t screen;
  54. u16_t width; // # of pixels of scrollable
  55. };
  56. struct grfa_packet {
  57. char opcode[4];
  58. u32_t length;
  59. u16_t x;
  60. u16_t y;
  61. u32_t offset;
  62. };
  63. struct visu_packet {
  64. char opcode[4];
  65. u8_t which;
  66. u8_t count;
  67. union {
  68. union {
  69. struct {
  70. u32_t bars;
  71. u32_t spectrum_scale;
  72. };
  73. u32_t style;
  74. } full;
  75. struct {
  76. u32_t width;
  77. u32_t height;
  78. s32_t col;
  79. s32_t row;
  80. u32_t border;
  81. u32_t bars;
  82. u32_t spectrum_scale;
  83. };
  84. struct {
  85. u32_t mono;
  86. u32_t bandwidth;
  87. u32_t preemph;
  88. struct {
  89. u32_t pos;
  90. u32_t width;
  91. u32_t orient;
  92. u32_t bar_width;
  93. u32_t bar_space;
  94. u32_t clipping;
  95. u32_t bar_intens;
  96. u32_t bar_cap_intens;
  97. } channels[2];
  98. };
  99. struct {
  100. u32_t mono;
  101. u32_t style;
  102. struct {
  103. u32_t pos;
  104. u32_t width;
  105. } channels[2];
  106. } classical_vu;
  107. };
  108. };
  109. struct ANIC_header {
  110. char opcode[4];
  111. u32_t length;
  112. u8_t mode;
  113. };
  114. #pragma pack(pop)
  115. static struct {
  116. TaskHandle_t task;
  117. SemaphoreHandle_t mutex;
  118. int width, height;
  119. bool dirty;
  120. bool owned;
  121. } displayer = { .dirty = true, .owned = true };
  122. #define LONG_WAKE (10*1000)
  123. #define SB_HEIGHT 32
  124. // lenght are number of frames, i.e. 2 channels of 16 bits
  125. #define FFT_LEN_BIT 7
  126. #define FFT_LEN (1 << FFT_LEN_BIT)
  127. #define RMS_LEN_BIT 6
  128. #define RMS_LEN (1 << RMS_LEN_BIT)
  129. #define VU_WIDTH 160
  130. #define VU_HEIGHT SB_HEIGHT
  131. #define VU_COUNT 48
  132. #define DISPLAY_BW 20000
  133. static struct scroller_s {
  134. // copy of grfs content
  135. u8_t screen;
  136. u32_t pause, speed;
  137. int wake;
  138. u16_t mode;
  139. s16_t by;
  140. // scroller management & sharing between grfg and scrolling task
  141. bool active, first, overflow;
  142. int scrolled;
  143. struct {
  144. u8_t *frame;
  145. u32_t width;
  146. u32_t max, size;
  147. } scroll;
  148. struct {
  149. u8_t *frame;
  150. u32_t width;
  151. } back;
  152. u8_t *frame;
  153. u32_t width;
  154. } scroller;
  155. static struct {
  156. u8_t *data;
  157. u32_t size;
  158. u16_t x, y;
  159. bool enable;
  160. } artwork;
  161. #define MAX_BARS 32
  162. #define VISU_ESP32 0x10
  163. static EXT_RAM_ATTR struct {
  164. int bar_gap, bar_width, bar_border;
  165. struct {
  166. int current, max;
  167. int limit;
  168. } bars[MAX_BARS];
  169. float spectrum_scale;
  170. int n, col, row, height, width, border, style, max;
  171. enum { VISU_BLANK, VISU_VUMETER, VISU_SPECTRUM, VISU_WAVEFORM } mode;
  172. int speed, wake;
  173. float fft[FFT_LEN*2], samples[FFT_LEN*2], hanning[FFT_LEN];
  174. struct {
  175. u8_t *frame;
  176. int width;
  177. bool active;
  178. } back;
  179. } visu;
  180. extern const uint8_t vu_bitmap[] asm("_binary_vu_data_start");
  181. #define ANIM_NONE 0x00
  182. #define ANIM_TRANSITION 0x01 // A transition animation has finished
  183. #define ANIM_SCROLL_ONCE 0x02
  184. #define ANIM_SCREEN_1 0x04
  185. #define ANIM_SCREEN_2 0x08
  186. static u8_t ANIC_resp = ANIM_NONE;
  187. static uint16_t SETD_width;
  188. #define SCROLL_STACK_SIZE (3*1024)
  189. #define LINELEN 40
  190. static log_level loglevel = lINFO;
  191. static bool (*slimp_handler_chain)(u8_t *data, int len);
  192. static void (*slimp_loop_chain)(void);
  193. static void (*notify_chain)(in_addr_t ip, u16_t hport, u16_t cport);
  194. static bool (*display_bus_chain)(void *from, enum display_bus_cmd_e cmd);
  195. #define max(a,b) (((a) > (b)) ? (a) : (b))
  196. static void server(in_addr_t ip, u16_t hport, u16_t cport);
  197. static void send_server(void);
  198. static bool handler(u8_t *data, int len);
  199. static bool display_bus_handler(void *from, enum display_bus_cmd_e cmd);
  200. static void vfdc_handler( u8_t *_data, int bytes_read);
  201. static void grfe_handler( u8_t *data, int len);
  202. static void grfb_handler(u8_t *data, int len);
  203. static void grfs_handler(u8_t *data, int len);
  204. static void grfg_handler(u8_t *data, int len);
  205. static void grfa_handler(u8_t *data, int len);
  206. static void visu_handler(u8_t *data, int len);
  207. static void displayer_task(void* arg);
  208. /* scrolling undocumented information
  209. grfs
  210. B: screen number
  211. B:1 = left, 2 = right,
  212. Q: scroll pause once done (ms)
  213. Q: scroll speed (ms)
  214. W: # of pixels to scroll each time
  215. W: 0 = continue scrolling after pause, 1 = scroll to scrollend and then stop, 2 = scroll to scrollend and then end animation (causing new update)
  216. W: width of total scroll area in pixels
  217. grfd
  218. W: screen number
  219. W: width of scrollable area in pixels
  220. anic ( two versions, don't know what to chose)
  221. B: flag
  222. ANIM_TRANSITION (0x01) - transition animation has finished (previous use of ANIC)
  223. ANIM_SCREEN_1 (0x04) - end of first scroll on screen 1
  224. ANIM_SCREEN_2 (0x08) - end of first scroll on screen 2
  225. ANIM_SCROLL_ONCE (0x02) | ANIM_SCREEN_1 (0x04) - end of scroll once on screen 1
  226. ANIM_SCROLL_ONCE (0x02) | ANIM_SCREEN_2 (0x08) - end of scroll once on screen 2
  227. - or -
  228. ANIM_TRANSITION 0x01 # A transition animation has finished
  229. ANIM_SCROLL_ONCE 0x02 # A scrollonce has finished
  230. ANIM_SCREEN_1 0x04 # For scrollonce only, screen 1 was scrolling
  231. ANIM_SCREEN_2 0x08 # For scrollonce only, screen 2 was scrolling
  232. */
  233. /* classical visu not our specific version)
  234. Parameters for the spectrum analyzer:
  235. 0 - Channels: stereo == 0, mono == 1
  236. 1 - Bandwidth: 0..22050Hz == 0, 0..11025Hz == 1
  237. 2 - Preemphasis in dB per KHz
  238. Left channel parameters:
  239. 3 - Position in pixels
  240. 4 - Width in pixels
  241. 5 - orientation: left to right == 0, right to left == 1
  242. 6 - Bar width in pixels
  243. 7 - Bar spacing in pixels
  244. 8 - Clipping: show all subbands == 0, clip higher subbands == 1
  245. 9 - Bar intensity (greyscale): 1-3
  246. 10 - Bar cap intensity (greyscale): 1-3
  247. Right channel parameters (not required for mono):
  248. 11-18 - same as left channel parameters
  249. Parameters for the vumeter:
  250. 0 - Channels: stereo == 0, mono == 1
  251. 1 - Style: digital == 0, analog == 1
  252. Left channel parameters:
  253. 2 - Position in pixels
  254. 3 - Width in pixels
  255. Right channel parameters (not required for mono):
  256. 4-5 - same as left channel parameters
  257. */
  258. /****************************************************************************************
  259. *
  260. */
  261. bool sb_display_init(void) {
  262. static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
  263. static EXT_RAM_ATTR StackType_t xStack[SCROLL_STACK_SIZE] __attribute__ ((aligned (4)));
  264. // no display, just make sure we won't have requests
  265. if (!display || GDS_GetWidth(display) <= 0 || GDS_GetHeight(display) <= 0) {
  266. LOG_INFO("no display for LMS");
  267. return false;
  268. }
  269. // need to force height to 32 maximum
  270. displayer.width = GDS_GetWidth(display);
  271. displayer.height = min(GDS_GetHeight(display), SB_HEIGHT);
  272. SETD_width = displayer.width;
  273. // create visu configuration
  274. visu.bar_gap = 1;
  275. visu.speed = 100;
  276. visu.back.frame = calloc(1, (displayer.width * displayer.height) / 8);
  277. dsps_fft2r_init_fc32(visu.fft, FFT_LEN);
  278. dsps_wind_hann_f32(visu.hanning, FFT_LEN);
  279. // create scroll management task
  280. displayer.mutex = xSemaphoreCreateMutex();
  281. displayer.task = xTaskCreateStatic( (TaskFunction_t) displayer_task, "displayer_thread", SCROLL_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, &xTaskBuffer);
  282. // size scroller (width + current screen)
  283. scroller.scroll.max = (displayer.width * displayer.height / 8) * (15 + 1);
  284. scroller.scroll.frame = malloc(scroller.scroll.max);
  285. scroller.back.frame = malloc(displayer.width * displayer.height / 8);
  286. scroller.frame = malloc(displayer.width * displayer.height / 8);
  287. // chain handlers
  288. slimp_handler_chain = slimp_handler;
  289. slimp_handler = handler;
  290. slimp_loop_chain = slimp_loop;
  291. slimp_loop = send_server;
  292. notify_chain = server_notify;
  293. server_notify = server;
  294. display_bus_chain = display_bus;
  295. display_bus = display_bus_handler;
  296. return true;
  297. }
  298. /****************************************************************************************
  299. * Receive display bus commands
  300. */
  301. static bool display_bus_handler(void *from, enum display_bus_cmd_e cmd) {
  302. // don't answer to own requests
  303. if (from == &displayer) return false ;
  304. LOG_INFO("Display bus command %d", cmd);
  305. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  306. switch (cmd) {
  307. case DISPLAY_BUS_TAKE:
  308. displayer.owned = false;
  309. break;
  310. case DISPLAY_BUS_GIVE:
  311. displayer.owned = true;
  312. break;
  313. }
  314. xSemaphoreGive(displayer.mutex);
  315. // chain to rest of "bus"
  316. if (display_bus_chain) return (*display_bus_chain)(from, cmd);
  317. else return true;
  318. }
  319. /****************************************************************************************
  320. * Send message to server (ANIC at that time)
  321. */
  322. static void send_server(void) {
  323. /*
  324. This complication is needed as we cannot send direclty to LMS, because
  325. send_packet is not thread safe. So must subscribe to slimproto busy loop
  326. end send from there
  327. */
  328. if (ANIC_resp != ANIM_NONE) {
  329. struct ANIC_header pkt_header;
  330. memset(&pkt_header, 0, sizeof(pkt_header));
  331. memcpy(&pkt_header.opcode, "ANIC", 4);
  332. pkt_header.length = htonl(sizeof(pkt_header) - 8);
  333. pkt_header.mode = ANIC_resp;
  334. send_packet((uint8_t *) &pkt_header, sizeof(pkt_header));
  335. ANIC_resp = ANIM_NONE;
  336. }
  337. if (SETD_width) {
  338. struct SETD_header pkt_header;
  339. memset(&pkt_header, 0, sizeof(pkt_header));
  340. memcpy(&pkt_header.opcode, "SETD", 4);
  341. pkt_header.id = 0xfe; // id 0xfe is width S:P:Squeezebox2
  342. pkt_header.length = htonl(sizeof(pkt_header) + 4 - 8);
  343. u16_t height = GDS_GetHeight(display);
  344. LOG_INFO("sending dimension %ux%u", SETD_width, height);
  345. SETD_width = htons(SETD_width);
  346. height = htons(height);
  347. send_packet((uint8_t *) &pkt_header, sizeof(pkt_header));
  348. send_packet((uint8_t *) &SETD_width, 2);
  349. send_packet((uint8_t *) &height, 2);
  350. SETD_width = 0;
  351. }
  352. if (slimp_loop_chain) (*slimp_loop_chain)();
  353. }
  354. /****************************************************************************************
  355. *
  356. */
  357. static void server(in_addr_t ip, u16_t hport, u16_t cport) {
  358. char msg[32];
  359. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  360. sprintf(msg, "%s:%hu", inet_ntoa(ip), hport);
  361. if (displayer.owned) GDS_TextPos(display, GDS_FONT_DEFAULT, GDS_TEXT_CENTERED, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, msg);
  362. SETD_width = displayer.width;
  363. displayer.dirty = true;
  364. xSemaphoreGive(displayer.mutex);
  365. if (notify_chain) (*notify_chain)(ip, hport, cport);
  366. }
  367. /****************************************************************************************
  368. * Process graphic display data
  369. */
  370. static bool handler(u8_t *data, int len){
  371. bool res = true;
  372. if (!strncmp((char*) data, "vfdc", 4)) {
  373. vfdc_handler(data, len);
  374. } else if (!strncmp((char*) data, "grfe", 4)) {
  375. grfe_handler(data, len);
  376. } else if (!strncmp((char*) data, "grfb", 4)) {
  377. grfb_handler(data, len);
  378. } else if (!strncmp((char*) data, "grfs", 4)) {
  379. grfs_handler(data, len);
  380. } else if (!strncmp((char*) data, "grfg", 4)) {
  381. grfg_handler(data, len);
  382. } else if (!strncmp((char*) data, "grfa", 4)) {
  383. grfa_handler(data, len);
  384. } else if (!strncmp((char*) data, "visu", 4)) {
  385. visu_handler(data, len);
  386. } else {
  387. res = false;
  388. }
  389. // chain protocol handlers (bitwise or is fine)
  390. if (*slimp_handler_chain) res |= (*slimp_handler_chain)(data, len);
  391. return res;
  392. }
  393. /****************************************************************************************
  394. * Change special LCD chars to something more printable on screen
  395. */
  396. static void makeprintable(unsigned char * line) {
  397. for (int n = 0; n < LINELEN; n++) {
  398. switch (line[n]) {
  399. case 11: /* block */
  400. line[n] = '#';
  401. break;;
  402. case 16: /* rightarrow */
  403. line[n] = '>';
  404. break;;
  405. case 22: /* circle */
  406. line[n] = '@';
  407. break;;
  408. case 145: /* note */
  409. line[n] = ' ';
  410. break;;
  411. case 152: /* bell */
  412. line[n] = 'o';
  413. break;
  414. default:
  415. break;
  416. }
  417. }
  418. }
  419. /****************************************************************************************
  420. * Check if char is printable, or a valid symbol
  421. */
  422. static bool charisok(unsigned char c) {
  423. switch (c) {
  424. case 11: /* block */
  425. case 16: /* rightarrow */
  426. case 22: /* circle */
  427. case 145: /* note */
  428. case 152: /* bell */
  429. return true;
  430. break;;
  431. default:
  432. return isprint(c);
  433. }
  434. }
  435. /****************************************************************************************
  436. * Show the display (text mode)
  437. */
  438. static void show_display_buffer(char *ddram) {
  439. char line1[LINELEN+1];
  440. char *line2;
  441. memset(line1, 0, LINELEN+1);
  442. strncpy(line1, ddram, LINELEN);
  443. line2 = &(ddram[LINELEN]);
  444. line2[LINELEN] = '\0';
  445. /* Convert special LCD chars */
  446. makeprintable((unsigned char *)line1);
  447. makeprintable((unsigned char *)line2);
  448. LOG_DEBUG("\n\t%.40s\n\t%.40s", line1, line2);
  449. GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_CLEAR, line1);
  450. GDS_TextLine(display, 2, GDS_TEXT_LEFT, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, line2);
  451. }
  452. /****************************************************************************************
  453. * Process display data
  454. */
  455. static void vfdc_handler( u8_t *_data, int bytes_read) {
  456. unsigned short *data = (unsigned short*) _data, *display_data;
  457. char ddram[(LINELEN + 1) * 2];
  458. int n, addr = 0; /* counter */
  459. bytes_read -= 4;
  460. if (bytes_read % 2) bytes_read--; /* even number of bytes */
  461. // if we use Noritake VFD codes, display data starts at 12
  462. display_data = &(data[5]); /* display data starts at byte 10 */
  463. memset(ddram, ' ', LINELEN * 2);
  464. for (n = 0; n < (bytes_read/2); n++) {
  465. unsigned short d; /* data element */
  466. unsigned char t, c;
  467. d = ntohs(display_data[n]);
  468. t = (d & 0x00ff00) >> 8; /* type of display data */
  469. c = (d & 0x0000ff); /* character/command */
  470. switch (t) {
  471. case 0x03: /* character */
  472. if (!charisok(c)) c = ' ';
  473. if (addr <= LINELEN * 2) {
  474. ddram[addr++] = c;
  475. }
  476. break;
  477. case 0x02: /* command */
  478. switch (c) {
  479. case 0x06: /* display clear */
  480. memset(ddram, ' ', LINELEN * 2);
  481. break;
  482. case 0x02: /* cursor home */
  483. addr = 0;
  484. break;
  485. case 0xc0: /* cursor home2 */
  486. addr = LINELEN;
  487. break;
  488. }
  489. }
  490. }
  491. show_display_buffer(ddram);
  492. }
  493. /****************************************************************************************
  494. * Display VU-Meter (lots of hard-coding)
  495. */
  496. void draw_VU(struct GDS_Device * display, const uint8_t *data, int level, int x, int y, int width) {
  497. // VU data is by columns and vertical flip to allow block offset
  498. data += level * VU_WIDTH * VU_HEIGHT;
  499. // adjust to current display window
  500. if (width > VU_WIDTH) {
  501. width = VU_WIDTH;
  502. x += (width - VU_WIDTH) / 2;
  503. } else {
  504. data += (VU_WIDTH - width) / 2 * VU_HEIGHT;
  505. }
  506. // this is 8 bits grayscale
  507. int scale = 8 - GDS_GetDepth(display);
  508. // use "fast" version as we are not beyond screen boundaries
  509. for (int r = 0; r < width; r++) {
  510. for (int c = 0; c < VU_HEIGHT; c++) {
  511. GDS_DrawPixelFast(display, r + x, c + y, *data++ >> scale);
  512. }
  513. }
  514. // need to manually set dirty flag as DrawPixel does not do it
  515. GDS_SetDirty(display);
  516. }
  517. /****************************************************************************************
  518. * Process graphic display data
  519. */
  520. static void grfe_handler( u8_t *data, int len) {
  521. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  522. scroller.active = false;
  523. // visu has priority when full screen on small screens
  524. if ((visu.mode & VISU_ESP32) && !visu.col && visu.row < SB_HEIGHT) {
  525. xSemaphoreGive(displayer.mutex);
  526. return;
  527. }
  528. // are we in control
  529. if (displayer.owned) {
  530. // did we have something that might have write on the bottom of a SB_HEIGHT+ display
  531. if (displayer.dirty) {
  532. GDS_ClearExt(display, true);
  533. displayer.dirty = false;
  534. }
  535. // draw new frame, it might be less than full screen (small visu)
  536. int width = ((len - sizeof(struct grfe_packet)) * 8) / displayer.height;
  537. // when doing screensaver, that frame becomes a visu background
  538. if (!(visu.mode & VISU_ESP32)) {
  539. visu.back.width = width;
  540. memset(visu.back.frame, 0, (displayer.width * displayer.height) / 8);
  541. memcpy(visu.back.frame, data + sizeof(struct grfe_packet), (width * displayer.height) / 8);
  542. // this is a bit tricky but basically that checks if frame if full of 0
  543. visu.back.active = *visu.back.frame || memcmp(visu.back.frame, visu.back.frame + 1, width - 1);
  544. }
  545. GDS_DrawBitmapCBR(display, data + sizeof(struct grfe_packet), width, displayer.height, GDS_COLOR_WHITE);
  546. GDS_Update(display);
  547. }
  548. xSemaphoreGive(displayer.mutex);
  549. LOG_DEBUG("grfe frame %u", len);
  550. }
  551. /****************************************************************************************
  552. * Brightness
  553. */
  554. static void grfb_handler(u8_t *data, int len) {
  555. struct grfb_packet *pkt = (struct grfb_packet*) data;
  556. pkt->brightness = htons(pkt->brightness);
  557. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  558. if (pkt->brightness < 0) {
  559. GDS_DisplayOff(display);
  560. } else {
  561. GDS_DisplayOn(display);
  562. GDS_SetContrast(display, pkt->brightness);
  563. }
  564. xSemaphoreGive(displayer.mutex);
  565. LOG_INFO("brightness %hu", pkt->brightness);
  566. }
  567. /****************************************************************************************
  568. * Scroll set
  569. */
  570. static void grfs_handler(u8_t *data, int len) {
  571. struct grfs_packet *pkt = (struct grfs_packet*) data;
  572. int size = len - sizeof(struct grfs_packet);
  573. int offset = htons(pkt->offset);
  574. LOG_DEBUG("grfs s:%u d:%u p:%u sp:%u by:%hu m:%hu w:%hu o:%hu",
  575. (int) pkt->screen,
  576. (int) pkt->direction, // 1=left, 2=right
  577. htonl(pkt->pause), // in ms
  578. htonl(pkt->speed), // in ms
  579. htons(pkt->by), // # of pixel of scroll step
  580. htons(pkt->mode), // 0=continuous, 1=once and stop, 2=once and end
  581. htons(pkt->width), // last column of animation that contains a "full" screen
  582. htons(pkt->offset) // offset if multiple packets are sent
  583. );
  584. // new grfs frame, build scroller info
  585. if (!offset) {
  586. // use the display as a general lock
  587. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  588. // copy & set scroll parameters
  589. scroller.screen = pkt->screen;
  590. scroller.pause = htonl(pkt->pause);
  591. scroller.speed = htonl(pkt->speed);
  592. scroller.mode = htons(pkt->mode);
  593. scroller.scroll.width = htons(pkt->width);
  594. scroller.first = true;
  595. scroller.overflow = false;
  596. // set scroller steps & beginning
  597. if (pkt->direction == 1) {
  598. scroller.scrolled = 0;
  599. scroller.by = htons(pkt->by);
  600. } else {
  601. scroller.scrolled = scroller.scroll.width;
  602. scroller.by = -htons(pkt->by);
  603. }
  604. xSemaphoreGive(displayer.mutex);
  605. }
  606. // copy scroll frame data (no semaphore needed)
  607. if (scroller.scroll.size + size < scroller.scroll.max && !scroller.overflow) {
  608. memcpy(scroller.scroll.frame + offset, data + sizeof(struct grfs_packet), size);
  609. scroller.scroll.size = offset + size;
  610. LOG_INFO("scroller current size %u (w:%u)", scroller.scroll.size, scroller.scroll.width);
  611. } else {
  612. LOG_INFO("scroller too large %u/%u (w:%u)", scroller.scroll.size + size, scroller.scroll.max, scroller.scroll.width);
  613. scroller.scroll.width = scroller.scroll.size / (displayer.height / 8) - scroller.back.width;
  614. scroller.overflow = true;
  615. }
  616. }
  617. /****************************************************************************************
  618. * Scroll background frame update & go
  619. */
  620. static void grfg_handler(u8_t *data, int len) {
  621. struct grfg_packet *pkt = (struct grfg_packet*) data;
  622. LOG_DEBUG("gfrg s:%hu w:%hu (len:%u)", htons(pkt->screen), htons(pkt->width), len);
  623. // on small screen, visu has priority when full screen
  624. if ((visu.mode & VISU_ESP32) && !visu.col && visu.row < SB_HEIGHT) return;
  625. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  626. // size of scrollable area (less than background)
  627. scroller.width = htons(pkt->width);
  628. scroller.back.width = ((len - sizeof(struct grfg_packet)) * 8) / displayer.height;
  629. memcpy(scroller.back.frame, data + sizeof(struct grfg_packet), len - sizeof(struct grfg_packet));
  630. // update display asynchronously (frames are organized by columns)
  631. memcpy(scroller.frame, scroller.back.frame, scroller.back.width * displayer.height / 8);
  632. for (int i = 0; i < scroller.width * displayer.height / 8; i++) scroller.frame[i] |= scroller.scroll.frame[scroller.scrolled * displayer.height / 8 + i];
  633. // can only write if we really own display
  634. if (displayer.owned) {
  635. GDS_DrawBitmapCBR(display, scroller.frame, scroller.back.width, displayer.height, GDS_COLOR_WHITE);
  636. GDS_Update(display);
  637. }
  638. // now we can active scrolling, but only if we are not on a small screen
  639. if (!visu.mode || visu.col || visu.row >= SB_HEIGHT) scroller.active = true;
  640. // if we just got a content update, let the scroller manage the screen
  641. LOG_DEBUG("resuming scrolling task");
  642. xSemaphoreGive(displayer.mutex);
  643. // resume task once we have background, not in grfs
  644. vTaskResume(displayer.task);
  645. }
  646. /****************************************************************************************
  647. * Artwork
  648. */
  649. static void grfa_handler(u8_t *data, int len) {
  650. struct grfa_packet *pkt = (struct grfa_packet*) data;
  651. int size = len - sizeof(struct grfa_packet);
  652. int offset = htonl(pkt->offset);
  653. int length = htonl(pkt->length);
  654. // when using full screen visualizer on small screen there is a brief overlay
  655. artwork.enable = (length != 0);
  656. // just a config or an actual artwork
  657. if (length < 32) {
  658. if (artwork.enable) {
  659. // this is just to specify artwork coordinates
  660. artwork.x = htons(pkt->x);
  661. artwork.y = htons(pkt->y);
  662. } else if (artwork.size) GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
  663. // done in any case
  664. return;
  665. }
  666. // new grfa artwork, allocate memory
  667. if (!offset) {
  668. // same trick to clean current/previous window
  669. if (artwork.size) {
  670. GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
  671. artwork.size = 0;
  672. }
  673. // now use new parameters
  674. artwork.x = htons(pkt->x);
  675. artwork.y = htons(pkt->y);
  676. if (artwork.data) free(artwork.data);
  677. artwork.data = malloc(length);
  678. }
  679. // copy artwork data
  680. memcpy(artwork.data + offset, data + sizeof(struct grfa_packet), size);
  681. artwork.size += size;
  682. if (artwork.size == length) {
  683. GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
  684. GDS_DrawJPEG(display, artwork.data, artwork.x, artwork.y, artwork.y < SB_HEIGHT ? (GDS_IMAGE_RIGHT | GDS_IMAGE_TOP) : GDS_IMAGE_CENTER);
  685. free(artwork.data);
  686. artwork.data = NULL;
  687. }
  688. LOG_INFO("gfra l:%u x:%hu, y:%hu, o:%u s:%u", length, artwork.x, artwork.y, offset, size);
  689. }
  690. /****************************************************************************************
  691. * Update visualization bars
  692. */
  693. static void visu_update(void) {
  694. // no need to protect against no woning the display as we are playing
  695. if (pthread_mutex_trylock(&visu_export.mutex)) return;
  696. int mode = visu.mode & ~VISU_ESP32;
  697. // not enough samples
  698. if (visu_export.level < (mode == VISU_VUMETER ? RMS_LEN : FFT_LEN) * 2 && visu_export.running) {
  699. pthread_mutex_unlock(&visu_export.mutex);
  700. return;
  701. }
  702. // reset bars for all cases first
  703. for (int i = visu.n; --i >= 0;) visu.bars[i].current = 0;
  704. if (visu_export.running) {
  705. if (mode == VISU_VUMETER) {
  706. s16_t *iptr = visu_export.buffer;
  707. // calculate sum(L²+R²), try to not overflow at the expense of some precision
  708. for (int i = RMS_LEN; --i >= 0;) {
  709. visu.bars[0].current += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
  710. iptr++;
  711. visu.bars[1].current += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
  712. iptr++;
  713. }
  714. // convert to dB (1 bit remaining for getting X²/N, 60dB dynamic starting from 0dBFS = 3 bits back-off)
  715. for (int i = visu.n; --i >= 0;) {
  716. visu.bars[i].current = SB_HEIGHT * (0.01667f*10*log10f(0.0000001f + (visu.bars[i].current >> 1)) - 0.2543f);
  717. if (visu.bars[i].current > visu.max) visu.bars[i].current = visu.max;
  718. else if (visu.bars[i].current < 0) visu.bars[i].current = 0;
  719. }
  720. } else {
  721. // on xtensa/esp32 the floating point FFT takes 1/2 cycles of the fixed point
  722. for (int i = 0 ; i < FFT_LEN ; i++) {
  723. // don't normalize here, but we are due INT16_MAX and FFT_LEN / 2 / 2
  724. visu.samples[i * 2 + 0] = (float) (visu_export.buffer[2*i] + visu_export.buffer[2*i + 1]) * visu.hanning[i];
  725. visu.samples[i * 2 + 1] = 0;
  726. }
  727. // actual FFT that might be less cycle than all the crap below
  728. dsps_fft2r_fc32_ae32(visu.samples, FFT_LEN);
  729. dsps_bit_rev_fc32_ansi(visu.samples, FFT_LEN);
  730. float rate = visu_export.rate;
  731. // now arrange the result with the number of bar and sampling rate (don't want DC)
  732. for (int i = 0, j = 1; i < visu.n && j < (FFT_LEN / 2); i++) {
  733. float power, count;
  734. // find the next point in FFT (this is real signal, so only half matters)
  735. for (count = 0, power = 0; j * visu_export.rate < visu.bars[i].limit * FFT_LEN && j < FFT_LEN / 2; j++, count += 1) {
  736. power += visu.samples[2*j] * visu.samples[2*j] + visu.samples[2*j+1] * visu.samples[2*j+1];
  737. }
  738. // due to sample rate, we have reached the end of the available spectrum
  739. if (j >= (FFT_LEN / 2)) {
  740. // normalize accumulated data
  741. if (count) power /= count * 2.;
  742. } else if (count) {
  743. // how much of what remains do we need to add
  744. float ratio = j - (visu.bars[i].limit * FFT_LEN) / rate;
  745. power += (visu.samples[2*j] * visu.samples[2*j] + visu.samples[2*j+1] * visu.samples[2*j+1]) * ratio;
  746. // normalize accumulated data
  747. power /= (count + ratio) * 2;
  748. } else {
  749. // no data for that band (sampling rate too high), just assume same as previous one
  750. power = (visu.samples[2*j] * visu.samples[2*j] + visu.samples[2*j+1] * visu.samples[2*j+1]) / 2.;
  751. }
  752. // convert to dB and bars, same back-off
  753. if (power) visu.bars[i].current = SB_HEIGHT * (0.01667f*10*(log10f(power) - log10f(FFT_LEN/2*2)) - 0.2543f);
  754. if (visu.bars[i].current > visu.max) visu.bars[i].current = visu.max;
  755. else if (visu.bars[i].current < 0) visu.bars[i].current = 0;
  756. }
  757. }
  758. }
  759. // we took what we want, we can release the buffer
  760. visu_export.level = 0;
  761. pthread_mutex_unlock(&visu_export.mutex);
  762. // don't refresh screen if all max are 0 (we were are somewhat idle)
  763. int clear = 0;
  764. for (int i = visu.n; --i >= 0;) clear = max(clear, visu.bars[i].max);
  765. if (clear) GDS_ClearExt(display, false, false, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  766. // draw background if we are in screensaver mode
  767. if (!(visu.mode & VISU_ESP32) && visu.back.active) {
  768. GDS_DrawBitmapCBR(display, visu.back.frame, visu.back.width, displayer.height, GDS_COLOR_WHITE);
  769. }
  770. if (mode != VISU_VUMETER || !visu.style) {
  771. // there is much more optimization to be done here, like not redrawing bars unless needed
  772. for (int i = visu.n; --i >= 0;) {
  773. int x1 = visu.col + visu.border + visu.bar_border + i*(visu.bar_width + visu.bar_gap);
  774. int y1 = visu.row + visu.height - 1;
  775. if (visu.bars[i].current > visu.bars[i].max) visu.bars[i].max = visu.bars[i].current;
  776. else if (visu.bars[i].max) visu.bars[i].max--;
  777. else if (!clear) continue;
  778. for (int j = 0; j <= visu.bars[i].current; j += 2)
  779. GDS_DrawLine(display, x1, y1 - j, x1 + visu.bar_width - 1, y1 - j, GDS_COLOR_WHITE);
  780. if (visu.bars[i].max > 2) {
  781. GDS_DrawLine(display, x1, y1 - visu.bars[i].max, x1 + visu.bar_width - 1, y1 - visu.bars[i].max, GDS_COLOR_WHITE);
  782. GDS_DrawLine(display, x1, y1 - visu.bars[i].max + 1, x1 + visu.bar_width - 1, y1 - visu.bars[i].max + 1, GDS_COLOR_WHITE);
  783. }
  784. }
  785. } else if (displayer.width / 2 > 3 * VU_WIDTH / 4) {
  786. draw_VU(display, vu_bitmap, visu.bars[0].current, 0, visu.row, displayer.width / 2);
  787. draw_VU(display, vu_bitmap, visu.bars[1].current, displayer.width / 2, visu.row, displayer.width / 2);
  788. } else {
  789. int level = (visu.bars[0].current + visu.bars[1].current) / 2;
  790. draw_VU(display, vu_bitmap, level, 0, visu.row, displayer.width);
  791. }
  792. }
  793. /****************************************************************************************
  794. * Visu packet handler
  795. */
  796. void spectrum_limits(int min, int n, int pos) {
  797. if (n / 2) {
  798. int step = ((DISPLAY_BW - min) * visu.spectrum_scale) / (n/2);
  799. visu.bars[pos].limit = min + step;
  800. for (int i = 1; i < n/2; i++) visu.bars[pos+i].limit = visu.bars[pos+i-1].limit + step;
  801. spectrum_limits(visu.bars[pos + n/2 - 1].limit, n - n/2, pos + n/2);
  802. } else {
  803. visu.bars[pos].limit = DISPLAY_BW;
  804. }
  805. }
  806. /****************************************************************************************
  807. * Visu packet handler
  808. */
  809. static void visu_handler( u8_t *data, int len) {
  810. struct visu_packet *pkt = (struct visu_packet*) data;
  811. int bars = 0;
  812. LOG_DEBUG("visu %u with %u parameters", pkt->which, pkt->count);
  813. /*
  814. If width is specified, then respect all coordinates, otherwise we try to
  815. use the bottom part of the display and if it is a small display, we overwrite
  816. text
  817. */
  818. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  819. visu.mode = pkt->which;
  820. // little trick to clean the taller screens when switching visu
  821. if (visu.row >= SB_HEIGHT) GDS_ClearExt(display, false, true, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  822. if (visu.mode) {
  823. // these will be overidden if necessary
  824. visu.col = visu.border = 0;
  825. visu.width = displayer.width;
  826. // what type of visu
  827. if (visu.mode & VISU_ESP32) {
  828. if (pkt->count >= 4) {
  829. // more than 4 parameters, this is small visu, then go were we are told to
  830. pkt->height = htonl(pkt->height);
  831. pkt->row = htonl(pkt->row);
  832. pkt->col = htonl(pkt->col);
  833. visu.style = 0;
  834. visu.width = htonl(pkt->width);
  835. visu.height = pkt->height ? pkt->height : SB_HEIGHT;
  836. visu.col = pkt->col < 0 ? displayer.width + pkt->col : pkt->col;
  837. visu.row = pkt->row < 0 ? GDS_GetHeight(display) + pkt->row : pkt->row;
  838. visu.border = htonl(pkt->border);
  839. bars = htonl(pkt->bars);
  840. visu.spectrum_scale = htonl(pkt->spectrum_scale) / 100.;
  841. } else {
  842. // full screen visu, try to use bottom screen if available
  843. visu.height = GDS_GetHeight(display) > SB_HEIGHT ? GDS_GetHeight(display) - SB_HEIGHT : GDS_GetHeight(display);
  844. visu.row = GDS_GetHeight(display) - visu.height;
  845. // is this spectrum or analogue/digital
  846. if ((visu.mode & ~VISU_ESP32) == VISU_SPECTRUM) {
  847. bars = htonl(pkt->full.bars);
  848. visu.spectrum_scale = htonl(pkt->full.spectrum_scale) / 100.;
  849. } else {
  850. // select analogue/digital style
  851. visu.style = htonl(pkt->full.style);
  852. }
  853. }
  854. } else {
  855. // classical (screensaver) mode, don't try to optimize screen usage & force some params
  856. visu.row = 0;
  857. visu.height = SB_HEIGHT;
  858. visu.spectrum_scale = 0.25;
  859. if (visu.mode == VISU_SPECTRUM) bars = visu.width / (htonl(pkt->channels[0].bar_width) + htonl(pkt->channels[0].bar_space));
  860. else visu.style = htonl(pkt->classical_vu.style);
  861. if (bars > MAX_BARS) bars = MAX_BARS;
  862. }
  863. // try to adapt to what we have
  864. if ((visu.mode & ~VISU_ESP32) == VISU_SPECTRUM) {
  865. visu.n = bars ? bars : MAX_BARS;
  866. visu.max = displayer.height - 1;
  867. if (visu.spectrum_scale <= 0 || visu.spectrum_scale > 0.5) visu.spectrum_scale = 0.5;
  868. spectrum_limits(0, visu.n, 0);
  869. } else {
  870. visu.n = 2;
  871. visu.max = visu.style ? (VU_COUNT - 1) : (displayer.height - 1);
  872. }
  873. do {
  874. visu.bar_width = (visu.width - visu.border - visu.bar_gap * (visu.n - 1)) / visu.n;
  875. if (visu.bar_width > 0) break;
  876. } while (--visu.n);
  877. visu.bar_border = (visu.width - visu.border - (visu.bar_width + visu.bar_gap) * visu.n + visu.bar_gap) / 2;
  878. // give up if not enough space
  879. if (visu.bar_width < 0) {
  880. visu.mode = VISU_BLANK;
  881. LOG_WARN("Not enough room for displaying visu");
  882. } else {
  883. // de-activate scroller if we are taking main screen
  884. if (visu.row < SB_HEIGHT) scroller.active = false;
  885. vTaskResume(displayer.task);
  886. }
  887. visu.wake = 0;
  888. // reset bars maximum
  889. for (int i = visu.n; --i >= 0;) visu.bars[i].max = 0;
  890. GDS_ClearExt(display, false, true, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  891. 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);
  892. } else {
  893. LOG_INFO("Stopping visualizer");
  894. }
  895. xSemaphoreGive(displayer.mutex);
  896. }
  897. /****************************************************************************************
  898. * Scroll task
  899. * - with the addition of the visualizer, it's a bit a 2-headed beast not easy to
  900. * maintain, so som better separation between the visu and scroll is probably needed
  901. */
  902. static void displayer_task(void *args) {
  903. int sleep;
  904. while (1) {
  905. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  906. // suspend ourselves if nothing to do, grfg or visu will wake us up
  907. if (!scroller.active && !visu.mode) {
  908. xSemaphoreGive(displayer.mutex);
  909. vTaskSuspend(NULL);
  910. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  911. scroller.wake = visu.wake = 0;
  912. }
  913. // go for long sleep when either item is disabled
  914. if (!visu.mode) visu.wake = LONG_WAKE;
  915. if (!scroller.active) scroller.wake = LONG_WAKE;
  916. // scroll required amount of columns (within the window)
  917. if (scroller.active && scroller.wake <= 0) {
  918. // by default go for the long sleep, will change below if required
  919. scroller.wake = LONG_WAKE;
  920. // do we have more to scroll (scroll.width is the last column from which we have a full zone)
  921. if (scroller.by > 0 ? (scroller.scrolled <= scroller.scroll.width) : (scroller.scrolled >= 0)) {
  922. memcpy(scroller.frame, scroller.back.frame, scroller.back.width * displayer.height / 8);
  923. for (int i = 0; i < scroller.width * displayer.height / 8; i++) scroller.frame[i] |= scroller.scroll.frame[scroller.scrolled * displayer.height / 8 + i];
  924. scroller.scrolled += scroller.by;
  925. if (displayer.owned) GDS_DrawBitmapCBR(display, scroller.frame, scroller.width, displayer.height, GDS_COLOR_WHITE);
  926. // short sleep & don't need background update
  927. scroller.wake = scroller.speed;
  928. } else if (scroller.first || !scroller.mode) {
  929. // at least one round done
  930. scroller.first = false;
  931. // see if we need to pause or if we are done
  932. if (scroller.mode) {
  933. // can't call directly send_packet from slimproto as it's not re-entrant
  934. ANIC_resp = ANIM_SCROLL_ONCE | ANIM_SCREEN_1;
  935. LOG_INFO("scroll-once terminated");
  936. } else {
  937. scroller.wake = scroller.pause;
  938. LOG_DEBUG("scroll cycle done, pausing for %u (ms)", scroller.pause);
  939. }
  940. // need to reset pointers for next scroll
  941. scroller.scrolled = scroller.by < 0 ? scroller.scroll.width : 0;
  942. }
  943. }
  944. // update visu if active
  945. if (visu.mode && visu.wake <= 0) {
  946. visu_update();
  947. visu.wake = 100;
  948. }
  949. // need to make sure we own display
  950. if (displayer.owned) GDS_Update(display);
  951. // release semaphore and sleep what's needed
  952. xSemaphoreGive(displayer.mutex);
  953. sleep = min(visu.wake, scroller.wake);
  954. vTaskDelay(sleep / portTICK_PERIOD_MS);
  955. scroller.wake -= sleep;
  956. visu.wake -= sleep;
  957. }
  958. }