hal.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. if (halIsHeating()) {
  257. logger(V_HAL, "Hot Heating Time: %f\n", difftime(time(NULL), heatingCycle[0]));
  258. return difftime(time(0), heatingCycle[0]);
  259. }
  260. else {
  261. logger(V_HAL, "Heating time: %f\n", difftime(heatingCycle[1], heatingCycle[0]));
  262. return difftime(heatingCycle[1], heatingCycle[0]);
  263. }
  264. }
  265. /**
  266. * Method to handle toggle of the proximity sensor
  267. */
  268. void halIntProximity(void) {
  269. delay(DELAY_DEBOUNCE);
  270. if (halProxSensorCovered() && !pinState[2]) {
  271. logger(V_HAL, "IntProximity triggered\n");
  272. pinState[2] = 1;
  273. halSendSignal(SigProxCvrd);
  274. } else if(!halProxSensorCovered() && pinState[2]){
  275. logger(V_HAL, "IntProximity triggered\n");
  276. pinState[2] = 0;
  277. halSendSignal(SigProxOpn);
  278. }
  279. }
  280. /**
  281. * Returns total flow trough sensor in ml
  282. */
  283. float halGetFlow(void) {
  284. return flowcnt * FLOW_ML_PULSE;
  285. }
  286. /**
  287. * Resets the Flow counter
  288. */
  289. void halResetFlow(void) {
  290. logger(V_HAL, "Flow counter reset, amount so far: %.2f ml\n", halGetFlow());
  291. flowcnt = 0;
  292. }
  293. /**
  294. * Reads the status of the Pressure Control
  295. * @return 1 (true) for closed Pressure Control(heating) and 0 (false) for open
  296. */
  297. bool halIsHeating(void) {
  298. if (digitalRead(PIN_PRESSURE_CTRL) == 0) {
  299. return true;
  300. } else {
  301. return false;
  302. }
  303. }
  304. /**
  305. * Returns status of the proximity switch
  306. * @return 1 if the proximity switch is covered and 0 if uncovered
  307. */
  308. bool halProxSensorCovered(void) {
  309. if(digitalRead(PIN_PROXIMITY_SENSOR) == 0){
  310. return false;
  311. } else {
  312. return true;
  313. }
  314. }
  315. /**
  316. * Returns the value of the top button Int0 (low active)
  317. * @return LOW or HIGH
  318. */
  319. int halGetInt0(void) {
  320. return digitalRead(PIN_INT0);
  321. }
  322. /**
  323. * Returns the value of the bottom button Int1 (low active)
  324. * @return LOW or HIGH
  325. */
  326. int halGetInt1(void) {
  327. return digitalRead(PIN_INT1);
  328. }
  329. /**
  330. * send Signal to coffee thread
  331. * @param val Integer value assigned to signal
  332. */
  333. void halSendSignal(HalSig val) {
  334. //catch if machine is idle and drop button event
  335. switch (val) {
  336. case SigInt0Psh:
  337. case SigInt1Psh:
  338. if (idle) {
  339. return;
  340. }
  341. break;
  342. case SigInt0Rls:
  343. case SigInt0RlsLong:
  344. case SigInt1Rls:
  345. case SigInt1RlsLong:
  346. if (idle) {
  347. halLeaveIdle();
  348. return;
  349. }
  350. break;
  351. default:
  352. break;
  353. }
  354. sigval value = { 0 };
  355. value.sival_int = (int) val;
  356. try {
  357. if (pthread_sigqueue(thread[THREAD_COFFEE], SIGUSR2, value)) {
  358. logger_error("hal.cpp: Failed to queue signal %d %s\n", val, strerror(errno));
  359. //No Signals reach the state machine anymore...
  360. exit(EXIT_FAILURE);
  361. }
  362. } catch (int e) {
  363. logger_error("Whoops.. %d\n", e);
  364. }
  365. }
  366. /**
  367. * Turn machine on
  368. */
  369. void halMachineOn(void) {
  370. halRelaisOn(RELAIS_HEAT);
  371. halRelaisOff(RELAIS_PUMP);
  372. halRelaisOn(RELAIS_POWER);
  373. idleTimer.stop();
  374. logger(V_HAL, "Turning machine on\n");
  375. }
  376. /**
  377. * Turn machine off
  378. */
  379. void halMachineOff(void) {
  380. halRelaisOff(RELAIS_HEAT);
  381. halRelaisOff(RELAIS_PUMP);
  382. halRelaisOff(RELAIS_POWER);
  383. idleCounter = 0;
  384. idleTimer.start();
  385. logger(V_HAL, "Turning machine off\n");
  386. }
  387. /**
  388. *
  389. */
  390. void halEnterIdle(void){
  391. logger(V_HAL, "Entering Idle Mode\n");
  392. idleTimer.stop();
  393. halDisplayOff();
  394. idle = true;
  395. }
  396. /**
  397. *
  398. */
  399. void halLeaveIdle(void){
  400. idleCounter = 0;
  401. logger(V_HAL, "Leaving Idle Mode\n");
  402. halDisplayOn();
  403. idleTimer.start();
  404. idle = false;
  405. }