hal.cpp 7.5 KB

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