/*
 * File:   menu.c
 * Author: Vojta
 *
 * Created on 2. zá?í 2022, 20:25
 */


#include "hlava.h"


void menu(void)
{
    if(!fire)
    {
        if(!tl_min || !tl_plus || !tl_sel)
        {
            if(butt_press == 0) butt_count=0; //vynuluj ze zacatku kdyz je presnute tlacitko 
            butt_press = 1;         
        }

        if (butt_press == 1) //funkce pro debounce, potvrzeni a zrychleneho pricitani nebo odecitani 
        {
            if(butt_count >= 60 && butt_watch == 0)
            {             
                operace1 = butt_sel();
                butt_watch = 1;
                oper_sel(operace1,mode1);
                
            } 
            
            if(butt_count >= 1000 && butt_watch == 1 && ( operace1 == minus || operace1 == plus ))
            {
                oper_sel(operace1,mode1);
                butt_count = 970;
            }
            
            if(butt_count >= 1000 && butt_watch == 1 && operace1 == potvrzeni) //pro potvrzení noveho coilu aby to na?etlo odpor R0
            {
                mode1--;
                operace1 = nic;
                seg3_tri_pomlcky();
            }
            
            if(butt_count >= 1000 && butt_watch == 1 && operace1 == select) //pro nacteni noveho odporu R0 po vyzhaveni nove spiralky
            {
                mode1 -=2;
                operace1 = nic;
                zmena=1;
            }          

            if( ( (operace1 == novy_odpor || operace1 == nic) && butt_watch == 1 ) || ( butt_watch == 1 && ( ( tl_min && operace1 == minus ) || ( tl_plus && operace1 == plus ) || ( tl_sel && (operace1 == select  ) ) ) ) ) //konec stisku tlacitka
            {
                butt_press = 0;
                butt_count = 0;
                butt_watch = 0;

                // ulozeni zmenenych hodnot a nastaveni hodnot po skonceni meneni tech hodnot
                if((tl_min && operace1 == minus) || (tl_plus && operace1 == plus))
                {
                 write_char_eeprom(vykon,adr_vykon);
                write_int_eeprom(tc_teplota,adr_teplota);    
                TC_AND_VYKON_reinit();                   
                }
            }
        }

       if (tc_teplota < 50 || coil_res0 >= 36000)//nemenit poradiiii
       {
           tc_teplota = 40;
           TC_ON = 0;

        }else TC_ON = 1;    
        
        if(operace1 == novy_odpor) //nemenit poradiii
        {
           TC_AND_VYKON_reinit();
           operace1 = nic;
        }

        mode_display(mode1);
    }
    else seg3_zhav();
    
}

operace butt_sel(void)
{
    if(!tl_min) return minus;
    else if(!tl_plus) return plus;
    else if(!tl_sel && !zmena) return select;
    else if(!tl_sel && zmena) return potvrzeni;
    else return nic;
}

void oper_sel(operace operace0, mode modes)
{
    if(operace0 == select)
    {
        if(mode1 <= 2) mode1++; else mode1 = 0; //aby to zacalo zase zobrazovat od prvniho modu kdyz se dostanu na konec
    }
    else if(modes == watt || modes == tc)
        {
            if(operace0 == minus)
            {
                if(modes == watt)
                {
                    if(vykon > 0) vykon--;
                    else vykon = 0;   
                }
                
                if(modes == tc)
                {
                    if(tc_teplota >= 50) 
                    {
                        tc_teplota -= 10;
                        
                    }
                }
            }

            if(operace0 == plus)
            {
                if(modes == watt)
                 {
                    unsigned char max_vykon0 = max_vykon();
                     if(vykon < max_vykon0) vykon++;
                     else vykon = max_vykon0;    
                 }

                 if(modes == tc)
                 {
                     if(tc_teplota < 400) tc_teplota += 10;
                     else tc_teplota = 400;      
                 } 
             }
        }
  
}

void mode_display(mode modes)
{
    switch(modes)
    {
            case ohm : 
                if(!zmena) seg3_print(coil_res_disp1,1); 
                else seg3_print(999,1); break;
            case watt : seg3_print(vykon,0); break;
            case tc : 
                if(TC_ON) seg3_print(tc_teplota,0); 
                else seg3_tri_pomlcky(); 
                break;  
            case battery : seg3_print(V_bat,1); break;
            
            default: break;
    }
            
}




