display.cpp 11 KB

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