display.c 36 KB

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