hal.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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(HAL_INT0, INPUT);
  33. pinMode(HAL_INT1, INPUT);
  34. pinMode(HAL_FLOW, INPUT);
  35. if (wiringPiISR(HAL_INT0, INT_EDGE_FALLING, &halInt0) < 0) {
  36. logger_error("Unable to setup ISR0: %s\n", strerror(errno));
  37. return;
  38. }
  39. if (wiringPiISR(HAL_INT1, INT_EDGE_FALLING, &halInt1) < 0) {
  40. logger_error("Unable to setup ISR1: %s\n", strerror(errno));
  41. return;
  42. }
  43. if (wiringPiISR(HAL_FLOW, INT_EDGE_FALLING, &halIntFlow) < 0) {
  44. logger_error("Unable to setup ISRFLOW: %s\n", strerror(errno));
  45. return;
  46. }
  47. }
  48. /*
  49. * Switches relais on
  50. * @param relais Relais ID
  51. */
  52. void halRelaisOn(int relais) {
  53. halRelaisSet(relais, LOW);
  54. }
  55. /*
  56. * Switches relais off
  57. * @param relais Relais ID
  58. */
  59. void halRelaisOff(int relais) {
  60. halRelaisSet(relais, HIGH);
  61. }
  62. /*
  63. * Switches relais to state
  64. * @param relais Relais ID
  65. * @param state LOW(0) or HIGH(1)
  66. */
  67. void halRelaisSet(int relais, int state) {
  68. if (state != HIGH && state != LOW)
  69. return;
  70. switch (relais) {
  71. case RELAIS_POWER:
  72. case RELAIS_HEAT:
  73. case RELAIS_PUMP:
  74. digitalWrite(relais, state);
  75. break;
  76. }
  77. }
  78. void halInt0(void) {
  79. //halRelaisOn(RELAIS_POWER);
  80. logger(V_BASIC, "Int0 triggered\n");
  81. flowcnt = 0;
  82. halRelaisOn(RELAIS_PUMP);
  83. }
  84. void halInt1(void) {
  85. //halRelaisOff(RELAIS_POWER);
  86. logger(V_BASIC, "Int1 triggered\n");
  87. flowcnt = 0;
  88. }
  89. void halIntFlow(void) {
  90. //halRelaisOff(RELAIS_POWER);
  91. logger(V_BASIC, "IntFlow triggered #%d total: %.2fml\n", flowcnt, halGetFlow());
  92. lcdPosition(lcd, 0, 0);
  93. lcdPrintf(lcd, "ml = %.2f ", halGetFlow());
  94. if (flowcnt == 99) {
  95. halRelaisOff(RELAIS_PUMP);
  96. }
  97. flowcnt++;
  98. }
  99. /*
  100. * Returns total flow trough sensor in ml
  101. */
  102. float halGetFlow(void) {
  103. return flowcnt*FLOW_ML_PULSE;
  104. }