display.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. display_lang_t displayLang;
  21. timer displayTimer(displayTimerHandler);
  22. int lcd = 0;
  23. volatile int timerScaler = 0;
  24. volatile int elapsedCnt = 0;
  25. int coffeeState = STATE_OFF;
  26. /**
  27. * Prints out the current time in a centered position
  28. * @param line Target line in display
  29. */
  30. void displayPrintTime(int line) {
  31. time_t rawtime;
  32. struct tm * timeinfo;
  33. if (line > DISPLAY_ROWS)
  34. line = 0;
  35. time(&rawtime);
  36. timeinfo = localtime(&rawtime);
  37. lcdPosition(lcd, 0, line);
  38. lcdPrintf(lcd, " %.2d:%.2d:%.2d ", timeinfo->tm_hour,
  39. timeinfo->tm_min, timeinfo->tm_sec);
  40. }
  41. /**
  42. * Prints out the total volume flow
  43. * @param line Target line in display
  44. */
  45. void displayPrintFlow(int line) {
  46. float flow = halGetFlow();
  47. lcdPosition(lcd, 0, line);
  48. lcdPrintf(lcd, "%s: %.0f", displayGetString(str_flow), flow);
  49. }
  50. /**
  51. * Prints a string to a specific line, optionally centered.
  52. * This function also fills out the remaining row of the display with spaces,
  53. * to ensure there is no old data left.
  54. * @param line Target line in display
  55. * @param *str String to print
  56. * @param centered Print centered or not
  57. */
  58. void displayPrintLn(int line, const char* str, bool centered) {
  59. char buf[DISPLAY_COLS + 1];
  60. int len = strlen(str);
  61. int i = 0;
  62. int spaces = 0;
  63. if (len > DISPLAY_COLS)
  64. len = DISPLAY_COLS;
  65. if (line > DISPLAY_ROWS)
  66. line = 0;
  67. if (centered) {
  68. spaces = (DISPLAY_COLS - len) / 2;
  69. if (spaces) {
  70. for (i = 0; i < spaces; i++) {
  71. buf[i] = ' ';
  72. }
  73. }
  74. }
  75. for (i = 0; i < len; i++) {
  76. buf[spaces + i] = str[i];
  77. }
  78. if ((len + spaces) < DISPLAY_COLS) { // fill remaining space
  79. for (i = i + spaces; i < DISPLAY_COLS; i++) {
  80. buf[i] = ' ';
  81. }
  82. }
  83. lcdPosition(lcd, 0, line);
  84. buf[DISPLAY_COLS] = '\0';
  85. lcdPrintf(lcd, buf);
  86. //logger(V_HAL, "Printed out on display: \"%s\"\n", buf);
  87. }
  88. /**
  89. * Main thread to handle display data
  90. * @param *threadid Thread ID
  91. */
  92. void* displayThread(void* threadid) {
  93. sleep(1); // Wait for other components to initialize
  94. int tmp = sqlGetConf(CFGdisplaylang);
  95. if (!tmp || tmp >= lang_last)
  96. tmp = DEFAULT_LANG;
  97. displaySetLang((display_lang_t) tmp);
  98. displayTimer.start();
  99. while (1) {
  100. pause();
  101. if (1) {
  102. timerScaler--;
  103. displayTimer.call();
  104. }
  105. }
  106. pthread_exit(EXIT_SUCCESS);
  107. }
  108. /**
  109. * Timer handler for display update
  110. * @param *threadid Thread ID
  111. */
  112. void* displayTimerHandler(void* threadid) {
  113. int scale1Hz = 0;
  114. if (timerScaler++ >= REFRESH_RATE) { // 1s elapsed, reset
  115. timerScaler = 0;
  116. scale1Hz = 1;
  117. }
  118. int scale5Hz = (timerScaler == (REFRESH_RATE / 5) ? 0 : 1);
  119. if (scale1Hz) {
  120. switch (coffeeState) {
  121. case STATE_OFF:
  122. case STATE_HEATING:
  123. case STATE_INITALHEATING:
  124. case STATE_IDLE:
  125. case STATE_CLEANING:
  126. case STATE_ERROR:
  127. displayRefresh();
  128. }
  129. }
  130. if (scale5Hz) {
  131. switch (coffeeState) {
  132. case STATE_BREW:
  133. case STATE_BREWMANUAL:
  134. displayRefresh();
  135. }
  136. }
  137. if (elapsedCnt < (5 * REFRESH_RATE)) // Don't let it grow too large
  138. elapsedCnt++;
  139. pthread_exit(EXIT_SUCCESS);
  140. }
  141. /**
  142. * Initializes display
  143. */
  144. void displayInit(void) {
  145. lcd = lcdInit();
  146. if (lcd < 0)
  147. logger_error("Error: unable to init LCD (%d)\n", lcd);
  148. lcdClear(lcd);
  149. lcdHome(lcd);
  150. displayPrintLn(0, (char*) "CoffeePi", true);
  151. displayPrintLn(1, (char*) "booting...", true);
  152. //lcdPrintf(lcd, " CoffeePi booting...");
  153. timerScaler = 0;
  154. displayTimer.setDivider((1000000 / TIMER_DELAY_US) / REFRESH_RATE);
  155. logger(V_BASIC, "Initialized display with a refresh rate of %d Hz\n",
  156. REFRESH_RATE);
  157. }
  158. /**
  159. * Handles cleanup before program termination
  160. */
  161. void displayTerminate(void) {
  162. displayPrintLn(0, "CoffeePi", true);
  163. displayPrintLn(1, displayGetString(str_bye), true);
  164. }
  165. /**
  166. * Sets the language of the display text
  167. * @param lang New language
  168. */
  169. void displaySetLang(display_lang_t lang) {
  170. displayLang = lang;
  171. }
  172. /**
  173. * Refreshed the display content and outputs it
  174. */
  175. void displayRefresh(void) {
  176. switch (coffeeState) { //coffeeGetState()
  177. case STATE_IDLE:
  178. displayPrintLn(0, "CoffeePi", true);
  179. displayPrintLn(1, displayGetString(str_ready), true);
  180. break;
  181. case STATE_INITALHEATING:
  182. displayPrintLn(0, "CoffeePi", true);
  183. displayPrintLn(1, displayGetString(str_heating), true);
  184. break;
  185. case STATE_HEATING:
  186. displayPrintLn(0, "CoffeePi", true);
  187. displayPrintLn(1, displayGetString(str_heatingready), true);
  188. break;
  189. case STATE_BREW:
  190. if (elapsedCnt <= REFRESH_RATE) {
  191. displayPrintLn(0, "CoffeePi", true);
  192. displayPrintLn(1, displayGetString(str_brewing), true);
  193. } else
  194. displayPrintFlow(1);
  195. break;
  196. case STATE_BREWMANUAL:
  197. displayPrintLn(0, displayGetString(str_brewing), true);
  198. displayPrintFlow(1);
  199. break;
  200. case STATE_CLEANING:
  201. displayPrintLn(0, "CoffeePi", true);
  202. displayPrintLn(1, displayGetString(str_cleaning), true);
  203. break;
  204. case STATE_ERROR:
  205. displayPrintLn(0, "CoffeePi", true);
  206. displayPrintLn(1, displayGetString(str_error), true);
  207. break;
  208. case STATE_OFF:
  209. default:
  210. displayPrintLn(0, "CoffeePi", true);
  211. displayPrintTime(1);
  212. break;
  213. }
  214. }
  215. /**
  216. * Returns the matching translation of a string
  217. * @param string Requested string
  218. * @return Translated string
  219. */
  220. const char* displayGetString(display_strings_t string) {
  221. if (displayLang >= lang_last)
  222. displayLang = DEFAULT_LANG;
  223. return display_strings[string].text[displayLang];
  224. }
  225. /**
  226. * Updates the display state to the matching coffee state
  227. * @param state New state
  228. */
  229. void displayPushState(int state) {
  230. if (state != coffeeState) {
  231. coffeeState = state;
  232. timerScaler--;
  233. displayTimer.call();
  234. }
  235. }