display2.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 4000
  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. /**
  122. * Prints out the total volume flow
  123. * @param line Target line in display
  124. */
  125. void displayPrintFlow(int line) {
  126. float flow = halGetFlow();
  127. lcdPosition(lcd, 0, line);
  128. lcdPrintf(lcd, "%s: %.0f ml ", displayGetString(str_flow), flow);
  129. }
  130. /**
  131. * Prints a string to a specific line, optionally centered.
  132. * This function also fills out the remaining row of the display with spaces,
  133. * to ensure there is no old data left.
  134. * @param line Target line in display
  135. * @param *str String to print
  136. * @param centered Print centered or not
  137. */
  138. void displayPrintLn(int line, const char* str, bool centered) {
  139. char buf[DISPLAY_COLS + 1];
  140. int len = strlen(str);
  141. int i = 0;
  142. int spaces = 0;
  143. if (len > DISPLAY_COLS)
  144. len = DISPLAY_COLS;
  145. if (line > DISPLAY_ROWS)
  146. line = 0;
  147. if (centered) {
  148. spaces = (DISPLAY_COLS - len) / 2;
  149. if (spaces) {
  150. for (i = 0; i < spaces; i++) {
  151. buf[i] = ' ';
  152. }
  153. }
  154. }
  155. for (i = 0; i < len; i++) {
  156. buf[spaces + i] = str[i];
  157. }
  158. if ((len + spaces) < DISPLAY_COLS) { // fill remaining space
  159. for (i = i + spaces; i < DISPLAY_COLS; i++) {
  160. buf[i] = ' ';
  161. }
  162. }
  163. //draw the descaling star
  164. if(coffeeDescaling && line == 0){
  165. buf[15] = '*';
  166. }
  167. lcdPosition(lcd, 0, line);
  168. buf[DISPLAY_COLS] = '\0';
  169. lcdPrintf(lcd, buf);
  170. //logger(V_HAL, "Printed out on display: \"%s\"\n", buf);
  171. }
  172. /**
  173. * Updates the display state to the matching coffee state
  174. * @param event Event data
  175. */
  176. void displayStateUpdated(event_t *event) {
  177. if (event->len != sizeof(coffee_status_t)) {
  178. logger_error("Invalid use of event %s\n", event->event);
  179. return;
  180. }
  181. coffee_status_t state = *(coffee_status_t*) event->data;
  182. coffeeState[NEXT] = state;
  183. displayTimer.call();
  184. }
  185. /**
  186. * Updates the display state to the matching coffee display mode
  187. * @param event Event data
  188. */
  189. void displayModeUpdated(event_t *event) {
  190. if (event->len != sizeof(coffee_mode_t)) {
  191. logger_error("Invalid use of event %s\n", event->event);
  192. return;
  193. }
  194. coffee_mode_t mode = *(coffee_mode_t*) event->data;
  195. coffeeMode[NEXT] = mode;
  196. displayTimer.call();
  197. }
  198. /**
  199. * Updates the display state to the matching coffee display page
  200. * The new state is put on hold (next) and the display can decided if it immediately makes the
  201. * new state to the current one or if a timeout is specified in which the new state is put on hold and the
  202. * state transition is processed.
  203. * One initial asynchronous call
  204. * @param event Event data
  205. */
  206. void displayPageUpdated(event_t *event) {
  207. if (event->len != sizeof(coffee_menuPage_t)) {
  208. logger_error("Invalid use of event %s\n", event->event);
  209. return;
  210. }
  211. coffee_menuPage_t page = *(coffee_menuPage_t*) event->data;
  212. coffeePage[NEXT] = page;
  213. displayTimer.call();
  214. }
  215. /**
  216. * Updates the current descaling status of the machine
  217. */
  218. void displayDescaling(event_t *event) {
  219. if (event->len != sizeof(bool)) {
  220. logger_error("Invalid use of event %s\n", event->event);
  221. return;
  222. }
  223. coffeeDescaling = *(bool*) event->data;
  224. }
  225. /**
  226. * Prints the logo (CoffeePi) and also the temperature with the logo (CoffeePi@72C) if the machine is on
  227. */
  228. void displayPrintLogo(void) {
  229. char buffer[17];
  230. switch (coffeeState[CURRENT]) {
  231. case STATE_HEATING:
  232. case STATE_INITALHEATING:
  233. case STATE_IDLE:
  234. sprintf(buffer, "CoffeePi @ %dC", DS18B20_readTemp());
  235. break;
  236. default:
  237. sprintf(buffer, "CoffeePi");
  238. break;
  239. }
  240. displayPrintLn(0, buffer, true);
  241. }
  242. /**
  243. * Handles cleanup before program termination
  244. */
  245. void displayTerminate(event_t *e) {
  246. logger(V_BASIC, "display.cpp: Terminating\n");
  247. displayPrintLn(0, "CoffeePi", true);
  248. displayPrintLn(1, displayGetString(str_bye), true);
  249. }
  250. /**
  251. * Main thread to handle display data
  252. * @param *threadid Thread ID
  253. */
  254. void* displayThread(void* threadid) {
  255. //sleep(1); // Wait for other components to initialize
  256. displayTimer.start();
  257. while (1) {
  258. pause();
  259. if (1) {
  260. displayTimer.call();
  261. }
  262. }
  263. pthread_exit(EXIT_SUCCESS);
  264. }
  265. /**
  266. * Timer handler for display update
  267. * @param *threadid Thread ID
  268. */
  269. void* displayTimerHandler(void* threadid) {
  270. //track time of the new state in hold and switch if necessary
  271. track(state_idx);
  272. track(page_idx);
  273. track(mode_idx);
  274. displayRefresh();
  275. pthread_exit(EXIT_SUCCESS);
  276. }
  277. /**
  278. * Tracks the elapsed time in ms of the new state waiting to become the current one if a hold time is specified
  279. */
  280. void track(idx_t idx){
  281. if (holdNext[idx] != 0) {
  282. nextWaitTime[idx] += 1000 / currentRefreshRate;
  283. if (holdNext[idx] <= nextWaitTime[idx]) {
  284. switch(idx){
  285. case state_idx:
  286. switchToNextState(coffeeState);
  287. break;
  288. case page_idx:
  289. switchToNextPage(coffeePage);
  290. break;
  291. case mode_idx:
  292. switchToNextMode(coffeeMode);
  293. break;
  294. }
  295. }
  296. }
  297. }
  298. /**
  299. * Initializes display
  300. */
  301. void displayInit(void) {
  302. lcd = lcdInit();
  303. if (lcd < 0)
  304. logger_error("Error: unable to init LCD (%d)\n", lcd);
  305. lcdClear(lcd);
  306. lcdHome(lcd);
  307. displayPrintLn(0, (char*) "CoffeePi", true);
  308. displayPrintLn(1, (char*) "booting...", true);
  309. //lcdPrintf(lcd, " CoffeePi booting...");
  310. setRefreshRate(refresh_std);
  311. logger(V_BASIC, "display2.cpp Initialized display with a refresh rate of %d Hz\n",
  312. refresh_std);
  313. /**The following block comes from void* displayThread(void* threadid)
  314. * The idea is that the initialization functions get the component ready to react on external events
  315. * once the threads start, events might be triggered and every component can process them
  316. * This should fix the TO_DO where a descaling event was triggered during startup of the coffeethread and
  317. * the displaythread did not react to it since it hadn't subscribed to the event yet
  318. */
  319. int tmp = sqlGetConf(CFGdisplaylang);
  320. if (!tmp || tmp >= lang_last)
  321. tmp = DEFAULT_LANG;
  322. displaySetLang((display_lang_t) tmp);
  323. event_subscribe("statechange", &displayStateUpdated);
  324. event_subscribe("modechange", &displayModeUpdated);
  325. event_subscribe("pagechange", &displayPageUpdated);
  326. event_subscribe("terminate", &displayTerminate);
  327. event_subscribe("descaling", &displayDescaling);
  328. }
  329. /**
  330. * Sets the language of the display text
  331. * @param lang New language
  332. */
  333. void displaySetLang(display_lang_t lang) {
  334. displayLang = lang;
  335. }
  336. /*
  337. *
  338. */
  339. void switchToNextPage(coffee_menuPage_t* history){
  340. if(history[NEXT] != PAGE_NULL) {
  341. history[CURRENT] = history[NEXT];
  342. history[NEXT] = PAGE_NULL;
  343. holdNext[page_idx] = 0;
  344. nextWaitTime[page_idx] = 0;
  345. }
  346. }
  347. /*
  348. *
  349. */
  350. void switchToNextMode(coffee_mode_t* history){
  351. if(history[NEXT] != MODE_NULL) {
  352. history[CURRENT] = history[NEXT];
  353. history[NEXT] = MODE_NULL;
  354. holdNext[mode_idx] = 0;
  355. nextWaitTime[mode_idx] = 0;
  356. }
  357. }
  358. /*
  359. *
  360. */
  361. void switchToNextState(coffee_status_t* history){
  362. if(history[NEXT] != STATE_NULL) {
  363. history[CURRENT] = history[NEXT];
  364. history[NEXT] = STATE_NULL;
  365. holdNext[state_idx] = 0;
  366. nextWaitTime[state_idx] = 0;
  367. }
  368. }
  369. /**
  370. * sets the refresh rate of the the display
  371. * to one of the predefined refresh rates.
  372. */
  373. void setRefreshRate(refreshRate_t rate) {
  374. currentRefreshRate = rate;
  375. displayTimer.setDivider(20 / rate);
  376. }
  377. /**
  378. * specifies a timeout at which the new state will be made to the current one
  379. * This function is necessary for the special state transitions which hold the new state while they
  380. * process the current transition. These cases set a timeout specifying how long the state transition should be paused.
  381. * 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.
  382. */
  383. void setSwitchToNextTimeout(idx_t idx, uint16_t millis) {
  384. holdNext[idx] = millis;
  385. }
  386. /**
  387. * The core description of what will be displayed in what displaystate
  388. */
  389. void displayRefresh(void) {
  390. //handle mode trainsitions
  391. switchToNextMode(coffeeMode);
  392. //FSM of the display
  393. if (coffeeMode[CURRENT] == MODE_STATE) {
  394. //handle state transitions
  395. if (coffeeState[NEXT] == STATE_BREW) { //Pre brew
  396. //check how long the new state is already in hold ... and either switch or process transition
  397. setSwitchToNextTimeout(state_idx, TIMEOUT_PREBREW);
  398. setRefreshRate(refresh_fast);
  399. displayPrintLogo();
  400. displayPrintLn(1, displayGetString(str_brewing), true);
  401. return;
  402. } else if (coffeeState[CURRENT] == STATE_BREW && coffeeState[NEXT] != STATE_NULL) { //Post brew
  403. setSwitchToNextTimeout(state_idx, TIMEOUT_POSTBREW);
  404. setRefreshRate(refresh_std);
  405. displayPrintPostBrew(1);
  406. return;
  407. } else { //no special state transition -> make new state to the current one
  408. switchToNextState(coffeeState);
  409. }
  410. switch (coffeeState[CURRENT]) { //coffeeGetState()
  411. case STATE_IDLE:
  412. displayPrintLogo();
  413. displayPrintLn(1, displayGetString(str_ready), true);
  414. break;
  415. case STATE_INITALHEATING:
  416. displayPrintLogo();
  417. displayPrintLn(1, displayGetString(str_heating), true);
  418. break;
  419. case STATE_HEATING:
  420. displayPrintLogo();
  421. displayPrintLn(1, displayGetString(str_heatingready), true);
  422. break;
  423. case STATE_BREW:
  424. displayPrintLn(0, displayGetString(str_brewing), true);
  425. displayPrintFlow(1);
  426. break;
  427. case STATE_BREWMANUAL:
  428. displayPrintLn(0, displayGetString(str_brewing), true);
  429. displayPrintFlow(1);
  430. break;
  431. case STATE_CLEANING:
  432. displayPrintLogo();
  433. displayPrintLn(1, displayGetString(str_cleaning), true);
  434. break;
  435. case STATE_ERROR:
  436. displayPrintLogo();
  437. displayPrintLn(1, displayGetString(str_error), true);
  438. break;
  439. case STATE_WAIT_OFF:
  440. displayPrintLogo();
  441. displayPrintLn(1, displayGetString(str_waitoff), true);
  442. break;
  443. case STATE_OFF:
  444. default:
  445. displayPrintLogo();
  446. displayPrintTime(1);
  447. break;
  448. }
  449. } else if (coffeeMode[CURRENT] == MODE_MENU) {
  450. //handle page transitions
  451. if(coffeePage[NEXT] == PAGE_EXIT) {
  452. setSwitchToNextTimeout(page_idx, 2000);
  453. displayPrintLn(0, "test vor exit", true);
  454. displayPrintLn(1, "Test", true);
  455. return;
  456. } else {
  457. switchToNextPage(coffeePage);
  458. }
  459. switch (coffeePage[CURRENT]) {
  460. case PAGE_SOFTOFF:
  461. displayPrintLn(0, displayGetString(str_menu), true);
  462. displayPrintLn(1, displayGetString(str_menu_softoff), true);
  463. break;
  464. case PAGE_KILL:
  465. displayPrintLn(0, displayGetString(str_menu), true);
  466. displayPrintLn(1, displayGetString(str_menu_kill), true);
  467. break;
  468. case PAGE_STATS:
  469. displayPrintLn(0, displayGetString(str_menu_stats), true);
  470. displayPrintStats(1);
  471. break;
  472. case PAGE_STATS2:
  473. displayPrintLn(0, displayGetString(str_menu_stats2), true);
  474. displayPrintStats2(1);
  475. break;
  476. case PAGE_DESCALING:
  477. displayPrintLn(0, displayGetString(str_menu_nextdesc), true);
  478. displayPrintNextDesc(1);
  479. break;
  480. case PAGE_TEMP:
  481. displayPrintLn(0, displayGetString(str_menu_temp), true);
  482. displayPrintTemp(1);
  483. break;
  484. case PAGE_CLEAN:
  485. displayPrintLn(0, displayGetString(str_menu), true);
  486. displayPrintLn(1, displayGetString(str_menu_clean), true);
  487. break;
  488. case PAGE_DEMO:
  489. displayPrintLn(0, displayGetString(str_menu), true);
  490. displayPrintLn(1, displayGetString(str_menu_demo), true);
  491. break;
  492. case PAGE_EXIT:
  493. displayPrintLn(0, displayGetString(str_menu), true);
  494. displayPrintLn(1, displayGetString(str_menu_exit), true);
  495. break;
  496. default:
  497. displayPrintLn(0, displayGetString(str_menu), true);
  498. displayPrintLn(1, "???", true);
  499. break;
  500. }
  501. }
  502. }
  503. /**
  504. * Returns the matching translation of a string
  505. * @param string Requested string
  506. * @return Translated string
  507. */
  508. const char* displayGetString(display_strings_t string) {
  509. if (displayLang >= lang_last)
  510. displayLang = DEFAULT_LANG;
  511. return display_strings[string].text[displayLang];
  512. }