display2.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. 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. if (timerScaler)
  156. timerScaler--;
  157. elapsedCnt = 0;
  158. displayTimer.call();
  159. }
  160. }
  161. /**
  162. * Updates the display state to the matching coffee display mode
  163. * @param event Event data
  164. */
  165. void displayModeUpdated(event_t *event) {
  166. if (event->len != sizeof(coffee_mode_t)) {
  167. logger_error("Invalid use of event %s\n", event->event);
  168. return;
  169. }
  170. coffee_mode_t mode = *(coffee_mode_t*) event->data;
  171. if (mode != coffeeMode) {
  172. coffeeMode = mode;
  173. if (timerScaler)
  174. timerScaler--;
  175. elapsedCnt = 0;
  176. displayTimer.call();
  177. }
  178. }
  179. /**
  180. * Updates the display state to the matching coffee display page
  181. * @param event Event data
  182. */
  183. void displayPageUpdated(event_t *event) {
  184. if (event->len != sizeof(coffee_menuPage_t)) {
  185. logger_error("Invalid use of event %s\n", event->event);
  186. return;
  187. }
  188. coffee_menuPage_t page = *(coffee_menuPage_t*) event->data;
  189. if (page != coffeePage) {
  190. coffeePage = page;
  191. timerScaler = REFRESH_RATE;
  192. elapsedCnt = 0;
  193. displayTimer.call();
  194. }
  195. }
  196. /**
  197. * Updates the current descaling status of the machine
  198. */
  199. void displayDescaling(event_t *event) {
  200. if (event->len != sizeof(bool)) {
  201. logger_error("Invalid use of event %s\n", event->event);
  202. return;
  203. }
  204. coffeeDescaling = *(bool*) event->data;
  205. }
  206. /**
  207. * Prints the logo (CoffeePi) and also the temperature with the logo (CoffeePi@72C) if the machine is on
  208. */
  209. void displayPrintLogo(void) {
  210. char buffer[17];
  211. switch (coffeeState) {
  212. case STATE_HEATING:
  213. case STATE_INITALHEATING:
  214. case STATE_IDLE:
  215. sprintf(buffer, "CoffeePi @ %dC", DS18B20_readTemp());
  216. break;
  217. default:
  218. sprintf(buffer, "CoffeePi");
  219. break;
  220. }
  221. displayPrintLn(0, buffer, true);
  222. }
  223. /**
  224. * Handles cleanup before program termination
  225. */
  226. void displayTerminate(event_t *e) {
  227. logger(V_BASIC, "display.cpp: Terminating\n");
  228. displayPrintLn(0, "CoffeePi", true);
  229. displayPrintLn(1, displayGetString(str_bye), true);
  230. }
  231. /**
  232. * Main thread to handle display data
  233. * @param *threadid Thread ID
  234. */
  235. void* displayThread(void* threadid) {
  236. //sleep(1); // Wait for other components to initialize
  237. displayTimer.start();
  238. while (1) {
  239. pause();
  240. if (1) {
  241. timerScaler = REFRESH_RATE;
  242. displayTimer.call();
  243. }
  244. }
  245. pthread_exit(EXIT_SUCCESS);
  246. }
  247. /**
  248. * Timer handler for display update
  249. * @param *threadid Thread ID
  250. */
  251. void* displayTimerHandler(void* threadid) {
  252. int scale1Hz = 0;
  253. if (timerScaler++ >= REFRESH_RATE) { // 1s elapsed, reset
  254. timerScaler = 0;
  255. scale1Hz = 1;
  256. }
  257. if (scale1Hz) {
  258. if (coffeeMode == MODE_STATE) {
  259. switch (coffeeState) {
  260. case STATE_OFF:
  261. case STATE_HEATING:
  262. case STATE_INITALHEATING:
  263. case STATE_IDLE:
  264. case STATE_CLEANING:
  265. case STATE_ERROR:
  266. case STATE_WAIT_OFF:
  267. displayRefresh();
  268. break;
  269. default:
  270. break;
  271. }
  272. } else if (coffeeMode == MODE_MENU) {
  273. displayRefresh();
  274. }
  275. }
  276. switch (coffeeState) {
  277. case STATE_BREW:
  278. case STATE_BREWMANUAL:
  279. displayRefresh();
  280. break;
  281. default:
  282. break;
  283. }
  284. if (elapsedCnt < (5 * REFRESH_RATE)) // Don't let it grow too large
  285. elapsedCnt++;
  286. pthread_exit(EXIT_SUCCESS);
  287. }
  288. /**
  289. * Initializes display
  290. */
  291. void displayInit(void) {
  292. lcd = lcdInit();
  293. if (lcd < 0)
  294. logger_error("Error: unable to init LCD (%d)\n", lcd);
  295. lcdClear(lcd);
  296. lcdHome(lcd);
  297. displayPrintLn(0, (char*) "CoffeePi", true);
  298. displayPrintLn(1, (char*) "booting...", true);
  299. //lcdPrintf(lcd, " CoffeePi booting...");
  300. timerScaler = 0;
  301. displayTimer.setDivider((1000000 / TIMER_DELAY_US) / REFRESH_RATE);
  302. logger(V_BASIC, "Initialized display with a refresh rate of %d Hz\n",
  303. REFRESH_RATE);
  304. /**The following block comes from void* displayThread(void* threadid)
  305. * The idea is that the initialization functions get the component ready to react on external events
  306. * once the threads start, events might be triggered and every component can process them
  307. * This should fix the TO_DO where a descaling event was triggered during startup of the coffeethread and
  308. * the displaythread did not react to it since it hadn't subscribed to the event yet
  309. */
  310. int tmp = sqlGetConf(CFGdisplaylang);
  311. if (!tmp || tmp >= lang_last)
  312. tmp = DEFAULT_LANG;
  313. displaySetLang((display_lang_t) tmp);
  314. event_subscribe("statechange", &displayStateUpdated);
  315. event_subscribe("modechange", &displayModeUpdated);
  316. event_subscribe("pagechange", &displayPageUpdated);
  317. event_subscribe("terminate", &displayTerminate);
  318. event_subscribe("descaling", &displayDescaling);
  319. }
  320. /**
  321. * Sets the language of the display text
  322. * @param lang New language
  323. */
  324. void displaySetLang(display_lang_t lang) {
  325. displayLang = lang;
  326. }
  327. /**
  328. * Refreshed the display content and outputs it
  329. */
  330. void displayRefresh(void) {
  331. if (coffeeMode == MODE_STATE) {
  332. switch (coffeeState) { //coffeeGetState()
  333. case STATE_IDLE:
  334. displayPrintLogo();
  335. displayPrintLn(1, displayGetString(str_ready), true);
  336. break;
  337. case STATE_INITALHEATING:
  338. displayPrintLogo();
  339. displayPrintLn(1, displayGetString(str_heating), true);
  340. break;
  341. case STATE_HEATING:
  342. displayPrintLogo();
  343. displayPrintLn(1, displayGetString(str_heatingready), true);
  344. break;
  345. case STATE_BREW:
  346. if (elapsedCnt <= 2 * REFRESH_RATE) {
  347. displayPrintLogo();
  348. displayPrintLn(1, displayGetString(str_brewing), true);
  349. } else {
  350. displayPrintLn(0, displayGetString(str_brewing), true);
  351. displayPrintFlow(1);
  352. }
  353. break;
  354. case STATE_BREWMANUAL:
  355. displayPrintLn(0, displayGetString(str_brewing), true);
  356. displayPrintFlow(1);
  357. break;
  358. case STATE_CLEANING:
  359. displayPrintLogo();
  360. displayPrintLn(1, displayGetString(str_cleaning), true);
  361. break;
  362. case STATE_ERROR:
  363. displayPrintLogo();
  364. displayPrintLn(1, displayGetString(str_error), true);
  365. break;
  366. case STATE_WAIT_OFF:
  367. displayPrintLogo();
  368. displayPrintLn(1, displayGetString(str_waitoff), true);
  369. break;
  370. case STATE_OFF:
  371. default:
  372. displayPrintLogo();
  373. displayPrintTime(1);
  374. break;
  375. }
  376. } else if (coffeeMode == MODE_MENU) {
  377. switch (coffeePage) {
  378. case PAGE_SOFTOFF:
  379. displayPrintLn(0, displayGetString(str_menu), true);
  380. displayPrintLn(1, displayGetString(str_menu_softoff), true);
  381. break;
  382. case PAGE_KILL:
  383. displayPrintLn(0, displayGetString(str_menu), true);
  384. displayPrintLn(1, displayGetString(str_menu_kill), true);
  385. break;
  386. case PAGE_STATS:
  387. displayPrintLn(0, displayGetString(str_menu_stats), true);
  388. displayPrintStats(1);
  389. break;
  390. case PAGE_STATS2:
  391. displayPrintLn(0, displayGetString(str_menu_stats2), true);
  392. displayPrintStats2(1);
  393. break;
  394. case PAGE_DESCALING:
  395. displayPrintLn(0, displayGetString(str_menu_nextdesc), true);
  396. displayPrintNextDesc(1);
  397. break;
  398. case PAGE_TEMP:
  399. displayPrintLn(0, displayGetString(str_menu_temp), true);
  400. displayPrintTemp(1);
  401. break;
  402. case PAGE_CLEAN:
  403. displayPrintLn(0, displayGetString(str_menu), true);
  404. displayPrintLn(1, displayGetString(str_menu_clean), true);
  405. break;
  406. case PAGE_DEMO:
  407. displayPrintLn(0, displayGetString(str_menu), true);
  408. displayPrintLn(1, displayGetString(str_menu_demo), true);
  409. break;
  410. case PAGE_EXIT:
  411. displayPrintLn(0, displayGetString(str_menu), true);
  412. displayPrintLn(1, displayGetString(str_menu_exit), true);
  413. break;
  414. default:
  415. displayPrintLn(0, displayGetString(str_menu), true);
  416. displayPrintLn(1, "???", true);
  417. break;
  418. }
  419. }
  420. }
  421. /**
  422. * Returns the matching translation of a string
  423. * @param string Requested string
  424. * @return Translated string
  425. */
  426. const char* displayGetString(display_strings_t string) {
  427. if (displayLang >= lang_last)
  428. displayLang = DEFAULT_LANG;
  429. return display_strings[string].text[displayLang];
  430. }