/*
 * Display.h
 *
 *  Created on: 15. 7. 2019
 *  Author: 	Vitoslavský Ondøej
 *  Company: 	UVB Technik
 *  Display: 	BC-1602A-YPLEH
 *  Datasheet: 	https://datasheet.octopart.com/BC-1602A-YPLEH-Bolymin-datasheet-41171537.pdf
 *
 *  Description:
 *  	This file is used for communication with BC-1602A-YPLEH display.
 *  	Thanks to 4bit communication are used only upper 4 data
 *  	pins on display's PCB (11...14) ie (DB4...DB7).
 *  	Pin R/W (5) on display can be grounded because no display data will be read.
 *
 */

#ifndef DISPLAY_DRIVER_H_
#define DISPLAY_DRIVER_H_




 #define LCD_DB7_PIN                 GPIO_PIN_0
 #define LCD_DB7_GPIO_PORT           GPIOE

 #define LCD_DB6_PIN                 GPIO_PIN_1
 #define LCD_DB6_GPIO_PORT           GPIOE

 #define LCD_DB5_PIN                 GPIO_PIN_2
 #define LCD_DB5_GPIO_PORT           GPIOE

 #define LCD_DB4_PIN                 GPIO_PIN_3
 #define LCD_DB4_GPIO_PORT           GPIOE

 #define LCD_E_PIN                   GPIO_PIN_4
 #define LCD_E_GPIO_PORT             GPIOE

 #define LCD_RS_PIN                  GPIO_PIN_5
 #define LCD_RS_GPIO_PORT            GPIOE

 #define LCD_COMMON_PORT 			GPIOE

#define BUFFERLENGTH 16
char string[BUFFERLENGTH];


//define 8 bits
#define NUL		0b0000000000
#define DB0		0b0000000001
#define DB1		0b0000000010
#define DB2		0b0000000100
#define DB3		0b0000001000
#define DB4		0b0000010000
#define DB5		0b0000100000
#define DB6		0b0001000000
#define DB7		0b0010000000
#define R_W		0b0100000000
#define R_S		0b1000000000


//define 4 bits
#define NUL_4	0b000000
#define DB4_4	0b000001
#define DB5_4	0b000010
#define DB6_4	0b000100
#define DB7_4	0b001000
#define R_W_4	0b010000
#define R_S_4	0b100000

//define commands for 4 bits
#define COM1_4	0b0000110011
#define COM2_4	0b0000110010
#define WID_4 	0b0000101000
#define DOF_4	0b0000001000
#define CLR_4 	0b0000000001
#define MOD_4 	0b0000000110
#define DON_4 	0b0000001100

/**
  * @brief  Initialize GPIO.
  * @note   Initialize GPIO pins used for LCD communication.
  * @param  No param.
  * @retval None.
  */
void Display_LowLevel_Init();

/**
  * @brief  Initialize Display for 4bit communication.
  * @note   This function initializes LCD with write to register sequence defined by Producer of LCD.
  * @param  No param.
  * @retval None.
  */
void Display_Init4();

/**
  * @brief  Sends commands to Display.
  * @note   Sends commands to LCD with 4 bit communication -> sends first 4 bits and then last 4 bits.
  * @param  binar Binary value that needs to be written to the LCD. This value consists of Register select bit (Data/instructions)
  * 		and of ASCII value of written character (char).
  * @retval None.
  */
void Display_Send4(uint16_t binar);

/**
  * @brief  Prints "UVB Technik".
  * @note   Prints "UVB Technik" and then it clears the display.
  * @param  None.
  * @retval None.
  */
void Display_UVB();

/**
  * @brief  Prints string.
  * @note  Prints string to selected row of display (row 0/1). String longer
  * than 16 characters or string that extends beyond the display boundaries
  * will be cut off in order to fit on the display.
  * @param  string pointer to string that needs to be written.
  * @param	row (0/1) where to start writing the string
  * @param	column (0...15) where to start writing the string
  * @retval None.
  */
void Display_Write_String(char* string, uint8_t row, uint8_t col);

/**
  * @brief  Prints number.
  * @note  Prints number of specified length to selected row and column
  * @param  number to string that needs to be written.
  * @param	row (0/1) where to start writing the string
  * @param	column (0...15) where to start writing the string
  * @param	length digits after decimal point e.g 3.141 means length=3, if length=0, then number is printed as integer
  * @retval None.
  */
void Display_Write_Number(float number, uint8_t row, uint8_t col,uint8_t length);

/**
  * @brief  Scrolls string.
  * @note  	Prints string to selected row of display(row 0/1) and scrolls
  * 		it to the left until whole string is printed. If the string is shorter than
  * 		16 letters, it won't scroll and prints the string just once.
  * 		The iterations defines how many times the string will be scrolled.
  * 		After specified number of iterations the row will be cleared.
  * @param  string pointer to string that needs to be written.
  * @param	row (0/1) where to start writing the string
  * @param	iterations How many times the string will scroll
  * @retval None.
  */
void Display_Scroll_String(char* string, uint8_t row, uint8_t iterations);

/**
  * @brief  Clears row.
  * @note  	Clears row on display, writes empty characters to entire row. If
  * 		row = 2 is selected, MCU will send CLR_4 command, which clears both rows by just this command.
  * @param	row (0/1/2) which row should be cleared. row = 2 means both rows.
  * @retval None.
  */
void Display_Clear_Row(uint8_t row);

/**
  * @brief  Wait in order to ongoing command.
  * @note   Wait in order to ongoing command, CLR and COM commands need more time.
  * @param  binar Binary command that was send to display.
  * @retval None.
  */
void Display_Wait(uint16_t binar);


/**
  * @brief  Transforms number
  * @note   Transforms number with specified length after decimal point to string and returns it. Function is
  * 		automatically called in Display_Write_Number function, therefore this function doesn't need to be called.
  * @param  number 	decimal number to transform
  * @param  length 	digits after decimal point e.g 3.141 means length=3, if length=0, then number is printed as integer
  * @param  buf 	pointer to buffer, where number will be transformed
  * @retval None.
  */
void Transform(double number, uint8_t length, char *buf);

/**
  * @brief 	Converts Hex to string.
  * @note   Converts 8 bit Hex value to string and returns pointer to first character of the string.
  * 		Format of string is "0x--".
  * @param  convert	 Decimal number to transform.
  * @retval Pointer to buffer.
  */
char *Hex_To_String8(uint8_t convert);

/**
  * @brief 	Converts Hex to string.
  * @note   Converts 16 bit Hex value to string and returns pointer to first character of the string.
  * 		Format of string is "0x----".
  * @param  convert	 Decimal number to transform.
  * @retval Pointer to buffer.
  */
char *Hex_To_String16(uint16_t convert);

/**
  * @brief 	Converts Hex to string.
  * @note   Converts 24 bit Hex value to string and returns pointer to first character of the string.
  * 		Format of string is "0x------".
  * @param  convert	 Decimal number to transform.
  * @retval Pointer to buffer.
  */
char *Hex_To_String24(uint32_t convert);

/**
  * @brief 	Converts Hex to string.
  * @note   Converts 8 bit Hex value to string and returns pointer to first character of the string.
  * 		Format of string is "0x--------".
  * @param  convert	 Decimal number to transform.
  * @retval Pointer to buffer.
  */
char *Hex_To_String32(uint32_t convert);

/**
  * @brief 	Converts Hex to string.
  * @note   Converts Hex value to string and returns pointer to first character of the string.
  * 		Format of string is "0x--" for bytes=1 and "0x--------" for bytes=4;
  * @param  convert	 Decimal number to transform.
  * @param  bytes	 number of bytes to convert
  * @retval Pointer to buffer.
  */
char* Hex_To_String(uint32_t convert,uint8_t bytes);





#endif /* DISPLAY_DRIVER_H_ */
