display.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. #include "DS18B20.h"
  22. display_lang_t displayLang;
  23. timer displayTimer(displayTimerHandler);
  24. int lcd = 0;
  25. volatile int timerScaler = 0;
  26. volatile int elapsedCnt = 0;
  27. coffee_status_t coffeeState = STATE_OFF;
  28. coffee_mode_t coffeeMode = MODE_STATE;
  29. coffee_menuPage_t coffeePage = PAGE_SOFTOFF;
  30. /**
  31. * Prints out the current time in a centered position
  32. * @param line Target line in display
  33. */
  34. void displayPrintTime(int line) {
  35. time_t rawtime;
  36. struct tm * timeinfo;
  37. if (line > DISPLAY_ROWS)
  38. line = 0;
  39. time(&rawtime);
  40. timeinfo = localtime(&rawtime);
  41. lcdPosition(lcd, 0, line);
  42. lcdPrintf(lcd, " %.2d:%.2d:%.2d ", timeinfo->tm_hour,
  43. timeinfo->tm_min, timeinfo->tm_sec);
  44. }
  45. /**
  46. * Prints out the current temperature in a centered position
  47. * @param line Target line in display
  48. */
  49. void displayPrintTemp(int line) {
  50. if (line > DISPLAY_ROWS)
  51. line = 0;
  52. lcdPosition(lcd, 0, line);
  53. lcdPrintf(lcd, " %d C ", DS18B20_readTemp());
  54. }
  55. /**
  56. * Prints out the total volume flow
  57. * @param line Target line in display
  58. */
  59. void displayPrintFlow(int line) {
  60. float flow = halGetFlow();
  61. lcdPosition(lcd, 0, line);
  62. lcdPrintf(lcd, "%s: %.0f ml ", displayGetString(str_flow), flow);
  63. }
  64. /**
  65. * Prints a string to a specific line, optionally centered.
  66. * This function also fills out the remaining row of the display with spaces,
  67. * to ensure there is no old data left.
  68. * @param line Target line in display
  69. * @param *str String to print
  70. * @param centered Print centered or not
  71. */
  72. void displayPrintLn(int line, const char* str, bool centered) {
  73. char buf[DISPLAY_COLS + 1];
  74. int len = strlen(str);
  75. int i = 0;
  76. int spaces = 0;
  77. if (len > DISPLAY_COLS)
  78. len = DISPLAY_COLS;
  79. if (line > DISPLAY_ROWS)
  80. line = 0;
  81. if (centered) {
  82. spaces = (DISPLAY_COLS - len) / 2;
  83. if (spaces) {
  84. for (i = 0; i < spaces; i++) {
  85. buf[i] = ' ';
  86. }
  87. }
  88. }
  89. for (i = 0; i < len; i++) {
  90. buf[spaces + i] = str[i];
  91. }
  92. if ((len + spaces) < DISPLAY_COLS) { // fill remaining space
  93. for (i = i + spaces; i < DISPLAY_COLS; i++) {
  94. buf[i] = ' ';
  95. }
  96. }
  97. lcdPosition(lcd, 0, line);
  98. buf[DISPLAY_COLS] = '\0';
  99. lcdPrintf(lcd, buf);
  100. //logger(V_HAL, "Printed out on display: \"%s\"\n", buf);
  101. }
  102. /**
  103. * Updates the display state to the matching coffee state
  104. * @param event Event data
  105. */
  106. void displayStateUpdated(event_t *event) {
  107. if (event->len != sizeof(coffee_status_t)) {
  108. logger_error("Invalid use of event %s\n", event->event);
  109. return;
  110. }
  111. coffee_status_t state = *(coffee_status_t*) event->data;
  112. if (state != coffeeState) {
  113. coffeeState = state;
  114. timerScaler = REFRESH_RATE;
  115. elapsedCnt = 0;
  116. displayTimer.call();
  117. }
  118. }
  119. /**
  120. * Updates the display state to the matching coffee display mode
  121. * @param event Event data
  122. */
  123. void displayModeUpdated(event_t *event) {
  124. if (event->len != sizeof(coffee_mode_t)) {
  125. logger_error("Invalid use of event %s\n", event->event);
  126. return;
  127. }
  128. coffee_mode_t mode = *(coffee_mode_t*) event->data;
  129. if (mode != coffeeMode) {
  130. coffeeMode = mode;
  131. timerScaler = REFRESH_RATE;
  132. elapsedCnt = 0;
  133. displayTimer.call();
  134. }
  135. }
  136. /**
  137. * Updates the display state to the matching coffee display page
  138. * @param event Event data
  139. */
  140. void displayPageUpdated(event_t *event) {
  141. if (event->len != sizeof(coffee_menuPage_t)) {
  142. logger_error("Invalid use of event %s\n", event->event);
  143. return;
  144. }
  145. coffee_menuPage_t page = *(coffee_menuPage_t*) event->data;
  146. if (page != coffeePage) {
  147. coffeePage = page;
  148. timerScaler = REFRESH_RATE;
  149. elapsedCnt = 0;
  150. displayTimer.call();
  151. }
  152. }
  153. /**
  154. * Handles cleanup before program termination
  155. */
  156. void displayTerminate(event_t *e) {
  157. displayPrintLn(0, "CoffeePi", true);
  158. displayPrintLn(1, displayGetString(str_bye), true);
  159. }
  160. /**
  161. * Main thread to handle display data
  162. * @param *threadid Thread ID
  163. */
  164. void* displayThread(void* threadid) {
  165. sleep(1); // Wait for other components to initialize
  166. int tmp = sqlGetConf(CFGdisplaylang);
  167. if (!tmp || tmp >= lang_last)
  168. tmp = DEFAULT_LANG;
  169. displaySetLang((display_lang_t) tmp);
  170. displayTimer.start();
  171. event_subscribe("statechange", &displayStateUpdated);
  172. event_subscribe("modechange", &displayModeUpdated);
  173. event_subscribe("pagechange", &displayPageUpdated);
  174. event_subscribe("terminate", &displayTerminate);
  175. while (1) {
  176. pause();
  177. if (1) {
  178. timerScaler = REFRESH_RATE;
  179. displayTimer.call();
  180. }
  181. }
  182. pthread_exit(EXIT_SUCCESS);
  183. }
  184. /**
  185. * Timer handler for display update
  186. * @param *threadid Thread ID
  187. */
  188. void* displayTimerHandler(void* threadid) {
  189. int scale1Hz = 0;
  190. if (timerScaler++ >= REFRESH_RATE) { // 1s elapsed, reset
  191. timerScaler = 0;
  192. scale1Hz = 1;
  193. }
  194. if (scale1Hz) {
  195. if (coffeeMode == MODE_STATE) {
  196. switch (coffeeState) {
  197. case STATE_OFF:
  198. case STATE_HEATING:
  199. case STATE_INITALHEATING:
  200. case STATE_IDLE:
  201. case STATE_CLEANING:
  202. case STATE_ERROR:
  203. case STATE_WAIT_OFF:
  204. displayRefresh();
  205. break;
  206. default:
  207. break;
  208. }
  209. } else if (coffeeMode == MODE_MENU) {
  210. displayRefresh();
  211. }
  212. }
  213. switch (coffeeState) {
  214. case STATE_BREW:
  215. case STATE_BREWMANUAL:
  216. displayRefresh();
  217. break;
  218. default:
  219. break;
  220. }
  221. if (elapsedCnt < (5 * REFRESH_RATE)) // Don't let it grow too large
  222. elapsedCnt++;
  223. pthread_exit(EXIT_SUCCESS);
  224. }
  225. /**
  226. * Initializes display
  227. */
  228. void displayInit(void) {
  229. lcd = lcdInit();
  230. if (lcd < 0)
  231. logger_error("Error: unable to init LCD (%d)\n", lcd);
  232. lcdClear(lcd);
  233. lcdHome(lcd);
  234. displayPrintLn(0, (char*) "CoffeePi", true);
  235. displayPrintLn(1, (char*) "booting...", true);
  236. //lcdPrintf(lcd, " CoffeePi booting...");
  237. timerScaler = 0;
  238. displayTimer.setDivider((1000000 / TIMER_DELAY_US) / REFRESH_RATE);
  239. logger(V_BASIC, "Initialized display with a refresh rate of %d Hz\n",
  240. REFRESH_RATE);
  241. }
  242. /**
  243. * Sets the language of the display text
  244. * @param lang New language
  245. */
  246. void displaySetLang(display_lang_t lang) {
  247. displayLang = lang;
  248. }
  249. /**
  250. * Refreshed the display content and outputs it
  251. */
  252. void displayRefresh(void) {
  253. if (coffeeMode == MODE_STATE) {
  254. switch (coffeeState) { //coffeeGetState()
  255. case STATE_IDLE:
  256. displayPrintLn(0, "CoffeePi", true);
  257. displayPrintLn(1, displayGetString(str_ready), true);
  258. break;
  259. case STATE_INITALHEATING:
  260. displayPrintLn(0, "CoffeePi", true);
  261. displayPrintLn(1, displayGetString(str_heating), true);
  262. break;
  263. case STATE_HEATING:
  264. displayPrintLn(0, "CoffeePi", true);
  265. displayPrintLn(1, displayGetString(str_heatingready), true);
  266. break;
  267. case STATE_BREW:
  268. if (elapsedCnt <= 2 * REFRESH_RATE) {
  269. displayPrintLn(0, "CoffeePi", true);
  270. displayPrintLn(1, displayGetString(str_brewing), true);
  271. } else {
  272. displayPrintLn(0, displayGetString(str_brewing), true);
  273. displayPrintFlow(1);
  274. }
  275. break;
  276. case STATE_BREWMANUAL:
  277. displayPrintLn(0, displayGetString(str_brewing), true);
  278. displayPrintFlow(1);
  279. break;
  280. case STATE_CLEANING:
  281. displayPrintLn(0, "CoffeePi", true);
  282. displayPrintLn(1, displayGetString(str_cleaning), true);
  283. break;
  284. case STATE_ERROR:
  285. displayPrintLn(0, "CoffeePi", true);
  286. displayPrintLn(1, displayGetString(str_error), true);
  287. break;
  288. case STATE_WAIT_OFF:
  289. displayPrintLn(0, "CoffeePi", true);
  290. displayPrintLn(1, displayGetString(str_waitoff), true);
  291. break;
  292. case STATE_OFF:
  293. default:
  294. displayPrintLn(0, "CoffeePi", true);
  295. displayPrintTime(1);
  296. break;
  297. }
  298. } else if (coffeeMode == MODE_MENU) {
  299. switch (coffeePage) {
  300. case PAGE_SOFTOFF:
  301. displayPrintLn(0, displayGetString(str_menu), true);
  302. displayPrintLn(1, displayGetString(str_menu_softoff), true);
  303. break;
  304. case PAGE_KILL:
  305. displayPrintLn(0, displayGetString(str_menu), true);
  306. displayPrintLn(1, displayGetString(str_menu_kill), true);
  307. break;
  308. case PAGE_STATS:
  309. displayPrintLn(0, displayGetString(str_menu), true);
  310. displayPrintLn(1, displayGetString(str_menu_stats), true); // FIXME: Show stats
  311. break;
  312. case PAGE_TEMP:
  313. displayPrintLn(0, displayGetString(str_menu_temp), true);
  314. displayPrintTemp(1);
  315. break;
  316. case PAGE_CLEAN:
  317. displayPrintLn(0, displayGetString(str_menu), true);
  318. displayPrintLn(1, displayGetString(str_menu_clean), true);
  319. break;
  320. case PAGE_DEMO:
  321. displayPrintLn(0, displayGetString(str_menu), true);
  322. displayPrintLn(1, displayGetString(str_menu_demo), true);
  323. break;
  324. case PAGE_EXIT:
  325. displayPrintLn(0, displayGetString(str_menu), true);
  326. displayPrintLn(1, displayGetString(str_menu_exit), true);
  327. break;
  328. default:
  329. displayPrintLn(0, displayGetString(str_menu), true);
  330. displayPrintLn(1, "???", true);
  331. break;
  332. }
  333. }
  334. }
  335. /**
  336. * Returns the matching translation of a string
  337. * @param string Requested string
  338. * @return Translated string
  339. */
  340. const char* displayGetString(display_strings_t string) {
  341. if (displayLang >= lang_last)
  342. displayLang = DEFAULT_LANG;
  343. return display_strings[string].text[displayLang];
  344. }