123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- /*
- * hal.cpp
- *
- * Created on: Aug 3, 2016
- * Author: Philipp Hinz
- */
- #include <wiringPi.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
- #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(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;
- }
- /*
- * Reads the status of the Pressure Control
- * @return 1 for closed Pressure Control and 0 for open
- */
- int halGetPressureCtrl(void) {
- if (digitalRead(PRESSURE_CTRL)) {
- return 0;
- } else
- return 1;
- }
- /*
- * Returns status of the proximity switch
- * @return 1 if the proximity switch is covered and 0 if uncovered
- */
- int halGetProximitySensor(void){
- return digitalRead(PROXIMITY_SENSOR);
- }
|