#include <avr/io.h>
#include "avrIOhelper/io-helper.h"

uint16_t abzug_speed = 100;

void timer3_init()
{
    TCCR3A |= (1<<COM3B1);
    TCCR3B |= _BV(WGM33);

    ICR3 = 100;
    OCR3B = 50;

    DDRE |= 1 << 4;
}


void send_info(void);
void do_abzug(){

    if (read_Input(BTN_ABZUG_EIN, RISING)) {
        TCCR3B |= _BV(CS31);
        set_Output(LED_ABZUG, ON);
#if PLC_MQTT_ENABLED
        send_info();
#endif
    }

    if (read_Input(BTN_ABZUG_AUS, RISING)) {
        TCCR3B &= ~(_BV(CS31));
        set_Output(LED_ABZUG, OFF);
#if PLC_MQTT_ENABLED
        send_info();
#endif
    }

    if (read_Input(BTN_ABZUG_PLUS, RISING)) {
        if(abzug_speed <= 900)
            abzug_speed += 100;
        else
            abzug_speed = 1000;
#if PLC_MQTT_ENABLED
        send_info();
#endif
    }

    if (read_Input(BTN_ABZUG_MINUS, RISING)) {
        if(abzug_speed >= 110)
            abzug_speed -= 100;
        else
            abzug_speed = 10;
#if PLC_MQTT_ENABLED
        send_info();
#endif
    }

    if (read_Input(BTN_ABZUG_PLUS_FEIN, RISING)) {
        if(abzug_speed <= 990)
            abzug_speed += 10;
        else
            abzug_speed = 1000;
#if PLC_MQTT_ENABLED
        send_info();
#endif
    }

    if (read_Input(BTN_ABZUG_MINUS_FEIN, RISING)) {
        if(abzug_speed >= 20)
            abzug_speed -= 10;
        else
            abzug_speed = 10;
#if PLC_MQTT_ENABLED
        send_info();
#endif
    }

    // 16000000/8

    ICR3 = ((1.0/abzug_speed)*3.14*42.0*(1.0/5.0))*2.0*500.0*(1/1.03);
    OCR3B = ICR3/2;
}


