hal.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * hal.cpp
  3. *
  4. * Created on: Aug 3, 2016
  5. * Author: Philipp Hinz
  6. */
  7. #include <wiringPi.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <signal.h>
  12. #include "hal.h"
  13. #include "global.h"
  14. #include "logger.h"
  15. #include "timer.h"
  16. volatile int flowcnt = 0;
  17. int Int0Time;
  18. int Int1Time;
  19. bool flagIgnoreRlsInt0;
  20. bool flagIgnoreRlsInt1;
  21. timer Int0Timer(&halInt0TimerHandler);
  22. timer Int1Timer(&halInt1TimerHandler);
  23. /**
  24. * Initializes HAL
  25. */
  26. void halInit(void) {
  27. if (optPower) {
  28. halMachineOn();
  29. } else {
  30. halMachineOff();
  31. }
  32. pinMode(RELAIS_HEAT, OUTPUT);
  33. pinMode(RELAIS_PUMP, OUTPUT);
  34. pinMode(RELAIS_POWER, OUTPUT);
  35. pinMode(PIN_PRESSURE_CTRL, INPUT);
  36. pinMode(PIN_PROXIMITY_SENSOR, INPUT);
  37. pinMode(PIN_INT0, INPUT);
  38. pinMode(PIN_INT1, INPUT);
  39. pinMode(PIN_FLOW, INPUT);
  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)
  63. < 0) {
  64. logger_error("Unable to setup ISRProximity: %s\n", strerror(errno));
  65. return;
  66. }
  67. }
  68. /**
  69. * Switches relais on
  70. * @param relais Relais ID
  71. */
  72. void halRelaisOn(int relais) {
  73. halRelaisSet(relais, LOW);
  74. }
  75. /**
  76. * Switches relais off
  77. * @param relais Relais ID
  78. */
  79. void halRelaisOff(int relais) {
  80. halRelaisSet(relais, HIGH);
  81. }
  82. /**
  83. * Switches relais to state
  84. * @param relais Relais ID
  85. * @param state LOW(0) or HIGH(1)
  86. */
  87. void halRelaisSet(int relais, int state) {
  88. if (state != HIGH && state != LOW)
  89. return;
  90. switch (relais) {
  91. case RELAIS_POWER:
  92. case RELAIS_HEAT:
  93. case RELAIS_PUMP:
  94. digitalWrite(relais, state);
  95. break;
  96. }
  97. }
  98. /**
  99. * Returns the state of the relais relais
  100. * Returns HIGH when Relais is ON
  101. * @param relais Relais ID
  102. */
  103. int halGetRelaisState(int relais) {
  104. switch (relais) {
  105. case RELAIS_POWER:
  106. case RELAIS_HEAT:
  107. case RELAIS_PUMP:
  108. return !digitalRead(relais);
  109. break;
  110. }
  111. return -1;
  112. }
  113. /**
  114. * Interrupt routine for Int0 (Top button)
  115. */
  116. void halInt0(void) {
  117. logger(V_HAL, "Int0 triggered\n");
  118. if (halGetInt0()) {
  119. if (flagIgnoreRlsInt0) {
  120. flagIgnoreRlsInt0 = false;
  121. } else {
  122. halSendSignal(SigInt0Rls);
  123. }
  124. } else {
  125. halSendSignal(SigInt0Psh);
  126. Int0Time = 0;
  127. Int0Timer.start();
  128. }
  129. }
  130. /**
  131. *
  132. */
  133. void halInt0TimerHandler(void) {
  134. Int0Time += 200;
  135. if (Int0Time >= (TIME_BUTTONLONGPRESS * 1000)) {
  136. halSendSignal(SigInt0RlsLong);
  137. flagIgnoreRlsInt0 = true;
  138. Int0Time = 0;
  139. Int0Timer.stop();
  140. }
  141. }
  142. /**
  143. * Interrupt routine for Int1 (Bottom button)
  144. */
  145. void halInt1(void) {
  146. logger(V_HAL, "Int1 triggered\n");
  147. if (halGetInt1()) {
  148. if (flagIgnoreRlsInt1) {
  149. flagIgnoreRlsInt1 = false;
  150. } else {
  151. halSendSignal(SigInt1Rls);
  152. }
  153. } else {
  154. halSendSignal(SigInt1Psh);
  155. Int1Time = 0;
  156. Int1Timer.start();
  157. }
  158. }
  159. /*
  160. *
  161. */
  162. void halInt1TimerHandler(void) {
  163. Int1Time += 200;
  164. if (Int1Time >= (TIME_BUTTONLONGPRESS * 1000)) {
  165. halSendSignal(SigInt1RlsLong);
  166. flagIgnoreRlsInt1 = true;
  167. Int1Time = 0;
  168. Int1Timer.stop();
  169. }
  170. }
  171. /**
  172. * Interrupt routine for the flow sensor
  173. * It counts the edgdes and stores the value in flowcnt
  174. */
  175. void halIntFlow(void) {
  176. //halRelaisOff(RELAIS_POWER);
  177. logger(V_HAL, "IntFlow triggered #%d total: %.2fml\n", flowcnt,
  178. halGetFlow());
  179. if (flowcnt == 99) {
  180. halRelaisOff(RELAIS_PUMP);
  181. }
  182. flowcnt++;
  183. }
  184. /**
  185. * Interrupt routine for the pressure control
  186. */
  187. void halIntPressure(void) {
  188. logger(V_HAL, "IntPressure Control triggered\n");
  189. if (halIsHeating()) {
  190. halSendSignal(SigPressCls);
  191. } else {
  192. halSendSignal(SigPressOpn);
  193. }
  194. }
  195. /**
  196. * Method to handle toggle of the proximity sensor
  197. */
  198. void halIntProximity(void) {
  199. logger(V_HAL, "IntProximity triggered\n");
  200. if (halProxSensorCovered()) {
  201. halSendSignal(SigProxCvrd);
  202. } else {
  203. halSendSignal(SigProxOpn);
  204. }
  205. }
  206. /**
  207. * Returns total flow trough sensor in ml
  208. */
  209. float halGetFlow(void) {
  210. return flowcnt * FLOW_ML_PULSE;
  211. }
  212. /**
  213. * Resets the Flow counter
  214. */
  215. void halResetFlow(void) {
  216. logger(V_HAL, "Flow counter reset, amount so far: %.2f ml\n", halGetFlow());
  217. flowcnt = 0;
  218. }
  219. /**
  220. * Reads the status of the Pressure Control
  221. * @return 0 for closed Pressure Control(heating) and 1 for open
  222. */
  223. bool halIsHeating(void) {
  224. if (digitalRead(PIN_PRESSURE_CTRL) == 0) {
  225. return true;
  226. } else {
  227. return false;
  228. }
  229. }
  230. /**
  231. * Returns status of the proximity switch
  232. * @return 1 if the proximity switch is covered and 0 if uncovered
  233. */
  234. bool halProxSensorCovered(void) {
  235. //for legacy till sensor is installed
  236. /*if(digitalRead(PROXIMITY_SENSOR) == 0){
  237. return false;
  238. } else {
  239. return true;
  240. }*/
  241. return true;
  242. }
  243. /**
  244. * Returns the value of the top button Int0 (low active)
  245. * @return LOW or HIGH
  246. */
  247. int halGetInt0(void) {
  248. return digitalRead(PIN_INT0);
  249. }
  250. /**
  251. * Returns the value of the bottom button Int1 (low active)
  252. * @return LOW or HIGH
  253. */
  254. int halGetInt1(void) {
  255. return digitalRead(PIN_INT1);
  256. }
  257. /**
  258. * send Signal to coffee thread
  259. * @param val Integer value assigned to signal
  260. */
  261. void halSendSignal(HalSig val) {
  262. sigval value = { 0 };
  263. value.sival_int = (int) val;
  264. if (pthread_sigqueue(thread[THREAD_COFFEE], SIGUSR2, value)) {
  265. logger_error("Failed to queue signal %d %s", val, strerror(errno));
  266. //No Signals reach the state machine anymore...
  267. exit(EXIT_FAILURE);
  268. }
  269. }
  270. /**
  271. * Turn machine on
  272. */
  273. void halMachineOn(void) {
  274. halRelaisOn(RELAIS_HEAT);
  275. halRelaisOff(RELAIS_PUMP);
  276. halRelaisOn(RELAIS_POWER);
  277. logger(V_HAL, "Turning machine on\n");
  278. }
  279. /**
  280. * Turn machine off
  281. */
  282. void halMachineOff(void) {
  283. halRelaisOff(RELAIS_HEAT);
  284. halRelaisOff(RELAIS_PUMP);
  285. halRelaisOff(RELAIS_POWER);
  286. logger(V_HAL, "Turning machine off\n");
  287. }