display.cpp 9.5 KB

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