display.cpp 10 KB

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