display.c 15 KB

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