void writeChar(char letter, int pos)
{
  selectDisplayChar(pos);
  switch(letter)
  {
   case ' ':
     write_blank();
     break;
     
   case 'A':
     write_A();
     break;
     
   case 'B':
     write_B();
     break;
     
   case 'C':
     write_C();
     break;
     
   case 'D':
     write_D();
     break;
     
   case 'E':
     write_E();
     break;
     
   case 'F':
     write_F();
     break;
     
   case 'G':
     write_G();
     break;
     
   case 'H':
     write_H();
     break;
     
   case 'I':
     write_I();
     break;
     
   case 'J':
     write_J();
     break;
     
   case 'K':
     write_K();
     break;
     
   case 'L':
     write_L();
     break;
     
   case 'M':
     write_M();
     break;
     
   case 'N':
     write_N();
     break;
     
   case 'O':
     write_O();
     break;
     
   case 'P':
     write_P();
     break;
     
   case 'Q':
     write_Q();
     break;
     
   case 'R':
     write_R();
     break;
     
   case 'S':
     write_S();
     break;
     
   case 'T':
     write_T();
     break;

   case 'U':
     write_U();
     break;
     
   case 'V':
     write_V();
     break;

   case 'W':
     write_W();
     break;
     
   case 'X':
     write_X();
     break;
     
   case 'Y':
     write_Y();
     break;
     
   case 'Z':
     write_Z();
     break;
     
   case ':':
     write_colon();
     break;
     
  default:
    write_checker();
    break;
  } 
}
int main(void) {
//	status_t status;		// Declaration of return variable for DAVE3 APIs (toggle comment if required)
	PORT0 ->HWSEL &= ~0x0000c000UL; //Faz pin 0.7 funcionar
	PORT0 ->HWSEL |= 0 << 14;
	DAVE_Init(); // Initialization of DAVE Apps
	//PORT0->HWSEL &= ~0x0000c000UL; //Faz pin 0.7 funcionar
	//PORT0->HWSEL |= 0 << 14;
	/*Etapa de inicializacao*/
	configure_E(); //Configura transceptor como emissor
	//IO004_SetPin(LED1); //Leds para debug
	//IO004_SetPin(LED2);
	//VER COMOFAS pra ligar analog do controle aqui ja

	psxHandShake();
	psxConfiguraControle();
	ADC001_GenerateLoadEvent(&ADC001_Handle0);
	ADC001_GetResult(&ADC001_Handle0, &result);
	Software_Timers_Init();
	/*Loop do controle*/
	while (1) {
		ADC001_GetResult(&ADC001_Handle0, &result);
		pwm_max = PWM_LIM;
		/*Inicializa o que sera mandado*/
		BOOLType blah2 = 1;
		BOOLType buzina = 1;
		BOOLType enable = 0;
		BOOLType albh2 = 1;
		BOOLType blah1 = 1;
		BOOLType albh1 = 1;
		int16_t pow1, pow2;
		/*Le controle*/
		psxLeControle();
		if (psx_status != 140) //Nao ta analogico
				{
			psxHandShake();
			psxConfiguraControle();
			continue;
		}
		/*Com dados do controle atribui valores e chama callbacks*/
		if (START && start) {
			start_state = 1;
			start();
		}
		if (SELECT && select) {
			select_state = 1;
			select();
		}
		if (L_DOIS && l_dois) {
			l_dois_state = 1;
			l_dois();
		}
		if (L_UM && l_um) {
			l_um_state = 1;
			l_um();
		}else if(!R_UM){
			acceleration_ticks=0;
		}
		if (L_TRES && l_tres) {
			l_tres_state = 1;
			l_tres();
		}
		if (R_UM && r_um) {
			r_um_state = 1;
			r_um();
		}else if(!L_UM){
			acceleration_ticks = 0;
		}
		if (R_DOIS && r_dois) {
			r_dois_state = 1;
			r_dois();
		}
		if (R_TRES && r_tres) {
			r_tres_state = 1;
			r_tres();
		}
		if (SQR && sqr) {
			sqr_state = 1;
			sqr(DEGRAU);
//			changeMode(DEGRAU);
		}
		if (TRIANGLE && triangle) {
			triangle_state = 1;
			triangle(LINEAR);
//			changeMode(LINEAR);
		}
		if (CIRCLE && circle) {
			circle_state = 1;
			circle(EXP);
//			changeMode(EXP);
		}
		if (CROSS && cross) {
			cross_state = 1;
			cross();
		}
		if (LEFT && left) {
			left_state = 1;
			left();
		}
		if (RIGHT && right) {
			right_state = 1;
			right();
		}
		if (UP && up) {
			up_state = 1;
			up();
		}
		if (DOWN && down) {
			down_state = 1;
			down();
		}

		//

		data_E[0] = pwm_max;
		//if (psxDado[5] == 0 && psxDado[3] == 0) continue; //Enquanto for zero nao faz nada -> tirar quando ligar o analogico
		pow1 = (psxDado[5] - 127); //<<1; //Analog esq //Subtrai 127 para saber o sentido
		pow2 = (psxDado[3] - 127); //<<1;
		data_E[3] = 0;
		int16_t temp; //Variavel para armazenamento temporario dos calculos
		if (!flipped) //Robo virado, variavel atribuida pelo clique de um botao
		{
			if (pow1 < -30)
				albh2 = 0; //ok
			else if (pow1 > 30)
				blah2 = 0; //ok
			else {
				albh2 = 1;
				blah2 = 0;
			}
			if (pow2 < -30)
				albh1 = 0;
			else if (pow2 > 30)
				blah1 = 0;
			else {
				albh1 = 1;
				blah1 = 0;
			}
			temp = pow1 > 0 ? pow1 * 2 : (-pow1) * 2;
			if (temp > 255)
				temp = 255;
			data_E[1] = temp * pwm_max / 100;
			temp = pow2 > 0 ? pow2 * 2 : (-pow2) * 2;
			if (temp > 255)
				temp = 255;
			data_E[2] = temp * pwm_max / 100;
		} else {
			if (pow2 > 30)
				albh2 = 0; //ok
			else if (pow2 < -30)
				blah2 = 0; //ok
			else {
				albh2 = 1;
				blah2 = 0;
			}
			if (pow1 > 30)
				albh1 = 0;
			else if (pow1 < -30)
				blah1 = 0;
			else {
				albh1 = 1;
				blah1 = 0;
			}
			temp = pow1 > 0 ? pow1 * 2 : (-pow1) * 2;
			if (temp > 255)
				temp = 255;
			data_E[2] = temp * pwm_max / 100;
			temp = pow2 > 0 ? pow2 * 2 : (-pow2) * 2;
			if (temp > 255)
				temp = 255;
			data_E[1] = temp * pwm_max / 100;
		}
		//if (data_E[1] > 20 || data_E[2] > 20) enable = 1;

		char data_0 = (mode & 1);
		char data_1 = ((mode >> 1) & 1);

//		data_E[3] = data_E[3] | (blah1 << BLAH1) | (blah2 << BLAH2)
//				| (albh1 << ALBH1) | (albh2 << ALBH2) | (enable << ENABLE)
//				| (buzina << BUZINA);
		data_E[3] = data_E[3] | (blah1 << BLAH1) | (blah2 << BLAH2)
						| (albh1 << ALBH1) | (albh2 << ALBH2) | (data_1 << ENABLE)
						| (data_0 << BUZINA);
		data_E[4] = result.Result >> 4; //Resultado tem precisao de 12bits, divide por 16 para obter 8 bits = 1 byte

		last_value_left = data_E[1];
		last_value_right = data_E[2];

		write_E();
		updateButtonStates();
	}
	return 0;
}