display.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. * (c) 2004,2006 Richard Titmuss for SlimProtoLib
  3. * (c) Philippe G. 2019, philippe_44@outlook.com
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include <ctype.h>
  20. #include "squeezelite.h"
  21. #include "slimproto.h"
  22. #include "display.h"
  23. #pragma pack(push, 1)
  24. struct grfb_packet {
  25. char opcode[4];
  26. s16_t brightness;
  27. };
  28. struct grfe_packet {
  29. char opcode[4];
  30. u16_t offset;
  31. u8_t transition;
  32. u8_t param;
  33. };
  34. struct grfs_packet {
  35. char opcode[4];
  36. u8_t screen;
  37. u8_t direction; // 1=left, 2=right
  38. u32_t pause; // in ms
  39. u32_t speed; // in ms
  40. u16_t by; // # of pixel of scroll step
  41. u16_t mode; // 0=continuous, 1=once and stop, 2=once and end
  42. u16_t width; // total width of animation
  43. u16_t offset; // offset if multiple packets are sent
  44. };
  45. struct grfg_packet {
  46. char opcode[4];
  47. u16_t screen;
  48. u16_t width; // # of pixels of scrollable
  49. };
  50. struct ANIC_header {
  51. char opcode[4];
  52. u32_t length;
  53. u8_t mode;
  54. };
  55. #pragma pack(pop)
  56. extern struct outputstate output;
  57. static struct scroller_s {
  58. // copy of grfs content
  59. u8_t screen, direction;
  60. u32_t pause, speed;
  61. u16_t by, mode, full_width, window_width;
  62. u16_t max, size;
  63. // scroller management & sharing between grfg and scrolling task
  64. TaskHandle_t task;
  65. u8_t *scroll_frame, *back_frame;
  66. bool active, updated;
  67. u8_t *scroll_ptr;
  68. int scroll_len, scroll_step;
  69. } scroller;
  70. #define ANIM_NONE 0x00
  71. #define ANIM_TRANSITION 0x01 // A transition animation has finished
  72. #define ANIM_SCROLL_ONCE 0x02
  73. #define ANIM_SCREEN_1 0x04
  74. #define ANIM_SCREEN_2 0x08
  75. static u8_t ANIC_resp = ANIM_NONE;
  76. static u8_t SETD_width;
  77. #define SCROLL_STACK_SIZE (3*1024)
  78. #define LINELEN 40
  79. static log_level loglevel = lINFO;
  80. static SemaphoreHandle_t display_mutex;
  81. static bool (*slimp_handler_chain)(u8_t *data, int len);
  82. static void (*slimp_loop_chain)(void);
  83. static void (*notify_chain)(in_addr_t ip, u16_t hport, u16_t cport);
  84. static int display_width, display_height;
  85. #define max(a,b) (((a) > (b)) ? (a) : (b))
  86. static void server(in_addr_t ip, u16_t hport, u16_t cport);
  87. static void send_server(void);
  88. static bool handler(u8_t *data, int len);
  89. static void vfdc_handler( u8_t *_data, int bytes_read);
  90. static void grfe_handler( u8_t *data, int len);
  91. static void grfb_handler(u8_t *data, int len);
  92. static void grfs_handler(u8_t *data, int len);
  93. static void grfg_handler(u8_t *data, int len);
  94. static void scroll_task(void* arg);
  95. /* scrolling undocumented information
  96. grfs
  97. B: screen number
  98. B:1 = left, 2 = right,
  99. Q: scroll pause once done (ms)
  100. Q: scroll speed (ms)
  101. W: # of pixels to scroll each time
  102. W: 0 = continue scrolling after pause, 1 = scroll to scrollend and then stop, 2 = scroll to scrollend and then end animation (causing new update)
  103. W: width of total scroll area in pixels
  104. grfd
  105. W: screen number
  106. W: width of scrollable area in pixels
  107. anic ( two versions, don't know what to chose)
  108. B: flag
  109. ANIM_TRANSITION (0x01) - transition animation has finished (previous use of ANIC)
  110. ANIM_SCREEN_1 (0x04) - end of first scroll on screen 1
  111. ANIM_SCREEN_2 (0x08) - end of first scroll on screen 2
  112. ANIM_SCROLL_ONCE (0x02) | ANIM_SCREEN_1 (0x04) - end of scroll once on screen 1
  113. ANIM_SCROLL_ONCE (0x02) | ANIM_SCREEN_2 (0x08) - end of scroll once on screen 2
  114. - or -
  115. ANIM_TRANSITION 0x01 # A transition animation has finished
  116. ANIM_SCROLL_ONCE 0x02 # A scrollonce has finished
  117. ANIM_SCREEN_1 0x04 # For scrollonce only, screen 1 was scrolling
  118. ANIM_SCREEN_2 0x08 # For scrollonce only, screen 2 was scrolling
  119. */
  120. /****************************************************************************************
  121. *
  122. */
  123. bool sb_display_init(void) {
  124. static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
  125. static EXT_RAM_ATTR StackType_t xStack[SCROLL_STACK_SIZE] __attribute__ ((aligned (4)));
  126. // no display, just make sure we won't have requests
  127. if (!display || display->height == 0 || display->width == 0) {
  128. LOG_INFO("no display for LMS");
  129. return false;
  130. }
  131. // need to force height to 32 maximum
  132. display_width = display->width;
  133. display_height = min(display->height, 32);
  134. SETD_width = display->width;
  135. // create scroll management task
  136. display_mutex = xSemaphoreCreateMutex();
  137. scroller.task = xTaskCreateStatic( (TaskFunction_t) scroll_task, "scroll_thread", SCROLL_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, &xTaskBuffer);
  138. // size scroller
  139. scroller.max = (display_width * display_height / 8) * 10;
  140. scroller.scroll_frame = malloc(scroller.max);
  141. scroller.back_frame = malloc(display_width * display_height / 8);
  142. // chain handlers
  143. slimp_handler_chain = slimp_handler;
  144. slimp_handler = handler;
  145. slimp_loop_chain = slimp_loop;
  146. slimp_loop = send_server;
  147. notify_chain = server_notify;
  148. server_notify = server;
  149. return true;
  150. }
  151. /****************************************************************************************
  152. * Send message to server (ANIC at that time)
  153. */
  154. static void send_server(void) {
  155. /*
  156. This complication is needed as we cannot send direclty to LMS, because
  157. send_packet is not thread safe. So must subscribe to slimproto busy loop
  158. end send from there
  159. */
  160. if (ANIC_resp != ANIM_NONE) {
  161. struct ANIC_header pkt_header;
  162. memset(&pkt_header, 0, sizeof(pkt_header));
  163. memcpy(&pkt_header.opcode, "ANIC", 4);
  164. pkt_header.length = htonl(sizeof(pkt_header) - 8);
  165. pkt_header.mode = ANIC_resp;
  166. send_packet((u8_t *)&pkt_header, sizeof(pkt_header));
  167. ANIC_resp = ANIM_NONE;
  168. }
  169. if (SETD_width) {
  170. struct SETD_header pkt_header;
  171. LOG_INFO("sending width %u", SETD_width);
  172. memset(&pkt_header, 0, sizeof(pkt_header));
  173. memcpy(&pkt_header.opcode, "SETD", 4);
  174. pkt_header.id = 0xfe; // id 0xfe is width S:P:Squeezebox2
  175. pkt_header.length = htonl(sizeof(pkt_header) + 2 - 8);
  176. send_packet((u8_t *)&pkt_header, sizeof(pkt_header));
  177. send_packet(&SETD_width, 2);
  178. SETD_width = 0;
  179. }
  180. if (slimp_loop_chain) (*slimp_loop_chain)();
  181. }
  182. /****************************************************************************************
  183. *
  184. */
  185. static void server(in_addr_t ip, u16_t hport, u16_t cport) {
  186. char msg[32];
  187. sprintf(msg, "%s:%hu", inet_ntoa(ip), hport);
  188. display->text(DISPLAY_FONT_DEFAULT, DISPLAY_CENTERED, DISPLAY_CLEAR | DISPLAY_UPDATE, msg);
  189. if (notify_chain) (*notify_chain)(ip, hport, cport);
  190. }
  191. /****************************************************************************************
  192. * Process graphic display data
  193. */
  194. static bool handler(u8_t *data, int len){
  195. bool res = true;
  196. // don't do anything if we dont own the display (no lock needed)
  197. if (!output.external || output.state < OUTPUT_STOPPED) {
  198. if (!strncmp((char*) data, "vfdc", 4)) {
  199. vfdc_handler(data, len);
  200. } else if (!strncmp((char*) data, "grfe", 4)) {
  201. grfe_handler(data, len);
  202. } else if (!strncmp((char*) data, "grfb", 4)) {
  203. grfb_handler(data, len);
  204. } else if (!strncmp((char*) data, "grfs", 4)) {
  205. grfs_handler(data, len);
  206. } else if (!strncmp((char*) data, "grfg", 4)) {
  207. grfg_handler(data, len);
  208. } else {
  209. res = false;
  210. }
  211. }
  212. // chain protocol handlers (bitwise or is fine)
  213. if (*slimp_handler_chain) res |= (*slimp_handler_chain)(data, len);
  214. return res;
  215. }
  216. /****************************************************************************************
  217. * Change special LCD chars to something more printable on screen
  218. */
  219. static void makeprintable(unsigned char * line) {
  220. for (int n = 0; n < LINELEN; n++) {
  221. switch (line[n]) {
  222. case 11: /* block */
  223. line[n] = '#';
  224. break;;
  225. case 16: /* rightarrow */
  226. line[n] = '>';
  227. break;;
  228. case 22: /* circle */
  229. line[n] = '@';
  230. break;;
  231. case 145: /* note */
  232. line[n] = ' ';
  233. break;;
  234. case 152: /* bell */
  235. line[n] = 'o';
  236. break;
  237. default:
  238. break;
  239. }
  240. }
  241. }
  242. /****************************************************************************************
  243. * Check if char is printable, or a valid symbol
  244. */
  245. static bool charisok(unsigned char c) {
  246. switch (c) {
  247. case 11: /* block */
  248. case 16: /* rightarrow */
  249. case 22: /* circle */
  250. case 145: /* note */
  251. case 152: /* bell */
  252. return true;
  253. break;;
  254. default:
  255. return isprint(c);
  256. }
  257. }
  258. /****************************************************************************************
  259. * Show the display (text mode)
  260. */
  261. static void show_display_buffer(char *ddram) {
  262. char line1[LINELEN+1];
  263. char *line2;
  264. memset(line1, 0, LINELEN+1);
  265. strncpy(line1, ddram, LINELEN);
  266. line2 = &(ddram[LINELEN]);
  267. line2[LINELEN] = '\0';
  268. /* Convert special LCD chars */
  269. makeprintable((unsigned char *)line1);
  270. makeprintable((unsigned char *)line2);
  271. LOG_DEBUG("\n\t%.40s\n\t%.40s", line1, line2);
  272. display->line(1, DISPLAY_LEFT, DISPLAY_CLEAR, line1);
  273. display->line(2, DISPLAY_LEFT, DISPLAY_CLEAR | DISPLAY_UPDATE, line2);
  274. }
  275. /****************************************************************************************
  276. * Process display data
  277. */
  278. static void vfdc_handler( u8_t *_data, int bytes_read) {
  279. unsigned short *data = (unsigned short*) _data, *display_data;
  280. char ddram[(LINELEN + 1) * 2];
  281. int n, addr = 0; /* counter */
  282. bytes_read -= 4;
  283. if (bytes_read % 2) bytes_read--; /* even number of bytes */
  284. // if we use Noritake VFD codes, display data starts at 12
  285. display_data = &(data[5]); /* display data starts at byte 10 */
  286. memset(ddram, ' ', LINELEN * 2);
  287. for (n = 0; n < (bytes_read/2); n++) {
  288. unsigned short d; /* data element */
  289. unsigned char t, c;
  290. d = ntohs(display_data[n]);
  291. t = (d & 0x00ff00) >> 8; /* type of display data */
  292. c = (d & 0x0000ff); /* character/command */
  293. switch (t) {
  294. case 0x03: /* character */
  295. if (!charisok(c)) c = ' ';
  296. if (addr <= LINELEN * 2) {
  297. ddram[addr++] = c;
  298. }
  299. break;
  300. case 0x02: /* command */
  301. switch (c) {
  302. case 0x06: /* display clear */
  303. memset(ddram, ' ', LINELEN * 2);
  304. break;
  305. case 0x02: /* cursor home */
  306. addr = 0;
  307. break;
  308. case 0xc0: /* cursor home2 */
  309. addr = LINELEN;
  310. break;
  311. }
  312. }
  313. }
  314. show_display_buffer(ddram);
  315. }
  316. /****************************************************************************************
  317. * Process graphic display data
  318. */
  319. static void grfe_handler( u8_t *data, int len) {
  320. xSemaphoreTake(display_mutex, portMAX_DELAY);
  321. scroller.active = false;
  322. display->draw_cbr(data + sizeof(struct grfe_packet), display_height);
  323. xSemaphoreGive(display_mutex);
  324. LOG_DEBUG("grfe frame %u", len);
  325. }
  326. /****************************************************************************************
  327. * Brightness
  328. */
  329. static void grfb_handler(u8_t *data, int len) {
  330. struct grfb_packet *pkt = (struct grfb_packet*) data;
  331. pkt->brightness = htons(pkt->brightness);
  332. LOG_INFO("brightness %hu", pkt->brightness);
  333. if (pkt->brightness < 0) {
  334. display->on(false);
  335. } else {
  336. display->on(true);
  337. display->brightness(pkt->brightness);
  338. }
  339. }
  340. /****************************************************************************************
  341. * Scroll set
  342. */
  343. static void grfs_handler(u8_t *data, int len) {
  344. struct grfs_packet *pkt = (struct grfs_packet*) data;
  345. int size = len - sizeof(struct grfs_packet);
  346. int offset = htons(pkt->offset);
  347. LOG_DEBUG("gfrs s:%u d:%u p:%u sp:%u by:%hu m:%hu w:%hu o:%hu",
  348. (int) pkt->screen,
  349. (int) pkt->direction, // 1=left, 2=right
  350. htonl(pkt->pause), // in ms
  351. htonl(pkt->speed), // in ms
  352. htons(pkt->by), // # of pixel of scroll step
  353. htons(pkt->mode), // 0=continuous, 1=once and stop, 2=once and end
  354. htons(pkt->width), // total width of animation
  355. htons(pkt->offset) // offset if multiple packets are sent
  356. );
  357. // new grfs frame, build scroller info
  358. if (!offset) {
  359. // use the display as a general lock
  360. xSemaphoreTake(display_mutex, portMAX_DELAY);
  361. // copy & set scroll parameters
  362. scroller.screen = pkt->screen;
  363. scroller.direction = pkt->direction;
  364. scroller.pause = htonl(pkt->pause);
  365. scroller.speed = htonl(pkt->speed);
  366. scroller.by = htons(pkt->by);
  367. scroller.mode = htons(pkt->mode);
  368. scroller.full_width = htons(pkt->width);
  369. scroller.updated = scroller.active = true;
  370. xSemaphoreGive(display_mutex);
  371. }
  372. // copy scroll frame data (no semaphore needed)
  373. if (scroller.size + size < scroller.max) {
  374. memcpy(scroller.scroll_frame + offset, data + sizeof(struct grfs_packet), size);
  375. scroller.size = offset + size;
  376. LOG_INFO("scroller current size %u", scroller.size);
  377. } else {
  378. LOG_INFO("scroller too larger %u/%u", scroller.size + size, scroller.max);
  379. }
  380. }
  381. /****************************************************************************************
  382. * Scroll background frame update & go
  383. */
  384. static void grfg_handler(u8_t *data, int len) {
  385. struct grfg_packet *pkt = (struct grfg_packet*) data;
  386. LOG_DEBUG("gfrg s:%hu w:%hu (len:%u)", htons(pkt->screen), htons(pkt->width), len);
  387. memcpy(scroller.back_frame, data + sizeof(struct grfg_packet), len - sizeof(struct grfg_packet));
  388. scroller.window_width = htons(pkt->width);
  389. xSemaphoreTake(display_mutex, portMAX_DELAY);
  390. // can't be in grfs as we need full size & scroll_width
  391. if (scroller.updated) {
  392. scroller.scroll_len = display_width * display_height / 8 - (display_width - scroller.window_width) * display_height / 8;
  393. if (scroller.direction == 1) {
  394. scroller.scroll_ptr = scroller.scroll_frame;
  395. scroller.scroll_step = scroller.by * display_height / 8;
  396. } else {
  397. scroller.scroll_ptr = scroller.scroll_frame + scroller.size - scroller.scroll_len;
  398. scroller.scroll_step = -scroller.by * display_height / 8;
  399. }
  400. scroller.updated = false;
  401. }
  402. if (!scroller.active) {
  403. // this is a background update and scroller has been finished, so need to update here
  404. u8_t *frame = malloc(display_width * display_height / 8);
  405. memcpy(frame, scroller.back_frame, display_width * display_height / 8);
  406. for (int i = 0; i < scroller.scroll_len; i++) frame[i] |= scroller.scroll_ptr[i];
  407. display->draw_cbr(frame, display_height);
  408. free(frame);
  409. LOG_DEBUG("direct drawing");
  410. }
  411. else {
  412. // if we just got a content update, let the scroller manage the screen
  413. LOG_DEBUG("resuming scrolling task");
  414. vTaskResume(scroller.task);
  415. }
  416. xSemaphoreGive(display_mutex);
  417. }
  418. /****************************************************************************************
  419. * Scroll task
  420. */
  421. static void scroll_task(void *args) {
  422. u8_t *frame = NULL;
  423. int len = display_width * display_height / 8;
  424. while (1) {
  425. xSemaphoreTake(display_mutex, portMAX_DELAY);
  426. // suspend ourselves if nothing to do, grfg will wake us up
  427. if (!scroller.active) {
  428. xSemaphoreGive(display_mutex);
  429. vTaskSuspend(NULL);
  430. xSemaphoreTake(display_mutex, portMAX_DELAY);
  431. }
  432. // lock screen & active status
  433. frame = malloc(display_width * display_height / 8);
  434. // scroll required amount of columns (within the window)
  435. while (scroller.direction == 1 ? (scroller.scroll_ptr <= scroller.scroll_frame + scroller.size - scroller.scroll_step - len) :
  436. (scroller.scroll_ptr + scroller.scroll_step >= scroller.scroll_frame) ) {
  437. // don't do anything if we have aborted
  438. if (!scroller.active) break;
  439. // scroll required amount of columns (within the window)
  440. memcpy(frame, scroller.back_frame, display_width * display_height / 8);
  441. for (int i = 0; i < scroller.scroll_len; i++) frame[i] |= scroller.scroll_ptr[i];
  442. scroller.scroll_ptr += scroller.scroll_step;
  443. display->draw_cbr(frame, display_height);
  444. xSemaphoreGive(display_mutex);
  445. vTaskDelay(scroller.speed / portTICK_PERIOD_MS);
  446. xSemaphoreTake(display_mutex, portMAX_DELAY);
  447. }
  448. // done with scrolling cycle reset scroller ptr
  449. scroller.scroll_ptr = scroller.scroll_frame + (scroller.direction == 2 ? scroller.size - scroller.scroll_len : 0);
  450. // scrolling done, update screen and see if we need to continue
  451. if (scroller.active) {
  452. memcpy(frame, scroller.back_frame, len);
  453. for (int i = 0; i < scroller.scroll_len; i++) frame[i] |= scroller.scroll_ptr[i];
  454. display->draw_cbr(frame, display_height);
  455. free(frame);
  456. // see if we need to pause or if we are done
  457. if (scroller.mode) {
  458. scroller.active = false;
  459. xSemaphoreGive(display_mutex);
  460. // can't call directly send_packet from slimproto as it's not re-entrant
  461. ANIC_resp = ANIM_SCROLL_ONCE | ANIM_SCREEN_1;
  462. LOG_INFO("scroll-once terminated");
  463. } else {
  464. xSemaphoreGive(display_mutex);
  465. vTaskDelay(scroller.pause / portTICK_PERIOD_MS);
  466. LOG_DEBUG("scroll cycle done, pausing for %u (ms)", scroller.pause);
  467. }
  468. } else {
  469. free(frame);
  470. xSemaphoreGive(display_mutex);
  471. LOG_INFO("scroll aborted");
  472. }
  473. }
  474. }