#include "lcd.h"
extern I2C_HandleTypeDef hi2c1;
void LCD_INIT(void)
{
unsigned char lcd_data[6]={0X02,0X28,0x0c,0X06,0X01,0X80};
for(uint8_t a=0;a<6;a++)
	{
	nibble_cmd(lcd_data[a]);
	}
build_pattern();
}
void LCD_Clear(void)
{
nibble_cmd(0x01);
}
/******************************************************************************************/
/*******            This data use for Command mode                         ****************/
/******* That RS=0, R/W=0, and EN 1 to 0.... first 1 than 0                ****************/
/******************************************************************************************/
/******************************************************************************************/ 
void nibble_cmd(unsigned char LCD_value)
{
unsigned char display_nibble;
display_nibble=LCD_value&0xF0;                 //mask lower nibble because DD4-DD7 pins are used
LCD_cmd(display_nibble);                       //Send upper Nibble
display_nibble=((LCD_value<<4) & 0xF0);       //mask the upper Nibble
LCD_cmd(display_nibble); 
}
void LCD_cmd(unsigned char cmd)
{
unsigned char data[3];
	data[0]=cmd;
	data[1]=cmd|0b00001100; // EN = 1 and Backlight ON(Pin –P3)
  HAL_I2C_Master_Transmit(&hi2c1,(Address<<1)|0,data,2,100);
	HAL_Delay(2);
	data[0]=cmd|0b00001000; //EN = 0 and Backlight ON
  HAL_I2C_Master_Transmit(&hi2c1,(Address<<1)|0,data,1,100);
}
/*******************************************************************************************/
/******            This data use for displaying LCD ASCII Character       ******************/
/******   That RS=1, R/W=0, and EN 1 to 0..... first 1 than 0             ******************/
/*******************************************************************************************/
/*******************************************************************************************/
void nibble_data(unsigned char LCD_data)
{
unsigned char data_nibble;
data_nibble=LCD_data&0xF0;                   //mask lower nibble because DD4-DD7 pins are used
LCD_write(data_nibble);                      //Send upper Nibble
data_nibble=((LCD_data<<4) & 0xF0);         //mask the upper Nibble
LCD_write(data_nibble); 
}
void LCD_write(unsigned char data)
{
unsigned char display[3];
	display[0]=data;
	display[1]=data|0b00001101; // EN = 1, R/S=1 and Backlight ON
  HAL_I2C_Master_Transmit(&hi2c1,(Address<<1)|0,display,2,100);
	HAL_Delay(2);
	display[0]=data|0b00001001; // EN = 0, R/S=1 and Backlight ON
  HAL_I2C_Master_Transmit(&hi2c1,(Address<<1)|0,display,1,100);
}
void LCD_write_string(unsigned char x, unsigned char y, unsigned char *str)     //store address value of the string in pointer *str
{
unsigned char lcd_address[4]={0x80,0xc0,0x90,0xd0};
nibble_cmd(lcd_address[y-1]+x-1);
uint8_t i=0;
	while(str[i]!='\0')                       // loop will go on till the NULL character in the string
	{
	nibble_data(str[i]);                      // sending data on LCD byte by byte
	i++;
	}
}
/*******************************************************************************************/
/******      Display Custom Char                                           *****************/
/******      Character size 5*7                                            *****************/
/******      CGRAM Char--- CGRAM Addr-----Command to display               *****************/
/******      1st char      0x40           nibble_cmd(0)                    *****************/
/******      2nd char      0x48           nibble_cmd(1)                    *****************/
/******      3rd char      0x56           nibble_cmd(2)                    *****************/
/******      4th           0x64           nibble_cmd((3)                   *****************/
/******      5th           0x72           nibble_cmd(4)                    *****************/
/******      6th           0x80           nibble_cmd(5)                    *****************/
/******      7th           0x88           nibble_cmd(6)                    *****************/
/******      8th           0x96           nibble_cmd(7)                    *****************/
/*******************************************************************************************/
/*******************************************************************************************/
unsigned char pattern[8][8]={
								{0x1E, 0x1E, 0x0C, 0x0C, 0x0D, 0x0E, 0x0C, 0x1E}, //1st Char----- Change what ever yot display
								{0x00, 0x00, 0x00, 0x00, 0x10, 0x08, 0x1F, 0x1F}, //2st Char----- Change what ever yot display
								{0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x00, 0x00, 0x00}, //3st Char----- Change what ever yot display
								{0x00, 0x11, 0x0E, 0x1B, 0x1B, 0x0E, 0x11, 0x00}, //4st Char----- Change what ever yot display
								{0x08, 0x10, 0x10, 0x09, 0x06, 0x00, 0x00, 0x00}, //5st Char----- Change what ever yot display
								{0x1E, 0x0A, 0x12, 0x12, 0x12, 0x13, 0x0A, 0x00}, //6st Char----- Change what ever yot display
								{0x03, 0x03, 0x00, 0x04, 0x0C, 0x14, 0x04, 0x00}, //7st Char----- Change what ever yot display
								{0x1E, 0x1A, 0x12, 0x02, 0x02, 0x02, 0x02, 0x00}  //8st Char----- Change what ever yot display
								
							};
void build_pattern(void)
{
  LCD_build(0,pattern[0]); //-------1st Character build in CGRAM
	LCD_build(1,pattern[1]); //-------2st Character build in CGRAM
	LCD_build(2,pattern[2]); //-------3st Character build in CGRAM
	LCD_build(3,pattern[3]); //-------4st Character build in CGRAM
	LCD_build(4,pattern[4]); //-------5st Character build in CGRAM
	LCD_build(5,pattern[5]); //-------6st Character build in CGRAM
	LCD_build(6,pattern[6]); //-------7st Character build in CGRAM
	LCD_build(7,pattern[7]); //-------8st Character build in CGRAM
}
 void LCD_build(unsigned char location ,unsigned char *ptr)
{
unsigned char i;
if(location <8)
	{
	nibble_cmd(0x40+(location*8));
	for(i=0;i<8;i++)
		{
		nibble_data(ptr[i]);
		}
	}
}
void LCD_write_custom(unsigned char x, unsigned char y, unsigned char custom_data)
{
unsigned char lcd_address[4]={0x80,0xc0,0x90,0xd0};
nibble_cmd(lcd_address[y-1]+x-1);
nibble_data(custom_data);
}
