hal.cpp 2.1 KB

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