display.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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 RGB332, so pixel will be 3 bits deep
  507. int depth = GDS_GetDepth(display);
  508. // use "fast" version as we are not beyond screen boundaries
  509. if (depth < 3) {
  510. int scale = 3 - depth;
  511. for (int r = 0; r < width; r++) {
  512. for (int c = 0; c < VU_HEIGHT; c++) {
  513. int pixel = *data++;
  514. pixel = ((((pixel & 0x3) * 11) << 1) + ((pixel >> 2) & 0x7) * 59 + (pixel >> 5) * 30 + 1) / 100;
  515. GDS_DrawPixelFast(display, r + x, c + y, pixel >> scale);
  516. }
  517. }
  518. } else {
  519. int scale = depth - 3;
  520. for (int r = 0; r < width; r++) {
  521. for (int c = 0; c < VU_HEIGHT; c++) {
  522. int pixel = *data++;
  523. pixel = ((((pixel & 0x3) * 11) << 1) + ((pixel >> 2) & 0x7) * 59 + (pixel >> 5) * 30 + 1) / 100;
  524. GDS_DrawPixelFast(display, r + x, c + y, pixel << scale);
  525. }
  526. }
  527. }
  528. // need to manually set dirty flag as DrawPixel does not do it
  529. GDS_SetDirty(display);
  530. }
  531. /****************************************************************************************
  532. * Process graphic display data
  533. */
  534. static void grfe_handler( u8_t *data, int len) {
  535. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  536. scroller.active = false;
  537. // we are not in control or we are displaying visu on a small screen, do not do screen update
  538. if ((visu.mode & VISU_ESP32) && !visu.col && visu.row < SB_HEIGHT) {
  539. xSemaphoreGive(displayer.mutex);
  540. return;
  541. }
  542. if (displayer.owned) {
  543. // did we have something that might have write on the bottom of a SB_HEIGHT+ display
  544. if (displayer.dirty) {
  545. GDS_ClearExt(display, true);
  546. displayer.dirty = false;
  547. }
  548. // draw new frame, it might be less than full screen (small visu)
  549. int width = ((len - sizeof(struct grfe_packet)) * 8) / displayer.height;
  550. // when doing screensaver, that frame becomes a visu background
  551. if (!(visu.mode & VISU_ESP32)) {
  552. visu.back.width = width;
  553. memset(visu.back.frame, 0, (displayer.width * displayer.height) / 8);
  554. memcpy(visu.back.frame, data + sizeof(struct grfe_packet), (width * displayer.height) / 8);
  555. // this is a bit tricky but basically that checks if frame if full of 0
  556. visu.back.active = *visu.back.frame || memcmp(visu.back.frame, visu.back.frame + 1, width - 1);
  557. }
  558. GDS_DrawBitmapCBR(display, data + sizeof(struct grfe_packet), width, displayer.height, GDS_COLOR_WHITE);
  559. GDS_Update(display);
  560. }
  561. xSemaphoreGive(displayer.mutex);
  562. LOG_DEBUG("grfe frame %u", len);
  563. }
  564. /****************************************************************************************
  565. * Brightness
  566. */
  567. static void grfb_handler(u8_t *data, int len) {
  568. struct grfb_packet *pkt = (struct grfb_packet*) data;
  569. pkt->brightness = htons(pkt->brightness);
  570. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  571. if (pkt->brightness < 0) {
  572. GDS_DisplayOff(display);
  573. } else {
  574. GDS_DisplayOn(display);
  575. GDS_SetContrast(display, pkt->brightness);
  576. }
  577. xSemaphoreGive(displayer.mutex);
  578. LOG_INFO("brightness %hu", pkt->brightness);
  579. }
  580. /****************************************************************************************
  581. * Scroll set
  582. */
  583. static void grfs_handler(u8_t *data, int len) {
  584. struct grfs_packet *pkt = (struct grfs_packet*) data;
  585. int size = len - sizeof(struct grfs_packet);
  586. int offset = htons(pkt->offset);
  587. LOG_DEBUG("grfs s:%u d:%u p:%u sp:%u by:%hu m:%hu w:%hu o:%hu",
  588. (int) pkt->screen,
  589. (int) pkt->direction, // 1=left, 2=right
  590. htonl(pkt->pause), // in ms
  591. htonl(pkt->speed), // in ms
  592. htons(pkt->by), // # of pixel of scroll step
  593. htons(pkt->mode), // 0=continuous, 1=once and stop, 2=once and end
  594. htons(pkt->width), // last column of animation that contains a "full" screen
  595. htons(pkt->offset) // offset if multiple packets are sent
  596. );
  597. // new grfs frame, build scroller info
  598. if (!offset) {
  599. // use the display as a general lock
  600. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  601. // copy & set scroll parameters
  602. scroller.screen = pkt->screen;
  603. scroller.pause = htonl(pkt->pause);
  604. scroller.speed = htonl(pkt->speed);
  605. scroller.mode = htons(pkt->mode);
  606. scroller.scroll.width = htons(pkt->width);
  607. scroller.first = true;
  608. scroller.overflow = false;
  609. // set scroller steps & beginning
  610. if (pkt->direction == 1) {
  611. scroller.scrolled = 0;
  612. scroller.by = htons(pkt->by);
  613. } else {
  614. scroller.scrolled = scroller.scroll.width;
  615. scroller.by = -htons(pkt->by);
  616. }
  617. xSemaphoreGive(displayer.mutex);
  618. }
  619. // copy scroll frame data (no semaphore needed)
  620. if (scroller.scroll.size + size < scroller.scroll.max && !scroller.overflow) {
  621. memcpy(scroller.scroll.frame + offset, data + sizeof(struct grfs_packet), size);
  622. scroller.scroll.size = offset + size;
  623. LOG_INFO("scroller current size %u (w:%u)", scroller.scroll.size, scroller.scroll.width);
  624. } else {
  625. LOG_INFO("scroller too large %u/%u (w:%u)", scroller.scroll.size + size, scroller.scroll.max, scroller.scroll.width);
  626. scroller.scroll.width = scroller.scroll.size / (displayer.height / 8) - scroller.back.width;
  627. scroller.overflow = true;
  628. }
  629. }
  630. /****************************************************************************************
  631. * Scroll background frame update & go
  632. */
  633. static void grfg_handler(u8_t *data, int len) {
  634. struct grfg_packet *pkt = (struct grfg_packet*) data;
  635. LOG_DEBUG("gfrg s:%hu w:%hu (len:%u)", htons(pkt->screen), htons(pkt->width), len);
  636. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  637. // size of scrollable area (less than background)
  638. scroller.width = htons(pkt->width);
  639. scroller.back.width = ((len - sizeof(struct grfg_packet)) * 8) / displayer.height;
  640. memcpy(scroller.back.frame, data + sizeof(struct grfg_packet), len - sizeof(struct grfg_packet));
  641. // update display asynchronously (frames are organized by columns)
  642. memcpy(scroller.frame, scroller.back.frame, scroller.back.width * displayer.height / 8);
  643. for (int i = 0; i < scroller.width * displayer.height / 8; i++) scroller.frame[i] |= scroller.scroll.frame[scroller.scrolled * displayer.height / 8 + i];
  644. // can only write if we really own display
  645. if (displayer.owned) {
  646. GDS_DrawBitmapCBR(display, scroller.frame, scroller.back.width, displayer.height, GDS_COLOR_WHITE);
  647. GDS_Update(display);
  648. }
  649. // now we can active scrolling, but only if we are not on a small screen
  650. if (!visu.mode || visu.col || visu.row >= SB_HEIGHT) scroller.active = true;
  651. // if we just got a content update, let the scroller manage the screen
  652. LOG_DEBUG("resuming scrolling task");
  653. xSemaphoreGive(displayer.mutex);
  654. // resume task once we have background, not in grfs
  655. vTaskResume(displayer.task);
  656. }
  657. /****************************************************************************************
  658. * Artwork
  659. */
  660. static void grfa_handler(u8_t *data, int len) {
  661. struct grfa_packet *pkt = (struct grfa_packet*) data;
  662. int size = len - sizeof(struct grfa_packet);
  663. int offset = htonl(pkt->offset);
  664. int length = htonl(pkt->length);
  665. // when using full screen visualizer on small screen there is a brief overlay
  666. artwork.enable = (length != 0);
  667. // just a config or an actual artwork
  668. if (length < 32) {
  669. if (artwork.enable) {
  670. // this is just to specify artwork coordinates
  671. artwork.x = htons(pkt->x);
  672. artwork.y = htons(pkt->y);
  673. } else if (artwork.size) GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
  674. // done in any case
  675. return;
  676. }
  677. // new grfa artwork, allocate memory
  678. if (!offset) {
  679. // same trick to clean current/previous window
  680. if (artwork.size) {
  681. GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
  682. artwork.size = 0;
  683. }
  684. // now use new parameters
  685. artwork.x = htons(pkt->x);
  686. artwork.y = htons(pkt->y);
  687. if (artwork.data) free(artwork.data);
  688. artwork.data = malloc(length);
  689. }
  690. // copy artwork data
  691. memcpy(artwork.data + offset, data + sizeof(struct grfa_packet), size);
  692. artwork.size += size;
  693. if (artwork.size == length) {
  694. GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
  695. GDS_DrawJPEG(display, artwork.data, artwork.x, artwork.y, artwork.y < SB_HEIGHT ? (GDS_IMAGE_RIGHT | GDS_IMAGE_TOP) : GDS_IMAGE_CENTER);
  696. free(artwork.data);
  697. artwork.data = NULL;
  698. }
  699. LOG_INFO("gfra l:%u x:%hu, y:%hu, o:%u s:%u", length, artwork.x, artwork.y, offset, size);
  700. }
  701. /****************************************************************************************
  702. * Update visualization bars
  703. */
  704. static void visu_update(void) {
  705. // no need to protect against no woning the display as we are playing
  706. if (pthread_mutex_trylock(&visu_export.mutex)) return;
  707. int mode = visu.mode & ~VISU_ESP32;
  708. // not enough samples
  709. if (visu_export.level < (mode == VISU_VUMETER ? RMS_LEN : FFT_LEN) * 2 && visu_export.running) {
  710. pthread_mutex_unlock(&visu_export.mutex);
  711. return;
  712. }
  713. // reset bars for all cases first
  714. for (int i = visu.n; --i >= 0;) visu.bars[i].current = 0;
  715. if (visu_export.running) {
  716. if (mode == VISU_VUMETER) {
  717. s16_t *iptr = visu_export.buffer;
  718. // calculate sum(L²+R²), try to not overflow at the expense of some precision
  719. for (int i = RMS_LEN; --i >= 0;) {
  720. visu.bars[0].current += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
  721. iptr++;
  722. visu.bars[1].current += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
  723. iptr++;
  724. }
  725. // convert to dB (1 bit remaining for getting X²/N, 60dB dynamic starting from 0dBFS = 3 bits back-off)
  726. for (int i = visu.n; --i >= 0;) {
  727. visu.bars[i].current = SB_HEIGHT * (0.01667f*10*log10f(0.0000001f + (visu.bars[i].current >> 1)) - 0.2543f);
  728. if (visu.bars[i].current > visu.max) visu.bars[i].current = visu.max;
  729. else if (visu.bars[i].current < 0) visu.bars[i].current = 0;
  730. }
  731. } else {
  732. // on xtensa/esp32 the floating point FFT takes 1/2 cycles of the fixed point
  733. for (int i = 0 ; i < FFT_LEN ; i++) {
  734. // don't normalize here, but we are due INT16_MAX and FFT_LEN / 2 / 2
  735. visu.samples[i * 2 + 0] = (float) (visu_export.buffer[2*i] + visu_export.buffer[2*i + 1]) * visu.hanning[i];
  736. visu.samples[i * 2 + 1] = 0;
  737. }
  738. // actual FFT that might be less cycle than all the crap below
  739. dsps_fft2r_fc32_ae32(visu.samples, FFT_LEN);
  740. dsps_bit_rev_fc32_ansi(visu.samples, FFT_LEN);
  741. float rate = visu_export.rate;
  742. // now arrange the result with the number of bar and sampling rate (don't want DC)
  743. for (int i = 0, j = 1; i < visu.n && j < (FFT_LEN / 2); i++) {
  744. float power, count;
  745. // find the next point in FFT (this is real signal, so only half matters)
  746. for (count = 0, power = 0; j * visu_export.rate < visu.bars[i].limit * FFT_LEN && j < FFT_LEN / 2; j++, count += 1) {
  747. power += visu.samples[2*j] * visu.samples[2*j] + visu.samples[2*j+1] * visu.samples[2*j+1];
  748. }
  749. // due to sample rate, we have reached the end of the available spectrum
  750. if (j >= (FFT_LEN / 2)) {
  751. // normalize accumulated data
  752. if (count) power /= count * 2.;
  753. } else if (count) {
  754. // how much of what remains do we need to add
  755. float ratio = j - (visu.bars[i].limit * FFT_LEN) / rate;
  756. power += (visu.samples[2*j] * visu.samples[2*j] + visu.samples[2*j+1] * visu.samples[2*j+1]) * ratio;
  757. // normalize accumulated data
  758. power /= (count + ratio) * 2;
  759. } else {
  760. // no data for that band (sampling rate too high), just assume same as previous one
  761. power = (visu.samples[2*j] * visu.samples[2*j] + visu.samples[2*j+1] * visu.samples[2*j+1]) / 2.;
  762. }
  763. // convert to dB and bars, same back-off
  764. if (power) visu.bars[i].current = SB_HEIGHT * (0.01667f*10*(log10f(power) - log10f(FFT_LEN/2*2)) - 0.2543f);
  765. if (visu.bars[i].current > visu.max) visu.bars[i].current = visu.max;
  766. else if (visu.bars[i].current < 0) visu.bars[i].current = 0;
  767. }
  768. }
  769. }
  770. // we took what we want, we can release the buffer
  771. visu_export.level = 0;
  772. pthread_mutex_unlock(&visu_export.mutex);
  773. // don't refresh screen if all max are 0 (we were are somewhat idle)
  774. int clear = 0;
  775. for (int i = visu.n; --i >= 0;) clear = max(clear, visu.bars[i].max);
  776. if (clear) GDS_ClearExt(display, false, false, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  777. // draw background if we are in screensaver mode
  778. if (!(visu.mode & VISU_ESP32) && visu.back.active) {
  779. GDS_DrawBitmapCBR(display, visu.back.frame, visu.back.width, displayer.height, GDS_COLOR_WHITE);
  780. }
  781. if (mode != VISU_VUMETER || !visu.style) {
  782. // there is much more optimization to be done here, like not redrawing bars unless needed
  783. for (int i = visu.n; --i >= 0;) {
  784. int x1 = visu.col + visu.border + visu.bar_border + i*(visu.bar_width + visu.bar_gap);
  785. int y1 = visu.row + visu.height - 1;
  786. if (visu.bars[i].current > visu.bars[i].max) visu.bars[i].max = visu.bars[i].current;
  787. else if (visu.bars[i].max) visu.bars[i].max--;
  788. else if (!clear) continue;
  789. for (int j = 0; j <= visu.bars[i].current; j += 2)
  790. GDS_DrawLine(display, x1, y1 - j, x1 + visu.bar_width - 1, y1 - j, GDS_COLOR_WHITE);
  791. if (visu.bars[i].max > 2) {
  792. GDS_DrawLine(display, x1, y1 - visu.bars[i].max, x1 + visu.bar_width - 1, y1 - visu.bars[i].max, GDS_COLOR_WHITE);
  793. GDS_DrawLine(display, x1, y1 - visu.bars[i].max + 1, x1 + visu.bar_width - 1, y1 - visu.bars[i].max + 1, GDS_COLOR_WHITE);
  794. }
  795. }
  796. } else if (displayer.width / 2 > 3 * VU_WIDTH / 4) {
  797. draw_VU(display, vu_bitmap, visu.bars[0].current, 0, visu.row, displayer.width / 2);
  798. draw_VU(display, vu_bitmap, visu.bars[1].current, displayer.width / 2, visu.row, displayer.width / 2);
  799. } else {
  800. int level = (visu.bars[0].current + visu.bars[1].current) / 2;
  801. draw_VU(display, vu_bitmap, level, 0, visu.row, displayer.width);
  802. }
  803. }
  804. /****************************************************************************************
  805. * Visu packet handler
  806. */
  807. void spectrum_limits(int min, int n, int pos) {
  808. if (n / 2) {
  809. int step = ((DISPLAY_BW - min) * visu.spectrum_scale) / (n/2);
  810. visu.bars[pos].limit = min + step;
  811. for (int i = 1; i < n/2; i++) visu.bars[pos+i].limit = visu.bars[pos+i-1].limit + step;
  812. spectrum_limits(visu.bars[pos + n/2 - 1].limit, n - n/2, pos + n/2);
  813. } else {
  814. visu.bars[pos].limit = DISPLAY_BW;
  815. }
  816. }
  817. /****************************************************************************************
  818. * Visu packet handler
  819. */
  820. static void visu_handler( u8_t *data, int len) {
  821. struct visu_packet *pkt = (struct visu_packet*) data;
  822. int bars = 0;
  823. LOG_DEBUG("visu %u with %u parameters", pkt->which, pkt->count);
  824. /*
  825. If width is specified, then respect all coordinates, otherwise we try to
  826. use the bottom part of the display and if it is a small display, we overwrite
  827. text
  828. */
  829. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  830. visu.mode = pkt->which;
  831. // little trick to clean the taller screens when switching visu
  832. if (visu.row >= SB_HEIGHT) GDS_ClearExt(display, false, true, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  833. if (visu.mode) {
  834. // these will be overidden if necessary
  835. visu.col = visu.border = 0;
  836. visu.width = displayer.width;
  837. // what type of visu
  838. if (visu.mode & VISU_ESP32) {
  839. if (pkt->count >= 4) {
  840. // more than 4 parameters, this is small visu, then go were we are told to
  841. pkt->height = htonl(pkt->height);
  842. pkt->row = htonl(pkt->row);
  843. pkt->col = htonl(pkt->col);
  844. visu.style = 0;
  845. visu.width = htonl(pkt->width);
  846. visu.height = pkt->height ? pkt->height : SB_HEIGHT;
  847. visu.col = pkt->col < 0 ? displayer.width + pkt->col : pkt->col;
  848. visu.row = pkt->row < 0 ? GDS_GetHeight(display) + pkt->row : pkt->row;
  849. visu.border = htonl(pkt->border);
  850. bars = htonl(pkt->bars);
  851. visu.spectrum_scale = htonl(pkt->spectrum_scale) / 100.;
  852. } else {
  853. // full screen visu, try to use bottom screen if available
  854. visu.height = GDS_GetHeight(display) > SB_HEIGHT ? GDS_GetHeight(display) - SB_HEIGHT : GDS_GetHeight(display);
  855. visu.row = GDS_GetHeight(display) - visu.height;
  856. // is this spectrum or analogue/digital
  857. if ((visu.mode & ~VISU_ESP32) == VISU_SPECTRUM) {
  858. bars = htonl(pkt->full.bars);
  859. visu.spectrum_scale = htonl(pkt->full.spectrum_scale) / 100.;
  860. } else {
  861. // select analogue/digital style
  862. visu.style = htonl(pkt->full.style);
  863. }
  864. }
  865. } else {
  866. // classical (screensaver) mode, don't try to optimize screen usage & force some params
  867. visu.row = 0;
  868. visu.height = SB_HEIGHT;
  869. visu.spectrum_scale = 0.25;
  870. if (visu.mode == VISU_SPECTRUM) bars = visu.width / (htonl(pkt->channels[0].bar_width) + htonl(pkt->channels[0].bar_space));
  871. else visu.style = htonl(pkt->classical_vu.style);
  872. if (bars > MAX_BARS) bars = MAX_BARS;
  873. }
  874. // try to adapt to what we have
  875. if ((visu.mode & ~VISU_ESP32) == VISU_SPECTRUM) {
  876. visu.n = bars ? bars : MAX_BARS;
  877. visu.max = displayer.height - 1;
  878. if (visu.spectrum_scale <= 0 || visu.spectrum_scale > 0.5) visu.spectrum_scale = 0.5;
  879. spectrum_limits(0, visu.n, 0);
  880. } else {
  881. visu.n = 2;
  882. visu.max = visu.style ? (VU_COUNT - 1) : (displayer.height - 1);
  883. }
  884. do {
  885. visu.bar_width = (visu.width - visu.border - visu.bar_gap * (visu.n - 1)) / visu.n;
  886. if (visu.bar_width > 0) break;
  887. } while (--visu.n);
  888. visu.bar_border = (visu.width - visu.border - (visu.bar_width + visu.bar_gap) * visu.n + visu.bar_gap) / 2;
  889. // give up if not enough space
  890. if (visu.bar_width < 0) {
  891. visu.mode = VISU_BLANK;
  892. LOG_WARN("Not enough room for displaying visu");
  893. } else {
  894. // de-activate scroller if we are taking main screen
  895. if (visu.row < SB_HEIGHT) scroller.active = false;
  896. vTaskResume(displayer.task);
  897. }
  898. visu.wake = 0;
  899. // reset bars maximum
  900. for (int i = visu.n; --i >= 0;) visu.bars[i].max = 0;
  901. GDS_ClearExt(display, false, true, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  902. 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);
  903. } else {
  904. LOG_INFO("Stopping visualizer");
  905. }
  906. xSemaphoreGive(displayer.mutex);
  907. }
  908. /****************************************************************************************
  909. * Scroll task
  910. * - with the addition of the visualizer, it's a bit a 2-headed beast not easy to
  911. * maintain, so som better separation between the visu and scroll is probably needed
  912. */
  913. static void displayer_task(void *args) {
  914. int sleep;
  915. while (1) {
  916. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  917. // suspend ourselves if nothing to do, grfg or visu will wake us up
  918. if (!scroller.active && !visu.mode) {
  919. xSemaphoreGive(displayer.mutex);
  920. vTaskSuspend(NULL);
  921. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  922. scroller.wake = visu.wake = 0;
  923. }
  924. // go for long sleep when either item is disabled
  925. if (!visu.mode) visu.wake = LONG_WAKE;
  926. if (!scroller.active) scroller.wake = LONG_WAKE;
  927. // scroll required amount of columns (within the window)
  928. if (scroller.active && scroller.wake <= 0) {
  929. // by default go for the long sleep, will change below if required
  930. scroller.wake = LONG_WAKE;
  931. // do we have more to scroll (scroll.width is the last column from which we have a full zone)
  932. if (scroller.by > 0 ? (scroller.scrolled <= scroller.scroll.width) : (scroller.scrolled >= 0)) {
  933. memcpy(scroller.frame, scroller.back.frame, scroller.back.width * displayer.height / 8);
  934. for (int i = 0; i < scroller.width * displayer.height / 8; i++) scroller.frame[i] |= scroller.scroll.frame[scroller.scrolled * displayer.height / 8 + i];
  935. scroller.scrolled += scroller.by;
  936. if (displayer.owned) GDS_DrawBitmapCBR(display, scroller.frame, scroller.width, displayer.height, GDS_COLOR_WHITE);
  937. // short sleep & don't need background update
  938. scroller.wake = scroller.speed;
  939. } else if (scroller.first || !scroller.mode) {
  940. // at least one round done
  941. scroller.first = false;
  942. // see if we need to pause or if we are done
  943. if (scroller.mode) {
  944. // can't call directly send_packet from slimproto as it's not re-entrant
  945. ANIC_resp = ANIM_SCROLL_ONCE | ANIM_SCREEN_1;
  946. LOG_INFO("scroll-once terminated");
  947. } else {
  948. scroller.wake = scroller.pause;
  949. LOG_DEBUG("scroll cycle done, pausing for %u (ms)", scroller.pause);
  950. }
  951. // need to reset pointers for next scroll
  952. scroller.scrolled = scroller.by < 0 ? scroller.scroll.width : 0;
  953. }
  954. }
  955. // update visu if active
  956. if (visu.mode && visu.wake <= 0) {
  957. visu_update();
  958. visu.wake = 100;
  959. }
  960. // need to make sure we own display
  961. if (displayer.owned) GDS_Update(display);
  962. // release semaphore and sleep what's needed
  963. xSemaphoreGive(displayer.mutex);
  964. sleep = min(visu.wake, scroller.wake);
  965. vTaskDelay(sleep / portTICK_PERIOD_MS);
  966. scroller.wake -= sleep;
  967. visu.wake -= sleep;
  968. }
  969. }