display.c 12 KB

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