display.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. bool coffeeDescaling = false;
  31. /**
  32. * Prints out the current time in a centered position
  33. * @param line Target line in display
  34. */
  35. void displayPrintTime(int line) {
  36. time_t rawtime;
  37. struct tm * timeinfo;
  38. if (line > DISPLAY_ROWS)
  39. line = 0;
  40. time(&rawtime);
  41. timeinfo = localtime(&rawtime);
  42. lcdPosition(lcd, 0, line);
  43. lcdPrintf(lcd, " %.2d:%.2d:%.2d ", timeinfo->tm_hour,
  44. timeinfo->tm_min, timeinfo->tm_sec);
  45. }
  46. /**
  47. * Prints out the current temperature in a centered position
  48. * @param line Target line in display
  49. */
  50. void displayPrintTemp(int line) {
  51. //TODO: the temperature is wrong(old value) the first time the page is opened
  52. //after the first refresh the right temperature is displayed
  53. //no idea where this comes from, datasheet says nothing about dummy readouts.
  54. //no old values are used, conversion is started before every readout...
  55. if (line > DISPLAY_ROWS)
  56. line = 0;
  57. lcdPosition(lcd, 0, line);
  58. lcdPrintf(lcd, " %d C ", DS18B20_readTemp());
  59. }
  60. /**
  61. * Prints the total epsressi brewed since reset
  62. */
  63. void displayPrintStats(int line) {
  64. char buffer[17];
  65. if (line > DISPLAY_ROWS)
  66. line = 0;
  67. lcdPosition(lcd, 0, line);
  68. sprintf(buffer, "%7d espressi", getBrewCounter());
  69. lcdPrintf(lcd, buffer);
  70. }
  71. /**
  72. * Prints the total heating time in kWh
  73. */
  74. void displayPrintStats2(int line) {
  75. char buffer[17];
  76. if (line > DISPLAY_ROWS)
  77. line = 0;
  78. uint64_t totalkWh = 2 * getTotalHeatingTime()/(60*60);
  79. sprintf(buffer, "%lld kWh", totalkWh);
  80. displayPrintLn(line, buffer, true);
  81. }
  82. /**
  83. * Prints the remaining time or the remaining cups until the next descaling is necessary
  84. */
  85. void displayPrintNextDesc(int line) {
  86. char buffer[17];
  87. if (line > DISPLAY_ROWS)
  88. line = 0;
  89. sprintf(buffer, "%d d or %d C", checkDirtyTime(), checkDirtyEspresso());
  90. displayPrintLn(line, buffer, true);
  91. }
  92. /**
  93. * Prints out the total volume flow
  94. * @param line Target line in display
  95. */
  96. void displayPrintFlow(int line) {
  97. float flow = halGetFlow();
  98. lcdPosition(lcd, 0, line);
  99. lcdPrintf(lcd, "%s: %.0f ml ", displayGetString(str_flow), flow);
  100. }
  101. /**
  102. * Prints a string to a specific line, optionally centered.
  103. * This function also fills out the remaining row of the display with spaces,
  104. * to ensure there is no old data left.
  105. * @param line Target line in display
  106. * @param *str String to print
  107. * @param centered Print centered or not
  108. */
  109. void displayPrintLn(int line, const char* str, bool centered) {
  110. char buf[DISPLAY_COLS + 1];
  111. int len = strlen(str);
  112. int i = 0;
  113. int spaces = 0;
  114. if (len > DISPLAY_COLS)
  115. len = DISPLAY_COLS;
  116. if (line > DISPLAY_ROWS)
  117. line = 0;
  118. if (centered) {
  119. spaces = (DISPLAY_COLS - len) / 2;
  120. if (spaces) {
  121. for (i = 0; i < spaces; i++) {
  122. buf[i] = ' ';
  123. }
  124. }
  125. }
  126. for (i = 0; i < len; i++) {
  127. buf[spaces + i] = str[i];
  128. }
  129. if ((len + spaces) < DISPLAY_COLS) { // fill remaining space
  130. for (i = i + spaces; i < DISPLAY_COLS; i++) {
  131. buf[i] = ' ';
  132. }
  133. }
  134. //draw the descaling star
  135. if(coffeeDescaling && line == 0){
  136. buf[15] = '*';
  137. }
  138. lcdPosition(lcd, 0, line);
  139. buf[DISPLAY_COLS] = '\0';
  140. lcdPrintf(lcd, buf);
  141. //logger(V_HAL, "Printed out on display: \"%s\"\n", buf);
  142. }
  143. /**
  144. * Updates the display state to the matching coffee state
  145. * @param event Event data
  146. */
  147. void displayStateUpdated(event_t *event) {
  148. if (event->len != sizeof(coffee_status_t)) {
  149. logger_error("Invalid use of event %s\n", event->event);
  150. return;
  151. }
  152. coffee_status_t state = *(coffee_status_t*) event->data;
  153. if (state != coffeeState) {
  154. coffeeState = state;
  155. timerScaler = REFRESH_RATE;
  156. elapsedCnt = 0;
  157. displayTimer.call();
  158. }
  159. }
  160. /**
  161. * Updates the display state to the matching coffee display mode
  162. * @param event Event data
  163. */
  164. void displayModeUpdated(event_t *event) {
  165. if (event->len != sizeof(coffee_mode_t)) {
  166. logger_error("Invalid use of event %s\n", event->event);
  167. return;
  168. }
  169. coffee_mode_t mode = *(coffee_mode_t*) event->data;
  170. if (mode != coffeeMode) {
  171. coffeeMode = mode;
  172. timerScaler = REFRESH_RATE;
  173. elapsedCnt = 0;
  174. displayTimer.call();
  175. }
  176. }
  177. /**
  178. * Updates the display state to the matching coffee display page
  179. * @param event Event data
  180. */
  181. void displayPageUpdated(event_t *event) {
  182. if (event->len != sizeof(coffee_menuPage_t)) {
  183. logger_error("Invalid use of event %s\n", event->event);
  184. return;
  185. }
  186. coffee_menuPage_t page = *(coffee_menuPage_t*) event->data;
  187. if (page != coffeePage) {
  188. coffeePage = page;
  189. timerScaler = REFRESH_RATE;
  190. elapsedCnt = 0;
  191. displayTimer.call();
  192. }
  193. }
  194. /**
  195. * Updates the current descaling status of the machine
  196. */
  197. void displayDescaling(event_t *event) {
  198. if (event->len != sizeof(bool)) {
  199. logger_error("Invalid use of event %s\n", event->event);
  200. return;
  201. }
  202. coffeeDescaling = *(bool*) event->data;
  203. }
  204. /**
  205. * Prints the logo (CoffeePi) and also the temperature with the logo (CoffeePi@72C) if the machine is on
  206. */
  207. void displayPrintLogo(void) {
  208. char buffer[17];
  209. switch (coffeeState) {
  210. case STATE_HEATING:
  211. case STATE_INITALHEATING:
  212. case STATE_IDLE:
  213. sprintf(buffer, "CoffeePi @ %dC", DS18B20_readTemp());
  214. break;
  215. default:
  216. sprintf(buffer, "CoffeePi");
  217. break;
  218. }
  219. displayPrintLn(0, buffer, true);
  220. }
  221. /**
  222. * Handles cleanup before program termination
  223. */
  224. void displayTerminate(event_t *e) {
  225. logger(V_BASIC, "display.cpp: Terminating\n");
  226. displayPrintLn(0, "CoffeePi", true);
  227. displayPrintLn(1, displayGetString(str_bye), true);
  228. }
  229. /**
  230. * Main thread to handle display data
  231. * @param *threadid Thread ID
  232. */
  233. void* displayThread(void* threadid) {
  234. //sleep(1); // Wait for other components to initialize
  235. displayTimer.start();
  236. while (1) {
  237. pause();
  238. if (1) {
  239. timerScaler = REFRESH_RATE;
  240. displayTimer.call();
  241. }
  242. }
  243. pthread_exit(EXIT_SUCCESS);
  244. }
  245. /**
  246. * Timer handler for display update
  247. * @param *threadid Thread ID
  248. */
  249. void* displayTimerHandler(void* threadid) {
  250. int scale1Hz = 0;
  251. if (timerScaler++ >= REFRESH_RATE) { // 1s elapsed, reset
  252. timerScaler = 0;
  253. scale1Hz = 1;
  254. }
  255. if (scale1Hz) {
  256. if (coffeeMode == MODE_STATE) {
  257. switch (coffeeState) {
  258. case STATE_OFF:
  259. case STATE_HEATING:
  260. case STATE_INITALHEATING:
  261. case STATE_IDLE:
  262. case STATE_CLEANING:
  263. case STATE_ERROR:
  264. case STATE_WAIT_OFF:
  265. displayRefresh();
  266. break;
  267. default:
  268. break;
  269. }
  270. } else if (coffeeMode == MODE_MENU) {
  271. displayRefresh();
  272. }
  273. }
  274. switch (coffeeState) {
  275. case STATE_BREW:
  276. case STATE_BREWMANUAL:
  277. displayRefresh();
  278. break;
  279. default:
  280. break;
  281. }
  282. if (elapsedCnt < (5 * REFRESH_RATE)) // Don't let it grow too large
  283. elapsedCnt++;
  284. pthread_exit(EXIT_SUCCESS);
  285. }
  286. /**
  287. * Initializes display
  288. */
  289. void displayInit(void) {
  290. lcd = lcdInit();
  291. if (lcd < 0)
  292. logger_error("Error: unable to init LCD (%d)\n", lcd);
  293. lcdClear(lcd);
  294. lcdHome(lcd);
  295. displayPrintLn(0, (char*) "CoffeePi", true);
  296. displayPrintLn(1, (char*) "booting...", true);
  297. //lcdPrintf(lcd, " CoffeePi booting...");
  298. timerScaler = 0;
  299. displayTimer.setDivider((1000000 / TIMER_DELAY_US) / REFRESH_RATE);
  300. logger(V_BASIC, "Initialized display with a refresh rate of %d Hz\n",
  301. REFRESH_RATE);
  302. /**The following block comes from void* displayThread(void* threadid)
  303. * The idea is that the initialization functions get the component ready to react on external events
  304. * once the threads start, events might be triggered and every component can process them
  305. * This should fix the TO_DO where a descaling event was triggered during startup of the coffeethread and
  306. * the displaythread did not react to it since it hadn't subscribed to the event yet
  307. */
  308. int tmp = sqlGetConf(CFGdisplaylang);
  309. if (!tmp || tmp >= lang_last)
  310. tmp = DEFAULT_LANG;
  311. displaySetLang((display_lang_t) tmp);
  312. event_subscribe("statechange", &displayStateUpdated);
  313. event_subscribe("modechange", &displayModeUpdated);
  314. event_subscribe("pagechange", &displayPageUpdated);
  315. event_subscribe("terminate", &displayTerminate);
  316. event_subscribe("descaling", &displayDescaling);
  317. }
  318. /**
  319. * Sets the language of the display text
  320. * @param lang New language
  321. */
  322. void displaySetLang(display_lang_t lang) {
  323. displayLang = lang;
  324. }
  325. /**
  326. * Refreshed the display content and outputs it
  327. */
  328. void displayRefresh(void) {
  329. if (coffeeMode == MODE_STATE) {
  330. switch (coffeeState) { //coffeeGetState()
  331. case STATE_IDLE:
  332. displayPrintLogo();
  333. displayPrintLn(1, displayGetString(str_ready), true);
  334. break;
  335. case STATE_INITALHEATING:
  336. displayPrintLogo();
  337. displayPrintLn(1, displayGetString(str_heating), true);
  338. break;
  339. case STATE_HEATING:
  340. displayPrintLogo();
  341. displayPrintLn(1, displayGetString(str_heatingready), true);
  342. break;
  343. case STATE_BREW:
  344. if (elapsedCnt <= 2 * REFRESH_RATE) {
  345. displayPrintLogo();
  346. displayPrintLn(1, displayGetString(str_brewing), true);
  347. } else {
  348. displayPrintLn(0, displayGetString(str_brewing), true);
  349. displayPrintFlow(1);
  350. }
  351. break;
  352. case STATE_BREWMANUAL:
  353. displayPrintLn(0, displayGetString(str_brewing), true);
  354. displayPrintFlow(1);
  355. break;
  356. case STATE_CLEANING:
  357. displayPrintLogo();
  358. displayPrintLn(1, displayGetString(str_cleaning), true);
  359. break;
  360. case STATE_ERROR:
  361. displayPrintLogo();
  362. displayPrintLn(1, displayGetString(str_error), true);
  363. break;
  364. case STATE_WAIT_OFF:
  365. displayPrintLogo();
  366. displayPrintLn(1, displayGetString(str_waitoff), true);
  367. break;
  368. case STATE_OFF:
  369. default:
  370. displayPrintLogo();
  371. displayPrintTime(1);
  372. break;
  373. }
  374. } else if (coffeeMode == MODE_MENU) {
  375. switch (coffeePage) {
  376. case PAGE_SOFTOFF:
  377. displayPrintLn(0, displayGetString(str_menu), true);
  378. displayPrintLn(1, displayGetString(str_menu_softoff), true);
  379. break;
  380. case PAGE_KILL:
  381. displayPrintLn(0, displayGetString(str_menu), true);
  382. displayPrintLn(1, displayGetString(str_menu_kill), true);
  383. break;
  384. case PAGE_STATS:
  385. displayPrintLn(0, displayGetString(str_menu_stats), true);
  386. displayPrintStats(1);
  387. break;
  388. case PAGE_STATS2:
  389. displayPrintLn(0, displayGetString(str_menu_stats2), true);
  390. displayPrintStats2(1);
  391. break;
  392. case PAGE_DESCALING:
  393. displayPrintLn(0, displayGetString(str_menu_nextdesc), true);
  394. displayPrintNextDesc(1);
  395. break;
  396. case PAGE_TEMP:
  397. displayPrintLn(0, displayGetString(str_menu_temp), true);
  398. displayPrintTemp(1);
  399. break;
  400. case PAGE_CLEAN:
  401. displayPrintLn(0, displayGetString(str_menu), true);
  402. displayPrintLn(1, displayGetString(str_menu_clean), true);
  403. break;
  404. case PAGE_DEMO:
  405. displayPrintLn(0, displayGetString(str_menu), true);
  406. displayPrintLn(1, displayGetString(str_menu_demo), true);
  407. break;
  408. case PAGE_EXIT:
  409. displayPrintLn(0, displayGetString(str_menu), true);
  410. displayPrintLn(1, displayGetString(str_menu_exit), true);
  411. break;
  412. default:
  413. displayPrintLn(0, displayGetString(str_menu), true);
  414. displayPrintLn(1, "???", true);
  415. break;
  416. }
  417. }
  418. }
  419. /**
  420. * Returns the matching translation of a string
  421. * @param string Requested string
  422. * @return Translated string
  423. */
  424. const char* displayGetString(display_strings_t string) {
  425. if (displayLang >= lang_last)
  426. displayLang = DEFAULT_LANG;
  427. return display_strings[string].text[displayLang];
  428. }