hal.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*
  2. * hal.cpp
  3. *
  4. * Created on: Aug 3, 2016
  5. * Author: Philipp Hinz, Sebastian Vendt
  6. */
  7. #include <wiringPi.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #include <errno.h>
  11. #include <string.h>
  12. #include <signal.h>
  13. #include <ctime>
  14. #include <time.h>
  15. #include <unistd.h>
  16. #include "hal.h"
  17. #include "global.h"
  18. #include "logger.h"
  19. #include "timer.h"
  20. #include "database.h"
  21. const char* SigName[] = {
  22. "NULL",
  23. "SigInt0Psh",
  24. "SigInt0Rls",
  25. "SigInt0RlsLong",
  26. "SigInt1Psh",
  27. "SigInt1Rls",
  28. "SigInt1RlsLong",
  29. "SigPressCls",
  30. "SigPressOpn",
  31. "SigProxOpn",
  32. "SigProxCvrd",
  33. "SigBrewOn",
  34. "SigBrewOff",
  35. "SigPowerUp",
  36. "SigPowerDown",
  37. "SigRotCW",
  38. "SigRotCCW"};
  39. typedef struct timespec timespec;
  40. volatile int flowcnt = 0;
  41. int lastFlowcnt = 0;
  42. int flowResetValue = -1;
  43. bool brewmanual = false;
  44. //local copies of the corresponding database entries
  45. time_t heatingCycleStart = 0;
  46. uint64_t totalHeatingTime = 0;
  47. uint16_t flowResetTime = TIME_FLOWRESET; //see TIME_FLOWRESET for details
  48. int Int0Time, Int1Time;
  49. int idleCounter;
  50. bool idle;
  51. bool flagIgnoreRlsInt0, flagIgnoreRlsInt1;
  52. //storage for the last state of the buttons, the proximity sensor and the pressure sensor
  53. int pinState[4] = {1, 1, 1, 0};
  54. //state of the rotary encoder
  55. //0: rotary1 state at t-1
  56. //1: rotary2 state at t-1
  57. //2: rotary1 state at t
  58. //3: rotary2 state at t
  59. int rotaryState[4] = {1, 0, 0, 1};
  60. timer Int0Timer(&halInt0TimerHandler);
  61. timer Int1Timer(&halInt1TimerHandler);
  62. timer idleTimer(&halIdleTimerHandler);
  63. timer flowResetTimer (&flowResetTimerHandler);
  64. timer flowTimer(&halFlowTimerHandler);
  65. timespec flowTimestep[] = {{0,0},{0,0}};
  66. uint8_t flowIndex = 0;
  67. int16_t tickCounter = 0; //rotary encoder
  68. uint16_t flowtime = 0;
  69. uint16_t lastFlowTime = 0;
  70. bool brewSigFired = false;
  71. //delay of the debounce in milliseconds
  72. #define DELAY_DEBOUNCE 50
  73. #define DELAY_MICRODEB 2
  74. //display turn off after idle time in min
  75. //minimal time is 2min
  76. #define IDLE_TIME 10
  77. /**
  78. * Initializes HAL
  79. */
  80. void halInit(void) {
  81. pinMode(RELAIS_HEAT, OUTPUT);
  82. pinMode(RELAIS_PUMP, OUTPUT);
  83. pinMode(RELAIS_POWER, OUTPUT);
  84. pinMode(PIN_PRESSURE_CTRL, INPUT);
  85. pinMode(PIN_PROXIMITY_SENSOR, INPUT);
  86. pinMode(PIN_INT0, INPUT);
  87. pinMode(PIN_INT1, INPUT);
  88. pinMode(PIN_FLOW, INPUT);
  89. pinMode(PIN_DISP, OUTPUT);
  90. pinMode(PIN_ROTARY1, INPUT);
  91. pinMode(PIN_ROTARY2, INPUT);
  92. idleTimer.setDivider(ms2Divider(60000));
  93. idleCounter = 0;
  94. idle = false;
  95. clock_gettime(CLOCK_REALTIME, &flowTimestep[0]);
  96. clock_gettime(CLOCK_REALTIME, &flowTimestep[1]);
  97. halDisplayOn();
  98. if (!(totalHeatingTime = sqlGetConf(CFGHeatingTime))) {
  99. logger_error("hal.cpp: Couldn't read the total heating time from the database\n");
  100. //pthread_exit(EXIT_SUCCESS);
  101. exit(EXIT_FAILURE);
  102. }
  103. if (!(flowResetTime = sqlGetConf(CFGFlowResetTime))) {
  104. logger_error("hal.cpp: Couldn't read the flow reset time from the database\nFalling back to defaults.\n");
  105. }
  106. if (optPower) {
  107. halMachineOn();
  108. } else {
  109. halMachineOff();
  110. }
  111. sleep(1); //wait till the machine eventually turned on when optPower
  112. pinState[3] = halIsHeating();
  113. Int0Timer.setDivider(ms2Divider(200));
  114. Int1Timer.setDivider(ms2Divider(200));
  115. Int0Time = 0;
  116. Int1Time = 0;
  117. flowResetTimer.setDivider(ms2Divider(TIME_FLOWRESET));
  118. flowTimer.setDivider(ms2Divider(200));
  119. flagIgnoreRlsInt0 = false;
  120. flagIgnoreRlsInt1 = false;
  121. event_subscribe("terminate", &halTerminate, "hal.cpp");
  122. if (wiringPiISR(PIN_INT0, INT_EDGE_BOTH, &halInt0) < 0) {
  123. logger_error("Unable to setup ISR0: %s\n", strerror(errno));
  124. return;
  125. }
  126. if (wiringPiISR(PIN_INT1, INT_EDGE_BOTH, &halInt1) < 0) {
  127. logger_error("Unable to setup ISR1: %s\n", strerror(errno));
  128. return;
  129. }
  130. if (wiringPiISR(PIN_FLOW, INT_EDGE_FALLING, &halIntFlow) < 0) {
  131. logger_error("Unable to setup ISRFLOW: %s\n", strerror(errno));
  132. return;
  133. }
  134. if (wiringPiISR(PIN_PRESSURE_CTRL, INT_EDGE_BOTH, &halIntPressure) < 0) {
  135. logger_error("Unable to setup ISRPressure: %s\n", strerror(errno));
  136. return;
  137. }
  138. if (wiringPiISR(PIN_PROXIMITY_SENSOR, INT_EDGE_BOTH, &halIntProximity) < 0) {
  139. logger_error("Unable to setup ISRProximity: %s\n", strerror(errno));
  140. return;
  141. }
  142. if (wiringPiISR(PIN_ROTARY1, INT_EDGE_BOTH, &halIntRotary) < 0) {
  143. logger_error("Unable to setup ISRRotary2: %s\n", strerror(errno));
  144. return;
  145. }
  146. logger(V_BASIC, "hal.cpp: Initialized\n");
  147. }
  148. /**
  149. * Switches relais on
  150. * @param relais Relais ID
  151. */
  152. void halRelaisOn(int relais) {
  153. halRelaisSet(relais, LOW);
  154. }
  155. /**
  156. * Turn the display off
  157. */
  158. void halDisplayOff(){
  159. digitalWrite(PIN_DISP, LOW);
  160. }
  161. /**
  162. * Turn the display on
  163. */
  164. void halDisplayOn(){
  165. digitalWrite(PIN_DISP, HIGH);
  166. }
  167. /**
  168. * Switches relais off
  169. * @param relais Relais ID
  170. */
  171. void halRelaisOff(int relais) {
  172. halRelaisSet(relais, HIGH);
  173. }
  174. /**
  175. * Switches relais to state
  176. * @param relais Relais ID
  177. * @param state LOW(0) or HIGH(1)
  178. */
  179. void halRelaisSet(int relais, int state) {
  180. if (state != HIGH && state != LOW)
  181. return;
  182. switch (relais) {
  183. case RELAIS_POWER:
  184. case RELAIS_HEAT:
  185. case RELAIS_PUMP:
  186. digitalWrite(relais, state);
  187. break;
  188. }
  189. }
  190. /**
  191. * Returns the state of the relais relais
  192. * Returns HIGH when Relais is ON
  193. * @param relais Relais ID
  194. */
  195. int halGetRelaisState(int relais) {
  196. switch (relais) {
  197. case RELAIS_POWER:
  198. case RELAIS_HEAT:
  199. case RELAIS_PUMP:
  200. return !digitalRead(relais);
  201. break;
  202. }
  203. return -1;
  204. }
  205. /**
  206. *
  207. */
  208. void halIntRotary(void) {
  209. //delay(DELAY_MICRODEB);
  210. rotaryState[2] = digitalRead(PIN_ROTARY1);
  211. rotaryState[3] = digitalRead(PIN_ROTARY2);
  212. if (rotaryState[0] != rotaryState[2]) {
  213. //check for the status of the other pin
  214. if (rotaryState[1] != rotaryState[3]) {
  215. if ((rotaryState[2] == HIGH && rotaryState[3] == LOW) || (rotaryState[2] == LOW && rotaryState[3] == HIGH)) {
  216. //clockwise rotation
  217. tickCounter++;
  218. if (!(abs(tickCounter) % ROTARY_STEPSIZE)) {
  219. //logger(V_HAL, "rotary encoder CW \n");
  220. halSendSignal(SigRotCW);
  221. tickCounter = 0;
  222. }
  223. } else if ((rotaryState[2] == HIGH && rotaryState[3] == HIGH) || (rotaryState[2] == LOW && rotaryState[3] == LOW)) {
  224. //counterclockwise rotation
  225. tickCounter--;
  226. if (!(abs(tickCounter) % ROTARY_STEPSIZE)) {
  227. //logger(V_HAL, "rotary encoder CCW \n");
  228. halSendSignal(SigRotCCW);
  229. tickCounter = 0;
  230. }
  231. }
  232. rotaryState[1] = rotaryState[3];
  233. }
  234. rotaryState[0] = rotaryState[2];
  235. }
  236. }
  237. /**
  238. * Interrupt routine for Int0 (Top button)
  239. */
  240. void halInt0(void) {
  241. //wait for a debounce
  242. delay(DELAY_DEBOUNCE);
  243. if (halGetInt0() && !pinState[0]) { //released
  244. logger(V_HAL, "Int0 released\n");
  245. pinState[0] = 1;
  246. if (flagIgnoreRlsInt0) {
  247. flagIgnoreRlsInt0 = false;
  248. } else {
  249. Int0Time = 0;
  250. Int0Timer.stop();
  251. halSendSignal(SigInt0Rls);
  252. }
  253. } else if(!halGetInt0() && pinState[0]) { //pressed
  254. logger(V_HAL, "Int0 pushed\n");
  255. pinState[0] = 0;
  256. halSendSignal(SigInt0Psh);
  257. Int0Time = 0;
  258. Int0Timer.start();
  259. }
  260. }
  261. /**
  262. *
  263. */
  264. void halInt0TimerHandler(void) {
  265. Int0Time += 200;
  266. if (Int0Time >= (TIME_BUTTONLONGPRESS * 1000)) {
  267. halSendSignal(SigInt0RlsLong);
  268. flagIgnoreRlsInt0 = true;
  269. Int0Time = 0;
  270. Int0Timer.stop();
  271. }
  272. }
  273. /**
  274. *
  275. */
  276. void halIdleTimerHandler(void) {
  277. if(++idleCounter == IDLE_TIME){
  278. halEnterIdle();
  279. }
  280. }
  281. /**
  282. * Interrupt routine for Int1 (Bottom button)
  283. */
  284. void halInt1(void) {
  285. delay(DELAY_DEBOUNCE);
  286. if (halGetInt1() && !pinState[1]) {
  287. logger(V_HAL, "Int1 released\n");
  288. pinState[1] = 1;
  289. if (flagIgnoreRlsInt1) {
  290. flagIgnoreRlsInt1 = false;
  291. } else {
  292. Int1Time = 0;
  293. Int1Timer.stop();
  294. halSendSignal(SigInt1Rls);
  295. }
  296. } else if(!halGetInt1() && pinState[1]) {
  297. logger(V_HAL, "Int1 pushed\n");
  298. pinState[1] = 0;
  299. halSendSignal(SigInt1Psh);
  300. Int1Time = 0;
  301. Int1Timer.start();
  302. }
  303. }
  304. /*
  305. *
  306. */
  307. void halInt1TimerHandler(void) {
  308. Int1Time += 200;
  309. if (Int1Time >= (TIME_BUTTONLONGPRESS * 1000)) {
  310. halSendSignal(SigInt1RlsLong);
  311. flagIgnoreRlsInt1 = true;
  312. Int1Time = 0;
  313. Int1Timer.stop();
  314. }
  315. }
  316. /**
  317. * Timer handler to auto-reset the flow counter.
  318. * The timer is started when the flow interrupt is triggered.
  319. * It compares the last value (flowResetValue) with the current flow value
  320. * If they match (e.g no flow within 1000ms) the flow counter is reseted.
  321. * Setting the last value to -1 ensures that when the timer gets called immediately after it is started the comparison will fail.
  322. * This implementation is read-only and so it doesn't need semaphores.
  323. */
  324. void flowResetTimerHandler() {
  325. if(flowResetValue == flowcnt) {
  326. halResetFlow();
  327. return;
  328. }
  329. flowResetValue = flowcnt;
  330. }
  331. /**
  332. *
  333. */
  334. void halFlowTimerHandler() {
  335. flowtime += 200;
  336. }
  337. /**
  338. * Interrupt routine for the flow sensor
  339. * It counts the edges and stores the value in flowcnt
  340. */
  341. void halIntFlow(void) {
  342. //halRelaisOff(RELAIS_POWER);
  343. if(!flowResetTimer.isActive()) flowResetTimer.start();
  344. flowcnt++;
  345. logger(V_HAL, "IntFlow triggered #%d total: %.2fml\n", flowcnt, halGetFlow());
  346. //tracking of flowtime
  347. if(!flowTimer.isActive()) flowTimer.start();
  348. //trigger the brewing signal using the total flow time
  349. if (halGetFlowTime() >= BREW_MANUAL_TRIGGER && !brewSigFired) {
  350. halSendSignal(SigBrewOn);
  351. brewSigFired = true;
  352. }
  353. //subroutine to log the flow to the database
  354. /*timespec deltaT;
  355. clock_gettime(CLOCK_REALTIME, &flowTimestep[flowIndex]);
  356. timespec_diff(&flowTimestep[((flowIndex + 1) % 2)], &flowTimestep[flowIndex], &deltaT);
  357. if (sqlLogFlow(logcycle, halGetFlow()*1000, deltaT.tv_sec * 1000 + deltaT.tv_nsec/1000000)) {
  358. logger_error("hal.cpp: could not log flow to database!");
  359. return;
  360. }
  361. flowIndex = (flowIndex + 1) % 2;*/
  362. }
  363. /**
  364. * Interrupt routine for the pressure control
  365. * It captures the time at closing point and opening point
  366. * Reading heating time via the getHeatingTime function
  367. */
  368. void halIntPressure(void) {
  369. delay(DELAY_DEBOUNCE);
  370. if (halIsHeating() && !pinState[3]) {
  371. logger(V_HAL, "hal.cpp: Pressure Control closed\n");
  372. pinState[3] = 1;
  373. halStartHeatingTime();
  374. halSendSignal(SigPressCls);
  375. } else if(!halIsHeating() && pinState[3]) {
  376. logger(V_HAL, "hal.cpp: Pressure Control opened\n");
  377. pinState[3] = 0;
  378. halStopHeatingTime();
  379. halSendSignal(SigPressOpn);
  380. }
  381. }
  382. /**
  383. *
  384. */
  385. void halStartHeatingTime(void) {
  386. time(&heatingCycleStart);
  387. }
  388. /**
  389. *
  390. */
  391. void halStopHeatingTime(void) {
  392. if (heatingCycleStart != 0) { //only track time if stopwatch was started!
  393. uint64_t timediff = (uint64_t) difftime(time(0), heatingCycleStart);
  394. logger(V_HAL, "Heating time: %f\n", timediff);
  395. totalHeatingTime += timediff;
  396. heatingCycleStart = 0;
  397. }
  398. }
  399. /**
  400. * Method to handle toggle of the proximity sensor
  401. */
  402. void halIntProximity(void) {
  403. delay(DELAY_DEBOUNCE);
  404. if (halProxSensorCovered() && !pinState[2]) {
  405. logger(V_HAL, "IntProximity triggered\n");
  406. pinState[2] = 1;
  407. halSendSignal(SigProxCvrd);
  408. } else if(!halProxSensorCovered() && pinState[2]){
  409. logger(V_HAL, "IntProximity triggered\n");
  410. pinState[2] = 0;
  411. halSendSignal(SigProxOpn);
  412. }
  413. }
  414. /**
  415. * Returns total flow through sensor in ml
  416. */
  417. float halGetFlow(void) {
  418. return flowcnt * FLOW_ML_PULSE;
  419. }
  420. /**
  421. * Returns the total flow time in ms
  422. */
  423. uint16_t halGetFlowTime(void){
  424. return flowtime;
  425. }
  426. /*
  427. * Returns the last total flow through the sensor in ml after reset
  428. */
  429. float halGetLastFlow(void) {
  430. return lastFlowcnt * FLOW_ML_PULSE;
  431. }
  432. /**
  433. * Returns the total flow time in ms before the last reset.
  434. */
  435. uint16_t halGetLastFlowTime(void){
  436. return lastFlowTime;
  437. }
  438. /**
  439. * Returns the time interval in ms between the last received flow signal and the reset of the flow counter
  440. */
  441. uint16_t halGetFlowResetTime(void) {
  442. return flowResetTime;
  443. }
  444. /**
  445. * Set the value of the Flow reset time. Its the responsibility of the caller to also call halWriteBackCache()!
  446. */
  447. void halSetFlowResetTime(uint16_t val) {
  448. flowResetTime = val;
  449. }
  450. /**
  451. * Resets the Flow counter. Do not call this function manually. It will be automatically triggered
  452. * from the flow reset timer routine!
  453. */
  454. void halResetFlow(void) {
  455. logger(V_HAL, "Flow counter reset, amount so far: %.2f ml\n", halGetFlow());
  456. lastFlowcnt = flowcnt;
  457. flowcnt = 0;
  458. lastFlowTime = flowtime;
  459. flowtime = 0;
  460. flowResetTimer.stop();
  461. flowTimer.stop();
  462. flowResetValue = -1;
  463. if(brewSigFired) {
  464. brewSigFired = false;
  465. halSendSignal(SigBrewOff);
  466. }
  467. }
  468. /**
  469. * Reads the status of the Pressure Control
  470. * @return 1 (true) for closed Pressure Control (heating) and 0 (false) for open
  471. */
  472. bool halIsHeating(void) {
  473. //TODO: eventually rely on the pinState variable instead of reading the pin
  474. //then the state of the pin needs to be set at the very beginning of the initialization
  475. if (digitalRead(PIN_PRESSURE_CTRL) == 0) return true;
  476. else return false;
  477. }
  478. /**
  479. *
  480. */
  481. uint64_t getTotalHeatingTime() {
  482. return totalHeatingTime;
  483. }
  484. /**
  485. * Returns status of the proximity switch
  486. * @return 1 if the proximity switch is covered and 0 if uncovered
  487. */
  488. bool halProxSensorCovered(void) {
  489. if(digitalRead(PIN_PROXIMITY_SENSOR) == 0){
  490. return false;
  491. } else {
  492. return true;
  493. }
  494. }
  495. /**
  496. * Returns the value of the top button Int0 (low active)
  497. * @return LOW or HIGH
  498. */
  499. int halGetInt0(void) {
  500. return digitalRead(PIN_INT0);
  501. }
  502. /**
  503. * Returns the value of the bottom button Int1 (low active)
  504. * @return LOW or HIGH
  505. */
  506. int halGetInt1(void) {
  507. return digitalRead(PIN_INT1);
  508. }
  509. /**
  510. * send Signal to coffee thread
  511. * @param val Integer value assigned to signal
  512. */
  513. void halSendSignal(HalSig val) {
  514. //reboot pi when lower button is pressed long
  515. if (val == SigInt1RlsLong) {
  516. event_trigger("terminate");
  517. sleep(3);
  518. system("reboot");
  519. }
  520. if (halIdle(val)) return;
  521. sigval value = { 0 };
  522. value.sival_int = (int) val;
  523. try {
  524. if (pthread_sigqueue(thread[THREAD_COFFEE], SIGUSR2, value)) {
  525. logger_error("hal.cpp: Failed to queue signal %d %s\n", val, strerror(errno));
  526. //No Signals reach the state machine anymore...
  527. exit(EXIT_FAILURE);
  528. }
  529. } catch (int e) {
  530. logger_error("Whoops.. %d\n", e);
  531. }
  532. }
  533. /**
  534. * Turn machine on
  535. */
  536. void halMachineOn(void) {
  537. halRelaisOn(RELAIS_HEAT);
  538. halRelaisOff(RELAIS_PUMP);
  539. halRelaisOn(RELAIS_POWER);
  540. idleTimer.stop();
  541. if(halIsHeating()) halStartHeatingTime();
  542. logger(V_HAL, "hal.cpp: Turning machine on\n");
  543. }
  544. /**
  545. * Turn machine off
  546. */
  547. void halMachineOff(void) {
  548. halRelaisOff(RELAIS_HEAT);
  549. halRelaisOff(RELAIS_PUMP);
  550. halRelaisOff(RELAIS_POWER);
  551. idleCounter = 0;
  552. idleTimer.start();
  553. halStopHeatingTime();
  554. halWriteBackCache();
  555. logger(V_HAL, "hal.cpp: Turning machine off\n");
  556. }
  557. /**
  558. *
  559. */
  560. void halEnterIdle(void){
  561. logger(V_HAL, "hal.cpp: Entering Idle Mode\n");
  562. idleTimer.stop();
  563. halDisplayOff();
  564. idle = true;
  565. }
  566. /**
  567. *
  568. */
  569. void halLeaveIdle(void) {
  570. idleCounter = 0;
  571. logger(V_HAL, "hal.cpp: Leaving Idle Mode\n");
  572. halDisplayOn();
  573. idleTimer.start();
  574. idle = false;
  575. }
  576. /**
  577. * processes the signal value with respect to the idle state
  578. * returns true, if the current signal should be dropped
  579. */
  580. bool halIdle(HalSig val){
  581. //idle processing filter for signals
  582. if (idle) {
  583. switch (val) {
  584. case SigInt0Psh:
  585. case SigInt1Psh:
  586. return true;
  587. break;
  588. case SigInt0Rls:
  589. case SigInt0RlsLong:
  590. case SigInt1Rls:
  591. case SigRotCCW:
  592. case SigRotCW:
  593. halLeaveIdle();
  594. return true;
  595. break;
  596. case SigPowerUp:
  597. halLeaveIdle();
  598. return false;
  599. break;
  600. default:
  601. return false;
  602. }
  603. } else {
  604. //reset the idle time on every input
  605. halResetIdleTimer();
  606. return false;
  607. }
  608. }
  609. /**
  610. *
  611. */
  612. void halResetIdleTimer(void) {
  613. idleCounter = 0;
  614. }
  615. /**
  616. * Wrapper function to turn the pump on.
  617. */
  618. void halPumpOn(){
  619. halRelaisOn(RELAIS_PUMP);
  620. }
  621. /**
  622. * Wrapper function to turn the pump off
  623. * and to measure how long the pump is running
  624. */
  625. void halPumpOff(void){
  626. halRelaisOff(RELAIS_PUMP);
  627. }
  628. /**
  629. * Function to calculate the difference between two timespecs
  630. */
  631. void timespec_diff(timespec *start, timespec *stop, timespec *result) {
  632. long int secDiff = stop->tv_sec - start->tv_sec;
  633. long int nsecDiff = stop->tv_nsec - start->tv_nsec;
  634. if (secDiff > 0) {
  635. if (nsecDiff >= 0) {
  636. result->tv_sec = secDiff;
  637. result->tv_nsec = nsecDiff;
  638. } else if (nsecDiff < 0) {
  639. result->tv_sec = --secDiff;
  640. result->tv_nsec = 1000000000 + nsecDiff;
  641. }
  642. } else if (secDiff < 0) {
  643. if (nsecDiff >= 0) {
  644. result->tv_sec = ++secDiff;
  645. result->tv_nsec = -(1000000000 - nsecDiff);
  646. } else if (nsecDiff < 0) {
  647. result->tv_sec = secDiff;
  648. result->tv_nsec = nsecDiff;
  649. }
  650. } else if (secDiff == 0){
  651. result->tv_sec = secDiff;
  652. result->tv_nsec = nsecDiff;
  653. }
  654. return;
  655. }
  656. /*
  657. * Handler for Termination of the hal
  658. */
  659. void halTerminate(event_t *event){
  660. halStopHeatingTime();
  661. halWriteBackCache();
  662. }
  663. /*
  664. * writing back non volatile variables of the hal to the database: totalHeatingTime
  665. */
  666. void halWriteBackCache(){
  667. if (sqlSetConf(CFGHeatingTime, totalHeatingTime)) {
  668. logger_error("hal.cpp: Couldn't write heating time to database");
  669. return;
  670. }
  671. logger(V_BREW, "Writing back heating time: %lld sec\n", totalHeatingTime);
  672. }