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