display.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * display.cpp
  3. *
  4. * Created on: Sep 26, 2017
  5. * Author: Philipp Hinz
  6. */
  7. #include "display.h"
  8. display_lang_t displayLang;
  9. timer displayTimer(displayTimerHandler);
  10. int lcd = 0;
  11. volatile int timerScaler = 0;
  12. volatile int elapsedCnt = 0;
  13. coffee_status_t coffeeState = STATE_OFF;
  14. /**
  15. * Prints out the current time in a centered position
  16. * @param line Target line in display
  17. */
  18. void displayPrintTime(int line) {
  19. time_t rawtime;
  20. struct tm * timeinfo;
  21. if (line > DISPLAY_ROWS)
  22. line = 0;
  23. time(&rawtime);
  24. timeinfo = localtime(&rawtime);
  25. lcdPosition(lcd, 0, line);
  26. lcdPrintf(lcd, " %.2d:%.2d:%.2d ", timeinfo->tm_hour,
  27. timeinfo->tm_min, timeinfo->tm_sec);
  28. }
  29. /**
  30. * Prints out the total volume flow
  31. * @param line Target line in display
  32. */
  33. void displayPrintFlow(int line) {
  34. float flow = halGetFlow();
  35. lcdPosition(lcd, 0, line);
  36. lcdPrintf(lcd, "%s: %.0f ml ", displayGetString(str_flow), flow);
  37. }
  38. /**
  39. * Prints a string to a specific line, optionally centered.
  40. * This function also fills out the remaining row of the display with spaces,
  41. * to ensure there is no old data left.
  42. * @param line Target line in display
  43. * @param *str String to print
  44. * @param centered Print centered or not
  45. */
  46. void displayPrintLn(int line, const char* str, bool centered) {
  47. char buf[DISPLAY_COLS + 1];
  48. int len = strlen(str);
  49. int i = 0;
  50. int spaces = 0;
  51. if (len > DISPLAY_COLS)
  52. len = DISPLAY_COLS;
  53. if (line > DISPLAY_ROWS)
  54. line = 0;
  55. if (centered) {
  56. spaces = (DISPLAY_COLS - len) / 2;
  57. if (spaces) {
  58. for (i = 0; i < spaces; i++) {
  59. buf[i] = ' ';
  60. }
  61. }
  62. }
  63. for (i = 0; i < len; i++) {
  64. buf[spaces + i] = str[i];
  65. }
  66. if ((len + spaces) < DISPLAY_COLS) { // fill remaining space
  67. for (i = i + spaces; i < DISPLAY_COLS; i++) {
  68. buf[i] = ' ';
  69. }
  70. }
  71. lcdPosition(lcd, 0, line);
  72. buf[DISPLAY_COLS] = '\0';
  73. lcdPrintf(lcd, buf);
  74. //logger(V_HAL, "Printed out on display: \"%s\"\n", buf);
  75. }
  76. /**
  77. * Updates the display state to the matching coffee state
  78. * @param event Event data
  79. */
  80. void displayStateUpdated(event_t *event) {
  81. if (event->len != sizeof(coffee_status_t)) {
  82. logger_error("Invalid use of event %s\n", event->event);
  83. return;
  84. }
  85. coffee_status_t state = *(coffee_status_t*)event->data;
  86. if (state != coffeeState) {
  87. coffeeState = state;
  88. timerScaler--;
  89. elapsedCnt = 0;
  90. displayTimer.call();
  91. }
  92. }
  93. /**
  94. * Handles cleanup before program termination
  95. */
  96. void displayTerminate(event_t *e) {
  97. displayPrintLn(0, "CoffeePi", true);
  98. displayPrintLn(1, displayGetString(str_bye), true);
  99. }
  100. /**
  101. * Main thread to handle display data
  102. * @param *threadid Thread ID
  103. */
  104. void* displayThread(void* threadid) {
  105. sleep(1); // Wait for other components to initialize
  106. int tmp = sqlGetConf(CFGdisplaylang);
  107. if (!tmp || tmp >= lang_last)
  108. tmp = DEFAULT_LANG;
  109. displaySetLang((display_lang_t) tmp);
  110. displayTimer.start();
  111. event_subscribe("statechange", &displayStateUpdated);
  112. event_subscribe("terminate", &displayTerminate);
  113. while (1) {
  114. pause();
  115. if (1) {
  116. timerScaler--;
  117. displayTimer.call();
  118. }
  119. }
  120. pthread_exit(EXIT_SUCCESS);
  121. }
  122. /**
  123. * Timer handler for display update
  124. * @param *threadid Thread ID
  125. */
  126. void* displayTimerHandler(void* threadid) {
  127. int scale1Hz = 0;
  128. if (timerScaler++ >= REFRESH_RATE) { // 1s elapsed, reset
  129. timerScaler = 0;
  130. scale1Hz = 1;
  131. }
  132. int scale5Hz = (timerScaler == (REFRESH_RATE / 5) ? 0 : 1);
  133. if (scale1Hz) {
  134. switch (coffeeState) {
  135. case STATE_OFF:
  136. case STATE_HEATING:
  137. case STATE_INITALHEATING:
  138. case STATE_IDLE:
  139. case STATE_CLEANING:
  140. case STATE_ERROR:
  141. displayRefresh();
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. if (scale5Hz) {
  148. switch (coffeeState) {
  149. case STATE_BREW:
  150. case STATE_BREWMANUAL:
  151. displayRefresh();
  152. break;
  153. default:
  154. break;
  155. }
  156. }
  157. if (elapsedCnt < (5 * REFRESH_RATE)) // Don't let it grow too large
  158. elapsedCnt++;
  159. pthread_exit(EXIT_SUCCESS);
  160. }
  161. /**
  162. * Initializes display
  163. */
  164. void displayInit(void) {
  165. lcd = lcdInit();
  166. if (lcd < 0)
  167. logger_error("Error: unable to init LCD (%d)\n", lcd);
  168. lcdClear(lcd);
  169. lcdHome(lcd);
  170. displayPrintLn(0, (char*) "CoffeePi", true);
  171. displayPrintLn(1, (char*) "booting...", true);
  172. //lcdPrintf(lcd, " CoffeePi booting...");
  173. timerScaler = 0;
  174. displayTimer.setDivider((1000000 / TIMER_DELAY_US) / REFRESH_RATE);
  175. logger(V_BASIC, "Initialized display with a refresh rate of %d Hz\n",
  176. REFRESH_RATE);
  177. }
  178. /**
  179. * Sets the language of the display text
  180. * @param lang New language
  181. */
  182. void displaySetLang(display_lang_t lang) {
  183. displayLang = lang;
  184. }
  185. /**
  186. * Refreshed the display content and outputs it
  187. */
  188. void displayRefresh(void) {
  189. switch (coffeeState) { //coffeeGetState()
  190. case STATE_IDLE:
  191. displayPrintLn(0, "CoffeePi", true);
  192. displayPrintLn(1, displayGetString(str_ready), true);
  193. break;
  194. case STATE_INITALHEATING:
  195. displayPrintLn(0, "CoffeePi", true);
  196. displayPrintLn(1, displayGetString(str_heating), true);
  197. break;
  198. case STATE_HEATING:
  199. displayPrintLn(0, "CoffeePi", true);
  200. displayPrintLn(1, displayGetString(str_heatingready), true);
  201. break;
  202. case STATE_BREW:
  203. if (elapsedCnt <= 2 * REFRESH_RATE) {
  204. displayPrintLn(0, "CoffeePi", true);
  205. displayPrintLn(1, displayGetString(str_brewing), true);
  206. } else {
  207. displayPrintLn(0, displayGetString(str_brewing), true);
  208. displayPrintFlow(1);
  209. }
  210. break;
  211. case STATE_BREWMANUAL:
  212. displayPrintLn(0, displayGetString(str_brewing), true);
  213. displayPrintFlow(1);
  214. break;
  215. case STATE_CLEANING:
  216. displayPrintLn(0, "CoffeePi", true);
  217. displayPrintLn(1, displayGetString(str_cleaning), true);
  218. break;
  219. case STATE_ERROR:
  220. displayPrintLn(0, "CoffeePi", true);
  221. displayPrintLn(1, displayGetString(str_error), true);
  222. break;
  223. case STATE_OFF:
  224. default:
  225. displayPrintLn(0, "CoffeePi", true);
  226. displayPrintTime(1);
  227. break;
  228. }
  229. }
  230. /**
  231. * Returns the matching translation of a string
  232. * @param string Requested string
  233. * @return Translated string
  234. */
  235. const char* displayGetString(display_strings_t string) {
  236. if (displayLang >= lang_last)
  237. displayLang = DEFAULT_LANG;
  238. return display_strings[string].text[displayLang];
  239. }