coffee.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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 brewTime; //Brew time in ms
  29. timer brewTimer(&brewTimeHandler);
  30. uint64_t totalHeatingTime; //local copies of the corresponding database entries
  31. uint16_t brewCounter;
  32. bool initalHeating;
  33. bool descaling; //flag to indicate descaling and cleaning
  34. uint16_t descBrewcount;
  35. time_t descRawTimestamp;
  36. const char* PageName[] = { "SoftOff", "Kill", "Stats", "Temp", "Clean", "Demo",
  37. "Exit" };
  38. const char* StateName[] = { "OFF", "HEATING", "INITHEAT", "IDLE", "BREW",
  39. "BREWMAN", "CLEAN", "ERROR", "WAITOFF" };
  40. /**
  41. * Thread for the finite state machine
  42. * It represents the current state of the machine and handles signals coming from
  43. * the pressure control, buttons, the brew switch and the proximity sensor
  44. * @param threadid the ID of the thread
  45. */
  46. void *coffeeThread(void *threadid) {
  47. logger(V_BASIC, "Initializing coffee thread...\n");
  48. //installing new Signal handler for coffeethread
  49. struct sigaction action;
  50. sigemptyset(&action.sa_mask);
  51. action.sa_flags = SA_SIGINFO;
  52. action.sa_sigaction = coffeeHandler;
  53. sigaction(SIGUSR2, &action, NULL);
  54. brewTimer.setDivider(4);
  55. brewTimer.stop();
  56. brewTime = 0;
  57. initalHeating = true;
  58. mode = MODE_STATE; //Unless we enter the menu we start in state mode
  59. page = PAGE_SOFTOFF;
  60. descaling = false;
  61. //read the database values
  62. if (!(totalHeatingTime = sqlGetConf(CFGHeatingTime))) {
  63. logger_error("coffee.cpp: Couldn't read the heating time from the database");
  64. //pthread_exit(EXIT_SUCCESS);
  65. exit(EXIT_FAILURE);
  66. }
  67. if (!(brewCounter = sqlGetConf(CFGbrewcounter))) {
  68. logger_error("coffee.cpp: Couldn't read the brew counter from the database");
  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");
  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");
  79. //pthread_exit(EXIT_SUCCESS);
  80. exit(EXIT_FAILURE);
  81. }
  82. checkDescaling();
  83. event_subscribe("terminate", &coffeeTerminate);
  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. sleep(1);
  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_SOFTOFF: //this page is only available when the machine is on
  110. if (SigValueEmpty() && mode == MODE_MENU)
  111. pause();
  112. switch (getSigValue(MODE_MENU)) {
  113. case SigInt0Rls:
  114. changeState(STATE_WAIT_OFF);
  115. leaveMenu();
  116. break;
  117. case SigInt1Psh:
  118. changePage(PAGE_KILL);
  119. break;
  120. }
  121. break;
  122. case PAGE_KILL: //this page is only available when the machine is on
  123. if (SigValueEmpty() && mode == MODE_MENU)
  124. pause();
  125. switch (getSigValue(MODE_MENU)) {
  126. case SigInt0Rls:
  127. if (halIsHeating()) {
  128. coffeeIncreaseHeatingTime(halgetHeatingTime());
  129. }
  130. changeState(STATE_OFF);
  131. leaveMenu();
  132. break;
  133. case SigInt1Psh:
  134. if (state == STATE_IDLE || state == STATE_HEATING) {
  135. changePage(PAGE_CLEAN);
  136. } else {
  137. changePage(PAGE_DEMO);
  138. }
  139. break;
  140. }
  141. break;
  142. case PAGE_CLEAN: //this page is only be available when the machine is hot
  143. if (SigValueEmpty() && mode == MODE_MENU)
  144. pause();
  145. switch (getSigValue(MODE_MENU)) {
  146. case SigInt0Rls:
  147. changeMode(MODE_STATE);
  148. if (!halProxSensorCovered()) {
  149. changeState(STATE_CLEANING);
  150. leaveMenu();
  151. } else {
  152. changeState(STATE_FULLTANK);
  153. leaveMenu();
  154. }
  155. break;
  156. case SigInt1Psh:
  157. changePage(PAGE_DEMO);
  158. break;
  159. }
  160. break;
  161. case PAGE_DEMO:
  162. if (SigValueEmpty() && mode == MODE_MENU)
  163. pause();
  164. switch (getSigValue(MODE_MENU)) {
  165. case SigInt1Psh:
  166. changePage(PAGE_TEMP);
  167. break;
  168. }
  169. break;
  170. case PAGE_TEMP:
  171. if (SigValueEmpty() && mode == MODE_MENU)
  172. pause();
  173. switch (getSigValue(MODE_MENU)) {
  174. case SigInt1Psh:
  175. changePage(PAGE_STATS);
  176. break;
  177. }
  178. break;
  179. case PAGE_STATS:
  180. if (SigValueEmpty() && mode == MODE_MENU)
  181. pause();
  182. switch (getSigValue(MODE_MENU)) {
  183. case SigInt1Psh:
  184. changePage(PAGE_EXIT);
  185. break;
  186. }
  187. break;
  188. case PAGE_EXIT:
  189. if (SigValueEmpty() && mode == MODE_MENU)
  190. pause();
  191. switch (getSigValue(MODE_MENU)) {
  192. case SigInt0Rls:
  193. leaveMenu();
  194. break;
  195. case SigInt1Psh:
  196. if (state == STATE_HEATING || state == STATE_ERROR
  197. || state == STATE_IDLE
  198. || state == STATE_INITALHEATING) {
  199. changePage(PAGE_SOFTOFF);
  200. } else {
  201. changePage(PAGE_DEMO);
  202. }
  203. break;
  204. }
  205. break;
  206. } //end switch (page)
  207. /*
  208. * Hardware FSM
  209. */
  210. switch (state) {
  211. /*
  212. *
  213. */
  214. case STATE_OFF:
  215. if (mode == MODE_STATE) {
  216. halMachineOff();
  217. writeBackCache();
  218. changePage(PAGE_DEMO);
  219. if (SigValueEmpty())
  220. pause();
  221. }
  222. switch (getSigValue(MODE_STATE)) {
  223. case SigInt0Rls:
  224. //Check waterlevel in gray water tank
  225. //turn machine on
  226. halMachineOn();
  227. sleep(1);
  228. if (halIsHeating() && !halProxSensorCovered()) { //check if System starts to heat when turned on
  229. changeState(STATE_INITALHEATING);
  230. } else if (!halIsHeating() && !halProxSensorCovered()) {
  231. changeState(STATE_IDLE);
  232. } else if (halProxSensorCovered()) {
  233. logger_error("Empty Tank please!\n");
  234. changeState(STATE_FULLTANK);
  235. }
  236. if (page != PAGE_SOFTOFF)
  237. changePage(PAGE_SOFTOFF); //the machine is on, the menu starts with the turning off page
  238. break;
  239. case SigInt1Psh:
  240. //Enter the menu
  241. if (page != PAGE_DEMO)
  242. changePage(PAGE_DEMO); //machine is off, the menu starts with the demo page
  243. changeMode(MODE_MENU);
  244. break;
  245. }
  246. break;
  247. /*
  248. *
  249. */
  250. case STATE_WAIT_OFF:
  251. if (SigValueEmpty() && mode == MODE_STATE)
  252. pause();
  253. switch (getSigValue(MODE_STATE)) {
  254. case SigPressOpn:
  255. usleep(100000); //wait so no load will be switched
  256. coffeeIncreaseHeatingTime(halgetHeatingTime());
  257. changeState(STATE_OFF);
  258. break;
  259. case SigInt0Psh:
  260. case SigInt1Psh:
  261. if (halProxSensorCovered()) {
  262. changeState(STATE_FULLTANK);
  263. } else if (initalHeating) {
  264. changeState(STATE_INITALHEATING);
  265. } else {
  266. changeState(STATE_HEATING);
  267. }
  268. break;
  269. }
  270. break;
  271. /*
  272. *
  273. */
  274. case STATE_INITALHEATING:
  275. initalHeating = true;
  276. if (SigValueEmpty() && mode == MODE_STATE)
  277. pause();
  278. switch (getSigValue(MODE_STATE)) {
  279. // case SigInt0RlsLong:
  280. // //Turn machine off again
  281. // coffeeIncreaseHeatingTime(halgetHeatingTime());
  282. // changeState(STATE_OFF);
  283. // break;
  284. //
  285. // case SigInt0Rls:
  286. // changeState(STATE_WAIT_OFF);
  287. // break;
  288. case SigProxCvrd:
  289. changeState(STATE_FULLTANK);
  290. break;
  291. case SigPressOpn:
  292. //Inital heating finished
  293. initalHeating = false;
  294. coffeeIncreaseHeatingTime(halgetHeatingTime());
  295. changeState(STATE_IDLE);
  296. break;
  297. case SigInt1Psh:
  298. changeMode(MODE_MENU);
  299. break;
  300. }
  301. break;
  302. /*
  303. *
  304. */
  305. case STATE_HEATING:
  306. if (SigValueEmpty() && mode == MODE_STATE)
  307. pause();
  308. switch (getSigValue(MODE_STATE)) {
  309. // case SigInt1RlsLong:
  310. // //Turn machine _immediately_ off again
  311. // coffeeIncreaseHeatingTime(halgetHeatingTime());
  312. // changeState(STATE_OFF);
  313. // break;
  314. //
  315. // case SigInt1Rls:
  316. // //turn machine off when heating is finished
  317. // changeState(STATE_WAIT_OFF);
  318. // break;
  319. case SigPressOpn:
  320. coffeeIncreaseHeatingTime(halgetHeatingTime());
  321. changeState(STATE_IDLE);
  322. break;
  323. case SigInt0Psh:
  324. //start to brew a delicious coffee
  325. changeState(STATE_BREW);
  326. break;
  327. case SigProxCvrd:
  328. changeState(STATE_FULLTANK);
  329. break;
  330. case SigBrewOn:
  331. //someone brews manually
  332. changeState(STATE_BREWMANUAL);
  333. break;
  334. case SigInt1Psh:
  335. //Enter the menu
  336. changeMode(MODE_MENU);
  337. break;
  338. }
  339. break;
  340. /*
  341. *
  342. */
  343. case STATE_IDLE:
  344. if (SigValueEmpty() && mode == MODE_STATE)
  345. pause();
  346. switch (getSigValue(MODE_STATE)) {
  347. // case SigInt1RlsLong:
  348. // //turn machine _immediately_ off
  349. // changeState(STATE_OFF);
  350. // break;
  351. //
  352. // case SigInt1Rls:
  353. // changeState(STATE_OFF);
  354. // break;
  355. case SigPressCls:
  356. changeState(STATE_HEATING);
  357. break;
  358. case SigInt0Psh:
  359. changeState(STATE_BREW);
  360. break;
  361. case SigProxCvrd:
  362. changeState(STATE_FULLTANK);
  363. break;
  364. case SigBrewOn:
  365. //someone brews manually
  366. changeState(STATE_BREWMANUAL);
  367. break;
  368. case SigInt1Psh:
  369. //Enter the menu
  370. changeMode(MODE_MENU);
  371. break;
  372. }
  373. break;
  374. /*
  375. *
  376. */
  377. case STATE_BREW:
  378. //make sure the tank is not full
  379. if (halProxSensorCovered()) {
  380. changeState(STATE_FULLTANK);
  381. logger_error("coffee.cpp: Full tank detection failed..\n");
  382. } else {
  383. coffeeBrew();
  384. checkDescaling();
  385. logger(V_BREW, "Finishing brewing\n");
  386. if (!halProxSensorCovered()) {
  387. if (halIsHeating()) {
  388. changeState(STATE_HEATING);
  389. } else {
  390. changeState(STATE_IDLE);
  391. }
  392. } else {
  393. changeState(STATE_FULLTANK);
  394. }
  395. }
  396. break;
  397. /*
  398. *
  399. */
  400. case STATE_BREWMANUAL:
  401. if (SigValueEmpty() && mode == MODE_STATE)
  402. pause();
  403. break;
  404. /*
  405. *
  406. */
  407. case STATE_CLEANING: //this can only be executed once the machine is hot!
  408. if (!halProxSensorCovered()) {
  409. //execute the cleaning procedure
  410. coffeeClean();
  411. if (halIsHeating()) {
  412. changeState(STATE_HEATING);
  413. } else {
  414. changeState(STATE_IDLE);
  415. }
  416. } else {
  417. changeState(STATE_FULLTANK);
  418. }
  419. break;
  420. /*
  421. * Full tank is detected at the beginning and the end of a brewing process, during
  422. * idle times, initial heating and heating
  423. */
  424. case STATE_FULLTANK:
  425. if (SigValueEmpty() && mode == MODE_STATE)
  426. pause();
  427. switch (getSigValue(MODE_STATE)) {
  428. case SigInt1Psh:
  429. case SigInt0Psh:
  430. if (halIsHeating() && initalHeating) {
  431. changeState(STATE_INITALHEATING);
  432. } else if (halIsHeating() && !initalHeating) {
  433. changeState(STATE_HEATING);
  434. } else {
  435. changeState(STATE_IDLE);
  436. }
  437. break;
  438. }
  439. break;
  440. /*
  441. *
  442. */
  443. case STATE_ERROR:
  444. if (SigValueEmpty() && mode == MODE_STATE)
  445. pause();
  446. switch (getSigValue(MODE_STATE)) {
  447. case SigInt1RlsLong:
  448. case SigInt0RlsLong:
  449. if (halIsHeating()) {
  450. coffeeIncreaseHeatingTime(halgetHeatingTime());
  451. }
  452. changeState(STATE_OFF);
  453. break;
  454. }
  455. }
  456. }
  457. pthread_exit(EXIT_SUCCESS);
  458. }
  459. /**
  460. * Handler for the Signal send to this thread
  461. * It saves the type of signal received and tracks the time between a push and release event of up to 4 signals
  462. * The time is stored in the HalEvent variable when a release event is received
  463. * @param signum
  464. * @param siginfo
  465. * @param context
  466. */
  467. void coffeeHandler(int signum, siginfo_t *siginfo, void *context) {
  468. sigval_t sigVal = (siginfo->si_value);
  469. sigValue = sigVal.sival_int;
  470. logger(V_BREW, "coffee.cpp: CoffeeHandler called with Signal %d\n",
  471. sigValue);
  472. }
  473. /**
  474. * returns the Signal value from the last received Signal for the given mode and clears the variable
  475. * @return value sent with the last signal
  476. */
  477. int getSigValue(coffee_mode_t mode) {
  478. int tmp = sigValue;
  479. if (mode == MODE_MENU) {
  480. switch (sigValue) {
  481. case SigInt0Psh:
  482. case SigInt0Rls:
  483. case SigInt0RlsLong:
  484. case SigInt1Psh:
  485. case SigInt1Rls:
  486. case SigInt1RlsLong:
  487. sigValue = 0;
  488. return tmp;
  489. break;
  490. default:
  491. break;
  492. }
  493. } else { //State Mode
  494. sigValue = 0;
  495. return tmp;
  496. }
  497. //int tmp = sigValue;
  498. //sigValue = 0;
  499. //return tmp;
  500. return 0;
  501. }
  502. bool SigValueEmpty(void) {
  503. if (sigValue == 0)
  504. return true;
  505. else
  506. return false;
  507. }
  508. /**
  509. * Changes the state of the machine to newState
  510. * prints the change to the logger
  511. * @param newState
  512. */
  513. void changeState(coffee_status_t newState) {
  514. logger(V_BREW, "Changing state to %s\n", StateName[newState]);
  515. state = newState;
  516. event_trigger("statechange", &state, sizeof(state));
  517. }
  518. /*
  519. * Change Page to new menu page
  520. */
  521. void changePage(coffee_menuPage_t newPage) {
  522. logger(V_BREW, "Change Page to %s\n", PageName[newPage]);
  523. event_trigger("pagechange", &newPage, sizeof(newPage));
  524. page = newPage;
  525. }
  526. /*
  527. * Changes the mode of the machine to the given mode
  528. */
  529. void changeMode(coffee_mode_t newMode) {
  530. if (newMode == MODE_MENU)
  531. logger(V_BREW, "Changing to menu mode\n");
  532. else
  533. logger(V_BREW, "Changing to state mode\n");
  534. event_trigger("modechange", &newMode, sizeof(newMode));
  535. mode = newMode;
  536. }
  537. /*
  538. * leaving the menu
  539. * sets the start page for the next menu call to softoff
  540. */
  541. void leaveMenu() {
  542. logger(V_BREW, "Leaving the menu again...\n");
  543. //leave the menu again
  544. changeMode(MODE_STATE);
  545. //change page to initial page
  546. changePage(PAGE_SOFTOFF);
  547. }
  548. /**
  549. * Returns the current state of the FSM
  550. */
  551. coffee_status_t getState(void) {
  552. return state;
  553. }
  554. /**
  555. * Returns the local up-to-date brewcounter
  556. */
  557. uint16_t getBrewCounter(void) {
  558. return brewCounter;
  559. }
  560. /**
  561. * Counter for the brew time
  562. * refresh every 200ms
  563. */
  564. void brewTimeHandler(void) {
  565. brewTime += 200;
  566. }
  567. /**
  568. * handles program termination
  569. */
  570. void coffeeTerminate(event_t *event) {
  571. logger(V_BREW, "Coffee.cpp: Terminating\n");
  572. //stop brewing
  573. halRelaisOff(RELAIS_PUMP);
  574. brewTimer.stop();
  575. writeBackCache();
  576. }
  577. /**
  578. * Function to write back the values of the local copies
  579. * brewCounter and totalHeatingTime
  580. *
  581. */
  582. void writeBackCache(void) {
  583. if (sqlSetConf(CFGbrewcounter, brewCounter)) {
  584. logger_error("coffee.cpp: Couldn't write brewcounter to database");
  585. return;
  586. }
  587. if (sqlSetConf(CFGHeatingTime, totalHeatingTime)) {
  588. logger_error("coffee.cpp: Couldn't write heating time to database");
  589. return;
  590. }
  591. if (sqlSetConf(CFGDescBrewCount, descBrewcount)) {
  592. logger_error("coffee.cpp: Couldn't write descaling brewcount to database");
  593. return;
  594. }
  595. if (sqlSetConf(CFGDescTimestamp, (uint64_t)descRawTimestamp)) {
  596. logger_error("coffee.cpp: Couldn't write descaling timestamp to database");
  597. return;
  598. }
  599. }
  600. /*
  601. * Procedure for cleaning the machine
  602. */
  603. void coffeeClean(void) {
  604. logger(V_BREW, "Cleaning...\n");
  605. for (int i = 0; i < 20; i++) {
  606. halRelaisOn(RELAIS_PUMP);
  607. //TODO the sleep function returns when a signal is delivered
  608. //this causes the cleaning to not work properly when the pressure is triggered
  609. sleep(3);
  610. halRelaisOff(RELAIS_PUMP);
  611. sleep(15);
  612. }
  613. updateDescaling();
  614. descaling = false;
  615. event_trigger("descaling", &descaling, sizeof(bool));
  616. }
  617. /**
  618. * Brewing process
  619. */
  620. void coffeeBrew(void) {
  621. coffeeIncreaseBrewCounter();
  622. /*
  623. * Preinfusion
  624. */
  625. logger(V_BREW, "Starting preinfusion...\n");
  626. halResetFlow();
  627. halRelaisOn(RELAIS_PUMP);
  628. brewTime = 0;
  629. brewTimer.start();
  630. while (halGetFlow() < AMOUNT_PREINFUSION) {
  631. usleep(50000);
  632. if (getSigValue(MODE_STATE) == SigInt0Psh){
  633. stopBrewing();
  634. return;
  635. }
  636. }
  637. stopBrewing();
  638. /*
  639. * Wait for coffee to soak in infused water
  640. */
  641. brewTimer.start();
  642. while (brewTime < TIME_SOAK) {
  643. usleep(100000);
  644. if (getSigValue(MODE_STATE) == SigInt0Psh) {
  645. stopBrewing();
  646. return;
  647. }
  648. }
  649. stopBrewing();
  650. /*
  651. * Brewing the actual espresso
  652. */
  653. logger(V_BREW, "Starting infusion...\n");
  654. halRelaisOn(RELAIS_PUMP);
  655. brewTimer.start();
  656. while (brewTime < TIME_INFUSION && halGetFlow() < AMOUNT_DBLESPRESSO) {
  657. usleep(100000);
  658. if (getSigValue(MODE_STATE) == SigInt0Psh){
  659. stopBrewing();
  660. break;
  661. }
  662. }
  663. stopBrewing();
  664. return;
  665. }
  666. /*
  667. * Wrapper function for the end of a brewing process
  668. * this function stops the pump, brewtimer and resets the flow and brew time to zero
  669. */
  670. void stopBrewing() {
  671. halRelaisOff(RELAIS_PUMP);
  672. brewTimer.stop();
  673. brewTime = 0;
  674. halResetFlow();
  675. }
  676. /**
  677. *
  678. */
  679. void coffeeIncreaseBrewCounter(void) {
  680. brewCounter++;
  681. }
  682. /**
  683. *
  684. */
  685. void coffeeIncreaseHeatingTime(uint64_t heatingTime) {
  686. totalHeatingTime += heatingTime;
  687. }
  688. /**
  689. * Checks if the descaling is necessary
  690. * uses descBrewcount and descTimestamp
  691. */
  692. void checkDescaling(){
  693. time_t rawtime;
  694. time(&rawtime);
  695. double diffseconds = difftime(rawtime, descRawTimestamp);
  696. diffseconds /= 24*60*60;
  697. if((brewCounter - descBrewcount) >= DIRTY_ESPRESSO) {
  698. logger(V_BREW, "Descaling necessary due to quantity: %d\n", brewCounter - descBrewcount);
  699. descaling = true;
  700. event_trigger("descaling", &descaling, sizeof(bool));
  701. }
  702. if(diffseconds >= DIRTY_TIME) {
  703. logger(V_BREW, "Descaling necessary due to time in days: %d\n", diffseconds);
  704. descaling = true;
  705. event_trigger("descaling", &descaling, sizeof(bool));
  706. }
  707. }
  708. /**
  709. * updates the corresponding variables after a descaling process
  710. */
  711. void updateDescaling(){
  712. descBrewcount = brewCounter;
  713. time_t newDesTimestamp;
  714. time(&newDesTimestamp);
  715. if(newDesTimestamp == -1){
  716. logger(V_BREW, "Whoops, couldn't retrieve new descaling timestamp\n");
  717. }
  718. else {
  719. descRawTimestamp = newDesTimestamp;
  720. }
  721. }