/*  jLCD.C
 *	Implementación para la interfaz de un LCD estándar.
 *	Jose Emilio Ledo Galano
 *
 *	Ver lcd.h para mas info.
 */

#include	"lcd.h"

//Conversión a bcd para visualización

void int_to_bcd(int16_t value)
{
     uint16_t resto;
	 resto = value;
	 if(value>=10000)
	 {
       digitos.dec_miles = 48 +  value / 10000;
       resto = value % 10000;
	 }
	 else
	   digitos.dec_miles = 48; 	   
     digitos.miles = 48 + resto / 1000;
     resto=resto % 1000;
     digitos.centenas = 48 + resto / 100;
     resto=resto % 100;
     digitos.decenas = 48 + resto / 10;
     resto=resto % 10;
     digitos.unidades = 48 + resto;

 }

void char_to_bcd(uint8_t c)
{

     uint8_t resto;
     digitos.centenas = 48 +  c / 100;   //centenas
     resto = c % 100;
     digitos.decenas = 48 +  resto / 10; //decenas
     resto = resto % 10;
     digitos.unidades = 48 +  resto;    //unidades

}


void lcd_menu(void)
{
	if(KeyMode())
	{
		while(KeyMode());
		if(Screen++>= SCREENS)
		 Screen = proceso;
		delay_ms(ANTIREBOTE);

	}
	
	switch(Screen)
	{
		case proceso:
		mainMenu();
		break;
		case pwmFreq:
		strcpy((char *)lcd_buffer_H," Frecuencia Hz  ");
		freqMenu();
		break;
		case pwmDuty:
		strcpy((char *)lcd_buffer_H," Duty Cycle  %  ");
		dutyMenu();
		break;
		case sampleDelay:
		strcpy((char *)lcd_buffer_H,"   Delay   uS   ");
		sampleDelayMenu();
		break;
		case samples:
		strcpy((char *)lcd_buffer_H,"    Samples     ");
		samplesMenu();
		break;
		case volume:
		strcpy((char *)lcd_buffer_H,"   Volume    %  ");
		volumeMenu();
	}
}



void mainMenu(void)
{
	if(KeyMode())
	{
		while(KeyMode());
		if(Screen++>= SCREENS)
		Screen = proceso;
		delay_ms(ANTIREBOTE);

	}

	 sprintf(lcd_buffer_H,"T:%03i   I:%05i",adc_ntc,I);  
	 sprintf(lcd_buffer_L," AREA: %09i",iArea); 
	if(flags.visualizar)  
	{
		flags.visualizar = false;
	    send_to_lcd(0);
	}
}


void freqMenu(void)
{
	if(KeyMode())
	{
		while(KeyMode());
		if(Screen++>= SCREENS)
		Screen = proceso;
				delay_ms(ANTIREBOTE);

	}
	
	if(KeyUp())
	{
		while(KeyUp());
		Params.pwmFreq++;
		if(Params.pwmFreq >=4)
		 Params.pwmFreq = Hz_200;
		Mosfet_gate_pulse_Off();
		delay_ms(50);
		cpu_irq_disable();
		waves_init();
		cpu_irq_enable();
		delay_ms(ANTIREBOTE); 
	}
	
	
	switch(Params.pwmFreq)
	{
		case 0:
		strcpy((char *)lcd_buffer_L," UP   200   DWN ");
		break;
		case 1:
		strcpy((char *)lcd_buffer_L," UP   400   DWN ");
		break;
		case 2:
		strcpy((char *)lcd_buffer_L," UP   600   DWN ");
		break;
		case 3:
		strcpy((char *)lcd_buffer_L," UP   800   DWN ");
		break;
		default:
		break;
		
	}
	
	//sprintf(lcd_buffer_L," UP   %03i   DWN ",Params.pwmFreq);  
	if(flags.visualizar)
	{
		flags.visualizar = false;
		send_to_lcd(0);
	}
}


void dutyMenu(void)
{
	if(KeyMode())
	{
		while(KeyMode());
		if(Screen++>= SCREENS)
		Screen = proceso;
		delay_ms(ANTIREBOTE);
	}
	
	if(KeyUp())
	{
		while(KeyUp());
		Params.pwmDuty++;
		if(Params.pwmDuty >=4)
		Params.pwmDuty = Duty_10;
		Mosfet_gate_pulse_Off();
		delay_ms(50);
		cpu_irq_disable();
		waves_init();
		cpu_irq_enable();
		delay_ms(ANTIREBOTE);
	}
	
	switch(Params.pwmDuty)
	{
		case 0:
		strcpy((char *)lcd_buffer_L," UP   010   DWN ");
		break;
		case 1:
		strcpy((char *)lcd_buffer_L," UP   020   DWN ");
		break;
		case 2:
		strcpy((char *)lcd_buffer_L," UP   030   DWN ");
		break;
		case 3:
		strcpy((char *)lcd_buffer_L," UP   040   DWN ");
		break;
		default:
		break;
		
	}
	
	//sprintf(lcd_buffer_L," UP   %03i   DWN ",Params.pwmDuty);
	if(flags.visualizar)
	{
		flags.visualizar = false;
		send_to_lcd(0);
	}
	
} 


void sampleDelayMenu(void)
{
	if(KeyMode())
	{
		while(KeyMode());
		if(Screen++>= SCREENS)
		Screen = proceso;
				delay_ms(ANTIREBOTE);

	}
	
	if(KeyUp())
	{
		while(KeyUp());
		Params.sampleDelay += 10;
		if(Params.sampleDelay > 200)
		Params.sampleDelay = 10;
		Mosfet_gate_pulse_Off();
		delay_ms(50);
		cpu_irq_disable();
		waves_init();
		cpu_irq_enable();
		delay_ms(ANTIREBOTE);
	}
	
	sprintf(lcd_buffer_L," UP   %03i   DWN ",Params.sampleDelay);
	if(flags.visualizar)
	{
		flags.visualizar = false;
		send_to_lcd(0);
	}
	
}

void samplesMenu(void)
{
	if(KeyMode())
	{
		while(KeyMode());
		if(Screen++>= SCREENS)
		Screen = proceso;
				delay_ms(ANTIREBOTE);
	}
	
	if(KeyUp())
	{
		while(KeyUp());
		Params.samples += 10;
		if(Params.samples > 200)
		Params.samples = 20;
		delay_ms(ANTIREBOTE);
	}
	
	sprintf(lcd_buffer_L," UP   %03i   DWN ",Params.samples);
	if(flags.visualizar)
	{
		flags.visualizar = false;
		send_to_lcd(0);
	}

}

void volumeMenu(void)
{
	if(KeyMode())
	{
		while(KeyMode());
		if(Screen++>= SCREENS)
		Screen = proceso;
				delay_ms(ANTIREBOTE);

	}
	
	if(KeyUp())
	{
		while(KeyUp());
		Params.volume += 10;
		if(Params.volume > 100)
		Params.volume = 60;
		delay_ms(ANTIREBOTE);
	}
	
	sprintf(lcd_buffer_L," UP   %03i   DWN ",Params.volume);
	if(flags.visualizar)
	{
		flags.visualizar = false;
		send_to_lcd(0);
	}
	
}








