#include "gefran_gtf.h"
#include "avrIOhelper/io-helper.h"
#include "temperature.h"
#include <stdint.h>
#include <stdio.h>

#include "mqtt.h"
extern Client mqtt_client;

uint8_t heizung_state = 0;
uint8_t heizung_ontemp = 0;
float heizung_setpoint = 40;

void do_heizung(){

    if(ADC_reading[1] > heizung_setpoint)
        heizung_ontemp = 1;
    if(ADC_reading[1] < heizung_setpoint)
        heizung_ontemp = 0;

    if(read_Input(BTN_HEIZEN_PLUS, LEVEL) && heizung_setpoint < 80){
        heizung_setpoint += 0.2;
        send_value(&mqtt_client, "/Filamentanlage/02_Wasserbecken/state/heizung_sp", heizung_setpoint);
    }

    if(read_Input(BTN_HEIZEN_MINUS, LEVEL) && heizung_setpoint > 5){
        heizung_setpoint -= 0.2;
        send_value(&mqtt_client, "/Filamentanlage/02_Wasserbecken/state/heizung_sp", heizung_setpoint);
    }

    if(read_Input(BTN_HEIZEN_AN, LEVEL)){
        heizung_state = 1;
        send_value(&mqtt_client, "/Filamentanlage/02_Wasserbecken/state/heizung", 1);
    }

    if(read_Input(BTN_HEIZEN_AUS, LEVEL)){
        send_value(&mqtt_client, "/Filamentanlage/02_Wasserbecken/state/heizung", 0);
        heizung_state = 0;
    }
}

int8_t comm_heizung(){
    int8_t res = 0;
    if(heizung_state && !heizung_ontemp){
        res |= gtf_write_register(5, 56, 500);
        res |= gtf_write_register(6, 56, 500);
        res |= gtf_write_register(7, 56, 500);
        set_Output(LED_HEIZUNG, BLINK);
    }
    else if(heizung_state && heizung_ontemp){
        res |= gtf_write_register(5, 56, 0);
        res |= gtf_write_register(6, 56, 0);
        res |= gtf_write_register(7, 56, 0);
        set_Output(LED_HEIZUNG, ON);
    }
    else{
        res |= gtf_write_register(5, 56, 0);
        res |= gtf_write_register(6, 56, 0);
        res |= gtf_write_register(7, 56, 0);
        set_Output(LED_HEIZUNG, OFF);
    }
    return res;
}
