display.cpp 11 KB

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