/* * hal.cpp * * Created on: Aug 3, 2016 * Author: Philipp Hinz */ #include #include #include #include #include #include "lcd.h" #include "hal.h" #include "global.h" #include "logger.h" int flowcnt = 0; /** * Initializes HAL */ void halInit(void) { if (optPower) { halRelaisOn(RELAIS_HEAT); halRelaisOff(RELAIS_PUMP); halRelaisOn(RELAIS_POWER); } else { halRelaisOff(RELAIS_HEAT); halRelaisOff(RELAIS_PUMP); halRelaisOff(RELAIS_POWER); } pinMode(RELAIS_HEAT, OUTPUT); pinMode(RELAIS_PUMP, OUTPUT); pinMode(RELAIS_POWER, OUTPUT); pinMode(PRESSURE_CTRL, INPUT); pinMode(PROXIMITY_SENSOR, INPUT); pinMode(INT0, INPUT); pinMode(INT1, INPUT); pinMode(FLOW, INPUT); if (wiringPiISR(INT0, INT_EDGE_BOTH, &halInt0) < 0) { logger_error("Unable to setup ISR0: %s\n", strerror(errno)); return; } if (wiringPiISR(INT1, INT_EDGE_BOTH, &halInt1) < 0) { logger_error("Unable to setup ISR1: %s\n", strerror(errno)); return; } if (wiringPiISR(FLOW, INT_EDGE_FALLING, &halIntFlow) < 0) { logger_error("Unable to setup ISRFLOW: %s\n", strerror(errno)); return; } if(wiringPiISR(PRESSURE_CTRL, INT_EDGE_BOTH, &halIntPressure) < 0) { logger_error("Unable to setup ISRPressure: %s\n", strerror(errno)); return; } if(wiringPiISR(PROXIMITY_SENSOR, INT_EDGE_BOTH, &halIntProximity) < 0) { logger_error("Unable to setup ISRProximity: %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; } } /** * Returns the state of the relais relais * Returns HIGH when Relais is ON * @param relais Relais ID */ int halGetRelaisState(int relais) { switch (relais) { case RELAIS_POWER: case RELAIS_HEAT: case RELAIS_PUMP: return !digitalRead(relais); break; } return -1; } void halInt0(void) { logger(V_BASIC, "Int0 triggered\n"); if (halGetInt0()) { halSendSignal(SigInt0Rls); } else { halSendSignal(SigInt0Psh); } } void halInt1(void) { logger(V_BASIC, "Int1 triggered\n"); if (halGetInt1()) { halSendSignal(SigInt1Rls); } else { halSendSignal(SigInt1Psh); } } 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++; } /** * Method to handle toggle of the pressure control */ void halIntPressure(void) { logger(V_BASIC, "IntPressure Control triggered\n"); if (halIsHeating()) { halSendSignal(SigPressCls); } else { halSendSignal(SigPressOpn); } } /** * Method to handle toggle of the proximity sensor */ void halIntProximity(void) { logger(V_BASIC, "IntProximity triggered\n"); if (halProxSensorCovered()) { halSendSignal(SigProxCvrd); } else { halSendSignal(SigProxOpn); } } /** * Returns total flow trough sensor in ml */ float halGetFlow(void) { return flowcnt*FLOW_ML_PULSE; } /** * Resets the Flow counter */ void halResetFlow(void){ flowcnt = 0; } /** * Reads the status of the Pressure Control * @return 0 for closed Pressure Control(heating) and 1 for open */ bool halIsHeating(void) { if(digitalRead(PRESSURE_CTRL) == 0){ return true; } else { return false; } } /** * Returns status of the proximity switch * @return 1 if the proximity switch is covered and 0 if uncovered */ bool halProxSensorCovered(void){ /*if(digitalRead(PROXIMITY_SENSOR) == 0){ return false; } else { return true; }*/ return true; } /** * Returns the value of the top button Int0 (low active) * @return LOW or HIGH */ int halGetInt0(void){ return digitalRead(INT0); } /** * Returns the value of the bottom button Int1 (low active) * @return LOW or HIGH */ int halGetInt1(void){ return digitalRead(INT1); } /** * send Signal to coffee thread * @param val Integer value assigned to signal */ void halSendSignal(int val){ sigval value = {0}; value.sival_int = (int) val; if(pthread_sigqueue(thread[THREAD_COFFEE], SIGUSR2, value)) { logger_error("Failed to queue signal %d %s", val, strerror(errno)); //exit program? } }