/* * hal.cpp * * Created on: Aug 3, 2016 * Author: Philipp Hinz */ #include #include #include #include #include "lcd.h" #include "hal.h" #include "global.h" #include "logger.h" int flowcnt = 0; /* * Initializes HAL */ void halInit(void) { halRelaisOn(RELAIS_HEAT); halRelaisOff(RELAIS_PUMP); halRelaisOn(RELAIS_POWER); pinMode(RELAIS_HEAT, OUTPUT); pinMode(RELAIS_PUMP, OUTPUT); pinMode(RELAIS_POWER, OUTPUT); pinMode(HAL_INT0, INPUT); pinMode(HAL_INT1, INPUT); pinMode(HAL_FLOW, INPUT); if (wiringPiISR(HAL_INT0, INT_EDGE_FALLING, &halInt0) < 0) { logger_error("Unable to setup ISR0: %s\n", strerror(errno)); return; } if (wiringPiISR(HAL_INT1, INT_EDGE_FALLING, &halInt1) < 0) { logger_error("Unable to setup ISR1: %s\n", strerror(errno)); return; } if (wiringPiISR(HAL_FLOW, INT_EDGE_FALLING, &halIntFlow) < 0) { logger_error("Unable to setup ISRFLOW: %s\n", strerror(errno)); return; } } /* * Switches relais on * @param relais Relais ID */ void halRelaisOn(int relais) { halRelaisSet(relais, LOW); } /* * Switches relais off * @param relais Relais ID */ void halRelaisOff(int relais) { halRelaisSet(relais, HIGH); } /* * Switches relais to state * @param relais Relais ID * @param state LOW(0) or HIGH(1) */ void halRelaisSet(int relais, int state) { if (state != HIGH && state != LOW) return; switch (relais) { case RELAIS_POWER: case RELAIS_HEAT: case RELAIS_PUMP: digitalWrite(relais, state); break; } } void halInt0(void) { //halRelaisOn(RELAIS_POWER); logger(V_BASIC, "Int0 triggered\n"); flowcnt = 0; halRelaisOn(RELAIS_PUMP); } void halInt1(void) { //halRelaisOff(RELAIS_POWER); logger(V_BASIC, "Int1 triggered\n"); flowcnt = 0; } void halIntFlow(void) { //halRelaisOff(RELAIS_POWER); logger(V_BASIC, "IntFlow triggered #%d total: %.2fml\n", flowcnt, halGetFlow()); lcdPosition(lcd, 0, 0); lcdPrintf(lcd, "ml = %.2f ", halGetFlow()); if (flowcnt == 99) { halRelaisOff(RELAIS_PUMP); } flowcnt++; } /* * Returns total flow trough sensor in ml */ float halGetFlow(void) { return flowcnt*FLOW_ML_PULSE; }