display2.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 "display2.h"
  13. #include "global.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. #define CURRENT 0
  23. #define NEXT 1
  24. //timeouts for the transitions in ms
  25. #define TIMEOUT_PREBREW 1000
  26. #define TIMEOUT_POSTBREW 9000
  27. display_lang_t displayLang;
  28. timer displayTimer(displayTimerHandler);
  29. int lcd = 0;
  30. // currently only the history of states is of interest
  31. coffee_status_t coffeeState[] = {STATE_OFF, STATE_NULL}; // current - next state
  32. coffee_mode_t coffeeMode[] = {MODE_STATE, MODE_NULL};
  33. coffee_menuPage_t coffeePage[] = {PAGE_SOFTOFF, PAGE_NULL};
  34. bool coffeeDescaling = false;
  35. refreshRate_t currentRefreshRate = refresh_std;
  36. uint16_t holdNext[3] = {0, 0, 0}; //holdtime of the new {state, Mode, Page} in ms
  37. uint16_t nextWaitTime[3] = {0, 0, 0}; //actual waiting time of the new {state, Mode, Page}
  38. typedef enum {
  39. state_idx = 0,
  40. mode_idx = 1,
  41. page_idx = 2
  42. } idx_t;
  43. void track(idx_t idx);
  44. void switchToNextPage(coffee_menuPage_t* history);
  45. void switchToNextMode(coffee_mode_t* history);
  46. void switchToNextState(coffee_status_t* history);
  47. void setRefreshRate(refreshRate_t rate);
  48. void setSwitchToNextTimeout(idx_t idx, uint16_t millis);
  49. /**
  50. * Prints out the current time in a centered position
  51. * @param line Target line in display
  52. */
  53. void displayPrintTime(int line) {
  54. time_t rawtime;
  55. struct tm * timeinfo;
  56. if (line > DISPLAY_ROWS)
  57. line = 0;
  58. time(&rawtime);
  59. timeinfo = localtime(&rawtime);
  60. lcdPosition(lcd, 0, line);
  61. lcdPrintf(lcd, " %.2d:%.2d:%.2d ", timeinfo->tm_hour,
  62. timeinfo->tm_min, timeinfo->tm_sec);
  63. }
  64. /**
  65. * Prints out the current temperature in a centered position
  66. * @param line Target line in display
  67. */
  68. void displayPrintTemp(int line) {
  69. //TODO: the temperature is wrong(old value) the first time the page is opened
  70. //after the first refresh the right temperature is displayed
  71. //no idea where this comes from, datasheet says nothing about dummy readouts.
  72. //no old values are used, conversion is started before every readout...
  73. //Quick fix:
  74. DS18B20_readTemp();
  75. if (line > DISPLAY_ROWS)
  76. line = 0;
  77. lcdPosition(lcd, 0, line);
  78. lcdPrintf(lcd, " %d C ", DS18B20_readTemp());
  79. }
  80. /**
  81. * Prints the total epsressi brewed since reset
  82. */
  83. void displayPrintStats(int line) {
  84. char buffer[17];
  85. if (line > DISPLAY_ROWS)
  86. line = 0;
  87. lcdPosition(lcd, 0, line);
  88. sprintf(buffer, "%7d espressi", getBrewCounter());
  89. lcdPrintf(lcd, buffer);
  90. }
  91. /**
  92. * Prints the total heating time in kWh
  93. */
  94. void displayPrintStats2(int line) {
  95. char buffer[17];
  96. if (line > DISPLAY_ROWS)
  97. line = 0;
  98. uint64_t totalkWh = 2 * getTotalHeatingTime()/(60*60);
  99. sprintf(buffer, "%lld kWh", totalkWh);
  100. displayPrintLn(line, buffer, true);
  101. }
  102. /**
  103. * Prints the remaining time or the remaining cups until the next descaling is necessary
  104. */
  105. void displayPrintNextDesc(int line) {
  106. char buffer[17];
  107. if (line > DISPLAY_ROWS) line = 0;
  108. sprintf(buffer, "%d d or %d C", checkDirtyTime(), checkDirtyEspresso());
  109. displayPrintLn(line, buffer, true);
  110. }
  111. /**
  112. *
  113. */
  114. void displayPrintPostBrew(int line){
  115. char buffer[17];
  116. if (line > DISPLAY_ROWS) line = 0;
  117. float brewTime = (float)getLastBrewTime();
  118. sprintf(buffer, "%.1f ml / %.1f s", halGetLastFlow(), brewTime / 1000);
  119. displayPrintLn(line, buffer, true);
  120. }
  121. void displayPrintBrewManual(int line) {
  122. char buffer[17];
  123. if (line > DISPLAY_ROWS) line = 0;
  124. float brewTime = (float)getBrewTime();
  125. sprintf(buffer, "%.1f ml / %.1f s", halGetFlow(), brewTime / 1000);
  126. displayPrintLn(line, buffer, true);
  127. }
  128. /**
  129. * Prints out the total volume flow
  130. * @param line Target line in display
  131. */
  132. void displayPrintFlow(int line) {
  133. float flow = halGetFlow();
  134. lcdPosition(lcd, 0, line);
  135. lcdPrintf(lcd, "%s: %.0f ml ", displayGetString(str_flow), flow);
  136. }
  137. /**
  138. * Prints the current cycle of the cleaning process
  139. */
  140. void displayPrintCleanCycle(int line) {
  141. char buffer[17];
  142. if (line > DISPLAY_ROWS) line = 0;
  143. sprintf(buffer, "%2d of %2d", getCurrentCleanCycle(), CLEANING_CYCLES);
  144. displayPrintLn(line, buffer, true);
  145. }
  146. /**
  147. * Prints a string to a specific line, optionally centered.
  148. * This function also fills out the remaining row of the display with spaces,
  149. * to ensure there is no old data left.
  150. * @param line Target line in display
  151. * @param *str String to print
  152. * @param centered Print centered or not
  153. */
  154. void displayPrintLn(int line, const char* str, bool centered) {
  155. char buf[DISPLAY_COLS + 1];
  156. int len = strlen(str);
  157. int i = 0;
  158. int spaces = 0;
  159. if (len > DISPLAY_COLS)
  160. len = DISPLAY_COLS;
  161. if (line > DISPLAY_ROWS)
  162. line = 0;
  163. if (centered) {
  164. spaces = (DISPLAY_COLS - len) / 2;
  165. if (spaces) {
  166. for (i = 0; i < spaces; i++) {
  167. buf[i] = ' ';
  168. }
  169. }
  170. }
  171. for (i = 0; i < len; i++) {
  172. buf[spaces + i] = str[i];
  173. }
  174. if ((len + spaces) < DISPLAY_COLS) { // fill remaining space
  175. for (i = i + spaces; i < DISPLAY_COLS; i++) {
  176. buf[i] = ' ';
  177. }
  178. }
  179. //draw the descaling star
  180. if(coffeeDescaling && line == 0){
  181. buf[15] = '*';
  182. }
  183. lcdPosition(lcd, 0, line);
  184. buf[DISPLAY_COLS] = '\0';
  185. lcdPrintf(lcd, buf);
  186. //logger(V_HAL, "Printed out on display: \"%s\"\n", buf);
  187. }
  188. /**
  189. * Updates the display state to the matching coffee state
  190. * @param event Event data
  191. */
  192. void displayStateUpdated(event_t *event) {
  193. if (event->len != sizeof(coffee_status_t)) {
  194. logger_error("Invalid use of event %s\n", event->event);
  195. return;
  196. }
  197. coffee_status_t state = *(coffee_status_t*) event->data;
  198. coffeeState[NEXT] = state;
  199. displayTimer.call();
  200. }
  201. /**
  202. * Updates the display state to the matching coffee display mode
  203. * @param event Event data
  204. */
  205. void displayModeUpdated(event_t *event) {
  206. if (event->len != sizeof(coffee_mode_t)) {
  207. logger_error("Invalid use of event %s\n", event->event);
  208. return;
  209. }
  210. coffee_mode_t mode = *(coffee_mode_t*) event->data;
  211. coffeeMode[NEXT] = mode;
  212. displayTimer.call();
  213. }
  214. /**
  215. * Updates the display state to the matching coffee display page
  216. * The new state is put on hold (next) and the display can decided if it immediately makes the
  217. * new state to the current one or if a timeout is specified in which the new state is put on hold and the
  218. * state transition is processed.
  219. * One initial asynchronous call
  220. * @param event Event data
  221. */
  222. void displayPageUpdated(event_t *event) {
  223. if (event->len != sizeof(coffee_menuPage_t)) {
  224. logger_error("Invalid use of event %s\n", event->event);
  225. return;
  226. }
  227. coffee_menuPage_t page = *(coffee_menuPage_t*) event->data;
  228. coffeePage[NEXT] = page;
  229. displayTimer.call();
  230. }
  231. /**
  232. * Updates the current descaling status of the machine
  233. */
  234. void displayDescaling(event_t *event) {
  235. if (event->len != sizeof(bool)) {
  236. logger_error("Invalid use of event %s\n", event->event);
  237. return;
  238. }
  239. coffeeDescaling = *(bool*) event->data;
  240. }
  241. /**
  242. * Prints the logo (CoffeePi) and also the temperature with the logo (CoffeePi@72C) if the machine is on
  243. */
  244. void displayPrintLogo(void) {
  245. char buffer[17];
  246. switch (coffeeState[CURRENT]) {
  247. case STATE_HEATING:
  248. case STATE_INITALHEATING:
  249. case STATE_IDLE:
  250. sprintf(buffer, "CoffeePi @ %dC", DS18B20_readTemp());
  251. break;
  252. default:
  253. sprintf(buffer, "CoffeePi");
  254. break;
  255. }
  256. displayPrintLn(0, buffer, true);
  257. }
  258. /**
  259. * Handles cleanup before program termination
  260. */
  261. void displayTerminate(event_t *e) {
  262. logger(V_BASIC, "display.cpp: Terminating\n");
  263. displayPrintLn(0, "CoffeePi", true);
  264. displayPrintLn(1, displayGetString(str_bye), true);
  265. displayTimer.stop();
  266. }
  267. /**
  268. * Main thread to handle display data
  269. * @param *threadid Thread ID
  270. */
  271. void* displayThread(void* threadid) {
  272. //sleep(1); // Wait for other components to initialize
  273. displayTimer.start();
  274. while (1) {
  275. pause();
  276. if (1) {
  277. displayTimer.call();
  278. }
  279. }
  280. pthread_exit(EXIT_SUCCESS);
  281. }
  282. /**
  283. * Timer handler for display update
  284. * @param *threadid Thread ID
  285. */
  286. void* displayTimerHandler(void* threadid) {
  287. //track time of the new state in hold and switch if necessary
  288. track(state_idx);
  289. track(page_idx);
  290. track(mode_idx);
  291. displayRefresh();
  292. pthread_exit(EXIT_SUCCESS);
  293. }
  294. /**
  295. * Tracks the elapsed time in ms of the new state waiting to become the current one if a hold time is specified
  296. */
  297. void track(idx_t idx){
  298. if (holdNext[idx] != 0) {
  299. nextWaitTime[idx] += 1000 / currentRefreshRate;
  300. if (holdNext[idx] <= nextWaitTime[idx]) {
  301. switch(idx){
  302. case state_idx:
  303. switchToNextState(coffeeState);
  304. break;
  305. case page_idx:
  306. switchToNextPage(coffeePage);
  307. break;
  308. case mode_idx:
  309. switchToNextMode(coffeeMode);
  310. break;
  311. }
  312. }
  313. }
  314. }
  315. /**
  316. * Initializes display
  317. */
  318. void displayInit(void) {
  319. lcd = lcdInit();
  320. if (lcd < 0)
  321. logger_error("Error: unable to init LCD (%d)\n", lcd);
  322. lcdClear(lcd);
  323. lcdHome(lcd);
  324. displayPrintLn(0, (char*) "CoffeePi", true);
  325. displayPrintLn(1, (char*) "booting...", true);
  326. //lcdPrintf(lcd, " CoffeePi booting...");
  327. setRefreshRate(refresh_std);
  328. logger(V_BASIC, "display2.cpp Initialized display with a refresh rate of %d Hz\n",
  329. refresh_std);
  330. /**The following block comes from void* displayThread(void* threadid)
  331. * The idea is that the initialization functions get the component ready to react on external events
  332. * once the threads start, events might be triggered and every component can process them
  333. * This should fix the TO_DO where a descaling event was triggered during startup of the coffeethread and
  334. * the displaythread did not react to it since it hadn't subscribed to the event yet
  335. */
  336. int tmp = sqlGetConf(CFGdisplaylang);
  337. if (!tmp || tmp >= lang_last)
  338. tmp = DEFAULT_LANG;
  339. displaySetLang((display_lang_t) tmp);
  340. event_subscribe("statechange", &displayStateUpdated);
  341. event_subscribe("modechange", &displayModeUpdated);
  342. event_subscribe("pagechange", &displayPageUpdated);
  343. event_subscribe("terminate", &displayTerminate);
  344. event_subscribe("descaling", &displayDescaling);
  345. }
  346. /**
  347. * Sets the language of the display text
  348. * @param lang New language
  349. */
  350. void displaySetLang(display_lang_t lang) {
  351. displayLang = lang;
  352. }
  353. /*
  354. *
  355. */
  356. void switchToNextPage(coffee_menuPage_t* history){
  357. if(history[NEXT] != PAGE_NULL) {
  358. history[CURRENT] = history[NEXT];
  359. history[NEXT] = PAGE_NULL;
  360. holdNext[page_idx] = 0;
  361. nextWaitTime[page_idx] = 0;
  362. }
  363. }
  364. /*
  365. *
  366. */
  367. void switchToNextMode(coffee_mode_t* history){
  368. if(history[NEXT] != MODE_NULL) {
  369. history[CURRENT] = history[NEXT];
  370. history[NEXT] = MODE_NULL;
  371. holdNext[mode_idx] = 0;
  372. nextWaitTime[mode_idx] = 0;
  373. }
  374. }
  375. /*
  376. *
  377. */
  378. void switchToNextState(coffee_status_t* history){
  379. if(history[NEXT] != STATE_NULL) {
  380. history[CURRENT] = history[NEXT];
  381. history[NEXT] = STATE_NULL;
  382. holdNext[state_idx] = 0;
  383. nextWaitTime[state_idx] = 0;
  384. }
  385. }
  386. /**
  387. * sets the refresh rate of the the display
  388. * to one of the predefined refresh rates.
  389. */
  390. void setRefreshRate(refreshRate_t rate) {
  391. if (currentRefreshRate != rate) {
  392. currentRefreshRate = rate;
  393. displayTimer.setDivider(20 / rate);
  394. }
  395. }
  396. /**
  397. * specifies a timeout at which the new state/mode/page will be made to the current one
  398. * This function is enables for special state/mode/page transitions which hold the new state while
  399. * the current transition is displayed. These cases set a timeout specifying how long the state transition should be paused.
  400. * After the time specified in the timeout the timerhandler of the display will switch the states and makes the new one to the current one.
  401. */
  402. void setSwitchToNextTimeout(idx_t idx, uint16_t millis) {
  403. holdNext[idx] = millis;
  404. }
  405. /**
  406. * The core description of what will be displayed in what displaystate
  407. */
  408. void displayRefresh(void) {
  409. //handle mode trainsitions
  410. switchToNextMode(coffeeMode);
  411. //FSM of the display
  412. if (coffeeMode[CURRENT] == MODE_STATE) {
  413. //handle state transitions
  414. if (coffeeState[NEXT] == STATE_BREW) { //Pre brew
  415. //check how long the new state is already in hold ... and either switch or process transition
  416. setSwitchToNextTimeout(state_idx, TIMEOUT_PREBREW);
  417. setRefreshRate(refresh_fast);
  418. displayPrintLogo();
  419. displayPrintLn(1, displayGetString(str_brewing), true);
  420. return;
  421. } else if ((coffeeState[CURRENT] == STATE_BREW || coffeeState[CURRENT] == STATE_BREWMANUAL) && coffeeState[NEXT] != STATE_NULL) { //Post brew
  422. setSwitchToNextTimeout(state_idx, TIMEOUT_POSTBREW);
  423. setRefreshRate(refresh_std);
  424. displayPrintLn(0, displayGetString(str_postBrew), true);
  425. displayPrintPostBrew(1);
  426. return;
  427. } else { //no special state transition -> make new state to the current one
  428. switchToNextState(coffeeState);
  429. }
  430. switch (coffeeState[CURRENT]) { //coffeeGetState()
  431. case STATE_IDLE:
  432. displayPrintLogo();
  433. displayPrintLn(1, displayGetString(str_ready), true);
  434. break;
  435. case STATE_INITALHEATING:
  436. displayPrintLogo();
  437. displayPrintLn(1, displayGetString(str_heating), true);
  438. break;
  439. case STATE_HEATING:
  440. displayPrintLogo();
  441. displayPrintLn(1, displayGetString(str_heatingready), true);
  442. break;
  443. case STATE_BREW:
  444. displayPrintLn(0, displayGetString(str_brewing), true);
  445. displayPrintFlow(1);
  446. break;
  447. case STATE_BREWMANUAL:
  448. setRefreshRate(refresh_fast);
  449. displayPrintLn(0, displayGetString(str_brewing), true);
  450. //displayPrintFlow(1);
  451. displayPrintBrewManual(1);
  452. break;
  453. case STATE_CLEANING:
  454. displayPrintLn(0, displayGetString(str_cleaning), true);
  455. displayPrintCleanCycle(1);
  456. break;
  457. case STATE_ERROR:
  458. displayPrintLogo();
  459. displayPrintLn(1, displayGetString(str_error), true);
  460. break;
  461. case STATE_WAIT_OFF:
  462. displayPrintLogo();
  463. displayPrintLn(1, displayGetString(str_waitoff), true);
  464. break;
  465. case STATE_OFF:
  466. default:
  467. displayPrintLogo();
  468. displayPrintTime(1);
  469. break;
  470. }
  471. } else if (coffeeMode[CURRENT] == MODE_MENU) {
  472. //handle page transitions
  473. switchToNextPage(coffeePage);
  474. switch (coffeePage[CURRENT]) {
  475. case PAGE_SOFTOFF:
  476. displayPrintLn(0, displayGetString(str_menu), true);
  477. displayPrintLn(1, displayGetString(str_menu_softoff), true);
  478. break;
  479. case PAGE_KILL:
  480. displayPrintLn(0, displayGetString(str_menu), true);
  481. displayPrintLn(1, displayGetString(str_menu_kill), true);
  482. break;
  483. case PAGE_STATS:
  484. displayPrintLn(0, displayGetString(str_menu_stats), true);
  485. displayPrintStats(1);
  486. break;
  487. case PAGE_STATS2:
  488. displayPrintLn(0, displayGetString(str_menu_stats2), true);
  489. displayPrintStats2(1);
  490. break;
  491. case PAGE_DESCALING:
  492. displayPrintLn(0, displayGetString(str_menu_nextdesc), true);
  493. displayPrintNextDesc(1);
  494. break;
  495. case PAGE_TEMP:
  496. displayPrintLn(0, displayGetString(str_menu_temp), true);
  497. displayPrintTemp(1);
  498. break;
  499. case PAGE_CLEAN:
  500. displayPrintLn(0, displayGetString(str_menu), true);
  501. displayPrintLn(1, displayGetString(str_menu_clean), true);
  502. break;
  503. case PAGE_RESTART:
  504. displayPrintLn(0, displayGetString(str_menu), true);
  505. displayPrintLn(1, displayGetString(str_menu_restart), true);
  506. break;
  507. case PAGE_EXIT:
  508. displayPrintLn(0, displayGetString(str_menu), true);
  509. displayPrintLn(1, displayGetString(str_menu_exit), true);
  510. break;
  511. default:
  512. displayPrintLn(0, displayGetString(str_menu), true);
  513. displayPrintLn(1, "???", true);
  514. break;
  515. }
  516. }
  517. }
  518. /**
  519. * Returns the matching translation of a string
  520. * @param string Requested string
  521. * @return Translated string
  522. */
  523. const char* displayGetString(display_strings_t string) {
  524. if (displayLang >= lang_last)
  525. displayLang = DEFAULT_LANG;
  526. return display_strings[string].text[displayLang];
  527. }