#include "main.h"

void LCD_16T202DA1EA_Init(void){
     HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
     HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
     HAL_GPIO_WritePin(GPIOD, GPIO_PIN_10, GPIO_PIN_RESET);
     HAL_GPIO_WritePin(GPIOD, GPIO_PIN_10, GPIO_PIN_SET);
}

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  
	}
}
typedef enum{
    Menu_Layer = 0,
}Layer_t;
Layer_t Curr_Layer =Menu_Layer; 

void Layer_Set(Layer_t layer){
    Curr_Layer = layer;

}
Layer_t Layer_Get(void){
    return Curr_Layer;
}

void Character_Lcd_chMenu(AttenButton_t button){
    static uint8_t initialize = 0;
    static uint8_t temp_button = 0xFF;
    Layer_t temp_layer = Layer_Get();
    LCD_M68_DataWrite_Clear();
    if(initialize == 0){
        switch(button){
            case Power_On :
                LCD_M68_DataWrite_COMMAND(Line_default,Data_CMD,"Channel Setting");
                LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,"press any Button");    
                break;
            default:
                LCD_M68_DataWrite_COMMAND2(Line_default,Data_CMD,0x10);
        	    LCD_M68_DataWrite_COMMAND(Line_default,Data_CMD,"  A Channel");
                LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,"     Setting");
                initialize = 1;
                Layer_Set(Menu_Layer);
                break;
        }
    }else{
        if(temp_button != button){
            if(temp_layer == Menu_Layer){
                switch(button){
                    case Button_UP:
                	    LCD_M68_DataWrite_COMMAND(Line_default,Data_CMD,"   A Channel ");
                        LCD_M68_DataWrite_COMMAND2(Line_default,Data_CMD,0x10);
                        LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,"    Setting");
                        break;
                    case Button_DOWN:
                        LCD_M68_DataWrite_COMMAND2(Line_default,Data_CMD,0x11);
                	    LCD_M68_DataWrite_COMMAND(Line_default,Data_CMD,"   B Channel");
                        LCD_M68_DataWrite_COMMAND(Second_Line,Data_CMD,"    Setting");
                        break;    
                }
            }
            temp_layer = button;
        }
    }

}
void Character_Lcd_AttenSet(Switch_case_t ch,Switch_case_t button){
    if(ch == ACh_Setting){
        switch(button){
            case Atten_150M:break;
            case Atten_Wifi:break;
            case Atten_30G:break;
        }

    }
    else if(ch == BCh_Setting){
        switch(button){
            case Atten_150M:break;
            case Atten_Wifi:break;
            case Atten_30G:break;
        }
    }


}

