display.cpp 9.1 KB

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