display.c 16 KB

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