hal.cpp 8.8 KB

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