display.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. coffee_status_t 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. break;
  129. default:
  130. break;
  131. }
  132. }
  133. if (scale5Hz) {
  134. switch (coffeeState) {
  135. case STATE_BREW:
  136. case STATE_BREWMANUAL:
  137. displayRefresh();
  138. break;
  139. default:
  140. break;
  141. }
  142. }
  143. if (elapsedCnt < (5 * REFRESH_RATE)) // Don't let it grow too large
  144. elapsedCnt++;
  145. pthread_exit(EXIT_SUCCESS);
  146. }
  147. /**
  148. * Initializes display
  149. */
  150. void displayInit(void) {
  151. lcd = lcdInit();
  152. if (lcd < 0)
  153. logger_error("Error: unable to init LCD (%d)\n", lcd);
  154. lcdClear(lcd);
  155. lcdHome(lcd);
  156. displayPrintLn(0, (char*) "CoffeePi", true);
  157. displayPrintLn(1, (char*) "booting...", true);
  158. //lcdPrintf(lcd, " CoffeePi booting...");
  159. timerScaler = 0;
  160. displayTimer.setDivider((1000000 / TIMER_DELAY_US) / REFRESH_RATE);
  161. logger(V_BASIC, "Initialized display with a refresh rate of %d Hz\n",
  162. REFRESH_RATE);
  163. }
  164. /**
  165. * Handles cleanup before program termination
  166. */
  167. void displayTerminate(void) {
  168. displayPrintLn(0, "CoffeePi", true);
  169. displayPrintLn(1, displayGetString(str_bye), true);
  170. }
  171. /**
  172. * Sets the language of the display text
  173. * @param lang New language
  174. */
  175. void displaySetLang(display_lang_t lang) {
  176. displayLang = lang;
  177. }
  178. /**
  179. * Refreshed the display content and outputs it
  180. */
  181. void displayRefresh(void) {
  182. switch (coffeeState) { //coffeeGetState()
  183. case STATE_IDLE:
  184. displayPrintLn(0, "CoffeePi", true);
  185. displayPrintLn(1, displayGetString(str_ready), true);
  186. break;
  187. case STATE_INITALHEATING:
  188. displayPrintLn(0, "CoffeePi", true);
  189. displayPrintLn(1, displayGetString(str_heating), true);
  190. break;
  191. case STATE_HEATING:
  192. displayPrintLn(0, "CoffeePi", true);
  193. displayPrintLn(1, displayGetString(str_heatingready), true);
  194. break;
  195. case STATE_BREW:
  196. if (elapsedCnt <= REFRESH_RATE) {
  197. displayPrintLn(0, "CoffeePi", true);
  198. displayPrintLn(1, displayGetString(str_brewing), true);
  199. } else
  200. displayPrintFlow(1);
  201. break;
  202. case STATE_BREWMANUAL:
  203. displayPrintLn(0, displayGetString(str_brewing), true);
  204. displayPrintFlow(1);
  205. break;
  206. case STATE_CLEANING:
  207. displayPrintLn(0, "CoffeePi", true);
  208. displayPrintLn(1, displayGetString(str_cleaning), true);
  209. break;
  210. case STATE_ERROR:
  211. displayPrintLn(0, "CoffeePi", true);
  212. displayPrintLn(1, displayGetString(str_error), true);
  213. break;
  214. case STATE_OFF:
  215. default:
  216. displayPrintLn(0, "CoffeePi", true);
  217. displayPrintTime(1);
  218. break;
  219. }
  220. }
  221. /**
  222. * Returns the matching translation of a string
  223. * @param string Requested string
  224. * @return Translated string
  225. */
  226. const char* displayGetString(display_strings_t string) {
  227. if (displayLang >= lang_last)
  228. displayLang = DEFAULT_LANG;
  229. return display_strings[string].text[displayLang];
  230. }
  231. /**
  232. * Updates the display state to the matching coffee state
  233. * @param state New state
  234. */
  235. void displayPushState(coffee_status_t state) {
  236. if (state != coffeeState) {
  237. coffeeState = state;
  238. timerScaler--;
  239. displayTimer.call();
  240. }
  241. }