display.c 16 KB

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