hal.cpp 6.9 KB

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