#include <io.h>
#include "ESP8266.h"
#include "USART.h"
#include <stdlib.h>
#include <string.h>
// Alphanumeric LCD functions
#include <alcd.h>

// Declare your global variables here

#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)

// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
   {
    ESP_AppendChar(data);
   }
}

interrupt [TIM0_COMP] void ESP_CheckIRQ(void){
    ESP_CheckAnswer();                           
}

void main(void)
{
char TempBuffer[16];

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 7.813 kHz
// Mode: CTC top=OCR0
// OC0 output: Disconnected
// Timer Period: 29.952 ms
TCCR0=(0<<WGM00) | (0<<COM01) | (0<<COM00) | (1<<WGM01) | (1<<CS02) | (0<<CS01) | (1<<CS00);
TCNT0=0x00;
OCR0=0xE9;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (0<<TOIE1) | (1<<OCIE0) | (0<<TOIE0);

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=(0<<RXC) | (0<<TXC) | (0<<UDRE) | (0<<FE) | (0<<DOR) | (0<<UPE) | (0<<U2X) | (0<<MPCM);
UCSRB=(1<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (1<<RXEN) | (1<<TXEN) | (0<<UCSZ2) | (0<<RXB8) | (0<<TXB8);
UCSRC=(1<<URSEL) | (0<<UMSEL) | (0<<UPM1) | (0<<UPM0) | (0<<USBS) | (1<<UCSZ1) | (1<<UCSZ0) | (0<<UCPOL);
UBRRH=0x00;
UBRRL=0x33;

// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTA Bit 0
// RD - PORTA Bit 1
// EN - PORTA Bit 2
// D4 - PORTA Bit 4
// D5 - PORTA Bit 5
// D6 - PORTA Bit 6
// D7 - PORTA Bit 7
// Characters/line: 16
lcd_init(16);

ESP_Init();

lcd_putsf("Test");
lcd_gotoxy(0, 1);
lcd_putsf(ESP_StaticStrs.Strs[ESP_Ok]);

// Global enable interrupts
#asm("sei")

ESP_Echo(ESP_Echo_Off);
ESP_WiFiMode(ESP_Station);	
ESP_Connect("MrCrazy(Mobile)","@urService2020");	
ESP_TCPConnect("192.168.43.250",8585);    
    

//USART_PutS("Hello");

while (1)
      {
        if(ESP_Flags.DataReceived){
			    strncpy(TempBuffer, (char*)ESP_ReceiveData, ESP_ReceiveData_Index);
          TempBuffer[ESP_ReceiveData_Index] = '\0'; 			
			    ESP_TCPSendData((uint8_t*)TempBuffer,ESP_ReceiveData_Index);
			    ESP_ReleaseReceiveBuffer();
          lcd_clear();              
          lcd_puts(TempBuffer);
		    }
      }
}

void USART_PutChar(char c){
    USART_PutByte(c);
}
void USART_PutByte(unsigned char c){
    while ((UCSRA & (1 << UDRE)) == 0){}
    UDR = c;
}
void USART_PutS(char* str){
    while (*str != 0){
        USART_PutChar(*str++);
    }
}
void USART_PutN(int num){
    char Temp[7];
    itoa(num, Temp);
    USART_PutS(Temp);
}
void USART_PutData(unsigned char* data, unsigned char length){
    while (length--){
        USART_PutByte(*data++);
    }
}
