displayer.c 44 KB

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