coffee.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. /*
  2. * coffee.cpp
  3. *
  4. * Created on: Sep 25, 2017
  5. * Author: sebastian
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <stdint.h>
  12. #include <wiringPi.h>
  13. #include <pthread.h>
  14. #include <unistd.h>
  15. #include <iostream>
  16. #include <csignal>
  17. #include <time.h>
  18. #include <ctime>
  19. #include "coffee.h"
  20. #include "hal.h"
  21. #include "logger.h"
  22. #include "timer.h"
  23. #include "database.h"
  24. coffee_status_t state;
  25. coffee_menuPage_t page;
  26. coffee_mode_t mode;
  27. int sigValue;
  28. int menuTimeout;
  29. timer menuTimer(&menuTimerHandler);
  30. timer brewTimer(&brewTimerHandler);
  31. uint64_t totalHeatingTime; //local copies of the corresponding database entries
  32. uint16_t brewCounter;
  33. uint16_t brewTime;
  34. uint16_t currentCleanCycle;
  35. bool initalHeating;
  36. bool descaling; //flag to indicate descaling and cleaning
  37. uint16_t descBrewcount;
  38. time_t descRawTimestamp;
  39. const char* PageName[] = { "SoftOff", "Kill", "Stats", "Stats2", "Descaling", "Temp", "Clean", "Restart",
  40. "Exit" };
  41. const char* StateName[] = { "OFF", "HEATING", "INITHEAT", "IDLE", "BREW",
  42. "BREWMAN", "CLEAN", "ERROR", "WAITOFF" };
  43. /**
  44. * Thread for the finite state machine
  45. * It represents the current state of the machine and handles signals coming from
  46. * the pressure control, buttons, the brew switch and the proximity sensor
  47. * @param threadid the ID of the thread
  48. */
  49. void *coffeeThread(void *threadid) {
  50. logger(V_BASIC, "Initializing coffee thread...\n");
  51. //installing new Signal handler for coffeethread
  52. struct sigaction action;
  53. sigemptyset(&action.sa_mask);
  54. action.sa_flags = SA_SIGINFO;
  55. action.sa_sigaction = coffeeHandler;
  56. sigaction(SIGUSR2, &action, NULL);
  57. currentCleanCycle = 0;
  58. menuTimer.setDivider(4);
  59. menuTimer.stop();
  60. menuTimeout = 0;
  61. initalHeating = true;
  62. mode = MODE_STATE; //Unless we enter the menu we start in state mode
  63. page = PAGE_SOFTOFF;
  64. descaling = false;
  65. //read the database values
  66. if (!(totalHeatingTime = sqlGetConf(CFGHeatingTime))) {
  67. logger_error("coffee.cpp: Couldn't read the heating time from the database\n");
  68. //pthread_exit(EXIT_SUCCESS);
  69. exit(EXIT_FAILURE);
  70. }
  71. if (!(brewCounter = sqlGetConf(CFGbrewcounter))) {
  72. logger_error("coffee.cpp: Couldn't read the brew counter from the database\n");
  73. //pthread_exit(EXIT_SUCCESS);
  74. exit(EXIT_FAILURE);
  75. }
  76. if (!(descRawTimestamp = (time_t) sqlGetConf(CFGDescTimestamp))) {
  77. logger_error("coffee.cpp: Couldn't read the descaling time from the database\n");
  78. //pthread_exit(EXIT_SUCCESS);
  79. exit(EXIT_FAILURE);
  80. }
  81. if (!(descBrewcount = sqlGetConf(CFGDescBrewCount))) {
  82. logger_error("coffee.cpp: Couldn't read the descaling brewcount time from the database\n");
  83. //pthread_exit(EXIT_SUCCESS);
  84. exit(EXIT_FAILURE);
  85. }
  86. checkDescaling();
  87. event_subscribe("terminate", &coffeeTerminate);
  88. logger(V_BREW, "Determining inital state\n");
  89. //determine inital state
  90. if (halGetRelaisState(RELAIS_POWER) && halGetRelaisState(RELAIS_HEAT)
  91. && !halGetRelaisState(RELAIS_PUMP)) {
  92. //wait for heat relais to switch
  93. coffeeNap(1,0);
  94. if (halIsHeating()) { //Heating is on
  95. changeState(STATE_INITALHEATING);
  96. } else {
  97. initalHeating = false;
  98. changeState(STATE_IDLE);
  99. }
  100. } else if (halGetRelaisState(RELAIS_PUMP)) {
  101. logger_error("Whoops, why is the pump running...\n");
  102. changeState(STATE_ERROR);
  103. } else {
  104. changeState(STATE_OFF);
  105. }
  106. logger(V_BREW, "Starting Coffee FSM\n");
  107. //begin FSM
  108. while (1) {
  109. /*
  110. * Menue FSM
  111. */
  112. switch (page) {
  113. case PAGE_NULL:
  114. break;
  115. case PAGE_SOFTOFF: //this page is only available when the machine is on
  116. if (SigValueEmpty() && mode == MODE_MENU)
  117. pause();
  118. switch (getSigValue(MODE_MENU)) {
  119. case SigInt0Rls:
  120. if(halIsHeating()){
  121. changeState(STATE_WAIT_OFF);
  122. }
  123. else {
  124. changeState(STATE_OFF);
  125. }
  126. leaveMenu();
  127. break;
  128. case SigRotCW:
  129. changePage(PAGE_KILL);
  130. break;
  131. case SigRotCCW:
  132. changePage(PAGE_EXIT);
  133. break;
  134. }
  135. break;
  136. case PAGE_KILL: //this page is only available when the machine is on
  137. if (SigValueEmpty() && mode == MODE_MENU)
  138. pause();
  139. switch (getSigValue(MODE_MENU)) {
  140. case SigInt0Rls:
  141. if (halIsHeating()) {
  142. coffeeIncreaseHeatingTime(halgetHeatingTime());
  143. }
  144. changeState(STATE_OFF);
  145. leaveMenu();
  146. break;
  147. case SigRotCW:
  148. if (state == STATE_IDLE || state == STATE_HEATING) {
  149. changePage(PAGE_CLEAN);
  150. } else {
  151. changePage(PAGE_RESTART);
  152. }
  153. break;
  154. case SigRotCCW:
  155. changePage(PAGE_SOFTOFF);
  156. break;
  157. }
  158. break;
  159. case PAGE_CLEAN: //this page is only be available when the machine is hot
  160. if (SigValueEmpty() && mode == MODE_MENU)
  161. pause();
  162. switch (getSigValue(MODE_MENU)) {
  163. case SigInt0Rls:
  164. changeMode(MODE_STATE);
  165. if (!halProxSensorCovered()) {
  166. changeState(STATE_CLEANING);
  167. leaveMenu();
  168. } else {
  169. changeState(STATE_FULLTANK);
  170. leaveMenu();
  171. }
  172. break;
  173. case SigRotCW:
  174. changePage(PAGE_RESTART);
  175. break;
  176. case SigRotCCW:
  177. changePage(PAGE_KILL);
  178. break;
  179. }
  180. break;
  181. case PAGE_RESTART:
  182. if (SigValueEmpty() && mode == MODE_MENU)
  183. pause();
  184. switch (getSigValue(MODE_MENU)) {
  185. case SigInt0Rls:
  186. event_trigger("terminate");
  187. coffeeNap(4, 0);
  188. exit(EXIT_SUCCESS);
  189. break;
  190. case SigRotCW:
  191. changePage(PAGE_TEMP);
  192. break;
  193. case SigRotCCW:
  194. if(state == STATE_IDLE || state == STATE_HEATING){
  195. changePage(PAGE_CLEAN);
  196. } else if (state == STATE_ERROR || state == STATE_INITALHEATING) {
  197. changePage(PAGE_KILL);
  198. } else {
  199. changePage(PAGE_EXIT);
  200. }
  201. break;
  202. }
  203. break;
  204. case PAGE_TEMP:
  205. if (SigValueEmpty() && mode == MODE_MENU)
  206. pause();
  207. switch (getSigValue(MODE_MENU)) {
  208. case SigRotCW:
  209. changePage(PAGE_STATS);
  210. break;
  211. case SigRotCCW:
  212. changePage(PAGE_RESTART);
  213. break;
  214. }
  215. break;
  216. case PAGE_STATS:
  217. if (SigValueEmpty() && mode == MODE_MENU)
  218. pause();
  219. switch (getSigValue(MODE_MENU)) {
  220. case SigRotCW:
  221. changePage(PAGE_STATS2);
  222. break;
  223. case SigRotCCW:
  224. changePage(PAGE_TEMP);
  225. break;
  226. }
  227. break;
  228. case PAGE_STATS2:
  229. if (SigValueEmpty() && mode == MODE_MENU)
  230. pause();
  231. switch (getSigValue(MODE_MENU)) {
  232. case SigRotCW:
  233. changePage(PAGE_DESCALING);
  234. break;
  235. case SigRotCCW:
  236. changePage(PAGE_STATS);
  237. break;
  238. }
  239. break;
  240. case PAGE_DESCALING:
  241. if (SigValueEmpty() && mode == MODE_MENU)
  242. pause();
  243. switch (getSigValue(MODE_MENU)) {
  244. case SigRotCW:
  245. changePage(PAGE_EXIT);
  246. break;
  247. case SigRotCCW:
  248. changePage(PAGE_STATS2);
  249. break;
  250. }
  251. break;
  252. case PAGE_EXIT:
  253. if (SigValueEmpty() && mode == MODE_MENU)
  254. pause();
  255. switch (getSigValue(MODE_MENU)) {
  256. case SigInt0Rls:
  257. leaveMenu();
  258. break;
  259. case SigRotCW:
  260. if (state == STATE_HEATING || state == STATE_ERROR
  261. || state == STATE_IDLE
  262. || state == STATE_INITALHEATING) {
  263. changePage(PAGE_SOFTOFF);
  264. } else {
  265. changePage(PAGE_RESTART);
  266. }
  267. break;
  268. case SigRotCCW:
  269. changePage(PAGE_DESCALING);
  270. break;
  271. }
  272. break;
  273. } //end switch (page)
  274. /*
  275. * Hardware FSM
  276. */
  277. switch (state) {
  278. case STATE_NULL:
  279. break;
  280. /*
  281. *
  282. */
  283. case STATE_OFF:
  284. if (mode == MODE_STATE) {
  285. halMachineOff();
  286. writeBackCache();
  287. //TODO this might be a bit confusing to change the page here even if the menu isnt actually displayed
  288. changePage(PAGE_RESTART);
  289. if (SigValueEmpty())
  290. pause();
  291. }
  292. switch (getSigValue(MODE_STATE)) {
  293. case SigInt0Rls:
  294. case SigPowerUp:
  295. //Check waterlevel in gray water tank
  296. //turn machine on
  297. halMachineOn();
  298. coffeeNap(1,0);
  299. if (halIsHeating() && !halProxSensorCovered()) { //check if System starts to heat when turned on
  300. changeState(STATE_INITALHEATING);
  301. } else if (!halIsHeating() && !halProxSensorCovered()) {
  302. changeState(STATE_IDLE);
  303. } else if (halProxSensorCovered()) {
  304. logger_error("Empty Tank please!\n");
  305. changeState(STATE_FULLTANK);
  306. }
  307. if (page != PAGE_SOFTOFF)
  308. changePage(PAGE_SOFTOFF); //the machine is on, the menu starts with the turning off page
  309. break;
  310. case SigRotCCW:
  311. case SigRotCW:
  312. case SigInt1Rls:
  313. //Enter the menu
  314. /* This should be not necessary!
  315. * if (page != PAGE_DEMO)
  316. changePage(PAGE_DEMO); //machine is off, the menu starts with the demo page*/
  317. enterMenu();
  318. break;
  319. }
  320. break;
  321. /*
  322. *
  323. */
  324. case STATE_WAIT_OFF:
  325. if (SigValueEmpty() && mode == MODE_STATE)
  326. pause();
  327. switch (getSigValue(MODE_STATE)) {
  328. case SigPressOpn:
  329. coffeeNap(1,0); //wait so no load will be switched
  330. coffeeIncreaseHeatingTime(halgetHeatingTime());
  331. changeState(STATE_OFF);
  332. break;
  333. case SigInt0Rls:
  334. case SigInt1Rls:
  335. case SigPowerUp:
  336. if (halProxSensorCovered()) {
  337. changeState(STATE_FULLTANK);
  338. } else if (initalHeating) {
  339. changeState(STATE_INITALHEATING);
  340. } else {
  341. changeState(STATE_HEATING);
  342. }
  343. break;
  344. }
  345. break;
  346. /*
  347. *
  348. */
  349. case STATE_INITALHEATING:
  350. initalHeating = true;
  351. if (SigValueEmpty() && mode == MODE_STATE)
  352. pause();
  353. switch (getSigValue(MODE_STATE)) {
  354. // case SigInt0RlsLong:
  355. // //Turn machine off again
  356. // coffeeIncreaseHeatingTime(halgetHeatingTime());
  357. // changeState(STATE_OFF);
  358. // break;
  359. //
  360. // case SigInt0Rls:
  361. // changeState(STATE_WAIT_OFF);
  362. // break;
  363. case SigProxCvrd:
  364. changeState(STATE_FULLTANK);
  365. break;
  366. case SigPressOpn:
  367. //Inital heating finished
  368. initalHeating = false;
  369. coffeeIncreaseHeatingTime(halgetHeatingTime());
  370. changeState(STATE_IDLE);
  371. break;
  372. case SigPowerDown:
  373. changeState(STATE_WAIT_OFF);
  374. break;
  375. case SigRotCCW:
  376. case SigRotCW:
  377. case SigInt1Rls:
  378. enterMenu();
  379. break;
  380. }
  381. break;
  382. /*
  383. *
  384. */
  385. case STATE_HEATING:
  386. if (SigValueEmpty() && mode == MODE_STATE)
  387. pause();
  388. switch (getSigValue(MODE_STATE)) {
  389. // case SigInt1RlsLong:
  390. // //Turn machine _immediately_ off again
  391. // coffeeIncreaseHeatingTime(halgetHeatingTime());
  392. // changeState(STATE_OFF);
  393. // break;
  394. //
  395. // case SigInt1Rls:
  396. // //turn machine off when heating is finished
  397. // changeState(STATE_WAIT_OFF);
  398. // break;
  399. case SigPressOpn:
  400. coffeeIncreaseHeatingTime(halgetHeatingTime());
  401. changeState(STATE_IDLE);
  402. break;
  403. case SigInt0Rls:
  404. //start to brew a delicious coffee
  405. changeState(STATE_BREW);
  406. break;
  407. case SigProxCvrd:
  408. changeState(STATE_FULLTANK);
  409. break;
  410. case SigBrewOn:
  411. //someone brews manually
  412. coffeeManualBrewStart();
  413. changeState(STATE_BREWMANUAL);
  414. break;
  415. case SigPowerDown:
  416. changeState(STATE_WAIT_OFF);
  417. break;
  418. case SigRotCCW:
  419. case SigRotCW:
  420. case SigInt1Rls:
  421. //Enter the menu
  422. enterMenu();
  423. break;
  424. }
  425. break;
  426. /*
  427. *
  428. */
  429. case STATE_IDLE:
  430. if (SigValueEmpty() && mode == MODE_STATE)
  431. pause();
  432. switch (getSigValue(MODE_STATE)) {
  433. // case SigInt1RlsLong:
  434. // //turn machine _immediately_ off
  435. // changeState(STATE_OFF);
  436. // break;
  437. //
  438. // case SigInt1Rls:
  439. // changeState(STATE_OFF);
  440. // break;
  441. case SigPressCls:
  442. changeState(STATE_HEATING);
  443. break;
  444. case SigInt0Rls:
  445. changeState(STATE_BREW);
  446. break;
  447. case SigProxCvrd:
  448. changeState(STATE_FULLTANK);
  449. break;
  450. case SigBrewOn:
  451. //someone brews manually
  452. coffeeManualBrewStart();
  453. changeState(STATE_BREWMANUAL);
  454. break;
  455. case SigPowerDown:
  456. changeState(STATE_OFF);
  457. break;
  458. case SigRotCCW:
  459. case SigRotCW:
  460. case SigInt1Rls:
  461. //Enter the menu
  462. enterMenu();
  463. break;
  464. }
  465. break;
  466. /*
  467. *
  468. */
  469. case STATE_BREW:
  470. //make sure the tank is not full
  471. if (halProxSensorCovered()) {
  472. changeState(STATE_FULLTANK);
  473. logger_error("coffee.cpp: Full tank detection failed..\n");
  474. } else {
  475. coffeeBrew();
  476. checkDescaling();
  477. logger(V_BREW, "Finishing brewing\n");
  478. if (!halProxSensorCovered()) {
  479. if (halIsHeating()) {
  480. changeState(STATE_HEATING);
  481. } else {
  482. changeState(STATE_IDLE);
  483. }
  484. } else {
  485. changeState(STATE_FULLTANK);
  486. }
  487. }
  488. break;
  489. /*
  490. *
  491. */
  492. case STATE_BREWMANUAL:
  493. if (SigValueEmpty() && mode == MODE_STATE)
  494. pause();
  495. switch (getSigValue(MODE_STATE)) {
  496. case SigBrewOff:
  497. if (halIsHeating()) {
  498. changeState(STATE_HEATING);
  499. } else {
  500. changeState(STATE_IDLE);
  501. }
  502. break;
  503. }
  504. break;
  505. /*
  506. *
  507. */
  508. case STATE_CLEANING: //this can only be executed once the machine is hot!
  509. if (!halProxSensorCovered()) {
  510. //execute the cleaning procedure
  511. coffeeClean();
  512. if (halIsHeating()) {
  513. changeState(STATE_HEATING);
  514. } else {
  515. changeState(STATE_IDLE);
  516. }
  517. } else {
  518. changeState(STATE_FULLTANK);
  519. }
  520. break;
  521. /*
  522. * Full tank is detected at the beginning and the end of a brewing process, during
  523. * idle times, initial heating and heating
  524. */
  525. case STATE_FULLTANK:
  526. if (SigValueEmpty() && mode == MODE_STATE)
  527. pause();
  528. switch (getSigValue(MODE_STATE)) {
  529. case SigInt1Rls:
  530. case SigInt0Rls:
  531. if (halIsHeating() && initalHeating) {
  532. changeState(STATE_INITALHEATING);
  533. } else if (halIsHeating() && !initalHeating) {
  534. changeState(STATE_HEATING);
  535. } else {
  536. changeState(STATE_IDLE);
  537. }
  538. break;
  539. }
  540. break;
  541. /*
  542. *
  543. */
  544. case STATE_ERROR:
  545. if (SigValueEmpty() && mode == MODE_STATE)
  546. pause();
  547. switch (getSigValue(MODE_STATE)) {
  548. case SigInt0RlsLong:
  549. if (halIsHeating()) {
  550. coffeeIncreaseHeatingTime(halgetHeatingTime());
  551. }
  552. changeState(STATE_OFF);
  553. break;
  554. }
  555. }
  556. }
  557. pthread_exit(EXIT_SUCCESS);
  558. }
  559. /**
  560. * Handler for the Signal send to this thread
  561. * It saves the type of signal received and tracks the time between a push and release event of up to 4 signals
  562. * The time is stored in the HalEvent variable when a release event is received
  563. * @param signum
  564. * @param siginfo
  565. * @param context
  566. */
  567. void coffeeHandler(int signum, siginfo_t *siginfo, void *context) {
  568. sigval_t sigVal = (siginfo->si_value);
  569. sigValue = sigVal.sival_int;
  570. logger(V_BREW, "coffee.cpp: CoffeeHandler called with Signal %s\n",
  571. SigName[sigValue]);
  572. }
  573. /**
  574. * returns the Signal value from the last received Signal for the given mode and clears the variable
  575. * @return value sent with the last signal
  576. */
  577. int getSigValue(coffee_mode_t mode) {
  578. int tmp = sigValue;
  579. if (mode == MODE_MENU) {
  580. switch (sigValue) {
  581. case SigInt0Psh:
  582. case SigInt0Rls:
  583. case SigInt0RlsLong:
  584. case SigInt1Psh:
  585. case SigInt1Rls:
  586. case SigRotCCW:
  587. case SigRotCW:
  588. sigValue = 0;
  589. menuTimeout = 0;
  590. return tmp;
  591. break;
  592. default:
  593. break;
  594. }
  595. } else { //State Mode
  596. sigValue = 0;
  597. return tmp;
  598. }
  599. //int tmp = sigValue;
  600. //sigValue = 0;
  601. //return tmp;
  602. return 0;
  603. }
  604. bool SigValueEmpty(void) {
  605. if (sigValue == 0)
  606. return true;
  607. else
  608. return false;
  609. }
  610. /**
  611. * Changes the state of the machine to newState
  612. * prints the change to the logger
  613. * @param newState
  614. */
  615. void changeState(coffee_status_t newState) {
  616. logger(V_BREW, "Changing state to %s\n", StateName[newState]);
  617. state = newState;
  618. event_trigger("statechange", &state, sizeof(state));
  619. }
  620. /*
  621. * Change Page to new menu page
  622. */
  623. void changePage(coffee_menuPage_t newPage) {
  624. logger(V_BREW, "Change Page to %s\n", PageName[newPage]);
  625. event_trigger("pagechange", &newPage, sizeof(newPage));
  626. page = newPage;
  627. }
  628. /*
  629. * Changes the mode of the machine to the given mode
  630. */
  631. void changeMode(coffee_mode_t newMode) {
  632. if (newMode == MODE_MENU)
  633. logger(V_BREW, "Changing to menu mode\n");
  634. else
  635. logger(V_BREW, "Changing to state mode\n");
  636. event_trigger("modechange", &newMode, sizeof(newMode));
  637. mode = newMode;
  638. }
  639. /*
  640. * leaving the menu
  641. * sets the start page for the next menu call to softoff
  642. */
  643. void leaveMenu(void) {
  644. logger(V_BREW, "Leaving the menu again\n");
  645. //leave the menu again
  646. changeMode(MODE_STATE);
  647. //change page to initial page
  648. changePage(PAGE_SOFTOFF);
  649. //stop the timeout counter
  650. menuTimeout = 0;
  651. menuTimer.stop();
  652. }
  653. /**
  654. * entering the menu
  655. * starts the timeoutcounter and changes the mode to the Menu Mode
  656. */
  657. void enterMenu(void) {
  658. logger(V_BREW, "Entering the menu\n");
  659. changeMode(MODE_MENU);
  660. menuTimeout = 0;
  661. menuTimer.start();
  662. }
  663. /**
  664. * Returns the current cleaning cycle
  665. */
  666. uint16_t getCurrentCleanCycle(void) {
  667. return currentCleanCycle;
  668. }
  669. /**
  670. * Returns the current state of the FSM
  671. */
  672. coffee_status_t getState(void) {
  673. return state;
  674. }
  675. /**
  676. * Returns the local up-to-date brewcounter
  677. */
  678. uint16_t getBrewCounter(void) {
  679. return brewCounter;
  680. }
  681. /**
  682. * Returns the total heating time in seconds
  683. */
  684. uint64_t getTotalHeatingTime(void) {
  685. return totalHeatingTime;
  686. }
  687. /**
  688. * Returns the value of the brewcounter when the last descaling happened
  689. */
  690. uint16_t getDescBrewCounter (void) {
  691. return descBrewcount;
  692. }
  693. /**
  694. * Returns the raw time stamp when the last descaling happened
  695. */
  696. time_t * getDescTimestamp (void) {
  697. return &descRawTimestamp;
  698. }
  699. /**
  700. * Counter for the menu timeout
  701. * When no input is coming from the user the machine leaves the menu automatically after MENUTIMEOUT seconds
  702. */
  703. void menuTimerHandler(void){
  704. menuTimeout += 200;
  705. if((menuTimeout/1000) >= MENUTIMEOUT) {
  706. leaveMenu();
  707. }
  708. }
  709. /**
  710. * handles program termination
  711. */
  712. void coffeeTerminate(event_t *event) {
  713. logger(V_BREW, "Coffee.cpp: Terminating\n");
  714. //stop brewing
  715. halRelaisOff(RELAIS_PUMP);
  716. writeBackCache();
  717. }
  718. /**
  719. * coffeeNap does almost the same as sleep() but resumes to sleep after a signal got delivered
  720. * sec: seconds to sleep
  721. * nanosec: nanoseconds to sleep
  722. */
  723. void coffeeNap (uint64_t sec, uint64_t nanosec){
  724. struct timespec sleepTime;
  725. clock_gettime(CLOCK_REALTIME, &sleepTime);
  726. sleepTime.tv_sec += sec;
  727. sleepTime.tv_nsec += nanosec;
  728. //overflow
  729. if(sleepTime.tv_nsec >= 1000000000) {
  730. sleepTime.tv_nsec -= 1000000000;
  731. sleepTime.tv_sec++;
  732. }
  733. int errval = 0;
  734. do{
  735. errval = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &sleepTime, NULL);
  736. }
  737. while(errval == EINTR);
  738. if(errval) logger_error("coffee.cpp: suspending thread failed with error %d\n", errval);
  739. }
  740. /**
  741. * Function to write back the values of the local copies
  742. * brewCounter and totalHeatingTime
  743. *
  744. */
  745. void writeBackCache(void) {
  746. logger(V_BREW, "Writing back cache...\n");
  747. logger(V_BREW, "Writing back brewCounter with %d\n", brewCounter);
  748. if (sqlSetConf(CFGbrewcounter, brewCounter)) {
  749. logger_error("coffee.cpp: Couldn't write brewcounter to database");
  750. return;
  751. }
  752. logger(V_BREW, "Writing back total heating Time with %lld sec\n", totalHeatingTime);
  753. if (sqlSetConf(CFGHeatingTime, totalHeatingTime)) {
  754. logger_error("coffee.cpp: Couldn't write heating time to database");
  755. return;
  756. }
  757. logger(V_BREW, "Writing back descaling brew counter %d\n", descBrewcount);
  758. if (sqlSetConf(CFGDescBrewCount, descBrewcount)) {
  759. logger_error("coffee.cpp: Couldn't write descaling brewcount to database");
  760. return;
  761. }
  762. struct tm * timeinfo;
  763. timeinfo = localtime(&descRawTimestamp);
  764. logger(V_BREW, "Writing back descaling timestamp %d-%d-%d %d:%d\n", timeinfo->tm_mday, timeinfo->tm_mon, timeinfo->tm_year+1900, timeinfo->tm_hour, timeinfo->tm_min);
  765. if (sqlSetConf(CFGDescTimestamp, (uint64_t)descRawTimestamp)) {
  766. logger_error("coffee.cpp: Couldn't write descaling timestamp to database");
  767. return;
  768. }
  769. }
  770. /*
  771. * Procedure for cleaning the machine
  772. */
  773. void coffeeClean(void) {
  774. logger(V_BREW, "Cleaning...\n");
  775. for (int i = 0; i < CLEANING_CYCLES; i++) {
  776. currentCleanCycle++;
  777. halPumpOn();
  778. coffeeNap(3,0);
  779. halPumpOff();
  780. coffeeNap(15,0);
  781. }
  782. updateDescaling();
  783. descaling = false;
  784. currentCleanCycle = 0;
  785. event_trigger("descaling", &descaling, sizeof(bool));
  786. }
  787. /*
  788. *
  789. */
  790. void coffeeManualBrewStart (void) {
  791. logger(V_BREW, "Starting manual brewing...\n");
  792. coffeeIncreaseBrewCounter();
  793. }
  794. /**
  795. * Brewing process
  796. */
  797. void coffeeBrew(void) {
  798. coffeeIncreaseBrewCounter();
  799. /*
  800. * Preinfusion
  801. */
  802. logger(V_BREW, "Starting preinfusion...\n");
  803. halPumpOn();
  804. while (halGetFlow() < AMOUNT_PREINFUSION && halGetFlowTime() < TIME_PREINFUSION) {
  805. //TODO don't use coffeeNap here since we don't want to resume to sleep after a signal got caught...
  806. coffeeNap(0, 50000000);
  807. if (getSigValue(MODE_STATE) == SigInt0Rls){
  808. halPumpOff();
  809. return;
  810. }
  811. }
  812. halPumpOff();
  813. /*
  814. * Wait for coffee to soak in infused water
  815. */
  816. brewTimer.start();
  817. while (brewTime < TIME_SOAK) {
  818. coffeeNap(1, 100000000);
  819. if (getSigValue(MODE_STATE) == SigInt0Rls) {
  820. brewTimer.stop();
  821. brewTime = 0;
  822. return;
  823. }
  824. }
  825. brewTimer.stop();
  826. brewTime = 0;
  827. /*
  828. * Brewing the actual espresso
  829. */
  830. logger(V_BREW, "Starting infusion...\n");
  831. halPumpOn();
  832. while (brewTime < TIME_INFUSION && halGetFlow() < AMOUNT_DBLESPRESSO) {
  833. coffeeNap(1, 100000000);
  834. if (getSigValue(MODE_STATE) == SigInt0Rls){
  835. halPumpOff();
  836. return;
  837. }
  838. }
  839. halPumpOff();
  840. return;
  841. }
  842. void brewTimerHandler(){
  843. brewTime += 200;
  844. }
  845. /**
  846. *
  847. */
  848. void coffeeIncreaseBrewCounter(void) {
  849. brewCounter++;
  850. }
  851. /**
  852. *
  853. */
  854. void coffeeIncreaseHeatingTime(uint64_t heatingTime) {
  855. totalHeatingTime += heatingTime;
  856. }
  857. /**
  858. * Checks if the descaling is necessary
  859. * uses descBrewcount and descTimestamp
  860. */
  861. void checkDescaling(){
  862. int16_t dirtyEspresso = checkDirtyEspresso ();
  863. int16_t dirtyTime = checkDirtyTime ();
  864. if(dirtyEspresso <= 0) {
  865. logger(V_BREW, "Descaling necessary due to quantity: %d\n", dirtyEspresso);
  866. descaling = true;
  867. event_trigger("descaling", &descaling, sizeof(bool));
  868. }
  869. if(dirtyTime <= 0) {
  870. logger(V_BREW, "Descaling necessary due to time in days: %d\n", dirtyTime);
  871. descaling = true;
  872. event_trigger("descaling", &descaling, sizeof(bool));
  873. }
  874. }
  875. /**
  876. * this function returns the remaining espressi to be brewed before the descaling event is fired
  877. * returns a positive integer when there are cups remaining
  878. * and a negative when the number of cups are exceeded
  879. * Number of cups after a descaling is defined with DIRTY_ESPRESSO
  880. */
  881. int16_t checkDirtyEspresso (void) {
  882. return descBrewcount + DIRTY_ESPRESSO - brewCounter;
  883. }
  884. /**
  885. * Returns the remaining days before the next descaling event is fired
  886. * returns a positive integer if there is time left and a negative one if the descaling time was exceeded
  887. */
  888. int16_t checkDirtyTime (void) {
  889. time_t rawtime;
  890. time(&rawtime);
  891. double diffseconds = difftime(rawtime, descRawTimestamp);
  892. diffseconds /= 24*60*60; //calculate the days
  893. return DIRTY_TIME - diffseconds;
  894. }
  895. /**
  896. * updates the corresponding variables after a descaling process
  897. */
  898. void updateDescaling(){
  899. descBrewcount = brewCounter;
  900. time_t newDesTimestamp;
  901. time(&newDesTimestamp);
  902. if(newDesTimestamp == -1){
  903. logger(V_BREW, "Whoops, couldn't retrieve new descaling timestamp\n");
  904. }
  905. else {
  906. descRawTimestamp = newDesTimestamp;
  907. }
  908. }