display.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (C) 2003 by egnite Software GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * $Id: display.c 4473 2012-08-20 15:12:45Z haraldkipp $
  36. */
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <dev/term.h>
  41. #include <sys/thread.h>
  42. #include <sys/timer.h>
  43. #include <sys/event.h>
  44. #include <arpa/inet.h>
  45. #include "config.h"
  46. #include "player.h"
  47. #include "display.h"
  48. uint16_t lcd_offset;
  49. #define DISPLAY_LINES 2
  50. #define DISPLAY_VCOLUMNS 80
  51. char *sline[2];
  52. char *mline[2];
  53. uint16_t mticks[2];
  54. FILE *lcd;
  55. HANDLE updevt;
  56. /*
  57. * Background thread for playing stream.
  58. */
  59. THREAD(Displayer, arg)
  60. {
  61. unsigned int step[2] = { 0, 0 };
  62. unsigned int stepmx[2] = { 0, 0 };
  63. char *line;
  64. char *sptr = 0;
  65. uint8_t scrolling = DISPLAY_LINES;
  66. uint8_t ln;
  67. fputs(ESC_CURSOROFF, lcd);
  68. NutThreadSetPriority(128);
  69. for (;;) {
  70. NutEventWait(&updevt, 125);
  71. for (ln = 0; ln < DISPLAY_LINES; ln++) {
  72. if (mticks[ln]) {
  73. line = mline[ln];
  74. mticks[ln]--;
  75. } else
  76. line = sline[ln];
  77. if (stepmx[ln] != strlen(line)) {
  78. sptr = 0;
  79. scrolling = DISPLAY_LINES;
  80. stepmx[ln] = strlen(line);
  81. step[ln] = 0;
  82. }
  83. fprintf(lcd, ESC_POS "%c" "\x20", ln + 32);
  84. if (stepmx[ln] <= 16) {
  85. fputs(line, lcd);
  86. if (stepmx[ln] < 16)
  87. fputs(ESC_CLREOL, lcd);
  88. } else {
  89. if (step[ln] == stepmx[ln]) {
  90. if (scrolling == DISPLAY_LINES)
  91. scrolling = ln;
  92. if (scrolling == ln) {
  93. if (sptr == 0)
  94. sptr = line;
  95. else {
  96. sptr++;
  97. if (strlen(sptr) <= 16)
  98. step[ln]++;
  99. }
  100. fprintf(lcd, "%.16s", sptr);
  101. }
  102. } else if (step[ln]++ == 0) {
  103. fprintf(lcd, "%.16s", line);
  104. sptr = 0;
  105. scrolling = DISPLAY_LINES;
  106. } else if (step[ln] >= stepmx[ln] + 16)
  107. step[ln] = 0;
  108. }
  109. }
  110. }
  111. }
  112. void DisplayStation(RADIOSTATION * rsp)
  113. {
  114. if (rsp->rs_port) {
  115. if (rsp->rs_name && rsp->rs_name[0])
  116. strncpy(sline[0], rsp->rs_name, DISPLAY_VCOLUMNS);
  117. else
  118. strncpy(sline[0], inet_ntoa(rsp->rs_ip), DISPLAY_VCOLUMNS);
  119. } else
  120. sline[0][0] = 0;
  121. }
  122. /*!
  123. * \brief Display a specified status.
  124. *
  125. * \param status Status to be displayed.
  126. */
  127. void DisplayStatus(uint8_t status)
  128. {
  129. RADIOSTATION *rsp = &station[radio.rc_cstation];
  130. if (radio.rc_cstatus != status) {
  131. if (status == DIST_FORCE)
  132. status = radio.rc_cstatus;
  133. else
  134. radio.rc_cstatus = status;
  135. if (status == DIST_NONE) {
  136. strcpy(sline[0], "NutPiper");
  137. strcpy(sline[1], "Version 1.0");
  138. NutEventPost(&updevt);
  139. } else if (status == DIST_DEAD) {
  140. sprintf(sline[0], "Station %03u", radio.rc_cstation);
  141. strcpy(sline[1], "not available");
  142. NutEventPost(&updevt);
  143. } else if (status == DIST_CONNECTING) {
  144. DisplayStation(rsp);
  145. strcpy(sline[1], "Connecting...");
  146. NutEventPost(&updevt);
  147. } else if (status == DIST_CONNECTED) {
  148. DisplayStation(rsp);
  149. if (player.psi_metatitle && player.psi_metatitle[0])
  150. strncpy(sline[1], player.psi_metatitle, DISPLAY_VCOLUMNS);
  151. else if (rsp->rs_genre && rsp->rs_genre[0])
  152. strncpy(sline[1], rsp->rs_genre, DISPLAY_VCOLUMNS);
  153. NutEventPost(&updevt);
  154. }
  155. }
  156. }
  157. /*!
  158. * \brief Display a specified text.
  159. *
  160. * \param row Row position of the message.
  161. * \param secs Number of seconds to display the message. Set to 0 for
  162. * permanent display.
  163. * \param fmt Format string containing conversion specifications.
  164. */
  165. void DisplayMessage(uint8_t row, uint8_t secs, const char *fmt, ...)
  166. {
  167. va_list ap;
  168. va_start(ap, fmt);
  169. if (secs) {
  170. vsprintf(mline[row], fmt, ap);
  171. mticks[row] = secs * 4;
  172. } else
  173. vsprintf(sline[row], fmt, ap);
  174. va_end(ap);
  175. NutEventPost(&updevt);
  176. }
  177. /*!
  178. * \brief Display specified entry of the list of stations.
  179. *
  180. * \param rs Index of the list entry.
  181. */
  182. void DisplayEntry(uint8_t rs)
  183. {
  184. RADIOSTATION *rsp = &station[rs];
  185. if (rsp->rs_port && rsp->rs_name)
  186. sprintf(sline[0], "%03u %.12s", rs, rsp->rs_name);
  187. else
  188. sprintf(sline[0], "%03u", rs);
  189. if (rsp->rs_scandead)
  190. strcpy(sline[1], "not available");
  191. else if (rsp->rs_scantitle)
  192. strncpy(sline[1], rsp->rs_scantitle, DISPLAY_VCOLUMNS);
  193. else if (rsp->rs_genre)
  194. strncpy(sline[1], rsp->rs_genre, DISPLAY_VCOLUMNS);
  195. else if (rsp->rs_ip)
  196. strncpy(sline[1], inet_ntoa(rsp->rs_ip), DISPLAY_VCOLUMNS);
  197. else
  198. sline[1][0] = 0;
  199. NutEventPost(&updevt);
  200. }
  201. /*!
  202. * \brief Start background thread for display updates.
  203. *
  204. * \param name Display device name.
  205. *
  206. * \return 0 on success or -1 in case of a failure.
  207. */
  208. int DisplayInit(char *name)
  209. {
  210. if ((lcd = fopen(name, "w")) == 0)
  211. return -1;
  212. if ((sline[0] = malloc(DISPLAY_VCOLUMNS + 1)) == 0 ||
  213. (sline[1] = malloc(DISPLAY_VCOLUMNS + 1)) == 0 ||
  214. (mline[0] = malloc(DISPLAY_VCOLUMNS + 1)) == 0 || (mline[1] = malloc(DISPLAY_VCOLUMNS + 1)) == 0)
  215. return -1;
  216. sline[0][DISPLAY_VCOLUMNS] = 0;
  217. sline[1][DISPLAY_VCOLUMNS] = 0;
  218. if (NutThreadCreate("displ", Displayer, 0, 512) == 0)
  219. return -1;
  220. DisplayStatus(DIST_NONE);
  221. return 0;
  222. }