hal.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 <errno.h>
  10. #include <string.h>
  11. #include <signal.h>
  12. #include <ctime>
  13. #include <unistd.h>
  14. #include "hal.h"
  15. #include "global.h"
  16. #include "logger.h"
  17. #include "timer.h"
  18. volatile int flowcnt = 0;
  19. int Int0Time, Int1Time;
  20. int idleCounter;
  21. bool idle;
  22. bool flagIgnoreRlsInt0, flagIgnoreRlsInt1;
  23. //storage for the last state of the buttons, the proximity sensor and the pressure sensor
  24. int pinState[4] = {1, 1, 1, 0};
  25. timer Int0Timer(&halInt0TimerHandler);
  26. timer Int1Timer(&halInt1TimerHandler);
  27. timer idleTimer(&halIdleTimerHandler);
  28. time_t heatingCycle[] = {0, 0};
  29. //delay of the debounce in milliseconds
  30. #define DELAY_DEBOUNCE 50
  31. //display turn off after idle time in min
  32. //minimal time is 2min
  33. #define IDLE_TIME 10
  34. /**
  35. * Initializes HAL
  36. */
  37. void halInit(void) {
  38. pinMode(RELAIS_HEAT, OUTPUT);
  39. pinMode(RELAIS_PUMP, OUTPUT);
  40. pinMode(RELAIS_POWER, OUTPUT);
  41. pinMode(PIN_PRESSURE_CTRL, INPUT);
  42. pinMode(PIN_PROXIMITY_SENSOR, INPUT);
  43. pinMode(PIN_INT0, INPUT);
  44. pinMode(PIN_INT1, INPUT);
  45. pinMode(PIN_FLOW, INPUT);
  46. pinMode(PIN_DISP, OUTPUT);
  47. idleTimer.setDivider(1200); //1 min
  48. idleCounter = 0;
  49. idle = false;
  50. halDisplayOn();
  51. if (optPower) {
  52. halMachineOn();
  53. } else {
  54. halMachineOff();
  55. }
  56. sleep(1); //wait till the machine eventually turned on when optPower
  57. pinState[3] = halIsHeating();
  58. Int0Timer.setDivider(4); //200ms
  59. Int1Timer.setDivider(4);
  60. Int0Time = 0;
  61. Int1Time = 0;
  62. flagIgnoreRlsInt0 = false;
  63. flagIgnoreRlsInt1 = false;
  64. if (wiringPiISR(PIN_INT0, INT_EDGE_BOTH, &halInt0) < 0) {
  65. logger_error("Unable to setup ISR0: %s\n", strerror(errno));
  66. return;
  67. }
  68. if (wiringPiISR(PIN_INT1, INT_EDGE_BOTH, &halInt1) < 0) {
  69. logger_error("Unable to setup ISR1: %s\n", strerror(errno));
  70. return;
  71. }
  72. if (wiringPiISR(PIN_FLOW, INT_EDGE_FALLING, &halIntFlow) < 0) {
  73. logger_error("Unable to setup ISRFLOW: %s\n", strerror(errno));
  74. return;
  75. }
  76. if (wiringPiISR(PIN_PRESSURE_CTRL, INT_EDGE_BOTH, &halIntPressure) < 0) {
  77. logger_error("Unable to setup ISRPressure: %s\n", strerror(errno));
  78. return;
  79. }
  80. if (wiringPiISR(PIN_PROXIMITY_SENSOR, INT_EDGE_BOTH, &halIntProximity) < 0) {
  81. logger_error("Unable to setup ISRProximity: %s\n", strerror(errno));
  82. return;
  83. }
  84. }
  85. /**
  86. * Switches relais on
  87. * @param relais Relais ID
  88. */
  89. void halRelaisOn(int relais) {
  90. halRelaisSet(relais, LOW);
  91. }
  92. /**
  93. * Turn the display off
  94. */
  95. void halDisplayOff(){
  96. digitalWrite(PIN_DISP, LOW);
  97. }
  98. /**
  99. * Turn the display on
  100. */
  101. void halDisplayOn(){
  102. digitalWrite(PIN_DISP, HIGH);
  103. }
  104. /**
  105. * Switches relais off
  106. * @param relais Relais ID
  107. */
  108. void halRelaisOff(int relais) {
  109. halRelaisSet(relais, HIGH);
  110. }
  111. /**
  112. * Switches relais to state
  113. * @param relais Relais ID
  114. * @param state LOW(0) or HIGH(1)
  115. */
  116. void halRelaisSet(int relais, int state) {
  117. if (state != HIGH && state != LOW)
  118. return;
  119. switch (relais) {
  120. case RELAIS_POWER:
  121. case RELAIS_HEAT:
  122. case RELAIS_PUMP:
  123. digitalWrite(relais, state);
  124. break;
  125. }
  126. }
  127. /**
  128. * Returns the state of the relais relais
  129. * Returns HIGH when Relais is ON
  130. * @param relais Relais ID
  131. */
  132. int halGetRelaisState(int relais) {
  133. switch (relais) {
  134. case RELAIS_POWER:
  135. case RELAIS_HEAT:
  136. case RELAIS_PUMP:
  137. return !digitalRead(relais);
  138. break;
  139. }
  140. return -1;
  141. }
  142. /**
  143. * Interrupt routine for Int0 (Top button)
  144. */
  145. void halInt0(void) {
  146. //wait for a debounce
  147. delay(DELAY_DEBOUNCE);
  148. if (halGetInt0() && !pinState[0]) { //released
  149. logger(V_HAL, "Int0 released\n");
  150. pinState[0] = 1;
  151. if (flagIgnoreRlsInt0) {
  152. flagIgnoreRlsInt0 = false;
  153. } else {
  154. Int0Time = 0;
  155. Int0Timer.stop();
  156. halSendSignal(SigInt0Rls);
  157. }
  158. } else if(!halGetInt0() && pinState[0]) { //pressed
  159. logger(V_HAL, "Int0 pushed\n");
  160. pinState[0] = 0;
  161. halSendSignal(SigInt0Psh);
  162. Int0Time = 0;
  163. Int0Timer.start();
  164. }
  165. }
  166. /**
  167. *
  168. */
  169. void halInt0TimerHandler(void) {
  170. Int0Time += 200;
  171. if (Int0Time >= (TIME_BUTTONLONGPRESS * 1000)) {
  172. halSendSignal(SigInt0RlsLong);
  173. flagIgnoreRlsInt0 = true;
  174. Int0Time = 0;
  175. Int0Timer.stop();
  176. }
  177. }
  178. /**
  179. *
  180. */
  181. void halIdleTimerHandler(void) {
  182. if(++idleCounter == IDLE_TIME){
  183. halEnterIdle();
  184. }
  185. }
  186. /**
  187. * Interrupt routine for Int1 (Bottom button)
  188. */
  189. void halInt1(void) {
  190. delay(DELAY_DEBOUNCE);
  191. if (halGetInt1() && !pinState[1]) {
  192. logger(V_HAL, "Int1 released\n");
  193. pinState[1] = 1;
  194. if (flagIgnoreRlsInt1) {
  195. flagIgnoreRlsInt1 = false;
  196. } else {
  197. Int1Time = 0;
  198. Int1Timer.stop();
  199. halSendSignal(SigInt1Rls);
  200. }
  201. } else if(!halGetInt1() && pinState[1]) {
  202. logger(V_HAL, "Int1 pushed\n");
  203. pinState[1] = 0;
  204. halSendSignal(SigInt1Psh);
  205. Int1Time = 0;
  206. Int1Timer.start();
  207. }
  208. }
  209. /*
  210. *
  211. */
  212. void halInt1TimerHandler(void) {
  213. Int1Time += 200;
  214. if (Int1Time >= (TIME_BUTTONLONGPRESS * 1000)) {
  215. halSendSignal(SigInt1RlsLong);
  216. flagIgnoreRlsInt1 = true;
  217. Int1Time = 0;
  218. Int1Timer.stop();
  219. }
  220. }
  221. /**
  222. * Interrupt routine for the flow sensor
  223. * It counts the edgdes and stores the value in flowcnt
  224. */
  225. void halIntFlow(void) {
  226. //halRelaisOff(RELAIS_POWER);
  227. logger(V_HAL, "IntFlow triggered #%d total: %.2fml\n", flowcnt, halGetFlow());
  228. if (flowcnt == 99) {
  229. halRelaisOff(RELAIS_PUMP);
  230. }
  231. flowcnt++;
  232. }
  233. /**
  234. * Interrupt routine for the pressure control
  235. * It captures the time at closing point and opening point
  236. * Reading heating time via the getHeatingTime function
  237. */
  238. void halIntPressure(void) {
  239. logger(V_HAL, "IntPressure Control triggered\n");
  240. if (halIsHeating() && !pinState[3]) {
  241. pinState[3] = 1;
  242. time(&heatingCycle[0]);
  243. halSendSignal(SigPressCls);
  244. } else if(!halIsHeating() && pinState[3]) {
  245. pinState[3] = 0;
  246. time(&heatingCycle[1]);
  247. halSendSignal(SigPressOpn);
  248. }
  249. }
  250. /**
  251. * Function to read the heating time in sec
  252. * If called during a heating process, it returns the time elapsed since the heating started
  253. * If called after a heating process, it returns the total time elapsed during the heating cycle
  254. */
  255. double halgetHeatingTime(void){
  256. //TODO check return value on negative times
  257. if (halIsHeating()) {
  258. logger(V_HAL, "Hot Heating Time: %f\n", difftime(time(NULL), heatingCycle[0]));
  259. return difftime(time(0), heatingCycle[0]);
  260. }
  261. else {
  262. logger(V_HAL, "Heating time: %f\n", difftime(heatingCycle[1], heatingCycle[0]));
  263. return difftime(heatingCycle[1], heatingCycle[0]);
  264. }
  265. }
  266. /**
  267. * Method to handle toggle of the proximity sensor
  268. */
  269. void halIntProximity(void) {
  270. delay(DELAY_DEBOUNCE);
  271. if (halProxSensorCovered() && !pinState[2]) {
  272. logger(V_HAL, "IntProximity triggered\n");
  273. pinState[2] = 1;
  274. halSendSignal(SigProxCvrd);
  275. } else if(!halProxSensorCovered() && pinState[2]){
  276. logger(V_HAL, "IntProximity triggered\n");
  277. pinState[2] = 0;
  278. halSendSignal(SigProxOpn);
  279. }
  280. }
  281. /**
  282. * Returns total flow trough sensor in ml
  283. */
  284. float halGetFlow(void) {
  285. return flowcnt * FLOW_ML_PULSE;
  286. }
  287. /**
  288. * Resets the Flow counter
  289. */
  290. void halResetFlow(void) {
  291. logger(V_HAL, "Flow counter reset, amount so far: %.2f ml\n", halGetFlow());
  292. flowcnt = 0;
  293. }
  294. /**
  295. * Reads the status of the Pressure Control
  296. * @return 1 (true) for closed Pressure Control(heating) and 0 (false) for open
  297. */
  298. bool halIsHeating(void) {
  299. if (digitalRead(PIN_PRESSURE_CTRL) == 0) {
  300. return true;
  301. } else {
  302. return false;
  303. }
  304. }
  305. /**
  306. * Returns status of the proximity switch
  307. * @return 1 if the proximity switch is covered and 0 if uncovered
  308. */
  309. bool halProxSensorCovered(void) {
  310. if(digitalRead(PIN_PROXIMITY_SENSOR) == 0){
  311. return false;
  312. } else {
  313. return true;
  314. }
  315. }
  316. /**
  317. * Returns the value of the top button Int0 (low active)
  318. * @return LOW or HIGH
  319. */
  320. int halGetInt0(void) {
  321. return digitalRead(PIN_INT0);
  322. }
  323. /**
  324. * Returns the value of the bottom button Int1 (low active)
  325. * @return LOW or HIGH
  326. */
  327. int halGetInt1(void) {
  328. return digitalRead(PIN_INT1);
  329. }
  330. /**
  331. * send Signal to coffee thread
  332. * @param val Integer value assigned to signal
  333. */
  334. void halSendSignal(HalSig val) {
  335. //catch if machine is idle and drop button event
  336. switch (val) {
  337. case SigInt0Psh:
  338. case SigInt1Psh:
  339. if (idle) {
  340. return;
  341. }
  342. break;
  343. case SigInt0Rls:
  344. case SigInt0RlsLong:
  345. case SigInt1Rls:
  346. case SigInt1RlsLong:
  347. if (idle) {
  348. halLeaveIdle();
  349. return;
  350. }
  351. break;
  352. default:
  353. break;
  354. }
  355. sigval value = { 0 };
  356. value.sival_int = (int) val;
  357. try {
  358. if (pthread_sigqueue(thread[THREAD_COFFEE], SIGUSR2, value)) {
  359. logger_error("hal.cpp: Failed to queue signal %d %s\n", val, strerror(errno));
  360. //No Signals reach the state machine anymore...
  361. exit(EXIT_FAILURE);
  362. }
  363. } catch (int e) {
  364. logger_error("Whoops.. %d\n", e);
  365. }
  366. }
  367. /**
  368. * Turn machine on
  369. */
  370. void halMachineOn(void) {
  371. halRelaisOn(RELAIS_HEAT);
  372. halRelaisOff(RELAIS_PUMP);
  373. halRelaisOn(RELAIS_POWER);
  374. idleTimer.stop();
  375. logger(V_HAL, "Turning machine on\n");
  376. }
  377. /**
  378. * Turn machine off
  379. */
  380. void halMachineOff(void) {
  381. halRelaisOff(RELAIS_HEAT);
  382. halRelaisOff(RELAIS_PUMP);
  383. halRelaisOff(RELAIS_POWER);
  384. idleCounter = 0;
  385. idleTimer.start();
  386. logger(V_HAL, "Turning machine off\n");
  387. }
  388. /**
  389. *
  390. */
  391. void halEnterIdle(void){
  392. logger(V_HAL, "Entering Idle Mode\n");
  393. idleTimer.stop();
  394. halDisplayOff();
  395. idle = true;
  396. }
  397. /**
  398. *
  399. */
  400. void halLeaveIdle(void){
  401. idleCounter = 0;
  402. logger(V_HAL, "Leaving Idle Mode\n");
  403. halDisplayOn();
  404. idleTimer.start();
  405. idle = false;
  406. }