coffee.cpp 21 KB

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