#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>

#include "pcd8544.h"
#include "main.h"
#include "panel.h"
#include "twi_lib.h"
#include "timelib.h"
#include "uart.h"
#include"mylib.h"

//____SD card________

#include "fat.h"
#include "fat_config.h"
#include "partition.h"
#include "sd_raw.h"
#include "sd_raw_config.h"
#include "measuring.h"
#include "adc.h"

//________________Globální proměnný pro nastavení začátku měření a spuštění vzorkování
unsigned char gmStart=0;
unsigned char gmGetSample=0;

unsigned char gmSmazat=0;

int main()
{

    unsigned int mLoopCycle;
    configure_pin_ss();
    LcdInit();

    sMenu Menu;
    Menu.Radek = 1;
    Menu.Uroven = 1;
    Menu.AktualDelka = 0;
    Menu.Offset = 1;
    Menu.Bottom = 1;
    Menu.Error = 0;
    Menu.PocetPolozek = 0;
    Menu.Orientation = Vertical;
    Menu.Contrast = eeprom_read_byte((uint8_t*)1);

    if (Menu.Contrast == 0xFF)
        Menu.Contrast = 60;

    LcdContrast ( Menu.Contrast );

    Menu.Brightness = eeprom_read_byte((uint8_t*)2);
    PwmInit();
    OCR2 = Menu.Brightness;

//_______________________UART INIT____________________________

    uart_init();

    sSdCard mSdCard;

    if (sd_raw_available())
    {
        LcdClear();
        LcdGotoXYFont(2,2);
        LcdFStr(FONT_1X,(unsigned char*)PSTR("Mady (by)"));
        LcdGotoXYFont(8,4);
        LcdFStr(FONT_1X,(unsigned char*)PSTR("Byvoko"));
        LcdUpdate();
    }
    if(!sd_raw_init())
    {
        if (!sd_raw_available())
        {
            LcdClear();
            LcdImage(&sdinsert);
            LcdUpdate();
        }
        while (!sd_raw_init());
    }
    if (!MySDcard_init(&mSdCard))
        SoftReset();

    //________________ I2C init_________________

            i2c_init();
            //sei();
            sTime Cas;
           // PORTE |=_BV( PE7 );

    //________________ Nastaveni Panelu_________________

    KlavPort |= _BV( KlavDow ) | _BV( KlavUp ) | _BV( KlavLef ) | _BV( KlavRig ) | _BV( KlavOk );

    //________________ Povoleni nabijeni baterie________

    DDRA |= _BV( PA6 );
    PORTA |= _BV( PA6 );


    DDRC |= _BV( PC3 ) | _BV( PC4 ) | _BV( PC5 ) | _BV( PC6 );
    PORTC |=  _BV( PC6 );
    //________________ Nastaveni ADC__________________

    ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
    // ADC Enable and prescaler of 128

    //________________ Init Measuring_________________

    sMeasuring Measuring;
    Measuring.Init = 0;

uart_puts_p(PSTR("Main se hlasi\n"));
    for(;;)
    {

        mLoopCycle = 0;
        GetTime(&Cas);
        if (Measuring.Init)
        {
            if ( !LoadConfig(&Measuring) )        //dodělat zpětnou vazbu
            {
                Menu.Radek = 1;
                Menu.Uroven = uHlavni;
                Menu.AktualDelka = 3;
                Menu.Error = 1;
            }
            if (!MakeHeader(&Measuring,&mSdCard))
                uart_puts_p(PSTR("Chyba zapisu"));

        }
        LcdClear();
        DrawMenu(&Menu,Cas,&mSdCard);
        LcdUpdate();
        while (!PanelLoop(&Menu,&mSdCard,&Measuring))
        {
            if (!sd_raw_available())
                SoftReset();
            _delay_ms(1);
            mLoopCycle++;
            if (mLoopCycle==100)
                break;
        }
    }


    return 0;
}
