hal.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. void halInit(void) {
  17. halRelaisOn(RELAIS_HEAT);
  18. halRelaisOff(RELAIS_PUMP);
  19. halRelaisOn(RELAIS_POWER);
  20. pinMode(RELAIS_HEAT, OUTPUT);
  21. pinMode(RELAIS_PUMP, OUTPUT);
  22. pinMode(RELAIS_POWER, OUTPUT);
  23. pinMode(HAL_INT0, INPUT);
  24. pinMode(HAL_INT1, INPUT);
  25. pinMode(HAL_FLOW, INPUT);
  26. if (wiringPiISR(HAL_INT0, INT_EDGE_FALLING, &halInt0) < 0) {
  27. logger_error("Unable to setup ISR0: %s\n", strerror(errno));
  28. return;
  29. }
  30. if (wiringPiISR(HAL_INT1, INT_EDGE_FALLING, &halInt1) < 0) {
  31. logger_error("Unable to setup ISR1: %s\n", strerror(errno));
  32. return;
  33. }
  34. if (wiringPiISR(HAL_FLOW, INT_EDGE_FALLING, &halIntFlow) < 0) {
  35. logger_error("Unable to setup ISRFLOW: %s\n", strerror(errno));
  36. return;
  37. }
  38. }
  39. void halRelaisOn(int relais) {
  40. halRelaisSet(relais, LOW);
  41. }
  42. void halRelaisOff(int relais) {
  43. halRelaisSet(relais, HIGH);
  44. }
  45. void halRelaisSet(int relais, int state) {
  46. if (state != HIGH && state != LOW)
  47. return;
  48. switch (relais) {
  49. case RELAIS_POWER:
  50. case RELAIS_HEAT:
  51. case RELAIS_PUMP:
  52. digitalWrite(relais, state);
  53. break;
  54. }
  55. }
  56. void halInt0(void) {
  57. //halRelaisOn(RELAIS_POWER);
  58. logger(V_BASIC, "Int0 triggered\n");
  59. flowcnt = 0;
  60. halRelaisOn(RELAIS_PUMP);
  61. }
  62. void halInt1(void) {
  63. //halRelaisOff(RELAIS_POWER);
  64. logger(V_BASIC, "Int1 triggered\n");
  65. }
  66. void halIntFlow(void) {
  67. //halRelaisOff(RELAIS_POWER);
  68. logger(V_BASIC, "IntFlow triggered #%d total: %f\n", flowcnt, halGetFlow());
  69. lcdPosition(lcd, 0, 0);
  70. lcdPrintf(lcd, "ml = %.2f ", halGetFlow());
  71. if (flowcnt == 99) {
  72. halRelaisOff(RELAIS_PUMP);
  73. }
  74. flowcnt++;
  75. }
  76. float halGetFlow(void) {
  77. return flowcnt*FLOW_ML_PULSE;
  78. }