#include "Arduino.h"
#include "EDIPTFT.h"
#include "FT_2018_STW_CAN.h"
#include "FT18_STW_INIT.h"
#include "FT18_STW_DISPLAY.h"

EDIPTFT tft(true,false);
String bezeichnungen[]={"T_mot","T_oil","P_oil","% fa","U_batt","P_wat","T_air",
						"P_b_front","P_b_rear","Error Type","Speed_fl","Speed_fr","Speed"};
//"Drehzahl","P_fuel","Index"
int vergleichsindex;
int sizeaalt;
int sizeaneu;
int sizebalt;
int sizebneu;
int sizecalt;
int sizecneu;
int sizedalt;
int sizedneu;
int sizeealt;
int sizeeneu;
uint8_t clearcounter = 56;
uint8_t trcalt = Stw_data.trc; 
uint8_t modealt = Stw_data.mode;
uint8_t trccounter;// = Stw_data.trc;
uint8_t modecounter;// = Stw_data.mode;
bool trctimer;
bool modetimer;
int led_s[] = {led1,led2,led3,led4,led5,led6,led7,led8,led9,led10,led11,led12,led13,led14,led15,led16};
unsigned long poiltimer;
unsigned long tmottimer;
unsigned long toiltimer;
bool poilbool = true;
bool tmotbool = true;
bool toilbool = true;

void init_display() {
	pinMode(writeprotect, OUTPUT);
	digitalWrite(writeprotect, HIGH);
	pinMode(reset, OUTPUT);
	pinMode(disp_cs, OUTPUT);
	pinMode(MOSI, OUTPUT);
	pinMode(MISO, OUTPUT);
	//pinMode(CLK, INPUT);
	digitalWrite(disp_cs, HIGH);
	digitalWrite(MOSI, HIGH);
	digitalWrite(MISO, HIGH);
	digitalWrite(reset, LOW);
		//edip.smallProtoSelect(7);
		//edip.setNewColor(EA_GREY, 0xe3, 0xe3,0xe3);	 // redefine r-g-b-values of EA_GREY 
		//edip.drawImage(0,50,FASTTUBE_LOGO_PNG);
	digitalWrite(reset,HIGH);
	tft.begin(115200);  // start display communication
	/*int h = 20;
	char charh[2];
	String strh = String(h);
	strh.toCharArray(charh,2);
	tft.DisplayLight(charh);*/
	tft.cursorOn(false);
	tft.terminalOn(false);
	tft.setDisplayColor(EA_WHITE,EA_BLACK);
	tft.setTextColor(EA_WHITE,EA_BLACK);
	//tft.setTextFont('4');
	tft.setTextSize(5,8);
	tft.clear();
	//tft.displayLight('30');
	tft.drawText(0, 14, 'C', "FaSTTUBe");  //draw some text
	//tft.loadImage(0,0,1);
	//delay(2000);
}

double get_value(int a){
	double value;
	if (a == 0){ 
		value = Vehicle_data.gear;
	//}else if (a == 11){
	//	value = Stw_data.i;
	//}else if (a == 1){
	//	value = Vehicle_data.revol/2; 
	}else if (a == 1){		
		value = Vehicle_data.t_mot-40;
	}else if (a == 5){
		value = 0.0706949*Vehicle_data.u_batt;
	}else if (a == 7){
		value = Vehicle_data.t_air-40;
	}else if(a == 10){
		value = Stw_data.error_type;
	}else if (a == 2){		
		value = Vehicle_data.t_oil-40;
	}else if (a == 6){
		value = 0.0514*Vehicle_data.p_wat;
	//}else if (a == 7){		
	//	value = 0.0514*Vehicle_data.p_fuel;
	}else if (a == 3){		
		value = 0.0514*Vehicle_data.p_oil;
	}else if (a == 8){		
		value = Vehicle_data.p_brake_front;
	}else if (a == 9){		
		value = Vehicle_data.p_brake_rear;
	}else if(a == 4){
		value = 200*Vehicle_data.p_brake_front/(Vehicle_data.p_brake_rear+(2*Vehicle_data.p_brake_front));
	}else if (a == 11){		
		value = Vehicle_data.speed_fl;
	}else if (a == 12){		
		value = Vehicle_data.speed_fr;
	}else if(a == 13){
		value = Vehicle_data.speed;
	}
	return value;
}

void update_display(){
	if(((millis()-poiltimer)>=20000) and poilbool){
		poilbool = false;
		alarm("P_oil");
	}	
	if(((millis()-tmottimer)>=20000) and tmotbool){
		tmotbool = false;
		alarm("T_mot");
	}
	if(((millis()-toiltimer)>=10000) and toilbool){
		toilbool = false;
		alarm("T_oil");
	}
	if((0.0514*Vehicle_data.p_oil)>=0,1 or Vehicle_data.speed == 0){
		poiltimer = millis();
	}
	if(((Vehicle_data.t_mot - 40) <= 0x69) or ((Vehicle_data.t_mot - 40)==0xC8)){
		tmottimer = millis();
	}
	if((Vehicle_data.t_oil - 40) <= 0x96){
		toiltimer = millis();
	}
	if(Stw_data.buttonState1 & Stw_data.buttonState4){
		alarm("");
	}
	if(!tft._displaybool){
		tft.cursorOn(false);
		if(trcalt!=Stw_data.trc or trctimer == true or Stw_data.buttonStateEnc1 == HIGH){
			display_trc();
		}else if(modealt!=Stw_data.mode or modetimer == true or Stw_data.buttonStateEnc2 == HIGH){
			display_mode();
		}else{
			if(clearcounter>=56){
				tft.clear();
				clearcounter = 0;
			}
			clearcounter+=1;
			if(Stw_data.buttonState4){
				if(Stw_data.displayindex>=sizeof(bezeichnungen)/sizeof(String)-3){
					Stw_data.displayindex = 0;
					delay(250);
				}else{
					Stw_data.displayindex+=1;
					delay(250);
				}
			}
			if(Stw_data.buttonState1){
				if(Stw_data.displayindex<=0){
					Stw_data.displayindex = sizeof(bezeichnungen)/sizeof(String)-3;
					delay(250);
				}else{
					Stw_data.displayindex-=1;
					delay(250);
				}
			}
			if(vergleichsindex!=Stw_data.displayindex){
				tft.clear();
				vergleichsindex=Stw_data.displayindex;
			}	
			int a = sizeof(bezeichnungen[Stw_data.displayindex]);
			int g = sizeof(bezeichnungen[Stw_data.displayindex+1]);
			int h = sizeof(bezeichnungen[Stw_data.displayindex+2]);
			char d[a];
			char e[g];
			char f[h];
			bezeichnungen[Stw_data.displayindex].toCharArray(d,a);
			bezeichnungen[Stw_data.displayindex+1].toCharArray(e,g);
			bezeichnungen[Stw_data.displayindex+2].toCharArray(f,h);
			char b[2];
			char c[5];
			char i[5];
			char j[5];
			char k[5];
			char m[5];
			String str=String(int(get_value(0)));
			if(str.equals(String(0))){
				str = "N";
			}
			String str1;
			String str2;
			String str3;
			if(Stw_data.displayindex+1==0 or Stw_data.displayindex+1==2 or Stw_data.displayindex+1==1 or Stw_data.displayindex+1==10 or Stw_data.displayindex+1==7 or Stw_data.displayindex+1==11 or Stw_data.displayindex+1==12 or Stw_data.displayindex+1==13){
				str1=String(int(get_value(Stw_data.displayindex+1)));	
			}else{
				str1=String(get_value(Stw_data.displayindex+1));
			}
			if(Stw_data.displayindex+1 == 10){
				if(str1.equals(String(1))){
					str1 = "PC";
				}else if(str1.equals(String(2))){
					str1 = "BSE";
				}else if(str1.equals(String(3))){
					str1 = "APS";
				}else if(str1.equals(String(4))){
					str1 = "ETB";
				}else if(str1.equals(String(0))){
					str1 = "None";
				}
			}
			if(Stw_data.displayindex+2==0 or Stw_data.displayindex+2==2 or Stw_data.displayindex+2==1 or Stw_data.displayindex+2==10 or Stw_data.displayindex+2==7 or Stw_data.displayindex+2==11 or Stw_data.displayindex+2==12 or Stw_data.displayindex+2==13){
				str2=String(int(get_value(Stw_data.displayindex+2)));
			}else{
				str2=String(get_value(Stw_data.displayindex+2));
			}
			if(Stw_data.displayindex+2 == 10){
				if(str2.equals(String(1))){
					str2 = "PC";
				}else if(str2.equals(String(2))){
					str2 = "BSE";
				}else if(str2.equals(String(3))){
					str2 = "APS";
				}else if(str2.equals(String(4))){
					str2 = "ETB";
				}else if(str2.equals(String(0))){
					str2 = "None";
				}
			}
			if(Stw_data.displayindex+3==0 or Stw_data.displayindex+3==2 or Stw_data.displayindex+3==1 or Stw_data.displayindex+3==10 or Stw_data.displayindex+3==7 or Stw_data.displayindex+3==11 or Stw_data.displayindex+3==12 or Stw_data.displayindex+3==13){
				str3=String(int(get_value(Stw_data.displayindex+3)));
			}else{
				str3=String(get_value(Stw_data.displayindex+3));
			}
			if(Stw_data.displayindex+3 == 10){
				if(str3.equals(String(1))){
					str3 = "PC";
				}else if(str3.equals(String(2))){
					str3 = "BSE";
				}else if(str3.equals(String(3))){
					str3 = "APS";
				}else if(str3.equals(String(4))){
					str3 = "ETB";
				}else if(str3.equals(String(0))){
					str3 = "None";
				}
			}
			String str5=String(Vehicle_data.revol/2);
			String str4=String(0.0514*Vehicle_data.p_fuel);
			str.toCharArray(b,2);
			str1.toCharArray(c,5);
			str2.toCharArray(i,5);
			str3.toCharArray(j,5);
			str4.toCharArray(k,5);
			str5.toCharArray(m,5);
			sizeaneu = strlen(c);
			sizebneu = strlen(i);
			sizecneu = strlen(j);
			sizedneu = strlen(k);
			sizeeneu = strlen(m);
			if(sizeaalt!=sizeaneu){
				tft.clear();
				sizeaalt=sizeaneu;
			}
			if(sizebalt!=sizebneu){
				tft.clear();
				sizebalt=sizebneu;
			}
			if(sizecalt!=sizecneu){
				tft.clear();
				sizecalt=sizecneu;
			}
			if(sizedalt!=sizedneu){
				tft.clear();
				sizedalt=sizedneu;
			}
			if(sizeealt!=sizeeneu){
				tft.clear();
				sizeealt=sizeeneu;
			}
			tft.setTextSize(6,8);
			tft.drawText(132, 0, 'L', b);
			tft.setTextSize(2,7);
			tft.drawText(0, 20, 'L', k);
			tft.drawText(240, 20, 'L', m);
			tft.drawText(0,130, 'L', c);
			tft.drawText(120, 130, 'L', i);
			tft.drawText(240,130, 'L', j);
			tft.setTextSize(1,1);	
			tft.drawText(136,116, 'L', "Gang");
			tft.drawText(0,116, 'L', "P_fuel");
			tft.drawText(240,116, 'L', "Drehzahl");
			tft.drawText(0,226, 'L', d);
			tft.drawText(120,226, 'L', e);
			tft.drawText(240,226, 'L', f);
			/*if (cleaner){
				tft.setTextSize(5,8);
				tft.drawText(0, 14, 'C', "FaSTTUBe");  // draw some text
				tft.setTextSize(4,4);
				tft.drawText(24, 120, 'C', "insert coin");  // draw some text
				tft.drawText(0, 180, 'C', "to continue");
				cleaner = false;
			}else{
				tft.setTextSize(5,8);
				tft.drawText(0, 14, 'C', "FaSTTUBe");  // draw some text
				tft.setTextSize(4,4);
				tft.drawText(24, 120, 'C', "              ");  // draw some text
				tft.drawText(0, 180, 'C', "              ");
				cleaner = true;
			}
			delay(80);*/
		}
	}
}

void display_trc(){
	if(trcalt!=Stw_data.trc or Stw_data.buttonStateEnc1 == HIGH){
		tft.clear();
		tft.setTextSize(7,8);
		tft.setDisplayColor(EA_WHITE,EA_RED);
		tft.setTextColor(EA_WHITE,EA_RED);
		char trcanzeige[7];
		String str = String("ARB:");
		if(Stw_data.trc==11){
			str+="ED";
		}else{
			str+=String(Stw_data.trc);
		}
		str.toCharArray(trcanzeige,7);
		tft.drawText(0,0,'L',"            ");
		tft.drawText(0,60,'L',"            ");
		tft.drawText(0,120,'L',"            ");
		tft.drawText(0,180,'L',"            ");
		tft.drawText(15,68,'L',trcanzeige);
		trccounter = 0;
		trcalt = Stw_data.trc;
		trctimer = true;
	}else if(trccounter >= 255){
		tft.setDisplayColor(EA_WHITE,EA_BLACK);
		tft.setTextColor(EA_WHITE,EA_BLACK);
		tft.clear();
		trctimer = false;
	}else{
		trccounter+=1;
		delay(5);
	}
}

void display_mode(){
	if(modealt!=Stw_data.mode or Stw_data.buttonStateEnc2 == HIGH){
		tft.clear();
		tft.setTextSize(6,8);
		tft.setDisplayColor(EA_WHITE,EA_RED);
		tft.setTextColor(EA_WHITE,EA_RED);
		char modeanzeige[7];
		String str = String("MODE:");
		str+=String(Stw_data.mode);
		str.toCharArray(modeanzeige,7);
		tft.drawText(0,0,'L',"            ");
		tft.drawText(0,60,'L',"            ");
		tft.drawText(0,120,'L',"            ");
		tft.drawText(0,180,'L',"            ");
		tft.drawText(15,68,'L',modeanzeige);
		modecounter = 0;
		modealt = Stw_data.mode;
		modetimer = true;
	}else if(modecounter >= 255){
		tft.setDisplayColor(EA_WHITE,EA_BLACK);
		tft.setTextColor(EA_WHITE,EA_BLACK);
		tft.clear();
		modetimer = false;
	}else{
		modecounter+=1;
		delay(5);
	}
}

void alarm(String textstr){
	uint8_t x = 1;;
	char text[7];
	textstr.toCharArray(text,7);
	tft.setTextSize(8,8);
	while(x==1){
		if(!tft._displaybool){
			tft.setTextColor(EA_BLACK,EA_RED);
			tft.fillDisplayColor(EA_RED);
			tft.drawText(5,68,'L',text);
		}
		for (int j = 0; j < 16; j++){
			digitalWrite(led_s[j], HIGH);
		}
		delay(100);
		if(!tft._displaybool){
			tft.setTextColor(EA_BLACK,EA_WHITE);
			tft.fillDisplayColor(EA_WHITE);
			tft.drawText(5,68,'L',text);
		}
		for (int j = 0; j < 16; j++){
			digitalWrite(led_s[j], LOW);
		}
		delay(100);
		if(Stw_data.buttonState1 & Stw_data.buttonState4){
			x=0;
			tft.setTextColor(EA_WHITE,EA_BLACK);
		}
	}
}