//#include "atmega_IO.h"
#include <mega162.h>
#include <mega162_bits.h>
#include "communication.h"
#include "initialization.h"

//Globalni promenne
char programMemory = 0x01;
char switchMemory = 0x01;
char baudRateXBee = 0x03;  //komunikace s XBee
char parityXBee = 0x00;  //komunikace s XBee
char baudRateSensor = 0x03;  //komunikace se senzorem
char paritySensor = 0x00;  //komunikace se senzorem
char memory = 0x00;  //pro pamatovani zda-li prisly znaky O, K, E, R
char stackForSensor [32];  //zasobnik dat pro senzor
char countStackForSensor = 0;  //pocitadlo dat v zasobniku pro senzor
char stackForPC [32];  //zasobnik dat pro PC
char countStackForPC = 0;  //pocitadlo dat v zasobniku pro PC
char memorySPISend = 0;  //memory pro SPI

interrupt [SPI_STC] void recieve_sensor_SPI (void)  //preruseni pri prijmu dat ze Senzoru z SPI
{
    SREG.7 = 0;
    SensorSPIRecieve(&stackForPC[0], &countStackForPC);
    memorySPISend = 0;
    //setPORTC_LED(0x01); SVITI
    SREG.7 = 1;
}

interrupt [USART0_RXC] void recieve_sensor (void)  //preruseni pri prijmu dat ze Senzoru z USART
{
    SREG.7 = 0;
    SensorUsartRecieve(&stackForPC[0], &countStackForPC); 
    SREG.7 = 1;
}

interrupt [USART1_RXC] void recieve (void)  //preruseni pri prijmu dat z modulu XBee
{ 
    SREG.7 = 0;
    XBeeUsartRecieve (&programMemory, &switchMemory, &baudRateXBee, &parityXBee, &baudRateSensor, &paritySensor, &memory, &stackForSensor[0], &countStackForSensor);           
    SREG.7 = 1;
}


void main (void)
{
    char retezec [8];
    char testData [10];
    char count = 0;
    char memoryUDR0Send = 0; 
    char i = 0;             
    char firstStart = 0;

    atmegaSetUp();
    loadTestData(&testData[0]);

    while (1 == 1)
    {  
        switch(switchMemory)
        {
            case 0x00: break;
            case 0x01: switchMemory = XBeeEnterCommandMode(); break;
            case 0x02: XBeeSetBaudRate(&retezec[0], baudRateXBee); switchMemory = XBeeSendString(retezec);  break;
            case 0x03: XBeeSetParity(&retezec[0], parityXBee); switchMemory = XBeeSendString(retezec);  break;
            case 0x04: switchMemory = XBeeSendString(APPLY_COMMUNICATION_PARAMETERS); break;
            case 0x05: 
            {
                XBeeSetUSARTParameters(baudRateXBee, parityXBee);
                switchMemory = XBeeSendString(EXIT_COMMAND_MODE);
                if(firstStart != 0)
                {
                    XBeeSendString("OK");  //posle OK vzdy kdyz dojde k vzdalenemu prenastaveni komunikace s XBee modulem
                }
                firstStart = 1;
                setPORTC_LED(0x00);
                break;
            }
            case 0x06: count = XBeeTransmitTestData(&testData[0], count); break;
            default: break;
        } 
        
        if(UCSR0B.7 == 1 && (((memoryUDR0Send == 0) && (countStackForSensor > 0)) || ((countStackForSensor > 0) && (UCSR0A.6 == 1))))  //posila data ze zasobniku z XBee na senzor
        {
            SREG.7 = 0;
            memoryUDR0Send = 1;
            
            UDR0 = stackForSensor[0];
            for(i = 0; i < countStackForSensor; i++)
            {
                stackForSensor[i] = stackForSensor[i + 1];
            }                                             
            countStackForSensor--;
            SREG.7 = 1;
        }
        
        if((SPCR.7 == 1) && (memorySPISend == 0) && (countStackForSensor > 0))
        {                 
            SREG.7 = 0;
            memorySPISend = 1;
            SPDR = stackForSensor[0];
            for(i = 0; i < countStackForSensor; i++)
            {
                stackForSensor[i] = stackForSensor[i + 1];
            }                                             
            countStackForSensor--;                                                  

            //setPORTC_LED(0x00);  /////////////////////////////////Rozsviceni LED 0, SVITI  
            SREG.7 = 1;
        }
        
        if((countStackForPC > 0) && (UCSR1A.6 == 1))  //posila data ze zasobniku z procesoru do XBee (do PC)
        {               
            SREG.7 = 0;
            if(UCSR1A.6==1) //priznak vysilani dokonceno a v bufferu nejsou data k odeslani
            {
                UCSR1A.6=1; //nuluje se nastavanim do logicke 1
            };
            
            UDR1 = stackForPC[0];
            for(i = 0; i < countStackForPC; i++)
            {
                stackForPC[i] = stackForPC[i + 1];
            }    
            countStackForPC--;  
            //setPORTC_LED(0x00);  /////////////////////////////////, SVITI
            while (UCSR1A.6==0)  //dokud neni vysilani dokonceno
            { 
            }   
            //priznak vysilani dokonceno zustava nastaven                                           
            SREG.7 = 1;
        }
    }
}