display.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. TaskHandle_t task;
  52. bool active;
  53. u8_t screen, direction;
  54. u32_t pause, speed;
  55. u16_t by, mode, width, scroll_width;
  56. u16_t max, size;
  57. u8_t *scroll_frame, *back_frame;
  58. } scroller;
  59. #define SCROLL_STACK_SIZE 2048
  60. #define LINELEN 40
  61. static log_level loglevel = lINFO;
  62. static SemaphoreHandle_t display_sem;
  63. static bool (*slimp_handler_chain)(u8_t *data, int len);
  64. static void (*notify_chain)(in_addr_t ip, u16_t hport, u16_t cport);
  65. #define max(a,b) (((a) > (b)) ? (a) : (b))
  66. static void server(in_addr_t ip, u16_t hport, u16_t cport);
  67. static bool handler(u8_t *data, int len);
  68. static void vfdc_handler( u8_t *_data, int bytes_read);
  69. static void grfe_handler( u8_t *data, int len);
  70. static void grfb_handler(u8_t *data, int len);
  71. static void grfs_handler(u8_t *data, int len);
  72. static void grfg_handler(u8_t *data, int len);
  73. static void scroll_task(void* arg);
  74. /* scrolling undocumented information
  75. grfs
  76. B: screen number
  77. B:1 = left, 2 = right,
  78. Q: scroll pause once done (ms)
  79. Q: scroll speed (ms)
  80. W: # of pixels to scroll each time
  81. W: 0 = continue scrolling after pause, 1 = scroll to scrollend and then stop, 2 = scroll to scrollend and then end animation (causing new update)
  82. W: width of total scroll area in pixels
  83. grfd
  84. W: screen number
  85. W: width of scrollable area in pixels
  86. ANIC flags
  87. ANIM_TRANSITION 0x01 # A transition animation has finished
  88. ANIM_SCROLL_ONCE 0x02 # A scrollonce has finished
  89. ANIM_SCREEN_1 0x04 # For scrollonce only, screen 1 was scrolling
  90. ANIM_SCREEN_2 0x08 # For scrollonce only, screen 2 was scrolling
  91. */
  92. /****************************************************************************************
  93. *
  94. */
  95. void sb_display_init(void) {
  96. static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
  97. static EXT_RAM_ATTR StackType_t xStack[SCROLL_STACK_SIZE] __attribute__ ((aligned (4)));
  98. // create scroll management task
  99. display_sem = xSemaphoreCreateMutex();
  100. scroller.task = xTaskCreateStatic( (TaskFunction_t) scroll_task, "scroll_thread", SCROLL_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, &xTaskBuffer);
  101. // size scroller
  102. scroller.max = (display->width * display->height / 8) * 10;
  103. scroller.scroll_frame = malloc(scroller.max);
  104. scroller.back_frame = malloc(display->width * display->height / 8);
  105. // chain handlers
  106. slimp_handler_chain = slimp_handler;
  107. slimp_handler = handler;
  108. notify_chain = server_notify;
  109. server_notify = server;
  110. }
  111. /****************************************************************************************
  112. *
  113. */
  114. static void server(in_addr_t ip, u16_t hport, u16_t cport) {
  115. char msg[32];
  116. sprintf(msg, "%s:%hu", inet_ntoa(ip), hport);
  117. display->text(DISPLAY_CENTER, DISPLAY_CLEAR | DISPLAY_UPDATE, msg);
  118. if (notify_chain) (*notify_chain)(ip, hport, cport);
  119. }
  120. /****************************************************************************************
  121. * Process graphic display data
  122. */
  123. static bool handler(u8_t *data, int len){
  124. bool res = true;
  125. if (!strncmp((char*) data, "vfdc", 4)) {
  126. vfdc_handler(data, len);
  127. } else if (!strncmp((char*) data, "grfe", 4)) {
  128. grfe_handler(data, len);
  129. } else if (!strncmp((char*) data, "grfb", 4)) {
  130. grfb_handler(data, len);
  131. } else if (!strncmp((char*) data, "grfs", 4)) {
  132. grfs_handler(data, len);
  133. } else if (!strncmp((char*) data, "grfg", 4)) {
  134. grfg_handler(data, len);
  135. } else {
  136. res = false;
  137. }
  138. // chain protocol handlers (bitwise or is fine)
  139. if (*slimp_handler_chain) res |= (*slimp_handler_chain)(data, len);
  140. return res;
  141. }
  142. /****************************************************************************************
  143. * Change special LCD chars to something more printable on screen
  144. */
  145. static void makeprintable(unsigned char * line) {
  146. for (int n = 0; n < LINELEN; n++) {
  147. switch (line[n]) {
  148. case 11: /* block */
  149. line[n] = '#';
  150. break;;
  151. case 16: /* rightarrow */
  152. line[n] = '>';
  153. break;;
  154. case 22: /* circle */
  155. line[n] = '@';
  156. break;;
  157. case 145: /* note */
  158. line[n] = ' ';
  159. break;;
  160. case 152: /* bell */
  161. line[n] = 'o';
  162. break;
  163. default:
  164. break;
  165. }
  166. }
  167. }
  168. /****************************************************************************************
  169. * Check if char is printable, or a valid symbol
  170. */
  171. static bool charisok(unsigned char c) {
  172. switch (c) {
  173. case 11: /* block */
  174. case 16: /* rightarrow */
  175. case 22: /* circle */
  176. case 145: /* note */
  177. case 152: /* bell */
  178. return true;
  179. break;;
  180. default:
  181. return isprint(c);
  182. }
  183. }
  184. /****************************************************************************************
  185. * Show the display (text mode)
  186. */
  187. static void show_display_buffer(char *ddram) {
  188. char line1[LINELEN+1];
  189. char *line2;
  190. memset(line1, 0, LINELEN+1);
  191. strncpy(line1, ddram, LINELEN);
  192. line2 = &(ddram[LINELEN]);
  193. line2[LINELEN] = '\0';
  194. /* Convert special LCD chars */
  195. makeprintable((unsigned char *)line1);
  196. makeprintable((unsigned char *)line2);
  197. LOG_INFO("\n\t%.40s\n\t%.40s", line1, line2);
  198. display->text(DISPLAY_TOP_LEFT, DISPLAY_CLEAR, line1);
  199. display->text(DISPLAY_BOTTOM_LEFT, DISPLAY_UPDATE, line2);
  200. }
  201. /****************************************************************************************
  202. * Process display data
  203. */
  204. static void vfdc_handler( u8_t *_data, int bytes_read) {
  205. unsigned short *data = (unsigned short*) _data, *display_data;
  206. char ddram[(LINELEN + 1) * 2];
  207. int n, addr = 0; /* counter */
  208. bytes_read -= 4;
  209. if (bytes_read % 2) bytes_read--; /* even number of bytes */
  210. // if we use Noritake VFD codes, display data starts at 12
  211. display_data = &(data[5]); /* display data starts at byte 10 */
  212. memset(ddram, ' ', LINELEN * 2);
  213. for (n = 0; n < (bytes_read/2); n++) {
  214. unsigned short d; /* data element */
  215. unsigned char t, c;
  216. d = ntohs(display_data[n]);
  217. t = (d & 0x00ff00) >> 8; /* type of display data */
  218. c = (d & 0x0000ff); /* character/command */
  219. switch (t) {
  220. case 0x03: /* character */
  221. if (!charisok(c)) c = ' ';
  222. if (addr <= LINELEN * 2) {
  223. ddram[addr++] = c;
  224. }
  225. break;
  226. case 0x02: /* command */
  227. switch (c) {
  228. case 0x06: /* display clear */
  229. memset(ddram, ' ', LINELEN * 2);
  230. break;
  231. case 0x02: /* cursor home */
  232. addr = 0;
  233. break;
  234. case 0xc0: /* cursor home2 */
  235. addr = LINELEN;
  236. break;
  237. }
  238. }
  239. }
  240. show_display_buffer(ddram);
  241. }
  242. /****************************************************************************************
  243. * Process graphic display data
  244. */
  245. static void grfe_handler( u8_t *data, int len) {
  246. scroller.active = false;
  247. xSemaphoreTake(display_sem, portMAX_DELAY);
  248. display->draw_cbr(data + sizeof(struct grfe_packet));
  249. xSemaphoreGive(display_sem);
  250. }
  251. /****************************************************************************************
  252. * Brightness
  253. */
  254. static void grfb_handler(u8_t *data, int len) {
  255. struct grfb_packet *pkt = (struct grfb_packet*) data;
  256. pkt->brightness = htons(pkt->brightness);
  257. LOG_INFO("brightness %hu", pkt->brightness);
  258. if (pkt->brightness < 0) {
  259. display->on(false);
  260. } else {
  261. display->on(true);
  262. display->brightness(pkt->brightness);
  263. }
  264. }
  265. /****************************************************************************************
  266. * Scroll set
  267. */
  268. static void grfs_handler(u8_t *data, int len) {
  269. struct grfs_packet *pkt = (struct grfs_packet*) data;
  270. int size = len - sizeof(struct grfs_packet);
  271. int offset = htons(pkt->offset);
  272. LOG_INFO("gfrs s:%u d:%u p:%u sp:%u by:%hu m:%hu w:%hu o:%hu",
  273. (int) pkt->screen,
  274. (int) pkt->direction, // 1=left, 2=right
  275. htonl(pkt->pause), // in ms
  276. htonl(pkt->speed), // in ms
  277. htons(pkt->by), // # of pixel of scroll step
  278. htons(pkt->mode), // 0=continuous, 1=once and stop, 2=once and end
  279. htons(pkt->width), // total width of animation
  280. htons(pkt->offset) // offset if multiple packets are sent
  281. );
  282. // copy scroll parameters
  283. scroller.screen = pkt->screen;
  284. scroller.direction = pkt->direction;
  285. scroller.pause = htonl(pkt->pause);
  286. scroller.speed = htonl(pkt->speed);
  287. scroller.by = htons(pkt->by);
  288. scroller.mode = htons(pkt->mode);
  289. scroller.width = htons(pkt->width);
  290. // copy scroll frame data
  291. if (scroller.size + size < scroller.max) {
  292. memcpy(scroller.scroll_frame + offset, data + sizeof(struct grfs_packet), size);
  293. scroller.size = offset + size;
  294. LOG_INFO("scroller size now %u", scroller.size);
  295. } else {
  296. LOG_INFO("scroller too larger %u/%u", scroller.size + size, scroller.max);
  297. }
  298. }
  299. /****************************************************************************************
  300. * Scroll background frame update & go
  301. */
  302. static void grfg_handler(u8_t *data, int len) {
  303. struct grfg_packet *pkt = (struct grfg_packet*) data;
  304. memcpy(scroller.back_frame, data + sizeof(struct grfg_packet), len - sizeof(struct grfg_packet));
  305. scroller.scroll_width = htons(pkt->width);
  306. scroller.active = true;
  307. vTaskResume(scroller.task);
  308. LOG_INFO("gfrg s:%hu w:%hu (len:%u)", htons(pkt->screen), htons(pkt->width), len);
  309. }
  310. /****************************************************************************************
  311. * Scroll task
  312. */
  313. static void scroll_task(void *args) {
  314. u8_t *scroll_ptr, *frame;
  315. bool active = false;
  316. int len = display->width * display->height / 8;
  317. int scroll_len;
  318. while (1) {
  319. if (!active) vTaskSuspend(NULL);
  320. // restart at the beginning - I don't know what right scrolling means ...
  321. scroll_ptr = scroller.scroll_frame;
  322. scroll_len = len - (display->width - scroller.scroll_width) * display->height / 8;
  323. frame = malloc(display->width * display->height / 8);
  324. // scroll required amount of columns (within the window)
  325. while (scroll_ptr <= scroller.scroll_frame + scroller.size - scroller.by * display->height / 8 - len) {
  326. scroll_ptr += scroller.by * display->height / 8;
  327. // build a combined frame
  328. memcpy(frame, scroller.back_frame, len);
  329. for (int i = 0; i < scroll_len; i++) frame[i] |= scroll_ptr[i];
  330. xSemaphoreTake(display_sem, portMAX_DELAY);
  331. active = scroller.active;
  332. if (!active) {
  333. LOG_INFO("scrolling interrupted");
  334. xSemaphoreGive(display_sem);
  335. break;
  336. }
  337. display->draw_cbr(frame);
  338. xSemaphoreGive(display_sem);
  339. usleep(scroller.speed * 1000);
  340. }
  341. if (active) {
  342. // build default screen
  343. memcpy(frame, scroller.back_frame, len);
  344. for (int i = 0; i < scroll_len; i++) frame[i] |= scroller.scroll_frame[i];
  345. xSemaphoreTake(display_sem, portMAX_DELAY);
  346. display->draw_cbr(frame);
  347. xSemaphoreGive(display_sem);
  348. // pause for required time and continue (or not)
  349. usleep(scroller.pause * 1000);
  350. active = (scroller.mode == 0);
  351. }
  352. free(frame);
  353. }
  354. }