displayer.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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 48
  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 uint8_t* led_data;
  189. static EXT_RAM_ATTR struct {
  190. float fft[FFT_LEN*2], samples[FFT_LEN*2], hanning[FFT_LEN];
  191. int levels[2];
  192. } meters;
  193. static EXT_RAM_ATTR struct {
  194. int mode;
  195. int n, style, max;
  196. u16_t config;
  197. struct bar_s bars[MAX_BARS] ;
  198. } led_visu;
  199. static EXT_RAM_ATTR uint8_t vu_bitmap[VU_WIDTH * VU_HEIGHT];
  200. extern const uint8_t vu_base[] asm("_binary_vu_s_data_start");
  201. extern const struct {
  202. uint8_t offset;
  203. uint8_t data[VU_HEIGHT * ARROW_WIDTH];
  204. } vu_arrow[VU_COUNT] asm("_binary_arrow_data_start");
  205. #define ANIM_NONE 0x00
  206. #define ANIM_TRANSITION 0x01 // A transition animation has finished
  207. #define ANIM_SCROLL_ONCE 0x02
  208. #define ANIM_SCREEN_1 0x04
  209. #define ANIM_SCREEN_2 0x08
  210. #define SCROLL_STACK_SIZE (3*1024)
  211. #define LINELEN 40
  212. static log_level loglevel = lINFO;
  213. static bool (*slimp_handler_chain)(u8_t *data, int len);
  214. static void (*notify_chain)(in_addr_t ip, u16_t hport, u16_t cport);
  215. static bool (*display_bus_chain)(void *from, enum display_bus_cmd_e cmd);
  216. #define max(a,b) (((a) > (b)) ? (a) : (b))
  217. static void server(in_addr_t ip, u16_t hport, u16_t cport);
  218. static void sendSETD(u16_t width, u16_t height, u16_t led_config);
  219. static void sendANIC(u8_t code);
  220. static bool handler(u8_t *data, int len);
  221. static bool display_bus_handler(void *from, enum display_bus_cmd_e cmd);
  222. static void vfdc_handler( u8_t *_data, int bytes_read);
  223. static void grfe_handler( u8_t *data, int len);
  224. static void grfb_handler(u8_t *data, int len);
  225. static void grfs_handler(u8_t *data, int len);
  226. static void grfg_handler(u8_t *data, int len);
  227. static void grfa_handler(u8_t *data, int len);
  228. static void visu_handler(u8_t *data, int len);
  229. static void ledv_handler(u8_t *data, int len);
  230. static void ledd_handler(u8_t *data, int len);
  231. static void displayer_task(void* arg);
  232. /* scrolling undocumented information
  233. grfs
  234. B: screen number
  235. B:1 = left, 2 = right,
  236. Q: scroll pause once done (ms)
  237. Q: scroll speed (ms)
  238. W: # of pixels to scroll each time
  239. W: 0 = continue scrolling after pause, 1 = scroll to scrollend and then stop, 2 = scroll to scrollend and then end animation (causing new update)
  240. W: width of total scroll area in pixels
  241. grfd
  242. W: screen number
  243. W: width of scrollable area in pixels
  244. anic ( two versions, don't know what to chose)
  245. B: flag
  246. ANIM_TRANSITION (0x01) - transition animation has finished (previous use of ANIC)
  247. ANIM_SCREEN_1 (0x04) - end of first scroll on screen 1
  248. ANIM_SCREEN_2 (0x08) - end of first scroll on screen 2
  249. ANIM_SCROLL_ONCE (0x02) | ANIM_SCREEN_1 (0x04) - end of scroll once on screen 1
  250. ANIM_SCROLL_ONCE (0x02) | ANIM_SCREEN_2 (0x08) - end of scroll once on screen 2
  251. - or -
  252. ANIM_TRANSITION 0x01 # A transition animation has finished
  253. ANIM_SCROLL_ONCE 0x02 # A scrollonce has finished
  254. ANIM_SCREEN_1 0x04 # For scrollonce only, screen 1 was scrolling
  255. ANIM_SCREEN_2 0x08 # For scrollonce only, screen 2 was scrolling
  256. */
  257. /* classical visu not our specific version)
  258. Parameters for the spectrum analyzer:
  259. 0 - Channels: stereo == 0, mono == 1
  260. 1 - Bandwidth: 0..22050Hz == 0, 0..11025Hz == 1
  261. 2 - Preemphasis in dB per KHz
  262. Left channel parameters:
  263. 3 - Position in pixels
  264. 4 - Width in pixels
  265. 5 - orientation: left to right == 0, right to left == 1
  266. 6 - Bar width in pixels
  267. 7 - Bar spacing in pixels
  268. 8 - Clipping: show all subbands == 0, clip higher subbands == 1
  269. 9 - Bar intensity (greyscale): 1-3
  270. 10 - Bar cap intensity (greyscale): 1-3
  271. Right channel parameters (not required for mono):
  272. 11-18 - same as left channel parameters
  273. Parameters for the vumeter:
  274. 0 - Channels: stereo == 0, mono == 1
  275. 1 - Style: digital == 0, analog == 1
  276. Left channel parameters:
  277. 2 - Position in pixels
  278. 3 - Width in pixels
  279. Right channel parameters (not required for mono):
  280. 4-5 - same as left channel parameters
  281. */
  282. /****************************************************************************************
  283. *
  284. */
  285. bool sb_displayer_init(void) {
  286. static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
  287. static EXT_RAM_ATTR StackType_t xStack[SCROLL_STACK_SIZE] __attribute__ ((aligned (4)));
  288. // no display, just make sure we won't have requests
  289. if ((GDS_GetWidth(display) <= 0 || GDS_GetHeight(display) <= 0) && !led_display) {
  290. LOG_INFO("no display or led visualizer for LMS");
  291. return false;
  292. }
  293. if (display) {
  294. // need to force height to 32 maximum
  295. displayer.width = GDS_GetWidth(display);
  296. displayer.height = min(GDS_GetHeight(display), SB_HEIGHT);
  297. // allocate gray-color mapping if needed;
  298. if (GDS_GetMode(display) > GDS_GRAYSCALE) {
  299. grayMap = malloc(256*sizeof(*grayMap));
  300. for (int i = 0; i < 256; i++) grayMap[i] = GDS_GrayMap(display, i);
  301. }
  302. // create visu configuration
  303. visu.bar_gap = 1;
  304. visu.back.frame = calloc(1, (displayer.width * displayer.height) / 8);
  305. // prepare the VU raw data in PSRAM
  306. memcpy(vu_bitmap, vu_base, sizeof(vu_bitmap));
  307. // size scroller (width + current screen)
  308. scroller.scroll.max = (displayer.width * displayer.height / 8) * (15 + 1);
  309. scroller.scroll.frame = malloc(scroller.scroll.max);
  310. scroller.back.frame = malloc(displayer.width * displayer.height / 8);
  311. scroller.frame = malloc(displayer.width * displayer.height / 8);
  312. // chain handlers
  313. display_bus_chain = display_bus;
  314. display_bus = display_bus_handler;
  315. }
  316. if (led_display) {
  317. led_visu.config = led_vu_string_length();
  318. led_data = malloc(MAX_BARS);
  319. }
  320. // inform LMS of our screen/led dimensions
  321. sendSETD(GDS_GetWidth(display), GDS_GetHeight(display), led_visu.config);
  322. dsps_fft2r_init_fc32(meters.fft, FFT_LEN);
  323. dsps_wind_hann_f32(meters.hanning, FFT_LEN);
  324. // create displayer management task
  325. displayer.mutex = xSemaphoreCreateMutex();
  326. displayer.task = xTaskCreateStatic( (TaskFunction_t) displayer_task, "sb_displayer", SCROLL_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, &xTaskBuffer);
  327. // chain handlers
  328. slimp_handler_chain = slimp_handler;
  329. slimp_handler = handler;
  330. notify_chain = server_notify;
  331. server_notify = server;
  332. return display != NULL;
  333. }
  334. /****************************************************************************************
  335. * Receive display bus commands
  336. */
  337. static bool display_bus_handler(void *from, enum display_bus_cmd_e cmd) {
  338. // don't answer to own requests
  339. if (from == &displayer) return false ;
  340. LOG_INFO("Display bus command %d", cmd);
  341. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  342. switch (cmd) {
  343. case DISPLAY_BUS_TAKE:
  344. displayer.owned = false;
  345. break;
  346. case DISPLAY_BUS_GIVE:
  347. displayer.owned = true;
  348. break;
  349. }
  350. xSemaphoreGive(displayer.mutex);
  351. // chain to rest of "bus"
  352. if (display_bus_chain) return (*display_bus_chain)(from, cmd);
  353. else return true;
  354. }
  355. /****************************************************************************************
  356. * Send ANImation Complete
  357. */
  358. static void sendANIC(u8_t code) {
  359. struct ANIC_header pkt_header;
  360. memset(&pkt_header, 0, sizeof(pkt_header));
  361. memcpy(&pkt_header.opcode, "ANIC", 4);
  362. pkt_header.length = htonl(sizeof(pkt_header) - 8);
  363. pkt_header.mode = code;
  364. LOCK_P;
  365. send_packet((uint8_t *) &pkt_header, sizeof(pkt_header));
  366. UNLOCK_P;
  367. }
  368. /****************************************************************************************
  369. * Send SETD for width
  370. */
  371. static void sendSETD(u16_t width, u16_t height, u16_t led_config) {
  372. struct SETD_header pkt_header;
  373. memset(&pkt_header, 0, sizeof(pkt_header));
  374. memcpy(&pkt_header.opcode, "SETD", 4);
  375. pkt_header.id = 0xfe; // id 0xfe is width S:P:Squeezebox2
  376. pkt_header.length = htonl(sizeof(pkt_header) + 6 - 8);
  377. LOG_INFO("sending dimension display:%ux%u led_config:%u", width, height, led_config);
  378. width = htons(width);
  379. height = htons(height);
  380. led_config = htons(led_config);
  381. LOCK_P;
  382. send_packet((uint8_t *) &pkt_header, sizeof(pkt_header));
  383. send_packet((uint8_t *) &width, 2);
  384. send_packet((uint8_t *) &height, 2);
  385. send_packet((uint8_t *) &led_config, 2);
  386. UNLOCK_P;
  387. }
  388. /****************************************************************************************
  389. *
  390. */
  391. static void server(in_addr_t ip, u16_t hport, u16_t cport) {
  392. char msg[32];
  393. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  394. sprintf(msg, "%s:%hu", inet_ntoa(ip), hport);
  395. if (display && displayer.owned) GDS_TextPos(display, GDS_FONT_LINE_1, GDS_TEXT_CENTERED, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, msg);
  396. displayer.dirty = true;
  397. xSemaphoreGive(displayer.mutex);
  398. // inform new LMS server of our capabilities
  399. sendSETD(GDS_GetWidth(display), GDS_GetHeight(display), led_visu.config);
  400. if (notify_chain) (*notify_chain)(ip, hport, cport);
  401. }
  402. /****************************************************************************************
  403. * Process graphic display data
  404. */
  405. static bool handler(u8_t *data, int len){
  406. bool res = true;
  407. if (!strncmp((char*) data, "vfdc", 4)) {
  408. vfdc_handler(data, len);
  409. } else if (!strncmp((char*) data, "grfe", 4)) {
  410. grfe_handler(data, len);
  411. } else if (!strncmp((char*) data, "grfb", 4)) {
  412. grfb_handler(data, len);
  413. } else if (!strncmp((char*) data, "grfs", 4)) {
  414. grfs_handler(data, len);
  415. } else if (!strncmp((char*) data, "grfg", 4)) {
  416. grfg_handler(data, len);
  417. } else if (!strncmp((char*) data, "grfa", 4)) {
  418. grfa_handler(data, len);
  419. } else if (!strncmp((char*) data, "visu", 4)) {
  420. visu_handler(data, len);
  421. } else if (!strncmp((char*) data, "ledv", 4)) {
  422. ledv_handler(data, len);
  423. } else if (!strncmp((char*) data, "ledd", 4)) {
  424. ledd_handler(data, len);
  425. } else {
  426. res = false;
  427. }
  428. // chain protocol handlers (bitwise or is fine)
  429. if (*slimp_handler_chain) res |= (*slimp_handler_chain)(data, len);
  430. return res;
  431. }
  432. /****************************************************************************************
  433. * Change special LCD chars to something more printable on screen
  434. */
  435. static void makeprintable(unsigned char * line) {
  436. for (int n = 0; n < LINELEN; n++) {
  437. switch (line[n]) {
  438. case 11: /* block */
  439. line[n] = '#';
  440. break;;
  441. case 16: /* rightarrow */
  442. line[n] = '>';
  443. break;;
  444. case 22: /* circle */
  445. line[n] = '@';
  446. break;;
  447. case 145: /* note */
  448. line[n] = ' ';
  449. break;;
  450. case 152: /* bell */
  451. line[n] = 'o';
  452. break;
  453. default:
  454. break;
  455. }
  456. }
  457. }
  458. /****************************************************************************************
  459. * Check if char is printable, or a valid symbol
  460. */
  461. static bool charisok(unsigned char c) {
  462. switch (c) {
  463. case 11: /* block */
  464. case 16: /* rightarrow */
  465. case 22: /* circle */
  466. case 145: /* note */
  467. case 152: /* bell */
  468. return true;
  469. break;;
  470. default:
  471. return isprint(c);
  472. }
  473. }
  474. /****************************************************************************************
  475. * Show the display (text mode)
  476. */
  477. static void show_display_buffer(char *ddram) {
  478. char line1[LINELEN+1];
  479. char *line2;
  480. memset(line1, 0, LINELEN+1);
  481. strncpy(line1, ddram, LINELEN+1);
  482. line1[LINELEN] = '\0';
  483. line2 = &(ddram[LINELEN]);
  484. line2[LINELEN] = '\0';
  485. /* Convert special LCD chars */
  486. makeprintable((unsigned char *)line1);
  487. makeprintable((unsigned char *)line2);
  488. LOG_DEBUG("\n\t%.40s\n\t%.40s", line1, line2);
  489. GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_CLEAR, line1);
  490. GDS_TextLine(display, 2, GDS_TEXT_LEFT, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, line2);
  491. }
  492. /****************************************************************************************
  493. * Process display data
  494. */
  495. static void vfdc_handler( u8_t *_data, int bytes_read) {
  496. unsigned short *data = (unsigned short*) _data, *display_data;
  497. char ddram[(LINELEN + 1) * 2];
  498. int n, addr = 0; /* counter */
  499. bytes_read -= 4;
  500. if (bytes_read % 2) bytes_read--; /* even number of bytes */
  501. // if we use Noritake VFD codes, display data starts at 12
  502. display_data = &(data[5]); /* display data starts at byte 10 */
  503. memset(ddram, ' ', LINELEN * 2);
  504. for (n = 0; n < (bytes_read/2); n++) {
  505. unsigned short d; /* data element */
  506. unsigned char t, c;
  507. d = ntohs(display_data[n]);
  508. t = (d & 0x00ff00) >> 8; /* type of display data */
  509. c = (d & 0x0000ff); /* character/command */
  510. switch (t) {
  511. case 0x03: /* character */
  512. if (!charisok(c)) c = ' ';
  513. if (addr <= LINELEN * 2) {
  514. ddram[addr++] = c;
  515. }
  516. break;
  517. case 0x02: /* command */
  518. switch (c) {
  519. case 0x06: /* display clear */
  520. memset(ddram, ' ', LINELEN * 2);
  521. break;
  522. case 0x02: /* cursor home */
  523. addr = 0;
  524. break;
  525. case 0xc0: /* cursor home2 */
  526. addr = LINELEN;
  527. break;
  528. }
  529. }
  530. }
  531. show_display_buffer(ddram);
  532. }
  533. /****************************************************************************************
  534. * Display VU-Meter (lots of hard-coding)
  535. */
  536. void draw_VU(struct GDS_Device * display, int level, int x, int y, int width, bool rotate) {
  537. // VU data is by columns and vertical flip to allow block offset
  538. uint8_t *data = vu_bitmap;
  539. int offset = level > 0 ? vu_arrow[level].offset * VU_HEIGHT : 0;
  540. // place the arrow in base VU
  541. memcpy(data + offset, vu_arrow[level].data, sizeof(vu_arrow[level].data));
  542. // adjust to current display window
  543. if (width > VU_WIDTH) {
  544. if (rotate) y += (width - VU_WIDTH) / 2;
  545. else x += (width - VU_WIDTH) / 2;
  546. width = VU_WIDTH;
  547. } else {
  548. data += (VU_WIDTH - width) / 2 * VU_HEIGHT;
  549. }
  550. if (GDS_GetMode(display) <= GDS_GRAYSCALE) {
  551. // this is 8 bits grayscale
  552. int scale = 8 - GDS_GetDepth(display);
  553. // use "fast" version as we are not beyond screen boundaries
  554. if (rotate) {
  555. for (int r = 0; r < width; r++) {
  556. for (int c = VU_HEIGHT; --c >= 0;) {
  557. GDS_DrawPixelFast(display, c + x, r + y, *data++ >> scale);
  558. }
  559. }
  560. } else {
  561. for (int r = 0; r < width; r++) {
  562. for (int c = 0; c < VU_HEIGHT; c++) {
  563. GDS_DrawPixelFast(display, r + x, c + y, *data++ >> scale);
  564. }
  565. }
  566. }
  567. } else {
  568. // use "fast" version as we are not beyond screen boundaries
  569. if (rotate) {
  570. for (int r = 0; r < width; r++) {
  571. for (int c = VU_HEIGHT; --c >= 0;) {
  572. GDS_DrawPixelFast(display, c + x, r + y, grayMap[*data++]);
  573. }
  574. }
  575. } else {
  576. for (int r = 0; r < width; r++) {
  577. for (int c = 0; c < VU_HEIGHT; c++) {
  578. GDS_DrawPixelFast(display, r + x, c + y, grayMap[*data++]);
  579. }
  580. }
  581. }
  582. }
  583. // restore base VU
  584. memcpy(vu_bitmap + offset, vu_base + offset, sizeof(vu_arrow[level].data));
  585. // need to manually set dirty flag as DrawPixel does not do it
  586. GDS_SetDirty(display);
  587. }
  588. /****************************************************************************************
  589. * Process graphic display data
  590. */
  591. static void grfe_handler( u8_t *data, int len) {
  592. struct grfe_packet *pkt = (struct grfe_packet*) data;
  593. // we don't support transition, simply claim we're done
  594. if (pkt->transition != 'c') {
  595. LOG_INFO("Transition %c requested with offset %hu, param %d", pkt->transition, pkt->offset, pkt->param);
  596. sendANIC(ANIM_TRANSITION);
  597. }
  598. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  599. scroller.active = false;
  600. // full screen artwork or for small screen, full screen visu has priority
  601. if (((visu.mode & VISU_ESP32) && !visu.col && visu.row < displayer.height) || artwork.full) {
  602. xSemaphoreGive(displayer.mutex);
  603. return;
  604. }
  605. // are we in control
  606. if (displayer.owned) {
  607. // draw new frame, it might be less than full screen (small visu)
  608. int width = ((len - sizeof(struct grfe_packet)) * 8) / displayer.height;
  609. // did we have something that might have written on the bottom of a displayer's height + display
  610. if (displayer.dirty || (artwork.enable && width == displayer.width && artwork.y < displayer.height)) {
  611. GDS_Clear(display, GDS_COLOR_BLACK);
  612. displayer.dirty = false;
  613. }
  614. // when doing screensaver, that frame becomes a visu background
  615. if (!(visu.mode & VISU_ESP32)) {
  616. visu.back.width = width;
  617. memset(visu.back.frame, 0, (displayer.width * displayer.height) / 8);
  618. memcpy(visu.back.frame, data + sizeof(struct grfe_packet), (width * displayer.height) / 8);
  619. // this is a bit tricky but basically that checks if frame if full of 0
  620. visu.back.active = *visu.back.frame || memcmp(visu.back.frame, visu.back.frame + 1, width - 1);
  621. }
  622. GDS_DrawBitmapCBR(display, data + sizeof(struct grfe_packet), width, displayer.height, GDS_COLOR_WHITE);
  623. GDS_Update(display);
  624. }
  625. xSemaphoreGive(displayer.mutex);
  626. LOG_DEBUG("grfe frame %u", len);
  627. }
  628. /****************************************************************************************
  629. * Brightness
  630. */
  631. static void grfb_handler(u8_t *data, int len) {
  632. struct grfb_packet *pkt = (struct grfb_packet*) data;
  633. pkt->brightness = htons(pkt->brightness);
  634. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  635. // LMS driver sends 0..5 value, we assume driver is highly log
  636. if (pkt->brightness <= 0) {
  637. GDS_DisplayOff(display);
  638. } else {
  639. GDS_DisplayOn(display);
  640. GDS_SetContrast(display, 255 * powf(pkt->brightness / 5.0f, 3));
  641. }
  642. xSemaphoreGive(displayer.mutex);
  643. LOG_INFO("brightness %hu", pkt->brightness);
  644. }
  645. /****************************************************************************************
  646. * Scroll set
  647. */
  648. static void grfs_handler(u8_t *data, int len) {
  649. struct grfs_packet *pkt = (struct grfs_packet*) data;
  650. int size = len - sizeof(struct grfs_packet);
  651. int offset = htons(pkt->offset);
  652. LOG_DEBUG("grfs s:%u d:%u p:%u sp:%u by:%hu m:%hu w:%hu o:%hu",
  653. (int) pkt->screen,
  654. (int) pkt->direction, // 1=left, 2=right
  655. htonl(pkt->pause), // in ms
  656. htonl(pkt->speed), // in ms
  657. htons(pkt->by), // # of pixel of scroll step
  658. htons(pkt->mode), // 0=continuous, 1=once and stop, 2=once and end
  659. htons(pkt->width), // last column of animation that contains a "full" screen
  660. htons(pkt->offset) // offset if multiple packets are sent
  661. );
  662. // new grfs frame, build scroller info
  663. if (!offset) {
  664. // use the display as a general lock
  665. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  666. // copy & set scroll parameters
  667. scroller.screen = pkt->screen;
  668. scroller.pause = htonl(pkt->pause);
  669. scroller.speed = htonl(pkt->speed);
  670. scroller.mode = htons(pkt->mode);
  671. scroller.scroll.width = htons(pkt->width);
  672. scroller.first = true;
  673. scroller.overflow = false;
  674. // set scroller steps & beginning
  675. if (pkt->direction == 1) {
  676. scroller.scrolled = 0;
  677. scroller.by = htons(pkt->by);
  678. } else {
  679. scroller.scrolled = scroller.scroll.width;
  680. scroller.by = -htons(pkt->by);
  681. }
  682. xSemaphoreGive(displayer.mutex);
  683. }
  684. // copy scroll frame data (no semaphore needed)
  685. if (scroller.scroll.size + size < scroller.scroll.max && !scroller.overflow) {
  686. memcpy(scroller.scroll.frame + offset, data + sizeof(struct grfs_packet), size);
  687. scroller.scroll.size = offset + size;
  688. LOG_INFO("scroller current size %u (w:%u)", scroller.scroll.size, scroller.scroll.width);
  689. } else {
  690. LOG_INFO("scroller too large %u/%u (w:%u)", scroller.scroll.size + size, scroller.scroll.max, scroller.scroll.width);
  691. scroller.scroll.width = scroller.scroll.size / (displayer.height / 8) - scroller.back.width;
  692. scroller.overflow = true;
  693. }
  694. }
  695. /****************************************************************************************
  696. * Scroll background frame update & go
  697. */
  698. static void grfg_handler(u8_t *data, int len) {
  699. struct grfg_packet *pkt = (struct grfg_packet*) data;
  700. LOG_DEBUG("gfrg s:%hu w:%hu (len:%u)", htons(pkt->screen), htons(pkt->width), len);
  701. // full screen artwork or for small screen, visu has priority when full screen
  702. if (((visu.mode & VISU_ESP32) && !visu.col && visu.row < displayer.height) || artwork.full) {
  703. return;
  704. }
  705. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  706. // size of scrollable area (less than background)
  707. scroller.width = htons(pkt->width);
  708. scroller.back.width = ((len - sizeof(struct grfg_packet)) * 8) / displayer.height;
  709. memcpy(scroller.back.frame, data + sizeof(struct grfg_packet), len - sizeof(struct grfg_packet));
  710. // update display asynchronously (frames are organized by columns)
  711. memcpy(scroller.frame, scroller.back.frame, scroller.back.width * displayer.height / 8);
  712. for (int i = 0; i < scroller.width * displayer.height / 8; i++) scroller.frame[i] |= scroller.scroll.frame[scroller.scrolled * displayer.height / 8 + i];
  713. // can only write if we really own display
  714. if (displayer.owned) {
  715. GDS_DrawBitmapCBR(display, scroller.frame, scroller.back.width, displayer.height, GDS_COLOR_WHITE);
  716. GDS_Update(display);
  717. }
  718. // now we can active scrolling, but only if we are not on a small screen
  719. if (!visu.mode || visu.col || visu.row >= displayer.height) scroller.active = true;
  720. // if we just got a content update, let the scroller manage the screen
  721. LOG_DEBUG("resuming scrolling task");
  722. xSemaphoreGive(displayer.mutex);
  723. // resume task once we have background, not in grfs
  724. vTaskResume(displayer.task);
  725. }
  726. /****************************************************************************************
  727. * Artwork
  728. */
  729. static void grfa_handler(u8_t *data, int len) {
  730. struct grfa_packet *pkt = (struct grfa_packet*) data;
  731. int size = len - sizeof(struct grfa_packet);
  732. int offset = htonl(pkt->offset);
  733. int length = htonl(pkt->length);
  734. // when using full screen visualizer on small screen there is a brief overlay
  735. artwork.enable = (length != 0);
  736. // just a config or an actual artwork
  737. if (length < 32) {
  738. if (artwork.enable) {
  739. // this is just to specify artwork coordinates
  740. artwork.x = htons(pkt->x);
  741. artwork.y = htons(pkt->y);
  742. } else if (artwork.size) GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
  743. artwork.full = artwork.enable && artwork.x == 0 && artwork.y == 0;
  744. LOG_DEBUG("gfra en:%u x:%hu, y:%hu", artwork.enable, artwork.x, artwork.y);
  745. // done in any case
  746. return;
  747. }
  748. // new grfa artwork, allocate memory
  749. if (!offset) {
  750. // same trick to clean current/previous window
  751. if (artwork.size) {
  752. GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
  753. artwork.size = 0;
  754. }
  755. // now use new parameters
  756. artwork.x = htons(pkt->x);
  757. artwork.y = htons(pkt->y);
  758. artwork.full = artwork.enable && artwork.x == 0 && artwork.y == 0;
  759. if (artwork.data) free(artwork.data);
  760. artwork.data = malloc(length);
  761. }
  762. // copy artwork data
  763. memcpy(artwork.data + offset, data + sizeof(struct grfa_packet), size);
  764. artwork.size += size;
  765. if (artwork.size == length) {
  766. GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
  767. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  768. GDS_DrawJPEG(display, artwork.data, artwork.x, artwork.y, artwork.y < displayer.height ? (GDS_IMAGE_RIGHT | GDS_IMAGE_TOP) : GDS_IMAGE_CENTER);
  769. xSemaphoreGive(displayer.mutex);
  770. free(artwork.data);
  771. artwork.data = NULL;
  772. }
  773. LOG_DEBUG("gfra l:%u x:%hu, y:%hu, o:%u s:%u", length, artwork.x, artwork.y, offset, size);
  774. }
  775. /****************************************************************************************
  776. * Fit spectrum into N bands and convert to dB
  777. */
  778. void spectrum_scale(int n, struct bar_s *bars, int max, float *samples) {
  779. float rate = visu_export.rate;
  780. // now arrange the result with the number of bar and sampling rate (don't want DC)
  781. for (int i = 0, j = 1; i < n && j < (FFT_LEN / 2); i++) {
  782. float power, count;
  783. // find the next point in FFT (this is real signal, so only half matters)
  784. for (count = 0, power = 0; j * visu_export.rate < bars[i].limit * FFT_LEN && j < FFT_LEN / 2; j++, count += 1) {
  785. power += samples[2*j] * samples[2*j] + samples[2*j+1] * samples[2*j+1];
  786. }
  787. // due to sample rate, we have reached the end of the available spectrum
  788. if (j >= (FFT_LEN / 2)) {
  789. // normalize accumulated data
  790. if (count) power /= count * 2.;
  791. } else if (count) {
  792. // how much of what remains do we need to add
  793. float ratio = j - (bars[i].limit * FFT_LEN) / rate;
  794. power += (samples[2*j] * samples[2*j] + samples[2*j+1] * samples[2*j+1]) * ratio;
  795. // normalize accumulated data
  796. power /= (count + ratio) * 2;
  797. } else {
  798. // no data for that band (sampling rate too high), just assume same as previous one
  799. power = (samples[2*j] * samples[2*j] + samples[2*j+1] * samples[2*j+1]) / 2.;
  800. }
  801. // convert to dB and bars, same back-off
  802. bars[i].current = max * (0.01667f*10*(log10f(0.0000001f + power) - log10f(FFT_LEN*(visu_export.gain == FIXED_ONE ? 256 : 2))) - 0.2543f);
  803. if (bars[i].current > max) bars[i].current = max;
  804. else if (bars[i].current < 0) bars[i].current = 0;
  805. }
  806. }
  807. /****************************************************************************************
  808. * Fit levels to max and convert to dB
  809. */
  810. void vu_scale(struct bar_s *bars, int max, int *levels) {
  811. // convert to dB (1 bit remaining for getting X²/N, 60dB dynamic starting from 0dBFS = 3 bits back-off)
  812. for (int i = 2; --i >= 0;) {
  813. bars[i].current = max * (0.01667f*10*log10f(0.0000001f + (levels[i] >> (visu_export.gain == FIXED_ONE ? 8 : 1))) - 0.2543f);
  814. if (bars[i].current > max) bars[i].current = max;
  815. else if (bars[i].current < 0) bars[i].current = 0;
  816. }
  817. }
  818. /****************************************************************************************
  819. * visu draw
  820. */
  821. void visu_draw(void) {
  822. // don't refresh screen if all max are 0 (we were are somewhat idle)
  823. int clear = 0;
  824. for (int i = visu.n; --i >= 0;) clear = max(clear, visu.bars[i].max);
  825. if (clear) GDS_ClearExt(display, false, false, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  826. // draw background if we are in screensaver mode
  827. if (!(visu.mode & VISU_ESP32) && visu.back.active) {
  828. GDS_DrawBitmapCBR(display, visu.back.frame, visu.back.width, displayer.height, GDS_COLOR_WHITE);
  829. }
  830. if ((visu.mode & ~VISU_ESP32) != VISU_VUMETER || !visu.style) {
  831. // there is much more optimization to be done here, like not redrawing bars unless needed
  832. for (int i = visu.n; --i >= 0;) {
  833. // update maximum
  834. if (visu.bars[i].current > visu.bars[i].max) visu.bars[i].max = visu.bars[i].current;
  835. else if (visu.bars[i].max) visu.bars[i].max--;
  836. else if (!clear) continue;
  837. if (visu.rotate) {
  838. int x1 = visu.col;
  839. int y1 = visu.row + visu.border + visu.bar_border + i*(visu.bar_width + visu.bar_gap);
  840. for (int j = 0; j <= visu.bars[i].current; j += 2)
  841. GDS_DrawLine(display, x1 + j, y1, x1 + j, y1 + visu.bar_width - 1, GDS_COLOR_WHITE);
  842. if (visu.bars[i].max > 2) {
  843. GDS_DrawLine(display, x1 + visu.bars[i].max, y1, x1 + visu.bars[i].max, y1 + visu.bar_width - 1, GDS_COLOR_WHITE);
  844. 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);
  845. }
  846. } else {
  847. int x1 = visu.col + visu.border + visu.bar_border + i*(visu.bar_width + visu.bar_gap);
  848. int y1 = visu.row + visu.height - 1;
  849. for (int j = 0; j <= visu.bars[i].current; j += 2)
  850. GDS_DrawLine(display, x1, y1 - j, x1 + visu.bar_width - 1, y1 - j, GDS_COLOR_WHITE);
  851. if (visu.bars[i].max > 2) {
  852. GDS_DrawLine(display, x1, y1 - visu.bars[i].max, x1 + visu.bar_width - 1, y1 - visu.bars[i].max, GDS_COLOR_WHITE);
  853. 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);
  854. }
  855. }
  856. }
  857. } else if (displayer.width / 2 >= 3 * VU_WIDTH / 4) {
  858. if (visu.rotate) {
  859. draw_VU(display, visu.bars[0].current, 0, visu.row, visu.height / 2, visu.rotate);
  860. draw_VU(display, visu.bars[1].current, 0, visu.row + visu.height / 2, visu.height / 2, visu.rotate);
  861. } else {
  862. draw_VU(display, visu.bars[0].current, 0, visu.row, visu.width / 2, visu.rotate);
  863. draw_VU(display, visu.bars[1].current, visu.width / 2, visu.row, visu.width / 2, visu.rotate);
  864. }
  865. } else {
  866. int level = (visu.bars[0].current + visu.bars[1].current) / 2;
  867. draw_VU(display, level, 0, visu.row, visu.rotate ? visu.height : visu.width, visu.rotate);
  868. }
  869. }
  870. /****************************************************************************************
  871. * Update displayer
  872. */
  873. static void displayer_update(void) {
  874. // 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
  875. if ((artwork.full && !led_visu.mode) || pthread_mutex_trylock(&visu_export.mutex)) {
  876. return;
  877. }
  878. int mode = (visu.mode & ~VISU_ESP32) | led_visu.mode;
  879. // not enough frames
  880. if (visu_export.level < (mode & VISU_SPECTRUM ? FFT_LEN : RMS_LEN) && visu_export.running) {
  881. pthread_mutex_unlock(&visu_export.mutex);
  882. return;
  883. }
  884. // reset all levels no matter what
  885. meters.levels[0] = meters.levels[1] = 0;
  886. memset(meters.samples, 0, sizeof(meters.samples));
  887. if (visu_export.running) {
  888. // calculate data for VU-meter
  889. if (mode & VISU_VUMETER) {
  890. s16_t *iptr = (s16_t*) visu_export.buffer + (BYTES_PER_FRAME / 4) - 1;
  891. int *left = &meters.levels[0], *right = &meters.levels[1];
  892. // calculate sum(L²+R²), try to not overflow at the expense of some precision
  893. for (int i = RMS_LEN; --i >= 0;) {
  894. *left += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
  895. iptr += BYTES_PER_FRAME / 4;
  896. *right += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
  897. iptr += BYTES_PER_FRAME / 4;
  898. }
  899. }
  900. // calculate data for spectrum
  901. if (mode & VISU_SPECTRUM) {
  902. s16_t *iptr = (s16_t*) visu_export.buffer + (BYTES_PER_FRAME / 4) - 1;
  903. // on xtensa/esp32 the floating point FFT takes 1/2 cycles of the fixed point
  904. for (int i = 0 ; i < FFT_LEN ; i++) {
  905. // don't normalize here, but we are due INT16_MAX and FFT_LEN / 2 / 2
  906. meters.samples[i * 2 + 0] = (float) (*iptr + *(iptr+BYTES_PER_FRAME/4)) * meters.hanning[i];
  907. meters.samples[i * 2 + 1] = 0;
  908. iptr += 2 * BYTES_PER_FRAME / 4;
  909. }
  910. // actual FFT that might be less cycle than all the crap below
  911. dsps_fft2r_fc32_ae32(meters.samples, FFT_LEN);
  912. dsps_bit_rev_fc32_ansi(meters.samples, FFT_LEN);
  913. }
  914. }
  915. // we took what we want, we can release the buffer
  916. visu_export.level = 0;
  917. pthread_mutex_unlock(&visu_export.mutex);
  918. // actualize the display
  919. if (visu.mode && !artwork.full) {
  920. if (visu.mode & VISU_SPECTRUM) spectrum_scale(visu.n, visu.bars, visu.max, meters.samples);
  921. else for (int i = 2; --i >= 0;) vu_scale(visu.bars, visu.max, meters.levels);
  922. visu_draw();
  923. }
  924. // actualize led_vu
  925. if (led_display && led_visu.mode) {
  926. // run built in visualizer effects
  927. if (led_visu.mode == VISU_VUMETER) {
  928. vu_scale(led_visu.bars, led_visu.max, meters.levels);
  929. led_vu_display(led_visu.bars[0].current, led_visu.bars[1].current, led_visu.max, led_visu.style);
  930. } else if (led_visu.mode == VISU_SPECTRUM) {
  931. spectrum_scale(led_visu.n, led_visu.bars, led_visu.max, meters.samples);
  932. uint8_t* p = (uint8_t*) led_data;
  933. for (int i = 0; i < led_visu.n; i++) {
  934. *p = led_visu.bars[i].current;
  935. p++;
  936. }
  937. led_vu_spectrum(led_data, led_visu.max, led_visu.n, led_visu.style);
  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[led_visu.n-2].current,
  942. led_visu.bars[(led_visu.n/2)+1].current * 50 / led_visu.max,
  943. led_visu.bars[1].current * 4 / 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. }