#define FOSC 8000000UL
#define FCY (FOSC / 2)
#include <xc.h>
#include <libpic30.h>
#include <stdio.h>
#include "st7735.h"
#include "colors.h"
#include "hw.h"
//#include "I2C_HAL.h"
//#include "sht21.h"
#include <string.h>


/** Configuration START **/
#pragma config DSWDTPS = DSWDTPS3 	//DSWDT Postscale Select - 1:128
#pragma config DSWDTOSC = LPRC 		//Deep Sleep Watchdog Timer Oscillator Select - LPRC
#pragma config RTCOSC = LPRC 		//RTCC Reference Oscillator Select - LPRC
#pragma config DSBOREN = OFF 		//Deep Sleep BOR Enable bit - Disabled
#pragma config DSWDTEN = OFF 		//Deep Sleep Watchdog Timer - Disabled
#pragma config WPFP = WPFP0 		//Write Protection Flash Page Segment Boundary - Page 0 (0x0)
#pragma config SOSCSEL = IO 		//Secondary Oscillator Pin Mode Select - RA4, RB4
#pragma config WUTSEL = FST 		//Voltage Regulator Wake-up Time Select - normal fast
#pragma config WPDIS = WPDIS 		//Segment Write Protection Disable - disabled
#pragma config WPCFG = WPCFGDIS 	//Write Protect Configuration Page Select - Last page and Flash Configuration words are unprotected
#pragma config WPEND = WPENDMEM 	//Segment Write Protection End Page Select - Write Protect from WPFP to the last page of memory
#pragma config POSCMOD = NONE 		//Primary Oscillator Select - no primary oscillator
#pragma config I2C1SEL = PRI 		//I2C1 Pin Select bit - default I2C pins
#pragma config IOL1WAY = OFF 		//IOLOCK One-Way Set Enable - Use unlock sequence
#pragma config OSCIOFNC = ON 		//OSCO Pin Configuration - OSCO pin asi IO RA3
#pragma config FCKSM = CSECMD 		//Clock Switching and Fail-Safe Clock Monitor - Sw Enabled, Mon Disabled
#pragma config FNOSC = FRC
#pragma config PLL96MHZ = OFF
#pragma config PLLDIV = NODIV 		//USB 96 MHz PLL Prescaler Select - No div = 1:1 = FOSC = 32MHz
#pragma config IESO = OFF 			//Internal External Switchover - Disabled
#pragma config WDTPS = PS8192 		//Watchdog Timer Postscaler - 1:8192
#pragma config FWPSA = PR32 		//WDT Prescaler - 1:32
#pragma config WINDIS = OFF 		//Windowed WDT - Disabled
#pragma config FWDTEN = OFF 		//Watchdog Timer - Disabled
#pragma config ICS = PGx1 			//Emulator Pin Placement Select bits - PGEC1/PGED1
#pragma config GWRP = OFF 			//General Segment Write Protect - Allowed
#pragma config GCP = OFF 			//General Segment Code Protect - Disabled
#pragma config JTAGEN = OFF 		//JTAG Port Enable - Disabled
/** Configuration END **/

unsigned char row = 0x01;
unsigned int new_row = 0x0001;
unsigned char MENU_ST = 0xFF;
unsigned char GSM_ST = 0x00;
unsigned char new_menu = 1;
unsigned char new_gsm = 1;
unsigned char new_pin = 1;
unsigned char new_tel = 1;
unsigned char key = 0;
volatile unsigned char key_flag = 1;
unsigned char ilum = 1;
volatile unsigned int menu_counter = 0;
volatile unsigned int ilum_counter = 400;
volatile unsigned int pwrkey_counter = 0;
volatile unsigned int adc_counter = 0;
volatile unsigned char uartCnt = 0;
unsigned char rec_en = 0;

unsigned char uartBuffer[100];

char *string; 
unsigned char test;

char PIN[4] = {0xFF, 0xFF, 0xFF, 0xFF};
char TEL[20] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
unsigned char pos_pin = 0;
unsigned char pos_tel = 0;

//unsigned char Temp[7];  //-125.25 ... 7zn .... 3kan
//unsigned char Humi[6];  // 100.00 ... 6zn .... 3kan

#define S1 PORTAbits.RA9
#define S2 PORTAbits.RA4
#define S3 PORTAbits.RA8
#define S4 PORTAbits.RA2
#define S5 PORTAbits.RA3

#define VDD_EXT PORTBbits.RB7
#define RING PORTCbits.RC3

#define STAT1 PORTCbits.RC1
#define STAT2 PORTCbits.RC0

#define R1 TRISCbits.TRISC6
#define R2 TRISCbits.TRISC7
#define R3 TRISCbits.TRISC8
#define R4 TRISCbits.TRISC9

#define TRISC_mask 0x001F

unsigned int adc_res;
unsigned int adc_res2;
double volts;
unsigned int volts_int;
char bat_volt[5];

//prototypy
void ilumON(void);
void DrawATC(char *s);
void DrawATR(char *s);

int main(void)
{
    AD1PCFG = 0xFFFF;
	TRISA = 0x071C;
	TRISB = 0x6F83;
	TRISC = TRISC_mask;
	LATC = 0x03C0;
	LATAbits.LATA10 = 0; //Level 0 for power on, TRISA10 is hiZ 
	LATBbits.LATB2 = 1; 
	LATBbits.LATB3 = 1; 
	LATAbits.LATA7 = 1; //DEBUG

	LATBbits.LATB4 = 1; //RES DIV default off

	RPOR7bits.RP15R = 18; //OC1 is 18
	RPOR1bits.RP2R = 7; //SDO1 is 7
	RPOR1bits.RP3R = 8; //SCK1 is 8
	
	RPOR6bits.RP13R = 3; //U1TX is 3
	RPINR18bits.U1RXR = 14; //U1RX is 14

	TimerInit();
	PWM_Init();
	SPI_Init();
	UART_Init();

	ST7735_initR();
	
	ADC_Init();
	//I2C_init();

while(1)
{

if (MENU_ST == 255)
{
ST7735_fillScreen(colBlack);
ST7735_drawString(20, 50, "OPEN MOBILE", colYellow, 1);
ST7735_drawString(20, 60, "PHONE PLATFORM", colYellow, 1);
ST7735_drawString(20, 90, "Ondrej Malincik", colWhite, 1);
ilumON();
menu_counter = 0;
while(menu_counter < 100);
MENU_ST = 0;
new_menu = 1;
new_gsm = 1;
}

if (MENU_ST == 0) //DEFAULT
{	
	unsigned char i = 0;
	unsigned int chg_col = colWhite;
	if (new_menu == 1)
	{
	ST7735_fillRect(1,9,128,152,colBlack);
	ST7735_drawString(110, 76, "SET", colWhite, 1); //Settings
	ST7735_drawString(2, 140, "CAL", colYellow,1 ); //Call
	ST7735_drawString(110, 140, "END", colRed,1 ); //End Call
	ST7735_drawString(2, 76, "SEN", colWhite,1 ); //SEN
	new_menu = 0;
	}

	if ((STAT1 == 0 && STAT2 == 1)) chg_col = colOrange; 
	if ((STAT1 == 1 && STAT2 == 0)) chg_col = colGreen; 
	if ((STAT1 == 1 && STAT2 == 1)) chg_col = colWhite;

	if (adc_counter > 240)
	{
	LATBbits.LATB4 = 0; //Turn on RES DIV
	menu_counter = 0;
	while(menu_counter < 2);
	adc_res2 = 0;
	for (i = 0; i < 32; i++)
		{
		adc_res = get_adc();
		adc_res2 += adc_res;
		}
	LATBbits.LATB4 = 1; //Turn off RES DIV
	adc_res2 >>= 5;
	volts = (((float) (adc_res2))/1023.0)*2.0*2.8;
	volts_int = volts*1000;
	bat_volt[4] = (volts_int%10)+0x30;
	volts_int /= 10;
	bat_volt[3] = (volts_int%10)+0x30;
	volts_int /= 10;
	bat_volt[2] = (volts_int%10)+0x30;
	volts_int /= 10;
	bat_volt[0] = (volts_int%10)+0x30;
	bat_volt[1] = '.';

	ST7735_fillRect(30,76,80,20,colBlack);
	ST7735_drawString(30, 76, "BAT", chg_col,1 );
	for (i = 0; i < 5; i++)
		{ST7735_drawChar((54 + (i*6)), 76, bat_volt[i], chg_col, 1);}
	ST7735_drawChar(82, 76, 'V', chg_col,1 );
	adc_counter = 0;
	}

	if ((key == 0x24) && (key_flag == 0)) //B5 Go to settings
	{
	key_flag = 1;
	ilumON();
	MENU_ST = 2; //Go to settings
	new_menu = 1;
	//new_gsm = 1;
	}

	if ((key == 0x45) && (key_flag == 0)) //B3
	{
	key_flag = 1;
	ilumON();
	MENU_ST = 1; //Go to TEL.NO. ENTER
	new_menu = 1;
	}

	if ((key == 0x44) && (key_flag == 0)) //B6
	{
	key_flag = 1;
	ilumON();
	}

	if ((key == 0x25) && (key_flag == 0)) //B2
	{
	key_flag = 1;
	ilumON();
	//refresh_TempHumi();
	ST7735_fillRect(25,76,80,20,colBlack);
//	ST7735_drawString(25, 76, (Humi+0x30), colWhite, 1);
//	ST7735_drawString(25, 86, (Temp+0x30), colWhite, 1);

	/*for (i = 0; i < (7); i++)
		{ST7735_drawChar((25 + (i*6)), 76, (Humi[i]+0x30), colWhite, 1);}
	for (i = 0; i < (6); i++)
		{ST7735_drawChar((25 + (i*6)), 86, (Temp[i]+0x30), colWhite, 1);}*/
	}

			
}

if (MENU_ST == 1) //TEL.NO.
{
	unsigned char i = 0;
	if (new_menu == 1)
	{
	ST7735_fillRect(1,9,128,152,colBlack);
	ST7735_drawString(110, 76, "BCK", colWhite, 1); //Back
	ST7735_drawString(2, 140, "CAL", colGreen,1 ); //Call
	ST7735_drawString(110, 140, "END", colRed,1 ); //End Call
	ST7735_drawString(39, 66, "NUMBER ?", colWheat, 1); //NUMBER ? text
	//if (GSM_ST == 7) ST7735_drawString(39, 66, "CALL", colGreen, 1); //CALL
	//if (GSM_ST == 8) ST7735_drawString(39, 66, "IN CALL", colBlue, 1); //CALL
	new_menu = 0;
	}
	
	if (new_tel == 1)
	{
    ST7735_fillRect(1,83,128,8,colBlack);
	for (i = 0; i < (pos_tel); i++)
		{ST7735_drawChar((1 + (i*6)), 83, (TEL[i]+0x30), colWhite, 1);}
	new_tel = 0;
	}

	if ((key == 0x82) && (key_flag == 0)) //"0"
	{key_flag = 1; ilumON();
	if (pos_tel < 20) {TEL[pos_tel] = 0; pos_tel++; new_tel = 1;}}

	if ((key == 0x11) && (key_flag == 0)) //"1"
	{key_flag = 1; ilumON();
	if (pos_tel < 20) {TEL[pos_tel] = 1; pos_tel++; new_tel = 1;}}

	if ((key == 0x12) && (key_flag == 0)) //"2"
	{key_flag = 1; ilumON();
	if (pos_tel < 20) {TEL[pos_tel] = 2; pos_tel++; new_tel = 1;}}

	if ((key == 0x13) && (key_flag == 0)) //"3"
	{key_flag = 1; ilumON();
	if (pos_tel < 20) {TEL[pos_tel] = 3; pos_tel++; new_tel = 1;}}

	if ((key == 0x21) && (key_flag == 0)) //"4"
	{key_flag = 1; ilumON();
	if (pos_tel < 20) {TEL[pos_tel] = 4; pos_tel++; new_tel = 1;}}

	if ((key == 0x22) && (key_flag == 0)) //"5"
	{key_flag = 1; ilumON();
	if (pos_tel < 20) {TEL[pos_tel] = 5; pos_tel++; new_tel = 1;}}

	if ((key == 0x23) && (key_flag == 0)) //"6"
	{key_flag = 1; ilumON();
	if (pos_tel < 20) {TEL[pos_tel] = 6; pos_tel++; new_tel = 1;}}

	if ((key == 0x41) && (key_flag == 0)) //"7"
	{key_flag = 1; ilumON();
	if (pos_tel < 20) {TEL[pos_tel] = 7; pos_tel++; new_tel = 1;}}

	if ((key == 0x42) && (key_flag == 0)) //"8"
	{key_flag = 1; ilumON();
	if (pos_tel < 20) {TEL[pos_tel] = 8; pos_tel++; new_tel = 1;}}

	if ((key == 0x43) && (key_flag == 0)) //"9"
	{key_flag = 1; ilumON();
	if (pos_tel < 20) {TEL[pos_tel] = 9; pos_tel++; new_tel = 1;}}
	

	if ((key == 0x45) && (key_flag == 0)) //B3, Call entered number
	{
	key_flag = 1;
	ilumON();
	SendUARTString("ATD");
	for (i = 0; i < pos_tel; i++) SendUARTChar(TEL[i]+0x30); TEL[i] = 0xFF;
	SendUARTString(";\r");
	MENU_ST = 5;
	pwrkey_counter = 0;
	while (pwrkey_counter < 40);
	if (uartBuffer[1] == 'K') {GSM_ST = 7; LATBbits.LATB5 = 1;} else GSM_ST = 2;  //7 = CALL, 2 = Fault
	new_gsm = 1;
	pos_tel = 0;
	new_menu = 1;
	}	

	if ((key == 0x44) && (key_flag == 0)) //B6, Cancel entering number
	{
	key_flag = 1;
	ilumON();
	MENU_ST = 0; //Go to default
	new_menu = 1;
	for (i = 0; i < 20; i++)
		{TEL[i] = 0xFF;}
	pos_tel = 0;
	}

	if ((key == 0x24) && (key_flag == 0)) //B5, Backspace
	{
	key_flag = 1;
	ilumON();
	if (pos_tel != 0) {pos_tel--; TEL[pos_tel] = 0xFF; new_tel = 1;}
	}

}

if (MENU_ST == 2) //Settings
{
	if (new_menu == 1)
	{
	ST7735_fillRect(1,9,128,152,colBlack);
	ST7735_drawString(110, 140, "BCK", colWhite,1 ); //Stat
	ST7735_drawString(2, 140, "ILM", colYellow,1 ); //Backlight
	ST7735_drawString(2, 14, "GSM PWR", colWhite, 1);
	ST7735_drawString(90, 14, "Login", colWhite, 1); //Login & enter PIN
	new_menu = 0;
	}

	if ((key == 0x15) && (key_flag == 0)) //B1 Turn off/on GSM
	{
	key_flag = 1;
	ilumON();

	if ((GSM_ST == 0) && (VDD_EXT == 1)) {GSM_ST = 30; new_gsm = 1; pwrkey_counter = 0;} //GSM ON, try sending 3xAT
	if ((GSM_ST == 0) && (VDD_EXT == 0)) {GSM_ST = 1; new_gsm = 1; pwrkey_counter = 0;} //GSM OFF, try it turn on
	if (GSM_ST == 2) {GSM_ST = 0; new_gsm = 1;} //GSM Fault, go to start
	if ((GSM_ST == 4) || (GSM_ST == 6)) {GSM_ST = 5; new_gsm = 1; pwrkey_counter = 0;} //GSM ON, turn it off
	}

	if ((key == 0x45) && (key_flag == 0)) //B3
	{
	key_flag = 1;
	ilumON();
	MENU_ST = 3; //Go to backlight settings
	new_menu = 1;
	//new_gsm = 1;
	}

	if ((key == 0x14) && (key_flag == 0)) //B4 login & PIN
	{
	key_flag = 1;
	ilumON();
	MENU_ST = 4; //Go to PIN enter
	new_menu = 1;
	}

	if ((key == 0x44) && (key_flag == 0)) //B6
	{
	key_flag = 1;
	ilumON();
	MENU_ST = 0; //Go to Default
	new_menu = 1;
	}
}

if (MENU_ST == 4) //PIN enter
{
	unsigned char i = 0;
	if (new_menu == 1)
	{
	ST7735_fillRect(1,9,128,152,colBlack);
	ST7735_drawString(110, 140, "END", colRed,1 ); //Cancel entering PIN
	ST7735_drawString(110, 76, "OK", colYellow, 1); //ENTER PIN
	ST7735_drawString(2, 76, "BCK", colYellow,1 ); //Decrease
	ST7735_drawString(39, 66, "PIN ?", colWheat, 1); //PIN ? text
	new_menu = 0;
	}
	
	if (new_pin == 1)
	{
    ST7735_fillRect(39,80,72,8,colBlack);
	for (i = 0; i < (pos_pin); i++)
		{ST7735_drawChar((39 + (i*8)), 80, (PIN[i]+0x30), colWhite, 1);}
	new_pin = 0;
	}

	if ((key == 0x82) && (key_flag == 0)) //"0"
	{key_flag = 1; ilumON();
	if (pos_pin < 4) {PIN[pos_pin] = 0; pos_pin++; new_pin = 1;}}

	if ((key == 0x11) && (key_flag == 0)) //"1"
	{key_flag = 1; ilumON();
	if (pos_pin < 4) {PIN[pos_pin] = 1; pos_pin++; new_pin = 1;}}

	if ((key == 0x12) && (key_flag == 0)) //"2"
	{key_flag = 1; ilumON();
	if (pos_pin < 4) {PIN[pos_pin] = 2; pos_pin++; new_pin = 1;}}

	if ((key == 0x13) && (key_flag == 0)) //"3"
	{key_flag = 1; ilumON();
	if (pos_pin < 4) {PIN[pos_pin] = 3; pos_pin++; new_pin = 1;}}

	if ((key == 0x21) && (key_flag == 0)) //"4"
	{key_flag = 1; ilumON();
	if (pos_pin < 4) {PIN[pos_pin] = 4; pos_pin++; new_pin = 1;}}

	if ((key == 0x22) && (key_flag == 0)) //"5"
	{key_flag = 1; ilumON();
	if (pos_pin < 4) {PIN[pos_pin] = 5; pos_pin++; new_pin = 1;}}

	if ((key == 0x23) && (key_flag == 0)) //"6"
	{key_flag = 1; ilumON();
	if (pos_pin < 4) {PIN[pos_pin] = 6; pos_pin++; new_pin = 1;}}

	if ((key == 0x41) && (key_flag == 0)) //"7"
	{key_flag = 1; ilumON();
	if (pos_pin < 4) {PIN[pos_pin] = 7; pos_pin++; new_pin = 1;}}

	if ((key == 0x42) && (key_flag == 0)) //"8"
	{key_flag = 1; ilumON();
	if (pos_pin < 4) {PIN[pos_pin] = 8; pos_pin++; new_pin = 1;}}

	if ((key == 0x43) && (key_flag == 0)) //"9"
	{key_flag = 1; ilumON();
	if (pos_pin < 4) {PIN[pos_pin] = 9; pos_pin++; new_pin = 1;}}


	if ((key == 0x25) && (key_flag == 0)) //B2 Backspace
	{
	key_flag = 1;
	ilumON();
	if (pos_pin != 0) {pos_pin--; PIN[pos_pin] = 0xFF; new_pin = 1;}
	}	

	if ((key == 0x24) && (key_flag == 0)) //B5 Enter PIN
	{
	key_flag = 1;
	ilumON();
	if (pos_pin == 4)
		{
		SendUARTString("AT+CPIN=");
		SendUARTChar(PIN[0]+0x30);
		SendUARTChar(PIN[1]+0x30);
		SendUARTChar(PIN[2]+0x30);
		SendUARTChar(PIN[3]+0x30);
		SendUARTChar('\r');
		pwrkey_counter = 0;
		while (pwrkey_counter < 40);
		if (uartBuffer[1] == 'K') GSM_ST = 6; else GSM_ST = 2;  //6 = PIN entered, 2 = Fault
		new_gsm = 1;
		pos_pin = 0;
		PIN[0] = 0xFF;
		PIN[1] = 0xFF;
		PIN[2] = 0xFF;
		PIN[3] = 0xFF;
		MENU_ST = 2; //Go to Settings
		new_menu = 1;
		}
	}

	if ((key == 0x44) && (key_flag == 0)) //B6 Cancel PIN
	{
	key_flag = 1;
	ilumON();
	pos_pin = 0;
	PIN[0] = 0xFF;
	PIN[1] = 0xFF;
	PIN[2] = 0xFF;
	PIN[3] = 0xFF;
	MENU_ST = 2; //Go to Settings
	new_menu = 1;
	}
}

if (MENU_ST == 3) //Backlight setting
{
	if (new_menu == 1)
	{
	ST7735_fillRect(1,9,128,152,colBlack);
	ST7735_drawString(110, 76, "+", colYellow, 1); //Increase
	ST7735_drawString(2, 76, "-", colYellow,1 ); //Decrease
	ST7735_drawString(39, 66, "Backlight", colWheat, 1); //Settings
	DrawIlum();
	ST7735_drawChar(64,76, 0x30, colWhite, 1);
	ST7735_drawChar(69, 76, 0x25, colWhite,1 ); //% char
	ST7735_drawString(110, 140, "BCK", colWhite,1 ); //Stat
	new_menu = 0;
	}

	if ((key == 0x24) && (key_flag == 0)) //B5
	{
	key_flag = 1;
	if (ilum < 10) {ilum += 1;	DrawIlum();}
	ilumON();
	}

	if ((key == 0x25) && (key_flag == 0)) //B2
	{
	key_flag = 1;
	if (ilum > 1){ilum -= 1;DrawIlum();}
	ilumON();
	}

	if ((key == 0x44) && (key_flag == 0)) //B6
	{
	key_flag = 1;
	ilumON();
	MENU_ST = 2; //Go to Default
	new_menu = 1;
	}
}

if (MENU_ST == 5) //CALL
{
	if (new_menu == 1)
	{
	ST7735_fillRect(1,9,128,152,colBlack);
	ST7735_drawString(110, 140, "END", colRed,1 ); //End Call
	if (GSM_ST == 7) {ST7735_drawString(39, 66, "CALL", colGreen, 1); } //CALL
	if (GSM_ST == 8) {ST7735_drawString(30, 66, "INC CALL", colPink, 1); 
					ST7735_drawString(2, 140, "RCV", colGreen,1 );}//Call} //CALL
	new_menu = 0;
	}
	
	if ((key == 0x44) && (key_flag == 0)) //B6 END CALL
	{
	key_flag = 1;
	ilumON();
	MENU_ST = 0; //Go to Default
	new_menu = 1;
	SendUARTString("ATH\r"); pwrkey_counter = 0;
	while (pwrkey_counter < 100);
	if (uartBuffer[1] == 'K') {GSM_ST = 6;} else GSM_ST = 2;
	LATBbits.LATB5 = 0; 
	uartBuffer[1] = 0; 
	new_gsm = 1;
	}

	if ((key == 0x45) && (key_flag == 0) && (GSM_ST == 8)) //B3 PICK CALL
	{
	key_flag = 1;
	ilumON();
	SendUARTString("ATA\r"); pwrkey_counter = 0;
	while (pwrkey_counter < 100);
	if (uartBuffer[1] == 'K') {GSM_ST = 7; LATBbits.LATB5 = 1;} else GSM_ST = 2; 
	uartBuffer[1] = 0; 
	new_gsm = 1;
	}

}

//GSM states description START
if (GSM_ST == 0) //GSM default
{
	if (new_gsm == 1)
	{
	ST7735_fillRect(2,2,60,8,colBlack);
	ST7735_drawString(2, 2, "GSM off", colCyan, 1); 
	new_gsm = 0;
	}
}

if (GSM_ST == 1) //GSM PWRKEY to ON
{
	if (new_gsm == 1)
	{
	ST7735_fillRect(2,2,60,8,colBlack);
	ST7735_drawString(2, 2, "PWRKEY...", colCyan, 1); 
	new_gsm = 0;
	}
	if (pwrkey_counter > 88)
	{	if(VDD_EXT == 1) {GSM_ST = 30; pwrkey_counter = 0;} else GSM_ST = 2; // 2 = fault, 30 = ok, wait for sending 3x AT
		new_gsm = 1;
		TRISAbits.TRISA10 = 1; //release pwrkey
	} else TRISAbits.TRISA10 = 0; //hold pwrkey in 0
}

if (GSM_ST == 2) //GSM Fault
{
	if (new_gsm == 1)
	{
	ST7735_fillRect(2,2,60,8,colBlack);
	ST7735_drawString(2, 2, "Fault", colCyan, 1); 
	new_gsm = 0;
	}
}

if (GSM_ST == 30) //Wait 3 seconds
{
	if (new_gsm == 1)
	{
	ST7735_fillRect(2,2,60,8,colBlack);
	ST7735_drawString(2, 2, "VDD ON", colCyan, 1); 
	new_gsm = 0;
	}
	if (pwrkey_counter > 240) {GSM_ST = 31; new_gsm = 1;}
}

if (GSM_ST == 31) //Synchronize sending 3x AT
{
	if (new_gsm == 1)
	{
	ST7735_fillRect(2,2,60,8,colBlack);
	ST7735_drawString(2, 2, "Send AT", colCyan, 1); 
	new_gsm = 0;
	}
	SendUARTString("AT\r"); pwrkey_counter = 0;
	while (pwrkey_counter < 4);
	SendUARTString("AT\r"); pwrkey_counter = 0;
	while (pwrkey_counter < 4);
	SendUARTString("AT\r"); pwrkey_counter = 0;
	while (pwrkey_counter < 4);
	SendUARTString("AT+QAUDCH=1\r"); pwrkey_counter = 0;
	while (pwrkey_counter < 4);	
	SendUARTString("AT+QCFG="); 
	SendUARTChar(0x22);
	SendUARTString("RFTXburst");
	SendUARTChar(0x22);
	SendUARTString(", 1\r");
	pwrkey_counter = 0;
	while (pwrkey_counter < 4);	
	//TU BUDU OSTATNE ATCKA
	if (uartBuffer[1] == 'K') GSM_ST = 4; else GSM_ST = 2; 
	uartBuffer[1] = 0; 
	new_gsm = 1;
}

if (GSM_ST == 4) //GSM OK
{
	if (new_gsm == 1)
	{
	ST7735_fillRect(2,2,60,8,colBlack);
	ST7735_drawString(2, 2, "GSM OK", colCyan, 1); 
	new_gsm = 0;
	}
}

if (GSM_ST == 5) //GSM PWRKEY to OFF
{
	if (new_gsm == 1)
	{
	ST7735_fillRect(2,2,60,8,colBlack);
	ST7735_drawString(2, 2, "AT+QPOWD=1", colCyan, 1); 
	new_gsm = 0;
	}
	SendUARTString("AT+QPOWD=1\r");
	GSM_ST = 0;
	new_gsm = 1;
	new_menu = 1;
	/*if (pwrkey_counter > 64)
	{	GSM_ST = 0; //Go to default
		new_gsm = 1;
		new_menu = 1;
		TRISAbits.TRISA10 = 1; //release pwrkey
	} else TRISAbits.TRISA10 = 0; //hold pwrkey in 0*/
}

if (GSM_ST == 6) //GSM READY
{
	if (new_gsm == 1)
	{
	ST7735_fillRect(2,2,60,8,colBlack);
	ST7735_drawString(2, 2, "GSM READY", colCyan, 1); 
	new_gsm = 0;
	}
	if (RING == 0) {GSM_ST = 8; LATBbits.LATB5 = 1; MENU_ST = 5; new_gsm = 1; new_menu = 1; ilumON();}
}

if (GSM_ST == 7) //GSM CALL
{
	if (new_gsm == 1)
	{
	ST7735_fillRect(2,2,60,8,colBlack);
	ST7735_drawString(2, 2, "GSM CALL", colCyan, 1); 
	new_gsm = 0;
	}
	if (uartBuffer[0] == 'N') {new_gsm = 1; new_menu = 1; GSM_ST = 6; LATBbits.LATB5 = 0;}
}

if (GSM_ST == 8) //GSM INCOMING CALL
{
	if (new_gsm == 1)
	{
	ST7735_fillRect(2,2,60,8,colBlack);
	ST7735_drawString(2, 2, "IN CALL", colCyan, 1); 
	new_gsm = 0;
	}
	if (RING != 0) {new_gsm = 1; new_menu = 1; GSM_ST = 6; MENU_ST = 0; LATBbits.LATB5 = 0;}
}
//GSM states description END


};
return(0);
};

//_T1Interrupt() is the T1 interrupt service routine (ISR).
void __attribute__((__interrupt__)) _T1Interrupt(void);
void __attribute__((__interrupt__, auto_psv)) _T1Interrupt(void)
{
	IFS0bits.T1IF = 0; //reset interrupt flag

	if 		(S1 == 1) key = 0x01 | (row << 4);
	else if (S2 == 1) key = 0x02 | (row << 4);
	else if (S3 == 1) key = 0x03 | (row << 4); 
	else if (S4 == 1) key = 0x04 | (row << 4); 
	else if (S5 == 1) key = 0x05 | (row << 4); 
	else 
	{key = 0; key_flag = 0;
	if (row == 0x08) row = 0x01; else row <<= 1;		
	new_row = ~row;
	TRISC = TRISC_mask | (new_row << 6);
	}

	menu_counter++;
	pwrkey_counter++;
	adc_counter++;
	if (ilum_counter == 400) OC1R = 100; else ilum_counter++; //Decease backlight if 400 (5s) is reached
	
}

void __attribute__((__interrupt__)) _U1RXInterrupt(void); 
void __attribute__((__interrupt__, auto_psv)) _U1RXInterrupt(void)
{
	unsigned char rcvd_char; 
	rcvd_char = U1RXREG;
	if ((rcvd_char!='\n')&&(rcvd_char!='\r'))
		{uartBuffer[uartCnt] = rcvd_char;
		uartCnt++;}
	else
	{
		if (rcvd_char == '\n')
		uartCnt = 0;
	}
    IFS0bits.U1RXIF = 0; //reset interrupt flag
}

void DrawIlum(void)
{
	ST7735_fillRect(54,76,10,8,colBlack);
	if (ilum > 9) {ST7735_drawChar(54,76, 0x31, colWhite, 1); ST7735_drawChar(59,76, 0x30, colWhite, 1);}
	if (ilum != 10) ST7735_drawChar(59,76, (ilum +0x30), colWhite, 1); else ST7735_drawChar(64,76, 0x30, colWhite, 1);
}

void ilumON(void)
{
ilum_counter = 0;
if 		(ilum == 10) OC1R = 4080;
else if (ilum == 9)  OC1R = 3000;
else if (ilum == 8)  OC1R = 2000;
else if (ilum == 7)  OC1R = 1000;
else if (ilum == 6)  OC1R = 800;
else if (ilum == 5)  OC1R = 700;
else if (ilum == 4)  OC1R = 600;
else if (ilum == 3)  OC1R = 500;
else if (ilum == 2)  OC1R = 400;
else 				 OC1R = 300;
}

void DrawATC(char *s)
{
	ST7735_fillRect(3,25,123,16,colBlack);
	ST7735_drawString(3,25, s, colWhite, 1);
}

void DrawATR(char *s)
{
	ST7735_fillRect(3,43,123,16,colBlack);
	ST7735_drawString(3,43, s, colWhite, 1);
}
