display.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. //TODO: the display is loosing the first events here since it is waiting before it is subscribing
  185. //its loosing the descaling event triggered at the beginning of coffee initialization
  186. sleep(1); // Wait for other components to initialize
  187. int tmp = sqlGetConf(CFGdisplaylang);
  188. if (!tmp || tmp >= lang_last)
  189. tmp = DEFAULT_LANG;
  190. displaySetLang((display_lang_t) tmp);
  191. displayTimer.start();
  192. event_subscribe("statechange", &displayStateUpdated);
  193. event_subscribe("modechange", &displayModeUpdated);
  194. event_subscribe("pagechange", &displayPageUpdated);
  195. event_subscribe("terminate", &displayTerminate);
  196. event_subscribe("descaling", &displayDescaling);
  197. while (1) {
  198. pause();
  199. if (1) {
  200. timerScaler = REFRESH_RATE;
  201. displayTimer.call();
  202. }
  203. }
  204. pthread_exit(EXIT_SUCCESS);
  205. }
  206. /**
  207. * Timer handler for display update
  208. * @param *threadid Thread ID
  209. */
  210. void* displayTimerHandler(void* threadid) {
  211. int scale1Hz = 0;
  212. if (timerScaler++ >= REFRESH_RATE) { // 1s elapsed, reset
  213. timerScaler = 0;
  214. scale1Hz = 1;
  215. }
  216. if (scale1Hz) {
  217. if (coffeeMode == MODE_STATE) {
  218. switch (coffeeState) {
  219. case STATE_OFF:
  220. case STATE_HEATING:
  221. case STATE_INITALHEATING:
  222. case STATE_IDLE:
  223. case STATE_CLEANING:
  224. case STATE_ERROR:
  225. case STATE_WAIT_OFF:
  226. displayRefresh();
  227. break;
  228. default:
  229. break;
  230. }
  231. } else if (coffeeMode == MODE_MENU) {
  232. displayRefresh();
  233. }
  234. }
  235. switch (coffeeState) {
  236. case STATE_BREW:
  237. case STATE_BREWMANUAL:
  238. displayRefresh();
  239. break;
  240. default:
  241. break;
  242. }
  243. if (elapsedCnt < (5 * REFRESH_RATE)) // Don't let it grow too large
  244. elapsedCnt++;
  245. pthread_exit(EXIT_SUCCESS);
  246. }
  247. /**
  248. * Initializes display
  249. */
  250. void displayInit(void) {
  251. lcd = lcdInit();
  252. if (lcd < 0)
  253. logger_error("Error: unable to init LCD (%d)\n", lcd);
  254. lcdClear(lcd);
  255. lcdHome(lcd);
  256. displayPrintLn(0, (char*) "CoffeePi", true);
  257. displayPrintLn(1, (char*) "booting...", true);
  258. //lcdPrintf(lcd, " CoffeePi booting...");
  259. timerScaler = 0;
  260. displayTimer.setDivider((1000000 / TIMER_DELAY_US) / REFRESH_RATE);
  261. logger(V_BASIC, "Initialized display with a refresh rate of %d Hz\n",
  262. REFRESH_RATE);
  263. }
  264. /**
  265. * Sets the language of the display text
  266. * @param lang New language
  267. */
  268. void displaySetLang(display_lang_t lang) {
  269. displayLang = lang;
  270. }
  271. /**
  272. * Refreshed the display content and outputs it
  273. */
  274. void displayRefresh(void) {
  275. if (coffeeMode == MODE_STATE) {
  276. switch (coffeeState) { //coffeeGetState()
  277. case STATE_IDLE:
  278. displayPrintLn(0, "CoffeePi", true);
  279. displayPrintLn(1, displayGetString(str_ready), true);
  280. break;
  281. case STATE_INITALHEATING:
  282. displayPrintLn(0, "CoffeePi", true);
  283. displayPrintLn(1, displayGetString(str_heating), true);
  284. break;
  285. case STATE_HEATING:
  286. displayPrintLn(0, "CoffeePi", true);
  287. displayPrintLn(1, displayGetString(str_heatingready), true);
  288. break;
  289. case STATE_BREW:
  290. if (elapsedCnt <= 2 * REFRESH_RATE) {
  291. displayPrintLn(0, "CoffeePi", true);
  292. displayPrintLn(1, displayGetString(str_brewing), true);
  293. } else {
  294. displayPrintLn(0, displayGetString(str_brewing), true);
  295. displayPrintFlow(1);
  296. }
  297. break;
  298. case STATE_BREWMANUAL:
  299. displayPrintLn(0, displayGetString(str_brewing), true);
  300. displayPrintFlow(1);
  301. break;
  302. case STATE_CLEANING:
  303. displayPrintLn(0, "CoffeePi", true);
  304. displayPrintLn(1, displayGetString(str_cleaning), true);
  305. break;
  306. case STATE_ERROR:
  307. displayPrintLn(0, "CoffeePi", true);
  308. displayPrintLn(1, displayGetString(str_error), true);
  309. break;
  310. case STATE_WAIT_OFF:
  311. displayPrintLn(0, "CoffeePi", true);
  312. displayPrintLn(1, displayGetString(str_waitoff), true);
  313. break;
  314. case STATE_OFF:
  315. default:
  316. displayPrintLn(0, "CoffeePi", true);
  317. displayPrintTime(1);
  318. break;
  319. }
  320. //draw the descaling star
  321. if(coffeeDescaling){
  322. lcdPosition(lcd, 15, 0);
  323. lcdPutchar(lcd, '*');
  324. }
  325. } else if (coffeeMode == MODE_MENU) {
  326. switch (coffeePage) {
  327. case PAGE_SOFTOFF:
  328. displayPrintLn(0, displayGetString(str_menu), true);
  329. displayPrintLn(1, displayGetString(str_menu_softoff), true);
  330. break;
  331. case PAGE_KILL:
  332. displayPrintLn(0, displayGetString(str_menu), true);
  333. displayPrintLn(1, displayGetString(str_menu_kill), true);
  334. break;
  335. case PAGE_STATS:
  336. displayPrintLn(0, displayGetString(str_menu_stats), true);
  337. displayPrintStats(1);
  338. break;
  339. case PAGE_TEMP:
  340. displayPrintLn(0, displayGetString(str_menu_temp), true);
  341. displayPrintTemp(1);
  342. break;
  343. case PAGE_CLEAN:
  344. displayPrintLn(0, displayGetString(str_menu), true);
  345. displayPrintLn(1, displayGetString(str_menu_clean), true);
  346. break;
  347. case PAGE_DEMO:
  348. displayPrintLn(0, displayGetString(str_menu), true);
  349. displayPrintLn(1, displayGetString(str_menu_demo), true);
  350. break;
  351. case PAGE_EXIT:
  352. displayPrintLn(0, displayGetString(str_menu), true);
  353. displayPrintLn(1, displayGetString(str_menu_exit), true);
  354. break;
  355. default:
  356. displayPrintLn(0, displayGetString(str_menu), true);
  357. displayPrintLn(1, "???", true);
  358. break;
  359. }
  360. }
  361. }
  362. /**
  363. * Returns the matching translation of a string
  364. * @param string Requested string
  365. * @return Translated string
  366. */
  367. const char* displayGetString(display_strings_t string) {
  368. if (displayLang >= lang_last)
  369. displayLang = DEFAULT_LANG;
  370. return display_strings[string].text[displayLang];
  371. }