display.cpp 6.3 KB

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