hal.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "lcd.h"
  12. #include "hal.h"
  13. #include "global.h"
  14. #include "logger.h"
  15. int flowcnt = 0;
  16. /*
  17. * Initializes HAL
  18. */
  19. void halInit(void) {
  20. if (optPower) {
  21. halRelaisOn(RELAIS_HEAT);
  22. halRelaisOff(RELAIS_PUMP);
  23. halRelaisOn(RELAIS_POWER);
  24. } else {
  25. halRelaisOff(RELAIS_HEAT);
  26. halRelaisOff(RELAIS_PUMP);
  27. halRelaisOff(RELAIS_POWER);
  28. }
  29. pinMode(RELAIS_HEAT, OUTPUT);
  30. pinMode(RELAIS_PUMP, OUTPUT);
  31. pinMode(RELAIS_POWER, OUTPUT);
  32. pinMode(PRESSURE_CTRL, INPUT);
  33. pinMode(PROXIMITY_SENSOR, INPUT);
  34. pinMode(HAL_INT0, INPUT);
  35. pinMode(HAL_INT1, INPUT);
  36. pinMode(HAL_FLOW, INPUT);
  37. if (wiringPiISR(HAL_INT0, INT_EDGE_FALLING, &halInt0) < 0) {
  38. logger_error("Unable to setup ISR0: %s\n", strerror(errno));
  39. return;
  40. }
  41. if (wiringPiISR(HAL_INT1, INT_EDGE_FALLING, &halInt1) < 0) {
  42. logger_error("Unable to setup ISR1: %s\n", strerror(errno));
  43. return;
  44. }
  45. if (wiringPiISR(HAL_FLOW, INT_EDGE_FALLING, &halIntFlow) < 0) {
  46. logger_error("Unable to setup ISRFLOW: %s\n", strerror(errno));
  47. return;
  48. }
  49. }
  50. /*
  51. * Switches relais on
  52. * @param relais Relais ID
  53. */
  54. void halRelaisOn(int relais) {
  55. halRelaisSet(relais, LOW);
  56. }
  57. /*
  58. * Switches relais off
  59. * @param relais Relais ID
  60. */
  61. void halRelaisOff(int relais) {
  62. halRelaisSet(relais, HIGH);
  63. }
  64. /*
  65. * Switches relais to state
  66. * @param relais Relais ID
  67. * @param state LOW(0) or HIGH(1)
  68. */
  69. void halRelaisSet(int relais, int state) {
  70. if (state != HIGH && state != LOW)
  71. return;
  72. switch (relais) {
  73. case RELAIS_POWER:
  74. case RELAIS_HEAT:
  75. case RELAIS_PUMP:
  76. digitalWrite(relais, state);
  77. break;
  78. }
  79. }
  80. void halInt0(void) {
  81. //halRelaisOn(RELAIS_POWER);
  82. logger(V_BASIC, "Int0 triggered\n");
  83. flowcnt = 0;
  84. halRelaisOn(RELAIS_PUMP);
  85. }
  86. void halInt1(void) {
  87. //halRelaisOff(RELAIS_POWER);
  88. logger(V_BASIC, "Int1 triggered\n");
  89. flowcnt = 0;
  90. }
  91. void halIntFlow(void) {
  92. //halRelaisOff(RELAIS_POWER);
  93. logger(V_BASIC, "IntFlow triggered #%d total: %.2fml\n", flowcnt, halGetFlow());
  94. lcdPosition(lcd, 0, 0);
  95. lcdPrintf(lcd, "ml = %.2f ", halGetFlow());
  96. if (flowcnt == 99) {
  97. halRelaisOff(RELAIS_PUMP);
  98. }
  99. flowcnt++;
  100. }
  101. /*
  102. * Returns total flow trough sensor in ml
  103. */
  104. float halGetFlow(void) {
  105. return flowcnt*FLOW_ML_PULSE;
  106. }
  107. /*
  108. * Reads the status of the Pressure Control
  109. * @return 1 for closed Pressure Control and 0 for open
  110. */
  111. int halGetPressureCtrl(void) {
  112. if (digitalRead(PRESSURE_CTRL)) {
  113. return 0;
  114. } else
  115. return 1;
  116. }
  117. /*
  118. * Returns status of the proximity switch
  119. * @return 1 if the proximity switch is covered and 0 if uncovered
  120. */
  121. int halGetProximitySensor(void){
  122. return digitalRead(PROXIMITY_SENSOR);
  123. }