hal.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 "lcd.h"
  13. #include "hal.h"
  14. #include "global.h"
  15. #include "logger.h"
  16. #include "timer.h"
  17. int flowcnt = 0;
  18. int Int0Time;
  19. int Int1Time;
  20. bool flagIgnoreRlsInt0;
  21. bool flagIgnoreRlsInt1;
  22. timer Int0Timer(&halInt0TimerHandler);
  23. timer Int1Timer(&halInt1TimerHandler);
  24. /**
  25. * Initializes HAL
  26. */
  27. void halInit(void) {
  28. if (optPower) {
  29. halMachineOn();
  30. } else {
  31. halMachineOff();
  32. }
  33. pinMode(RELAIS_HEAT, OUTPUT);
  34. pinMode(RELAIS_PUMP, OUTPUT);
  35. pinMode(RELAIS_POWER, OUTPUT);
  36. pinMode(PIN_PRESSURE_CTRL, INPUT);
  37. pinMode(PIN_PROXIMITY_SENSOR, INPUT);
  38. pinMode(PIN_INT0, INPUT);
  39. pinMode(PIN_INT1, INPUT);
  40. pinMode(PIN_FLOW, INPUT);
  41. Int0Timer.setDivider(4); //200ms
  42. Int1Timer.setDivider(4);
  43. Int0Time = 0;
  44. Int1Time = 0;
  45. flagIgnoreRlsInt0 = false;
  46. flagIgnoreRlsInt1 = false;
  47. if (wiringPiISR(PIN_INT0, INT_EDGE_BOTH, &halInt0) < 0) {
  48. logger_error("Unable to setup ISR0: %s\n", strerror(errno));
  49. return;
  50. }
  51. if (wiringPiISR(PIN_INT1, INT_EDGE_BOTH, &halInt1) < 0) {
  52. logger_error("Unable to setup ISR1: %s\n", strerror(errno));
  53. return;
  54. }
  55. if (wiringPiISR(PIN_FLOW, INT_EDGE_FALLING, &halIntFlow) < 0) {
  56. logger_error("Unable to setup ISRFLOW: %s\n", strerror(errno));
  57. return;
  58. }
  59. if (wiringPiISR(PIN_PRESSURE_CTRL, INT_EDGE_BOTH, &halIntPressure) < 0) {
  60. logger_error("Unable to setup ISRPressure: %s\n", strerror(errno));
  61. return;
  62. }
  63. if (wiringPiISR(PIN_PROXIMITY_SENSOR, INT_EDGE_BOTH, &halIntProximity) < 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_BASIC, "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_BASIC, "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) {
  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_BASIC, "IntFlow triggered #%d total: %.2fml\n", flowcnt, halGetFlow());
  178. lcdPosition(lcd, 0, 0);
  179. lcdPrintf(lcd, "ml = %.2f ", halGetFlow());
  180. if (flowcnt == 99) {
  181. halRelaisOff(RELAIS_PUMP);
  182. }
  183. flowcnt++;
  184. }
  185. /**
  186. * Interrupt routine for the pressure control
  187. */
  188. void halIntPressure(void) {
  189. logger(V_BASIC, "IntPressure Control triggered\n");
  190. if (halIsHeating()) {
  191. halSendSignal(SigPressCls);
  192. } else {
  193. halSendSignal(SigPressOpn);
  194. }
  195. }
  196. /**
  197. * Method to handle toggle of the proximity sensor
  198. */
  199. void halIntProximity(void) {
  200. logger(V_BASIC, "IntProximity triggered\n");
  201. if (halProxSensorCovered()) {
  202. halSendSignal(SigProxCvrd);
  203. } else {
  204. halSendSignal(SigProxOpn);
  205. }
  206. }
  207. /**
  208. * Returns total flow trough sensor in ml
  209. */
  210. float halGetFlow(void) {
  211. return flowcnt * FLOW_ML_PULSE;
  212. }
  213. /**
  214. * Resets the Flow counter
  215. */
  216. void halResetFlow(void) {
  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(int 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. }
  278. /**
  279. * Turn machine off
  280. */
  281. void halMachineOff(void) {
  282. halRelaisOff(RELAIS_HEAT);
  283. halRelaisOff(RELAIS_PUMP);
  284. halRelaisOff(RELAIS_POWER);
  285. }