hal.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. int flowcnt = 0;
  17. /**
  18. * Initializes HAL
  19. */
  20. void halInit(void) {
  21. if (optPower) {
  22. halRelaisOn(RELAIS_HEAT);
  23. halRelaisOff(RELAIS_PUMP);
  24. halRelaisOn(RELAIS_POWER);
  25. } else {
  26. halRelaisOff(RELAIS_HEAT);
  27. halRelaisOff(RELAIS_PUMP);
  28. halRelaisOff(RELAIS_POWER);
  29. }
  30. pinMode(RELAIS_HEAT, OUTPUT);
  31. pinMode(RELAIS_PUMP, OUTPUT);
  32. pinMode(RELAIS_POWER, OUTPUT);
  33. pinMode(PRESSURE_CTRL, INPUT);
  34. pinMode(PROXIMITY_SENSOR, INPUT);
  35. pinMode(INT0, INPUT);
  36. pinMode(INT1, INPUT);
  37. pinMode(FLOW, INPUT);
  38. if (wiringPiISR(INT0, INT_EDGE_BOTH, &halInt0) < 0) {
  39. logger_error("Unable to setup ISR0: %s\n", strerror(errno));
  40. return;
  41. }
  42. if (wiringPiISR(INT1, INT_EDGE_BOTH, &halInt1) < 0) {
  43. logger_error("Unable to setup ISR1: %s\n", strerror(errno));
  44. return;
  45. }
  46. if (wiringPiISR(FLOW, INT_EDGE_FALLING, &halIntFlow) < 0) {
  47. logger_error("Unable to setup ISRFLOW: %s\n", strerror(errno));
  48. return;
  49. }
  50. if(wiringPiISR(PRESSURE_CTRL, INT_EDGE_BOTH, &halIntPressure) < 0) {
  51. logger_error("Unable to setup ISRPressure: %s\n", strerror(errno));
  52. return;
  53. }
  54. if(wiringPiISR(PROXIMITY_SENSOR, INT_EDGE_BOTH, &halIntProximity) < 0) {
  55. logger_error("Unable to setup ISRProximity: %s\n", strerror(errno));
  56. return;
  57. }
  58. }
  59. /**
  60. * Switches relais on
  61. * @param relais Relais ID
  62. */
  63. void halRelaisOn(int relais) {
  64. halRelaisSet(relais, LOW);
  65. }
  66. /**
  67. * Switches relais off
  68. * @param relais Relais ID
  69. */
  70. void halRelaisOff(int relais) {
  71. halRelaisSet(relais, HIGH);
  72. }
  73. /**
  74. * Switches relais to state
  75. * @param relais Relais ID
  76. * @param state LOW(0) or HIGH(1)
  77. */
  78. void halRelaisSet(int relais, int state) {
  79. if (state != HIGH && state != LOW)
  80. return;
  81. switch (relais) {
  82. case RELAIS_POWER:
  83. case RELAIS_HEAT:
  84. case RELAIS_PUMP:
  85. //digitalWrite(relais, state);
  86. break;
  87. }
  88. }
  89. /**
  90. * Returns the state of the relais relais
  91. * Returns HIGH when Relais is ON
  92. * @param relais Relais ID
  93. */
  94. int halGetRelaisState(int relais) {
  95. switch (relais) {
  96. case RELAIS_POWER:
  97. case RELAIS_HEAT:
  98. case RELAIS_PUMP:
  99. return !digitalRead(relais);
  100. break;
  101. }
  102. return -1;
  103. }
  104. void halInt0(void) {
  105. logger(V_BASIC, "Int0 triggered\n");
  106. if (halGetInt0()) {
  107. halSendSignal(SigInt0Rls);
  108. } else {
  109. halSendSignal(SigInt0Psh);
  110. }
  111. }
  112. void halInt1(void) {
  113. logger(V_BASIC, "Int1 triggered\n");
  114. if (halGetInt1()) {
  115. halSendSignal(SigInt1Rls);
  116. } else {
  117. halSendSignal(SigInt1Psh);
  118. }
  119. }
  120. void halIntFlow(void) {
  121. //halRelaisOff(RELAIS_POWER);
  122. logger(V_BASIC, "IntFlow triggered #%d total: %.2fml\n", flowcnt, halGetFlow());
  123. lcdPosition(lcd, 0, 0);
  124. lcdPrintf(lcd, "ml = %.2f ", halGetFlow());
  125. if (flowcnt == 99) {
  126. halRelaisOff(RELAIS_PUMP);
  127. }
  128. flowcnt++;
  129. }
  130. /**
  131. * Method to handle toggle of the pressure control
  132. */
  133. void halIntPressure(void) {
  134. logger(V_BASIC, "IntPressure Control triggered\n");
  135. if (halIsHeating()) {
  136. halSendSignal(SigPressCls);
  137. } else {
  138. halSendSignal(SigPressOpn);
  139. }
  140. }
  141. /**
  142. * Method to handle toggle of the proximity sensor
  143. */
  144. void halIntProximity(void) {
  145. logger(V_BASIC, "IntProximity triggered\n");
  146. if (halProxSensorCovered()) {
  147. halSendSignal(SigProxCvrd);
  148. } else {
  149. halSendSignal(SigProxOpn);
  150. }
  151. }
  152. /**
  153. * Returns total flow trough sensor in ml
  154. */
  155. float halGetFlow(void) {
  156. return flowcnt*FLOW_ML_PULSE;
  157. }
  158. /**
  159. * Resets the Flow counter
  160. */
  161. void halResetFlow(void){
  162. flowcnt = 0;
  163. }
  164. /**
  165. * Reads the status of the Pressure Control
  166. * @return 0 for closed Pressure Control(heating) and 1 for open
  167. */
  168. bool halIsHeating(void) {
  169. if(digitalRead(PRESSURE_CTRL) == 0){
  170. return true;
  171. } else {
  172. return false;
  173. }
  174. }
  175. /**
  176. * Returns status of the proximity switch
  177. * @return 1 if the proximity switch is covered and 0 if uncovered
  178. */
  179. bool halProxSensorCovered(void){
  180. /*if(digitalRead(PROXIMITY_SENSOR) == 0){
  181. return false;
  182. } else {
  183. return true;
  184. }*/
  185. return true;
  186. }
  187. /**
  188. * Returns the value of the top button Int0 (low active)
  189. * @return LOW or HIGH
  190. */
  191. int halGetInt0(void){
  192. return digitalRead(INT0);
  193. }
  194. /**
  195. * Returns the value of the bottom button Int1 (low active)
  196. * @return LOW or HIGH
  197. */
  198. int halGetInt1(void){
  199. return digitalRead(INT1);
  200. }
  201. /**
  202. * send Signal to coffee thread
  203. * @param val Integer value assigned to signal
  204. */
  205. void halSendSignal(int val){
  206. sigval value = {0};
  207. value.sival_int = (int) val;
  208. if(pthread_sigqueue(thread[THREAD_COFFEE], SIGUSR2, value)) {
  209. logger_error("Failed to queue signal %d %s", val, strerror(errno));
  210. //exit program?
  211. }
  212. }