hal.cpp 16 KB

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