displayer.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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, gain;
  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. GDS_CHECK_FOR_DEVICE(display,return);
  594. // we don't support transition, simply claim we're done
  595. if (pkt->transition != 'c') {
  596. LOG_INFO("Transition %c requested with offset %hu, param %d", pkt->transition, pkt->offset, pkt->param);
  597. sendANIC(ANIM_TRANSITION);
  598. }
  599. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  600. scroller.active = false;
  601. // full screen artwork or for small screen, full screen visu has priority
  602. if (((visu.mode & VISU_ESP32) && !visu.col && visu.row < displayer.height) || artwork.full) {
  603. xSemaphoreGive(displayer.mutex);
  604. return;
  605. }
  606. // are we in control
  607. if (displayer.owned) {
  608. // draw new frame, it might be less than full screen (small visu)
  609. int width = ((len - sizeof(struct grfe_packet)) * 8) / displayer.height;
  610. // did we have something that might have written on the bottom of a displayer's height + display
  611. if (displayer.dirty || (artwork.enable && width == displayer.width && artwork.y < displayer.height)) {
  612. GDS_Clear(display, GDS_COLOR_BLACK);
  613. displayer.dirty = false;
  614. }
  615. // when doing screensaver, that frame becomes a visu background
  616. if (!(visu.mode & VISU_ESP32)) {
  617. visu.back.width = width;
  618. memset(visu.back.frame, 0, (displayer.width * displayer.height) / 8);
  619. memcpy(visu.back.frame, data + sizeof(struct grfe_packet), (width * displayer.height) / 8);
  620. // this is a bit tricky but basically that checks if frame if full of 0
  621. visu.back.active = *visu.back.frame || memcmp(visu.back.frame, visu.back.frame + 1, width - 1);
  622. }
  623. GDS_DrawBitmapCBR(display, data + sizeof(struct grfe_packet), width, displayer.height, GDS_COLOR_WHITE);
  624. GDS_Update(display);
  625. }
  626. xSemaphoreGive(displayer.mutex);
  627. LOG_DEBUG("grfe frame %u", len);
  628. }
  629. /****************************************************************************************
  630. * Brightness
  631. */
  632. static void grfb_handler(u8_t *data, int len) {
  633. struct grfb_packet *pkt = (struct grfb_packet*) data;
  634. pkt->brightness = htons(pkt->brightness);
  635. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  636. // LMS driver sends 0..5 value, we assume driver is highly log
  637. if (pkt->brightness <= 0) {
  638. GDS_DisplayOff(display);
  639. } else {
  640. GDS_DisplayOn(display);
  641. GDS_SetContrast(display, 255 * powf(pkt->brightness / 5.0f, 3));
  642. }
  643. xSemaphoreGive(displayer.mutex);
  644. LOG_INFO("brightness %hu", pkt->brightness);
  645. }
  646. /****************************************************************************************
  647. * Scroll set
  648. */
  649. static void grfs_handler(u8_t *data, int len) {
  650. struct grfs_packet *pkt = (struct grfs_packet*) data;
  651. int size = len - sizeof(struct grfs_packet);
  652. int offset = htons(pkt->offset);
  653. LOG_DEBUG("grfs s:%u d:%u p:%u sp:%u by:%hu m:%hu w:%hu o:%hu",
  654. (int) pkt->screen,
  655. (int) pkt->direction, // 1=left, 2=right
  656. htonl(pkt->pause), // in ms
  657. htonl(pkt->speed), // in ms
  658. htons(pkt->by), // # of pixel of scroll step
  659. htons(pkt->mode), // 0=continuous, 1=once and stop, 2=once and end
  660. htons(pkt->width), // last column of animation that contains a "full" screen
  661. htons(pkt->offset) // offset if multiple packets are sent
  662. );
  663. GDS_CHECK_FOR_DEVICE(display,return);
  664. // new grfs frame, build scroller info
  665. if (!offset) {
  666. // use the display as a general lock
  667. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  668. // copy & set scroll parameters
  669. scroller.screen = pkt->screen;
  670. scroller.pause = htonl(pkt->pause);
  671. scroller.speed = htonl(pkt->speed);
  672. scroller.mode = htons(pkt->mode);
  673. scroller.scroll.width = htons(pkt->width);
  674. scroller.first = true;
  675. scroller.overflow = false;
  676. // set scroller steps & beginning
  677. if (pkt->direction == 1) {
  678. scroller.scrolled = 0;
  679. scroller.by = htons(pkt->by);
  680. } else {
  681. scroller.scrolled = scroller.scroll.width;
  682. scroller.by = -htons(pkt->by);
  683. }
  684. xSemaphoreGive(displayer.mutex);
  685. }
  686. // copy scroll frame data (no semaphore needed)
  687. if (scroller.scroll.size + size < scroller.scroll.max && !scroller.overflow) {
  688. memcpy(scroller.scroll.frame + offset, data + sizeof(struct grfs_packet), size);
  689. scroller.scroll.size = offset + size;
  690. LOG_INFO("scroller current size %u (w:%u)", scroller.scroll.size, scroller.scroll.width);
  691. } else {
  692. LOG_INFO("scroller too large %u/%u (w:%u)", scroller.scroll.size + size, scroller.scroll.max, scroller.scroll.width);
  693. scroller.scroll.width = scroller.scroll.size / (displayer.height / 8) - scroller.back.width;
  694. scroller.overflow = true;
  695. }
  696. }
  697. /****************************************************************************************
  698. * Scroll background frame update & go
  699. */
  700. static void grfg_handler(u8_t *data, int len) {
  701. struct grfg_packet *pkt = (struct grfg_packet*) data;
  702. LOG_DEBUG("gfrg s:%hu w:%hu (len:%u)", htons(pkt->screen), htons(pkt->width), len);
  703. GDS_CHECK_FOR_DEVICE(display,return);
  704. // full screen artwork or for small screen, visu has priority when full screen
  705. if (((visu.mode & VISU_ESP32) && !visu.col && visu.row < displayer.height) || artwork.full) {
  706. return;
  707. }
  708. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  709. // size of scrollable area (less than background)
  710. scroller.width = htons(pkt->width);
  711. scroller.back.width = ((len - sizeof(struct grfg_packet)) * 8) / displayer.height;
  712. memcpy(scroller.back.frame, data + sizeof(struct grfg_packet), len - sizeof(struct grfg_packet));
  713. // update display asynchronously (frames are organized by columns)
  714. memcpy(scroller.frame, scroller.back.frame, scroller.back.width * displayer.height / 8);
  715. for (int i = 0; i < scroller.width * displayer.height / 8; i++) scroller.frame[i] |= scroller.scroll.frame[scroller.scrolled * displayer.height / 8 + i];
  716. // can only write if we really own display
  717. if (displayer.owned) {
  718. GDS_DrawBitmapCBR(display, scroller.frame, scroller.back.width, displayer.height, GDS_COLOR_WHITE);
  719. GDS_Update(display);
  720. }
  721. // now we can active scrolling, but only if we are not on a small screen
  722. if (!visu.mode || visu.col || visu.row >= displayer.height) scroller.active = true;
  723. // if we just got a content update, let the scroller manage the screen
  724. LOG_DEBUG("resuming scrolling task");
  725. xSemaphoreGive(displayer.mutex);
  726. // resume task once we have background, not in grfs
  727. vTaskResume(displayer.task);
  728. }
  729. /****************************************************************************************
  730. * Artwork
  731. */
  732. static void grfa_handler(u8_t *data, int len) {
  733. struct grfa_packet *pkt = (struct grfa_packet*) data;
  734. int size = len - sizeof(struct grfa_packet);
  735. int offset = htonl(pkt->offset);
  736. int length = htonl(pkt->length);
  737. GDS_CHECK_FOR_DEVICE(display,return);
  738. // when using full screen visualizer on small screen there is a brief overlay
  739. artwork.enable = (length != 0);
  740. // just a config or an actual artwork
  741. if (length < 32) {
  742. if (artwork.enable) {
  743. // this is just to specify artwork coordinates
  744. artwork.x = htons(pkt->x);
  745. artwork.y = htons(pkt->y);
  746. } else if (artwork.size) GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
  747. artwork.full = artwork.enable && artwork.x == 0 && artwork.y == 0;
  748. LOG_DEBUG("gfra en:%u x:%hu, y:%hu", artwork.enable, artwork.x, artwork.y);
  749. // done in any case
  750. return;
  751. }
  752. // new grfa artwork, allocate memory
  753. if (!offset) {
  754. // same trick to clean current/previous window
  755. if (artwork.size) {
  756. GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
  757. artwork.size = 0;
  758. }
  759. // now use new parameters
  760. artwork.x = htons(pkt->x);
  761. artwork.y = htons(pkt->y);
  762. artwork.full = artwork.enable && artwork.x == 0 && artwork.y == 0;
  763. if (artwork.data) free(artwork.data);
  764. artwork.data = malloc(length);
  765. }
  766. // copy artwork data
  767. memcpy(artwork.data + offset, data + sizeof(struct grfa_packet), size);
  768. artwork.size += size;
  769. if (artwork.size == length) {
  770. GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
  771. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  772. GDS_DrawJPEG(display, artwork.data, artwork.x, artwork.y, artwork.y < displayer.height ? (GDS_IMAGE_RIGHT | GDS_IMAGE_TOP) : GDS_IMAGE_CENTER);
  773. xSemaphoreGive(displayer.mutex);
  774. free(artwork.data);
  775. artwork.data = NULL;
  776. }
  777. LOG_DEBUG("gfra l:%u x:%hu, y:%hu, o:%u s:%u", length, artwork.x, artwork.y, offset, size);
  778. }
  779. /****************************************************************************************
  780. * Fit spectrum into N bands and convert to dB
  781. */
  782. void spectrum_scale(int n, struct bar_s *bars, int max, float *samples) {
  783. float rate = visu_export.rate;
  784. // now arrange the result with the number of bar and sampling rate (don't want DC)
  785. for (int i = 0, j = 1; i < n && j < (FFT_LEN / 2); i++) {
  786. float power, count;
  787. // find the next point in FFT (this is real signal, so only half matters)
  788. for (count = 0, power = 0; j * visu_export.rate < bars[i].limit * FFT_LEN && j < FFT_LEN / 2; j++, count += 1) {
  789. power += samples[2*j] * samples[2*j] + samples[2*j+1] * samples[2*j+1];
  790. }
  791. // due to sample rate, we have reached the end of the available spectrum
  792. if (j >= (FFT_LEN / 2)) {
  793. // normalize accumulated data
  794. if (count) power /= count * 2.;
  795. } else if (count) {
  796. // how much of what remains do we need to add
  797. float ratio = j - (bars[i].limit * FFT_LEN) / rate;
  798. power += (samples[2*j] * samples[2*j] + samples[2*j+1] * samples[2*j+1]) * ratio;
  799. // normalize accumulated data
  800. power /= (count + ratio) * 2;
  801. } else {
  802. // no data for that band (sampling rate too high), just assume same as previous one
  803. power = (samples[2*j] * samples[2*j] + samples[2*j+1] * samples[2*j+1]) / 2.;
  804. }
  805. // convert to dB and bars, same back-off
  806. bars[i].current = max * (0.01667f*10*(log10f(0.0000001f + power) - log10f(FFT_LEN*(visu_export.gain == FIXED_ONE ? 256 : 2))) - 0.2543f);
  807. if (bars[i].current > max) bars[i].current = max;
  808. else if (bars[i].current < 0) bars[i].current = 0;
  809. }
  810. }
  811. /****************************************************************************************
  812. * Fit levels to max and convert to dB
  813. */
  814. void vu_scale(struct bar_s *bars, int max, int *levels) {
  815. // convert to dB (1 bit remaining for getting X²/N, 60dB dynamic starting from 0dBFS = 3 bits back-off)
  816. for (int i = 2; --i >= 0;) {
  817. bars[i].current = max * (0.01667f*10*log10f(0.0000001f + (levels[i] >> (visu_export.gain == FIXED_ONE ? 8 : 1))) - 0.2543f);
  818. if (bars[i].current > max) bars[i].current = max;
  819. else if (bars[i].current < 0) bars[i].current = 0;
  820. }
  821. }
  822. /****************************************************************************************
  823. * visu draw
  824. */
  825. void visu_draw(void) {
  826. // don't refresh screen if all max are 0 (we were are somewhat idle)
  827. int clear = 0;
  828. for (int i = visu.n; --i >= 0;) clear = max(clear, visu.bars[i].max);
  829. if (clear) GDS_ClearExt(display, false, false, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  830. // draw background if we are in screensaver mode
  831. if (!(visu.mode & VISU_ESP32) && visu.back.active) {
  832. GDS_DrawBitmapCBR(display, visu.back.frame, visu.back.width, displayer.height, GDS_COLOR_WHITE);
  833. }
  834. if ((visu.mode & ~VISU_ESP32) != VISU_VUMETER || !visu.style) {
  835. // there is much more optimization to be done here, like not redrawing bars unless needed
  836. for (int i = visu.n; --i >= 0;) {
  837. // update maximum
  838. if (visu.bars[i].current > visu.bars[i].max) visu.bars[i].max = visu.bars[i].current;
  839. else if (visu.bars[i].max) visu.bars[i].max--;
  840. else if (!clear) continue;
  841. if (visu.rotate) {
  842. int x1 = visu.col;
  843. int y1 = visu.row + visu.border + visu.bar_border + i*(visu.bar_width + visu.bar_gap);
  844. for (int j = 0; j <= visu.bars[i].current; j += 2)
  845. GDS_DrawLine(display, x1 + j, y1, x1 + j, y1 + visu.bar_width - 1, GDS_COLOR_WHITE);
  846. if (visu.bars[i].max > 2) {
  847. GDS_DrawLine(display, x1 + visu.bars[i].max, y1, x1 + visu.bars[i].max, y1 + visu.bar_width - 1, GDS_COLOR_WHITE);
  848. 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);
  849. }
  850. } else {
  851. int x1 = visu.col + visu.border + visu.bar_border + i*(visu.bar_width + visu.bar_gap);
  852. int y1 = visu.row + visu.height - 1;
  853. for (int j = 0; j <= visu.bars[i].current; j += 2)
  854. GDS_DrawLine(display, x1, y1 - j, x1 + visu.bar_width - 1, y1 - j, GDS_COLOR_WHITE);
  855. if (visu.bars[i].max > 2) {
  856. GDS_DrawLine(display, x1, y1 - visu.bars[i].max, x1 + visu.bar_width - 1, y1 - visu.bars[i].max, GDS_COLOR_WHITE);
  857. 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);
  858. }
  859. }
  860. }
  861. } else if (displayer.width / 2 >= 3 * VU_WIDTH / 4) {
  862. if (visu.rotate) {
  863. draw_VU(display, visu.bars[0].current, 0, visu.row, visu.height / 2, visu.rotate);
  864. draw_VU(display, visu.bars[1].current, 0, visu.row + visu.height / 2, visu.height / 2, visu.rotate);
  865. } else {
  866. draw_VU(display, visu.bars[0].current, 0, visu.row, visu.width / 2, visu.rotate);
  867. draw_VU(display, visu.bars[1].current, visu.width / 2, visu.row, visu.width / 2, visu.rotate);
  868. }
  869. } else {
  870. int level = (visu.bars[0].current + visu.bars[1].current) / 2;
  871. draw_VU(display, level, 0, visu.row, visu.rotate ? visu.height : visu.width, visu.rotate);
  872. }
  873. }
  874. /****************************************************************************************
  875. * Update displayer
  876. */
  877. static void displayer_update(void) {
  878. // 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
  879. if ((artwork.full && !led_visu.mode) || pthread_mutex_trylock(&visu_export.mutex)) {
  880. return;
  881. }
  882. int mode = (visu.mode & ~VISU_ESP32) | led_visu.mode;
  883. // not enough frames
  884. if (visu_export.level < (mode & VISU_SPECTRUM ? FFT_LEN : RMS_LEN) && visu_export.running) {
  885. pthread_mutex_unlock(&visu_export.mutex);
  886. return;
  887. }
  888. // reset all levels no matter what
  889. meters.levels[0] = meters.levels[1] = 0;
  890. memset(meters.samples, 0, sizeof(meters.samples));
  891. if (visu_export.running) {
  892. // calculate data for VU-meter
  893. if (mode & VISU_VUMETER) {
  894. s16_t *iptr = (s16_t*) visu_export.buffer + (BYTES_PER_FRAME / 4) - 1;
  895. int *left = &meters.levels[0], *right = &meters.levels[1];
  896. // calculate sum(L²+R²), try to not overflow at the expense of some precision
  897. for (int i = RMS_LEN; --i >= 0;) {
  898. *left += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
  899. iptr += BYTES_PER_FRAME / 4;
  900. *right += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
  901. iptr += BYTES_PER_FRAME / 4;
  902. }
  903. }
  904. // calculate data for spectrum
  905. if (mode & VISU_SPECTRUM) {
  906. s16_t *iptr = (s16_t*) visu_export.buffer + (BYTES_PER_FRAME / 4) - 1;
  907. // on xtensa/esp32 the floating point FFT takes 1/2 cycles of the fixed point
  908. for (int i = 0 ; i < FFT_LEN ; i++) {
  909. // don't normalize here, but we are due INT16_MAX and FFT_LEN / 2 / 2
  910. meters.samples[i * 2 + 0] = (float) (*iptr + *(iptr+BYTES_PER_FRAME/4)) * meters.hanning[i];
  911. meters.samples[i * 2 + 1] = 0;
  912. iptr += 2 * BYTES_PER_FRAME / 4;
  913. }
  914. // actual FFT that might be less cycle than all the crap below
  915. dsps_fft2r_fc32_ae32(meters.samples, FFT_LEN);
  916. dsps_bit_rev_fc32_ansi(meters.samples, FFT_LEN);
  917. }
  918. }
  919. // we took what we want, we can release the buffer
  920. visu_export.level = 0;
  921. pthread_mutex_unlock(&visu_export.mutex);
  922. // actualize the display
  923. if (visu.mode && !artwork.full) {
  924. if (visu.mode & VISU_SPECTRUM) spectrum_scale(visu.n, visu.bars, visu.max, meters.samples);
  925. else for (int i = 2; --i >= 0;) vu_scale(visu.bars, visu.max, meters.levels);
  926. visu_draw();
  927. }
  928. // actualize led_vu
  929. if (led_display && led_visu.mode) {
  930. // run built in visualizer effects
  931. if (led_visu.mode == VISU_VUMETER) {
  932. vu_scale(led_visu.bars, led_visu.gain, meters.levels);
  933. led_vu_display(led_visu.bars[0].current, led_visu.bars[1].current, led_visu.max, led_visu.style);
  934. } else if (led_visu.mode == VISU_SPECTRUM) {
  935. spectrum_scale(led_visu.n, led_visu.bars, led_visu.gain, meters.samples);
  936. uint8_t* p = (uint8_t*) led_data;
  937. for (int i = 0; i < led_visu.n; i++) {
  938. *p = led_visu.bars[i].current;
  939. p++;
  940. }
  941. led_vu_spectrum(led_data, led_visu.max, led_visu.n, led_visu.style);
  942. } else if (led_visu.mode == VISU_WAVEFORM) {
  943. spectrum_scale(led_visu.n, led_visu.bars, led_visu.gain, meters.samples);
  944. led_vu_spin_dial(
  945. led_visu.bars[led_visu.n-2].current,
  946. led_visu.bars[(led_visu.n/2)+1].current * 50 / led_visu.max,
  947. led_visu.bars[1].current * 4 / led_visu.max,
  948. led_visu.style);
  949. }
  950. }
  951. }
  952. /****************************************************************************************
  953. * Calculate spectrum spread
  954. */
  955. static void spectrum_limits(struct bar_s *bars, int min, int n, int pos, float spectrum_scale) {
  956. if (n / 2) {
  957. int step = ((DISPLAY_BW - min) * spectrum_scale) / (n/2);
  958. bars[pos].limit = min + step;
  959. for (int i = 1; i < n/2; i++) bars[pos+i].limit = bars[pos+i-1].limit + step;
  960. spectrum_limits(bars, bars[pos + n/2 - 1].limit, n - n/2, pos + n/2, spectrum_scale);
  961. } else {
  962. bars[pos].limit = DISPLAY_BW;
  963. }
  964. }
  965. /****************************************************************************************
  966. * Fit visu
  967. */
  968. static void visu_fit(int bars, int width, int height) {
  969. // try to adapt to what we have
  970. if ((visu.mode & ~VISU_ESP32) == VISU_SPECTRUM) {
  971. visu.n = bars ? bars : MAX_BARS;
  972. visu.max = height - 1;
  973. if (visu.spectrum_scale <= 0 || visu.spectrum_scale > 0.5) visu.spectrum_scale = 0.5;
  974. spectrum_limits(visu.bars, 0, visu.n, 0, visu.spectrum_scale);
  975. } else {
  976. visu.n = 2;
  977. visu.max = (visu.style ? VU_COUNT : height) - 1;
  978. }
  979. do {
  980. visu.bar_width = (width - visu.border - visu.bar_gap * (visu.n - 1)) / visu.n;
  981. if (visu.bar_width > 0) break;
  982. } while (--visu.n);
  983. visu.bar_border = (width - visu.border - (visu.bar_width + visu.bar_gap) * visu.n + visu.bar_gap) / 2;
  984. }
  985. /****************************************************************************************
  986. * Visu packet handler
  987. */
  988. static void visu_handler( u8_t *data, int len) {
  989. struct visu_packet *pkt = (struct visu_packet*) data;
  990. int bars = 0;
  991. LOG_DEBUG("visu %u with %u parameters", pkt->which, pkt->count);
  992. /*
  993. If width is specified, then respect all coordinates, otherwise we try to
  994. use the bottom part of the display and if it is a small display, we overwrite
  995. text
  996. */
  997. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  998. visu.mode = pkt->which;
  999. // little trick to clean the taller screens when switching visu
  1000. if (visu.row >= displayer.height) GDS_ClearExt(display, false, true, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  1001. if (visu.mode) {
  1002. // these will be overidden if necessary
  1003. visu.col = visu.border = 0;
  1004. visu.rotate = false;
  1005. // what type of visu
  1006. if (visu.mode & VISU_ESP32) {
  1007. if (pkt->count >= 4) {
  1008. // more than 4 parameters, this is small visu, then go were we are told to
  1009. pkt->height = htonl(pkt->height);
  1010. pkt->row = htonl(pkt->row);
  1011. pkt->col = htonl(pkt->col);
  1012. visu.style = 0;
  1013. visu.width = htonl(pkt->width);
  1014. visu.height = pkt->height ? pkt->height : displayer.height;
  1015. visu.col = pkt->col < 0 ? displayer.width + pkt->col : pkt->col;
  1016. visu.row = pkt->row < 0 ? GDS_GetHeight(display) + pkt->row : pkt->row;
  1017. visu.border = htonl(pkt->border);
  1018. bars = htonl(pkt->bars);
  1019. visu.spectrum_scale = htonl(pkt->spectrum_scale) / 100.;
  1020. } else {
  1021. // full screen visu, try to optimize orientation/shape
  1022. visu.width = htonl(pkt->full.width);
  1023. visu.height = GDS_GetHeight(display);
  1024. // do we have enough height to play with layout
  1025. if (GDS_GetHeight(display) > displayer.height) {
  1026. // by default, use up to the bottom of the display
  1027. visu.height -= displayer.height;
  1028. visu.row = displayer.height;
  1029. if (artwork.enable && artwork.y) {
  1030. // server sets width to artwork X offset to tell us to rotate
  1031. if (visu.width != artwork.x) {
  1032. visu.height = artwork.y - displayer.height;
  1033. if (visu.height <= 0) {
  1034. visu.height = displayer.height;
  1035. LOG_WARN("No room left for visualizer, disable it or increase artwork offset %d", artwork.y);
  1036. }
  1037. } else visu.rotate = true;
  1038. }
  1039. } else visu.row = 0;
  1040. // is this spectrum or analogue/digital
  1041. if ((visu.mode & ~VISU_ESP32) == VISU_SPECTRUM) {
  1042. bars = htonl(pkt->full.bars);
  1043. visu.spectrum_scale = htonl(pkt->full.spectrum_scale) / 100.;
  1044. } else {
  1045. // select analogue/digital style
  1046. visu.style = htonl(pkt->full.style);
  1047. }
  1048. }
  1049. } else {
  1050. // classical (screensaver) mode, don't try to optimize screen usage & force some params
  1051. visu.row = 0;
  1052. visu.height = GDS_GetHeight(display);
  1053. visu.width = displayer.width;
  1054. visu.spectrum_scale = 0.25;
  1055. if (visu.mode == VISU_SPECTRUM) {
  1056. bars = visu.width / (htonl(pkt->channels[0].bar_width) + htonl(pkt->channels[0].bar_space));
  1057. } else {
  1058. visu.style = htonl(pkt->classical_vu.style);
  1059. if (visu.style) visu.row = visu.height - VU_HEIGHT;
  1060. }
  1061. }
  1062. if (bars > MAX_BARS) bars = MAX_BARS;
  1063. // for rotate, swap width & height
  1064. if (visu.rotate) visu_fit(bars, visu.height, visu.width);
  1065. else visu_fit(bars, visu.width, visu.height);
  1066. // give up if not enough space
  1067. if (visu.bar_width < 0) {
  1068. visu.mode = VISU_BLANK;
  1069. LOG_WARN("Not enough room for displaying visu");
  1070. } else {
  1071. // de-activate scroller if we are taking main screen
  1072. if (visu.row < displayer.height) scroller.active = false;
  1073. vTaskResume(displayer.task);
  1074. }
  1075. displayer.wake = 0;
  1076. // reset bars maximum
  1077. for (int i = visu.n; --i >= 0;) visu.bars[i].max = 0;
  1078. GDS_ClearExt(display, false, true, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
  1079. 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);
  1080. } else {
  1081. LOG_INFO("Stopping visualizer");
  1082. }
  1083. xSemaphoreGive(displayer.mutex);
  1084. }
  1085. /****************************************************************************************
  1086. * Led_visu packet handler
  1087. */
  1088. static void ledv_handler( u8_t *data, int len) {
  1089. struct ledv_packet *pkt = (struct ledv_packet*) data;
  1090. LOG_DEBUG("led_visu %u with parameters", pkt->which);
  1091. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  1092. led_visu.mode = pkt->which;
  1093. led_visu.style = pkt->style;
  1094. led_visu.max = pkt->bright;
  1095. led_visu.gain = led_visu.max * led_vu_scale() / 100;
  1096. if (led_visu.mode) {
  1097. if (led_visu.mode == VISU_SPECTRUM) {
  1098. led_visu.n = (led_visu.config < MAX_BARS) ? led_visu.config : MAX_BARS;
  1099. spectrum_limits(led_visu.bars, 0, led_visu.n, 0, 0.25);
  1100. } else if (led_visu.mode == VISU_WAVEFORM) {
  1101. led_visu.n = 6;
  1102. spectrum_limits(led_visu.bars, 0, led_visu.n, 0, 0.25);
  1103. }
  1104. displayer.wake = 1; // wake up
  1105. // reset bars maximum
  1106. for (int i = led_visu.n; --i >= 0;) led_visu.bars[i].max = 0;
  1107. LOG_INFO("LED Visualizer mode %u with bars:%u max:%u style:%d gain:%u", led_visu.mode, led_visu.n, led_visu.max, led_visu.style, led_visu.gain);
  1108. } else {
  1109. led_vu_clear();
  1110. LOG_INFO("Stopping led visualizer");
  1111. }
  1112. xSemaphoreGive(displayer.mutex);
  1113. // resume displayer task
  1114. vTaskResume(displayer.task);
  1115. }
  1116. /****************************************************************************************
  1117. * Led_data dmx style packet handler
  1118. * ToDo: make packet match dmx protocol format
  1119. */
  1120. static void ledd_handler( u8_t *data, int len) {
  1121. struct ledd_packet *pkt = (struct ledd_packet*) data;
  1122. uint16_t offset = htons(pkt->x);
  1123. uint16_t length = htons(pkt->length);
  1124. LOG_INFO("dmx packet len:%u offset:%u", length, offset);
  1125. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  1126. led_vu_data(data + sizeof(struct ledd_packet), offset, length);
  1127. displayer.wake = 1000; // wait a little while
  1128. xSemaphoreGive(displayer.mutex);
  1129. }
  1130. /****************************************************************************************
  1131. * Scroll task
  1132. * - with the addition of the visualizer, it's a bit a 2-headed beast not easy to
  1133. * maintain, so som better separation between the visu and scroll is probably needed
  1134. */
  1135. static void displayer_task(void *args) {
  1136. int sleep;
  1137. while (1) {
  1138. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  1139. // suspend ourselves if nothing to do, grfg or visu will wake us up
  1140. if (!scroller.active && !visu.mode && !led_visu.mode) {
  1141. xSemaphoreGive(displayer.mutex);
  1142. vTaskSuspend(NULL);
  1143. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  1144. scroller.wake = displayer.wake = 0;
  1145. }
  1146. // go for long sleep when either item is disabled
  1147. if (!visu.mode && !led_visu.mode) displayer.wake = LONG_WAKE;
  1148. if (!scroller.active) scroller.wake = LONG_WAKE;
  1149. // scroll required amount of columns (within the window)
  1150. if (scroller.active && scroller.wake <= 0) {
  1151. // by default go for the long sleep, will change below if required
  1152. scroller.wake = LONG_WAKE;
  1153. // do we have more to scroll (scroll.width is the last column from which we have a full zone)
  1154. if (scroller.by > 0 ? (scroller.scrolled <= scroller.scroll.width) : (scroller.scrolled >= 0)) {
  1155. memcpy(scroller.frame, scroller.back.frame, scroller.back.width * displayer.height / 8);
  1156. for (int i = 0; i < scroller.width * displayer.height / 8; i++) scroller.frame[i] |= scroller.scroll.frame[scroller.scrolled * displayer.height / 8 + i];
  1157. scroller.scrolled += scroller.by;
  1158. if (displayer.owned) GDS_DrawBitmapCBR(display, scroller.frame, scroller.width, displayer.height, GDS_COLOR_WHITE);
  1159. // short sleep & don't need background update
  1160. scroller.wake = scroller.speed;
  1161. } else if (scroller.first || !scroller.mode) {
  1162. // at least one round done
  1163. scroller.first = false;
  1164. // see if we need to pause or if we are done
  1165. if (scroller.mode) {
  1166. sendANIC(ANIM_SCROLL_ONCE | ANIM_SCREEN_1);
  1167. LOG_INFO("scroll-once terminated");
  1168. } else {
  1169. scroller.wake = scroller.pause;
  1170. LOG_DEBUG("scroll cycle done, pausing for %u (ms)", scroller.pause);
  1171. }
  1172. // need to reset pointers for next scroll
  1173. scroller.scrolled = scroller.by < 0 ? scroller.scroll.width : 0;
  1174. }
  1175. }
  1176. // update visu if active
  1177. if ((visu.mode || led_visu.mode) && displayer.wake <= 0 && displayer.owned) {
  1178. displayer_update();
  1179. displayer.wake = 100;
  1180. }
  1181. // need to make sure we own display
  1182. if (display && displayer.owned) GDS_Update(display);
  1183. else if (!led_display) displayer.wake = LONG_WAKE;
  1184. // release semaphore and sleep what's needed
  1185. xSemaphoreGive(displayer.mutex);
  1186. sleep = min(displayer.wake, scroller.wake);
  1187. vTaskDelay(sleep / portTICK_PERIOD_MS);
  1188. scroller.wake -= sleep;
  1189. displayer.wake -= sleep;
  1190. }
  1191. }