display.c 33 KB

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