#include "main.h"
void Lcd_AchSetting_Display(void);
void Lcd_Ach150M_Display(void);
void Lcd_AchWifi_Display(void);
void Lcd_Ach30G_Display(void);
void Lcd_BchSetting_Display(void);
void Lcd_Bch150M_Display(void);
void Lcd_BchWifi_Display(void);
void Lcd_Bch30G_Display(void);
void LCD_Ach150M_ValueChange_Display(float offset);
void LCD_AchWifi_ValueChange_Display(float offset);
void LCD_Ach30G_ValueChange_Display(float offset);
void LCD_Bch150M_ValueChange_Display(float offset);
void LCD_BchWifi_ValueChange_Display(float offset);
void LCD_Bch30G_ValueChange_Display(float offset);
float Ach_150M_Value = 0;

float Ach_Wifi_Value = 0;
float Ach_30G_Value = 0;
float Bch_150M_Value = 0;
float Bch_Wifi_Value = 0;
float Bch_30G_Value = 0;

typedef struct{
	uint16_t RW_PIN;
	uint16_t RS;
	uint16_t E;
	uint16_t Reset;
	uint16_t DB0;
	uint16_t DB1;
	uint16_t DB2;
	uint16_t DB3;
	uint16_t DB4;
	uint16_t DB5;
	uint16_t DB6;
	uint16_t DB7;
	GPIO_TypeDef * RW_PIN_Port;
	GPIO_TypeDef * RS_Port;
	GPIO_TypeDef * E_Port;
	GPIO_TypeDef * DB0_Port;
	GPIO_TypeDef * DB1_Port;
	GPIO_TypeDef * DB2_Port;
	GPIO_TypeDef * DB3_Port;
	GPIO_TypeDef * DB4_Port;
	GPIO_TypeDef * DB5_Port;
	GPIO_TypeDef * DB6_Port;
	GPIO_TypeDef * DB7_Port;
}lcd_Pin_st;
void LCD_M68_DataWrite_COMMAND_Init(void){
	LCD_M68_DataWrite_COMMAND2(Line_default,Instruction_CMD,0x0F);
}

void LCD_M68_DataWrite_Clear(void){
    LCD_M68_DataWrite_COMMAND2(Line_default,Instruction_CMD,0x01);
}
void LCD_M68_DataWrite_LineChage(LCD_line_t line){
    if(line == First_Line)
        LCD_M68_DataWrite_COMMAND2(Line_default,Instruction_CMD,0x80);
    else
        LCD_M68_DataWrite_COMMAND2(Line_default,Instruction_CMD,0xC0);
}

uint8_t LCD_Str_Filter(uint8_t* str){
    uint8_t len = 0;
    uint8_t temp_str[32] = {0,};
    while (str[len] != NULL)               // 자른 문자열이 나오지 않을 때까지 반복
    {
        len++;
    }
//    printf("data Lenth = %d \r\n",len);
    return len;
}
void LCD_M68_DataWrite_COMMAND(LCD_line_t line,uint8_t cmd,uint8_t* data){
    uint8_t len = 0;
    lcd_Pin_st lcdset;
	uint16_t temp_Pinbuf[8]={0,};
	GPIO_TypeDef* temp_Portbuf[8]={0,};
    lcdset.RW_PIN = GPIO_PIN_12;//pd
    lcdset.RS     = GPIO_PIN_11;//pd
    lcdset.E      = GPIO_PIN_13;//pd
    lcdset.DB0    = GPIO_PIN_14;//pd
    lcdset.DB1	  = GPIO_PIN_15;//pd
    lcdset.DB2	  = GPIO_PIN_2;//pg
    lcdset.DB3	  = GPIO_PIN_3;//pg
    lcdset.DB5	  = GPIO_PIN_4;//pg
    lcdset.DB4    = GPIO_PIN_5;//pg
    lcdset.DB6	  = GPIO_PIN_6;//pg
    lcdset.DB7	  = GPIO_PIN_7;//pg
    len = LCD_Str_Filter(&data[0]);
    for(uint8_t i = 0; i < len; i++){
        if(line != Line_default)
            LCD_M68_DataWrite_LineChage(line); // Line feed
        if(line == Second_Line)
           line = Line_default;

    	HAL_GPIO_WritePin(GPIOD, lcdset.E|lcdset.RS|lcdset.RW_PIN|lcdset.DB0|lcdset.DB1  , GPIO_PIN_RESET); 		
    	HAL_GPIO_WritePin(GPIOG, lcdset.DB2|lcdset.DB3|lcdset.DB4|lcdset.DB5|lcdset.DB6|lcdset.DB7, GPIO_PIN_RESET);
    	//PIN Init

    	if(cmd == Instruction_CMD){
    		HAL_GPIO_WritePin(GPIOD, lcdset.RS , GPIO_PIN_RESET);  // Init Instruction CMD  
    	}else{
    		HAL_GPIO_WritePin(GPIOD, lcdset.RS , GPIO_PIN_SET);		//Init Data CMD
    	}

    	memcpy(&temp_Pinbuf[0] ,&lcdset.DB0     ,8);
    	memcpy(&temp_Portbuf[0],&lcdset.DB0_Port,8);
    	
        HAL_GPIO_WritePin(GPIOD, lcdset.E  , GPIO_PIN_SET);  	
    	if(data[i] & 0x01)
    		HAL_GPIO_WritePin(GPIOD, lcdset.DB0, GPIO_PIN_SET);
    	else
    		HAL_GPIO_WritePin(GPIOD, lcdset.DB0, GPIO_PIN_RESET);
    	
    	if(data[i] & 0x02)
    		HAL_GPIO_WritePin(GPIOD, lcdset.DB1, GPIO_PIN_SET);
    	else 
    		HAL_GPIO_WritePin(GPIOD, lcdset.DB1, GPIO_PIN_RESET);
    	
    	if(data[i] & 0x04)
    		HAL_GPIO_WritePin(GPIOG, lcdset.DB2, GPIO_PIN_SET);
    	else
    		HAL_GPIO_WritePin(GPIOG, lcdset.DB2, GPIO_PIN_RESET);
    	
    	if(data[i] & 0x08)
    		HAL_GPIO_WritePin(GPIOG, lcdset.DB3, GPIO_PIN_SET);
    	else
    		HAL_GPIO_WritePin(GPIOG, lcdset.DB3, GPIO_PIN_RESET);
    	
    	if(data[i] & 0x10)
    		HAL_GPIO_WritePin(GPIOG, lcdset.DB4, GPIO_PIN_SET);
    	else
    		HAL_GPIO_WritePin(GPIOG, lcdset.DB4, GPIO_PIN_RESET);
    	
    	if(data[i] & 0x20)
    		HAL_GPIO_WritePin(GPIOG, lcdset.DB5, GPIO_PIN_SET);
    	else
    		HAL_GPIO_WritePin(GPIOG, lcdset.DB5, GPIO_PIN_RESET);
    	
    	if(data[i] & 0x40)
    		HAL_GPIO_WritePin(GPIOG, lcdset.DB6, GPIO_PIN_SET);
    	else
    		HAL_GPIO_WritePin(GPIOG, lcdset.DB6, GPIO_PIN_RESET);
    	
    	if(data[i] & 0x80)
    		HAL_GPIO_WritePin(GPIOG, lcdset.DB7, GPIO_PIN_SET);
    	else
    		HAL_GPIO_WritePin(GPIOG, lcdset.DB7, GPIO_PIN_RESET);
    								
    		
    	HAL_GPIO_WritePin(GPIOD, lcdset.E  , GPIO_PIN_RESET);  		//Init E Line
    	if(cmd == Instruction_CMD){
    		HAL_GPIO_WritePin(GPIOD, lcdset.RS , GPIO_PIN_SET);		//Init Data CMD
    	}else{
    		HAL_GPIO_WritePin(GPIOD, lcdset.RS , GPIO_PIN_RESET);  // Init Instruction CMD  
    	}
    }
}
void LCD_M68_DataWrite_COMMAND2(LCD_line_t line,uint8_t cmd,uint8_t data){
    uint8_t len = 0;
    lcd_Pin_st lcdset;
	uint16_t temp_Pinbuf[8]={0,};
	GPIO_TypeDef* temp_Portbuf[8]={0,};
    lcdset.RW_PIN = GPIO_PIN_12;//pd
    lcdset.RS     = GPIO_PIN_11;//pd
    lcdset.E      = GPIO_PIN_13;//pd
    lcdset.DB0    = GPIO_PIN_14;//pd
    lcdset.DB1	  = GPIO_PIN_15;//pd
    lcdset.DB2	  = GPIO_PIN_2;//pg
    lcdset.DB3	  = GPIO_PIN_3;//pg
    lcdset.DB5	  = GPIO_PIN_4;//pg
    lcdset.DB4    = GPIO_PIN_5;//pg
    lcdset.DB6	  = GPIO_PIN_6;//pg
    lcdset.DB7	  = GPIO_PIN_7;//pg
    len = LCD_Str_Filter(&data);

    if(line != Line_default)
        LCD_M68_DataWrite_LineChage(line); // Line feed

	HAL_GPIO_WritePin(GPIOD, lcdset.E|lcdset.RS|lcdset.RW_PIN|lcdset.DB0|lcdset.DB1  , GPIO_PIN_RESET); 		
	HAL_GPIO_WritePin(GPIOG, lcdset.DB2|lcdset.DB3|lcdset.DB4|lcdset.DB5|lcdset.DB6|lcdset.DB7, GPIO_PIN_RESET);
	//PIN Init

	if(cmd == Instruction_CMD){
		HAL_GPIO_WritePin(GPIOD, lcdset.RS , GPIO_PIN_RESET);  // Init Instruction CMD  
	}else{
		HAL_GPIO_WritePin(GPIOD, lcdset.RS , GPIO_PIN_SET);		//Init Data CMD
	}

	memcpy(&temp_Pinbuf[0] ,&lcdset.DB0     ,8);
	memcpy(&temp_Portbuf[0],&lcdset.DB0_Port,8);
	
    HAL_GPIO_WritePin(GPIOD, lcdset.E  , GPIO_PIN_SET);  	
	if(data & 0x01)
		HAL_GPIO_WritePin(GPIOD, lcdset.DB0, GPIO_PIN_SET);
	else
		HAL_GPIO_WritePin(GPIOD, lcdset.DB0, GPIO_PIN_RESET);
	
	if(data & 0x02)
		HAL_GPIO_WritePin(GPIOD, lcdset.DB1, GPIO_PIN_SET);
	else 
		HAL_GPIO_WritePin(GPIOD, lcdset.DB1, GPIO_PIN_RESET);
	
	if(data & 0x04)
		HAL_GPIO_WritePin(GPIOG, lcdset.DB2, GPIO_PIN_SET);
	else
		HAL_GPIO_WritePin(GPIOG, lcdset.DB2, GPIO_PIN_RESET);
	
	if(data & 0x08)
		HAL_GPIO_WritePin(GPIOG, lcdset.DB3, GPIO_PIN_SET);
	else
		HAL_GPIO_WritePin(GPIOG, lcdset.DB3, GPIO_PIN_RESET);
	
	if(data & 0x10)
		HAL_GPIO_WritePin(GPIOG, lcdset.DB4, GPIO_PIN_SET);
	else
		HAL_GPIO_WritePin(GPIOG, lcdset.DB4, GPIO_PIN_RESET);
	
	if(data & 0x20)
		HAL_GPIO_WritePin(GPIOG, lcdset.DB5, GPIO_PIN_SET);
	else
		HAL_GPIO_WritePin(GPIOG, lcdset.DB5, GPIO_PIN_RESET);
	
	if(data & 0x40)
		HAL_GPIO_WritePin(GPIOG, lcdset.DB6, GPIO_PIN_SET);
	else
		HAL_GPIO_WritePin(GPIOG, lcdset.DB6, GPIO_PIN_RESET);
	
	if(data & 0x80)
		HAL_GPIO_WritePin(GPIOG, lcdset.DB7, GPIO_PIN_SET);
	else
		HAL_GPIO_WritePin(GPIOG, lcdset.DB7, GPIO_PIN_RESET);
								
		
	HAL_GPIO_WritePin(GPIOD, lcdset.E  , GPIO_PIN_RESET);  		//Init E Line
	if(cmd == Instruction_CMD){
		HAL_GPIO_WritePin(GPIOD, lcdset.RS , GPIO_PIN_SET);		//Init Data CMD
	}else{
		HAL_GPIO_WritePin(GPIOD, lcdset.RS , GPIO_PIN_RESET);  // Init Instruction CMD  
	}
}

Layer_t Curr_Layer =Menu_Layer; 

void Layer_Set(Layer_t layer){
    Curr_Layer = layer;

}
Layer_t Layer_Get(void){
    return Curr_Layer;
}
Layer_t prev_layer = 0xff;

void Character_Lcd_chMenu(AttenButton_t button){
    static uint8_t initialize = 0;
    static uint8_t temp_button = 0xFF;
    Layer_t temp_Layer = Layer_Get();
   
    if(initialize == 0){
        switch(button){
            case Power_On :
                LCD_M68_DataWrite_Clear();
                LCD_M68_DataWrite_COMMAND(Line_default,Data_CMD,"Channel Setting");
                LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,"press any Button");    
                break;
            default:
                Lcd_AchSetting_Display();
                initialize = 1;
                break;
        }
    }else{
        if(button == Button_MENU || button == Button_ESC){
            Lcd_AchSetting_Display();
        }else{
            switch(temp_Layer){
                case AChSetting_Layer:
                    if(button == Button_DOWN){
                        Lcd_BchSetting_Display();
                    }
                    else if(button == Button_ENTER){
                        Lcd_Ach150M_Display();
                    }                
                    break;
                case BChSetting_Layer:
                    if(button == Button_UP){
                        Lcd_AchSetting_Display();
                    }  
                    else if(button == Button_ENTER){
                        Lcd_Bch150M_Display();
                    }                
                    break;

/////////////////////////////////////////////////////////////////////////////////////////////////////////                    
                case AChSetting_150MLayer:
                    if(button == Button_DOWN){
                        Lcd_AchWifi_Display();
                    }
                    else if(button == Button_ENTER){
                        LCD_Ach150M_ValueChange_Display(0);
                    }
                    break;

                    
                case AChSetting_WIFIMLayer:
                    if(button == Button_DOWN){
                        Lcd_Ach30G_Display();
                    }
                    else if(button == Button_UP){
                         Lcd_Ach150M_Display();
                    }else if (button == Button_ENTER){
                        LCD_AchWifi_ValueChange_Display(0);

                    }
                    break;

                    
                case AChSetting_30GMLayer:
                    if(button == Button_UP){
                        Lcd_AchWifi_Display();
                    }else if (button == Button_ENTER){
                        LCD_Ach30G_ValueChange_Display(0);

                    }
                    break;

                    
                case BChSetting_150MLayer:
                    if(button == Button_DOWN){
                        Lcd_BchWifi_Display();
                    }else if (button == Button_ENTER){
                        LCD_Bch150M_ValueChange_Display(0);

                    }
                    break;

                    
                case BChSetting_WIFIMLayer:
                    if(button == Button_DOWN){
                        Lcd_Bch30G_Display();
                    }
                    else if(button == Button_UP){
                         Lcd_Bch150M_Display();
                    }else if (button == Button_ENTER){
                        LCD_BchWifi_ValueChange_Display(0);

                    }
                    break;

                    
                case BChSetting_30GMLayer:
                    if(button == Button_UP){
                         Lcd_BchWifi_Display();
                    }else if (button == Button_ENTER){
                        LCD_Bch30G_ValueChange_Display(0);
                    }
                    break;
                case AChSetting_Change150MLayer:  
                    if(button == Button_UP)
                        LCD_Ach150M_ValueChange_Display(0.5);
                    else if(button == Button_DOWN)
                        LCD_Ach150M_ValueChange_Display(-0.5);
                    break;
                case AChSetting_ChangeWIFIMLayer: 
                    if(button == Button_UP)
                        LCD_AchWifi_ValueChange_Display(0.5);
                    else if(button == Button_DOWN)
                        LCD_AchWifi_ValueChange_Display(-0.5);
                    break;
                case AChSetting_Change30GMLayer:  
                    if(button == Button_UP)
                        LCD_Ach30G_ValueChange_Display(0.5);
                    else if(button == Button_DOWN)
                        LCD_Ach30G_ValueChange_Display(-0.5);
                    break;
                case BChSetting_Change150MLayer:  
                    if(button == Button_UP)
                        LCD_Bch150M_ValueChange_Display(0.5);
                    else if(button == Button_DOWN)
                        LCD_Bch150M_ValueChange_Display(-0.5);
                    break;
                case BChSetting_ChangeWIFIMLayer: 
                    if(button == Button_UP)
                        LCD_BchWifi_ValueChange_Display(0.5);
                    else if(button == Button_DOWN)
                        LCD_BchWifi_ValueChange_Display(-0.5);
                    break;
                case BChSetting_Change30GMLayer:  
                    if(button == Button_UP)
                        LCD_Bch30G_ValueChange_Display(0.5);
                    else if(button == Button_DOWN)
                        LCD_Bch30G_ValueChange_Display(-0.5);
                    break;     


/////////////////////////////////////////////////////////////////////////////////////////////////////////                    
            }
        }
    }

}





void Lcd_AchSetting_Display(void)
{
    uint8_t temp_str[16]  = {0,};
    static float value = 0;
    LCD_M68_DataWrite_Clear();
    LCD_M68_DataWrite_COMMAND(Line_default,Data_CMD,"   A Channel   ");
    LCD_M68_DataWrite_COMMAND2(Line_default,Data_CMD,0x10);
    sprintf(temp_str, "    Setting");    
    LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,temp_str);
    Layer_Set(AChSetting_Layer);
}
void Lcd_Ach150M_Display(void){
    uint8_t temp_str[16]  = {0,};

    LCD_M68_DataWrite_Clear();
    LCD_M68_DataWrite_COMMAND(Line_default,Data_CMD,"  A Ch 150MHz ");
    LCD_M68_DataWrite_COMMAND2(Line_default,Data_CMD,0x10);
    sprintf(temp_str, "    %.2f dB", Ach_150M_Value);    
    LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,temp_str);
    Layer_Set(AChSetting_150MLayer);

}
void Lcd_AchWifi_Display(void){
    uint8_t temp_str[16]  = {0,};

    LCD_M68_DataWrite_Clear();
    LCD_M68_DataWrite_COMMAND2(Line_default,Data_CMD,0x11);
    LCD_M68_DataWrite_COMMAND(Line_default,Data_CMD,"   A Ch WIFI  ");
    LCD_M68_DataWrite_COMMAND2(Line_default,Data_CMD,0x10);
    sprintf(temp_str, "    %.2f dB", Ach_Wifi_Value);    
    LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,temp_str);
    Layer_Set(AChSetting_WIFIMLayer);   

}
void Lcd_Ach30G_Display(void){
    uint8_t temp_str[16]  = {0,};

    LCD_M68_DataWrite_Clear();
    LCD_M68_DataWrite_COMMAND2(Line_default,Data_CMD,0x11);
    LCD_M68_DataWrite_COMMAND(Line_default,Data_CMD,"   A Ch 30G");

    sprintf(temp_str, "    %.2f dB", Ach_30G_Value);    
    LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,temp_str);
    Layer_Set(AChSetting_30GMLayer);  
}
void Lcd_BchSetting_Display(void)
{
    uint8_t temp_str[16]  = {0,};
    LCD_M68_DataWrite_Clear();
    LCD_M68_DataWrite_COMMAND2(Line_default,Data_CMD,0x11);
    LCD_M68_DataWrite_COMMAND(Line_default,Data_CMD,"  B Channel");
    sprintf(temp_str, "    Setting"); 
    LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,temp_str);
    Layer_Set(BChSetting_Layer);
}
void Lcd_Bch150M_Display(void)
{
    uint8_t temp_str[16]  = {0,};

    LCD_M68_DataWrite_Clear();
    LCD_M68_DataWrite_COMMAND(Line_default,Data_CMD,"  B Ch 150MHz ");
    LCD_M68_DataWrite_COMMAND2(Line_default,Data_CMD,0x10);

    sprintf(temp_str, "    %.2f dB", Bch_150M_Value);    
    LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,temp_str);
    Layer_Set(BChSetting_150MLayer);
}
void Lcd_BchWifi_Display(void)
{
    uint8_t temp_str[16]  = {0,};

    LCD_M68_DataWrite_Clear();
    LCD_M68_DataWrite_COMMAND2(Line_default,Data_CMD,0x11);
    LCD_M68_DataWrite_COMMAND(Line_default,Data_CMD,"  B Ch WIFI  ");
    LCD_M68_DataWrite_COMMAND2(Line_default,Data_CMD,0x10);

    sprintf(temp_str, "    %.2f dB", Bch_Wifi_Value);    
    LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,temp_str);
    Layer_Set(BChSetting_WIFIMLayer); 

}
void Lcd_Bch30G_Display(void)
{
    uint8_t temp_str[16]  = {0,};
    LCD_M68_DataWrite_Clear();
    LCD_M68_DataWrite_COMMAND2(Line_default,Data_CMD,0x11);
    LCD_M68_DataWrite_COMMAND(Line_default,Data_CMD,"   B Ch 30G");

    sprintf(temp_str, "    %.2f dB", Bch_30G_Value);    
    LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,temp_str);
    Layer_Set(BChSetting_30GMLayer); 
}
void LCD_Ach150M_ValueChange_Display(float offset){
    uint8_t temp_str[16]  = {0,};
    Ach_150M_Value += offset;
    sprintf(temp_str, "    %.2f dB", Ach_150M_Value);    
    LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,temp_str);
     Layer_Set(AChSetting_Change150MLayer);

}
void LCD_AchWifi_ValueChange_Display(float offset){
    uint8_t temp_str[16]  = {0,};
    Ach_Wifi_Value += offset;
    sprintf(temp_str, "    %.2f dB", Ach_Wifi_Value);    
    LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,temp_str);
    Layer_Set(AChSetting_ChangeWIFIMLayer);
}
void LCD_Ach30G_ValueChange_Display(float offset){
    uint8_t temp_str[16]  = {0,};
    Ach_30G_Value += offset;
    sprintf(temp_str, "    %.2f dB", Ach_30G_Value);    
    LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,temp_str);
    Layer_Set(AChSetting_Change30GMLayer);
}
void LCD_Bch150M_ValueChange_Display(float offset){
    uint8_t temp_str[16]  = {0,};
    Bch_150M_Value += offset;
    sprintf(temp_str, "    %.2f dB", Bch_150M_Value);    
    LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,temp_str);
    Layer_Set(BChSetting_Change150MLayer);
}
void LCD_BchWifi_ValueChange_Display(float offset){
    uint8_t temp_str[16]  = {0,};
    Bch_Wifi_Value += offset;
    sprintf(temp_str, "    %.2f dB", Bch_Wifi_Value);    
    LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,temp_str);
    Layer_Set(BChSetting_ChangeWIFIMLayer);
}
void LCD_Bch30G_ValueChange_Display(float offset){
    uint8_t temp_str[16]  = {0,};
    Bch_30G_Value += offset;
    sprintf(temp_str, "    %.2f dB", Bch_30G_Value);    
    LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,temp_str);
    Layer_Set(BChSetting_Change30GMLayer);
}


























