display.c 35 KB

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