display.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. struct grfb_packet {
  23. char opcode[4];
  24. u16_t brightness;
  25. };
  26. struct grfe_packet {
  27. char opcode[4];
  28. u16_t offset;
  29. u8_t transition;
  30. u8_t param;
  31. };
  32. struct grfs_packet {
  33. char opcode[4];
  34. u8_t screen;
  35. u8_t direction; // 1=left, 2=right
  36. u32_t pause; // in ms
  37. u32_t speed; // in ms
  38. u16_t by; // # of pixel of scroll step
  39. u16_t mode; // 0=continuous, 1=once and stop, 2=once and end
  40. u16_t width; // total width of animation
  41. u16_t offset; // offset if multiple packets are sent
  42. };
  43. struct grfg_packet {
  44. char opcode[4];
  45. u16_t screen;
  46. u16_t width; // # of pixels of scrollable
  47. };
  48. #define LINELEN 40
  49. static log_level loglevel = lINFO;
  50. static bool (*slimp_handler_chain)(u8_t *data, int len);
  51. static void (*notify_chain)(in_addr_t ip, u16_t hport, u16_t cport);
  52. static void server(in_addr_t ip, u16_t hport, u16_t cport);
  53. static bool handler(u8_t *data, int len);
  54. static void vfdc_handler( u8_t *_data, int bytes_read);
  55. static void grfe_handler( u8_t *data, int len);
  56. static void grfb_handler(u8_t *data, int len);
  57. static void grfs_handler(u8_t *data, int len);
  58. static void grfg_handler(u8_t *data, int len);
  59. /* scrolling undocumented information
  60. grfs
  61. B: screen number
  62. B:1 = left, 2 = right,
  63. Q: scroll pause once done (ms)
  64. Q: scroll speed (ms)
  65. W: # of pixels to scroll each time
  66. W: 0 = continue scrolling after pause, 1 = scroll to scrollend and then stop, 2 = scroll to scrollend and then end animation (causing new update)
  67. W: width of total scroll area in pixels
  68. grfd
  69. W: screen number
  70. W: width of scrollable area in pixels
  71. ANIC flags
  72. ANIM_TRANSITION 0x01 # A transition animation has finished
  73. ANIM_SCROLL_ONCE 0x02 # A scrollonce has finished
  74. ANIM_SCREEN_1 0x04 # For scrollonce only, screen 1 was scrolling
  75. ANIM_SCREEN_2 0x08 # For scrollonce only, screen 2 was scrolling
  76. */
  77. /****************************************************************************************
  78. *
  79. */
  80. void sb_display_init(void) {
  81. slimp_handler_chain = slimp_handler;
  82. slimp_handler = handler;
  83. notify_chain = server_notify;
  84. server_notify = server;
  85. }
  86. /****************************************************************************************
  87. *
  88. */
  89. static void server(in_addr_t ip, u16_t hport, u16_t cport) {
  90. char msg[32];
  91. sprintf(msg, "%s:%hu", inet_ntoa(ip), hport);
  92. display->text(DISPLAY_CENTER, DISPLAY_CLEAR | DISPLAY_UPDATE, msg);
  93. if (notify_chain) (*notify_chain)(ip, hport, cport);
  94. }
  95. /****************************************************************************************
  96. * Process graphic display data
  97. */
  98. static bool handler(u8_t *data, int len){
  99. bool res = true;
  100. if (!strncmp((char*) data, "vfdc", 4)) {
  101. vfdc_handler(data, len);
  102. } else if (!strncmp((char*) data, "grfe", 4)) {
  103. grfe_handler(data, len);
  104. } else if (!strncmp((char*) data, "grfb", 4)) {
  105. grfb_handler(data, len);
  106. } else if (!strncmp((char*) data, "grfs", 4)) {
  107. grfs_handler(data, len);
  108. } else if (!strncmp((char*) data, "grfg", 4)) {
  109. grfg_handler(data, len);
  110. } else {
  111. res = false;
  112. }
  113. // chain protocol handlers (bitwise or is fine)
  114. if (*slimp_handler_chain) res |= (*slimp_handler_chain)(data, len);
  115. return res;
  116. }
  117. /****************************************************************************************
  118. * Change special LCD chars to something more printable on screen
  119. */
  120. static void makeprintable(unsigned char * line) {
  121. for (int n = 0; n < LINELEN; n++) {
  122. switch (line[n]) {
  123. case 11: /* block */
  124. line[n] = '#';
  125. break;;
  126. case 16: /* rightarrow */
  127. line[n] = '>';
  128. break;;
  129. case 22: /* circle */
  130. line[n] = '@';
  131. break;;
  132. case 145: /* note */
  133. line[n] = ' ';
  134. break;;
  135. case 152: /* bell */
  136. line[n] = 'o';
  137. break;
  138. default:
  139. break;
  140. }
  141. }
  142. }
  143. /****************************************************************************************
  144. * Check if char is printable, or a valid symbol
  145. */
  146. static bool charisok(unsigned char c) {
  147. switch (c) {
  148. case 11: /* block */
  149. case 16: /* rightarrow */
  150. case 22: /* circle */
  151. case 145: /* note */
  152. case 152: /* bell */
  153. return true;
  154. break;;
  155. default:
  156. return isprint(c);
  157. }
  158. }
  159. /****************************************************************************************
  160. * Show the display (text mode)
  161. */
  162. static void show_display_buffer(char *ddram) {
  163. char line1[LINELEN+1];
  164. char *line2;
  165. memset(line1, 0, LINELEN+1);
  166. strncpy(line1, ddram, LINELEN);
  167. line2 = &(ddram[LINELEN]);
  168. line2[LINELEN] = '\0';
  169. /* Convert special LCD chars */
  170. makeprintable((unsigned char *)line1);
  171. makeprintable((unsigned char *)line2);
  172. LOG_INFO("\n\t%.40s\n\t%.40s", line1, line2);
  173. display->text(DISPLAY_TOP_LEFT, DISPLAY_CLEAR, line1);
  174. display->text(DISPLAY_BOTTOM_LEFT, DISPLAY_UPDATE, line2);
  175. }
  176. /****************************************************************************************
  177. * Process display data
  178. */
  179. static void vfdc_handler( u8_t *_data, int bytes_read) {
  180. unsigned short *data = (unsigned short*) _data, *display_data;
  181. char ddram[(LINELEN + 1) * 2];
  182. int n, addr = 0; /* counter */
  183. bytes_read -= 4;
  184. if (bytes_read % 2) bytes_read--; /* even number of bytes */
  185. // if we use Noritake VFD codes, display data starts at 12
  186. display_data = &(data[5]); /* display data starts at byte 10 */
  187. memset(ddram, ' ', LINELEN * 2);
  188. for (n = 0; n < (bytes_read/2); n++) {
  189. unsigned short d; /* data element */
  190. unsigned char t, c;
  191. d = ntohs(display_data[n]);
  192. t = (d & 0x00ff00) >> 8; /* type of display data */
  193. c = (d & 0x0000ff); /* character/command */
  194. switch (t) {
  195. case 0x03: /* character */
  196. if (!charisok(c)) c = ' ';
  197. if (addr <= LINELEN * 2) {
  198. ddram[addr++] = c;
  199. }
  200. break;
  201. case 0x02: /* command */
  202. switch (c) {
  203. case 0x06: /* display clear */
  204. memset(ddram, ' ', LINELEN * 2);
  205. break;
  206. case 0x02: /* cursor home */
  207. addr = 0;
  208. break;
  209. case 0xc0: /* cursor home2 */
  210. addr = LINELEN;
  211. break;
  212. }
  213. }
  214. }
  215. show_display_buffer(ddram);
  216. }
  217. /****************************************************************************************
  218. * Process graphic display data
  219. */
  220. static void grfe_handler( u8_t *data, int len) {
  221. display->v_draw(data + 8);
  222. }
  223. /****************************************************************************************
  224. * Brightness
  225. */
  226. static void grfb_handler(u8_t *data, int len) {
  227. s16_t brightness = htons(*(uint16_t*) (data + 4));
  228. LOG_INFO("brightness %hx", brightness);
  229. if (brightness < 0) {
  230. display->on(false);
  231. } else {
  232. display->on(true);
  233. display->brightness(brightness);
  234. }
  235. }
  236. /****************************************************************************************
  237. * Scroll set
  238. */
  239. static void grfs_handler(u8_t *data, int len) {
  240. }
  241. /****************************************************************************************
  242. * Scroll go
  243. */
  244. static void grfg_handler(u8_t *data, int len) {
  245. }