/*
 * st7793_8bit.h
 * Library for ST7793 8bit interface
 *
 *  Created on: 18 дек. 2021 г.
 *      Author: mcfly
 */

#ifndef ST7793_8BIT_H_
#define ST7793_8BIT_H_


#include "stm32g0xx_hal.h"
#include "main.h"

#define ST7793_USE_FREERTOS		1			//1 for RTOS
#define HAS_TOUCHSCREEN

#ifdef HAS_TOUCHSCREEN
#define ST7793_Z_THRESHOLD	15		//Threshold value for Z (pressure)
#define ST7793_TS_XMIN			100		//Min ADC value for touch screen X axis
#define ST7793_TS_XMAX			950		//Max ADC value for touch screen X axis
#define ST7793_TS_YMIN			110		//Min ADC value for touch screen Y axis
#define ST7793_TS_YMAX			915		//Max ADC value for touch screen Y axis
#endif

//use PWM as light control?
#define ST7793_USE_PWM
#ifdef ST7793_USE_PWM
#define ST7793_TIM TIM15					//PWM timer (CH1)
#endif

//Control pins
#define ST7793_RST_pt	LCD_RST_GPIO_Port			//Reset port
#define ST7793_RST_pn	LCD_RST_Pin						//Reset pin

#define ST7793_CS_pt	LCD_CS_GPIO_Port			//Chip Select port
#define ST7793_CS_pn	LCD_CS_Pin						//Chip Select pin

#define ST7793_WR_pt	LCD_WR_GPIO_Port			//Write port
#define ST7793_WR_pn	LCD_WR_Pin						//Write pin				also Y+

#define ST7793_RD_pt	LCD_RD_GPIO_Port			//Read port
#define ST7793_RD_pn	LCD_RD_Pin						//Read pin

#define ST7793_RS_pt	LCD_RS_GPIO_Port			//Command/Data port
#define ST7793_RS_pn	LCD_RS_Pin						//Command/Data pin		also X-

#define ST7793_LED_pt	LCD_LED_GPIO_Port			//Backlight control port
#define ST7793_LED_pn	LCD_LED_Pin						//Backlight control pin

#define ST7793_DPort	GPIOA			//LCD Data bus port		DB6 is also X+; DB7 is also Y-
#define ST7793_DShift	0				//How many pins the data bus is shifted on the port.
										//Ex. pins 7-0 - shift is 0; pins 15-8 - shift is 8

#define ST7793_XM_CHAN	ADC_CHANNEL_8	//ADC channels for X- and Y+ pins
#define ST7793_YP_CHAN	ADC_CHANNEL_9

//Display
#define ST7793_XMAX 	400				//TFT X resolution
#define ST7793_YMAX 	240				//TFT Y resolution


//================ Config ends here ================

#define ST7793_RST_ON			HAL_GPIO_WritePin(ST7793_RST_pt, ST7793_RST_pn, GPIO_PIN_RESET)
#define ST7793_RST_OFF		HAL_GPIO_WritePin(ST7793_RST_pt, ST7793_RST_pn, GPIO_PIN_SET)
#define ST7793_CS_ON			HAL_GPIO_WritePin(ST7793_CS_pt, ST7793_CS_pn, GPIO_PIN_RESET)
#define ST7793_CS_OFF			HAL_GPIO_WritePin(ST7793_CS_pt, ST7793_CS_pn, GPIO_PIN_SET)
#define ST7793_WR_ON			HAL_GPIO_WritePin(ST7793_WR_pt, ST7793_WR_pn, GPIO_PIN_RESET)
#define ST7793_WR_OFF			HAL_GPIO_WritePin(ST7793_WR_pt, ST7793_WR_pn, GPIO_PIN_SET)
#define ST7793_RD_ON			HAL_GPIO_WritePin(ST7793_RD_pt, ST7793_RD_pn, GPIO_PIN_RESET)
#define ST7793_RD_OFF			HAL_GPIO_WritePin(ST7793_RD_pt, ST7793_RD_pn, GPIO_PIN_SET)
#define ST7793_COMMAND		HAL_GPIO_WritePin(ST7793_RS_pt, ST7793_RS_pn, GPIO_PIN_RESET)
#define ST7793_DATA				HAL_GPIO_WritePin(ST7793_RS_pt, ST7793_RS_pn, GPIO_PIN_SET)
#define ST7793_WR_PULSE		{ST7793_WR_ON;ST7793_WR_OFF;}


//Colors in 16-bit format (RGB 565)
#define Aliceblue					0xF7DF
#define Antiquewhite			0xFF5A
#define Aqua							0x07FF
#define Aquamarine				0x7FFA
#define Azure							0xF7FF
#define Beige							0xF7BB
#define Bisque						0xFF38
#define Black							0x0000
#define Blanchedalmond		0xFF59
#define Blue							0x001F
#define Blueviolet				0x895C
#define Brown							0xA145
#define Burlywood					0xDDD0
#define Cadetblue					0x5CF4
#define Chartreuse				0x7FE0
#define Chocolate					0xD343
#define Coral							0xFBEA
#define Cornflowerblue		0x64BD
#define Cornsilk					0xFFDB
#define Crimson						0xD8A7
#define Cyan							0x07FF
#define Darkblue					0x0011
#define Darkcyan					0x0451
#define Darkgoldenrod			0xBC21
#define Darkgray					0xAD55
#define Darkergray				0x39C7
#define Darkgreen					0x0320
#define Darkgreen1				0x03E0
#define Darkkhaki					0xBDAD
#define Darkmagenta				0x8811
#define Darkolivegreen		0x5345
#define Darkorange				0xFC60
#define Darkorchid				0x9999
#define Darkred						0x8800
#define Darksalmon				0xECAF
#define Darkseagreen			0x8DF1
#define Darkslateblue			0x49F1
#define Darkslategray			0x2A69
#define Darkturquoise			0x067A
#define Darkviolet				0x901A
#define Darkyellow				0xF700
#define Deeppink					0xF8B2
#define Deepskyblue				0x05FF
#define Dimgray						0x6B4D
#define Dodgerblue				0x1C9F
#define Firebrick					0xB104
#define Floralwhite				0xFFDE
#define Forestgreen				0x2444
#define Fuchsia						0xF81F
#define Gainsboro					0xDEFB
#define Ghostwhite				0xFFDF
#define Gold							0xFEA0
#define Goldenrod					0xDD24
#define Gray							0x8410
#define Green							0x07E0
#define Green1						0x0700
#define Greenyellow				0xAFE5
#define Honeydew					0xF7FE
#define Hotpink						0xFB56
#define Indianred					0xCAEB
#define Indigo						0x4810
#define Ivory							0xFFFE
#define Khaki							0xF731
#define Lavender					0xE73F
#define Lavenderblush			0xFF9E
#define Lawngreen					0x7FE0
#define Lemonchiffon			0xFFD9
#define Lightblue					0xAEDC
#define Lightcoral				0xF410
#define Lightcyan					0xE7FF
#define Lightgold					0xFFDA
#define Lightgreen				0x9772
#define Lightgray					0xD69A
#define Lightpink					0xFDB8
#define Lightsalmon				0xFD0F
#define Lightseagreen			0x2595
#define Lightskyblue			0x867F
#define Lightslategray		0x7453
#define Lightsteelblue		0xB63B
#define Lightyellow				0xFFFC
#define Lime							0x07E0
#define Limegreen					0x3666
#define Linen							0xFF9C
#define Magenta						0xF81F
#define Maroon						0x8000
#define Mediumaquamarine	0x6675
#define Mediumblue				0x0019
#define Mediumorchid			0xBABA
#define Mediumpurple			0x939B
#define Mediumseagreen		0x3D8E
#define Mediumslateblue		0x7B5D
#define Mediumspringgreen	0x07D3
#define Mediumturquoise		0x4E99
#define Mediumvioletred		0xC0B0
#define Midnightblue			0x18CE
#define Mintcream					0xF7FF
#define Mistyrose					0xFF3C
#define Moccasin					0xFF36
#define Navajowhite				0xFEF5
#define Navy							0x0010
#define Oldlace						0xFFBC
#define Olive							0x8400
#define Olivedrab					0x6C64
#define Orange						0xFD20
#define Orangered					0xFA20
#define Orchid						0xDB9A
#define Palegoldenrod			0xEF55
#define Palegreen					0x9FD3
#define Paleturquoise			0xAF7D
#define Palevioletred			0xDB92
#define Papayawhip				0xFF7A
#define Peachpuff					0xFED7
#define Peru							0xCC27
#define Plum							0xDD1B
#define Powderblue				0xB71C
#define Purple						0x8010
#define Red								0xF800
#define Rosybrown					0xBC71
#define Royalblue					0x435C
#define Saddlebrown				0x8A22
#define Salmon						0xFC0E
#define Sandybrown				0xF52C
#define Seagreen					0x2C4A
#define Seashell					0xFFBD
#define Sienna						0xA285
#define Silver						0xC618
#define Skyblue						0x867D
#define Slateblue					0x6AD9
#define Slategray					0x7412
#define Snow							0xFFDF
#define Springgreen				0x07EF
#define Steelblue					0x4416
#define Teal							0x0410
#define Thistle						0xDDFB
#define Tomato						0xFB08
#define Turquoise					0x471A
#define Violet						0xEC1D
#define Wheat							0xF6F6
#define White							0xFFFF
#define Whitesmoke				0xF7BE
#define Yellow						0xFFE0
#define Yellowgreen				0x9E66


//Button types
#define BTN_TEXT	(uint8_t)0
#define BTN_ICON	(uint8_t)1

//Delay function
#if (ST7793_USE_FREERTOS==1)
#define	ST7793_Delay(delay)		osDelay(delay)
#include "cmsis_os.h"
#else
#define	ST7793_Delay(delay)		HAL_Delay(delay)
#endif

#ifdef HAS_TOUCHSCREEN
	typedef struct TouchPoint{
	  uint16_t x;
	  uint16_t y;
	  uint16_t z;	//Pressure
	} TouchPoint;
#endif


#define ST7793_CHAR_H  	12				//Font character height (+2 lines above and under)
#define ST7793_CHAR_W  	8				//Font character width


void ST7793_SendCommand(uint16_t index, uint16_t data);		//Send a command. Register index and data to fill it with
void ST7793_SendData(uint16_t data);						//Send data
uint16_t ST7793_ReadReg(uint16_t index);					//Read data from a register
void ST7793_Reset(void);									//Display Reset
uint8_t ST7793_Init(void);									//Display initialization. Returns 0 if OK. 0x10 if Driver code is not right


//Set backlight brightness (0-99)
void ST7793_Brightness(uint8_t level);
//Fill the entire screen with color
void ST7793_FillScreen(uint16_t color);
//Fill a rectangle zone with color
void ST7793_FillRect(uint16_t color, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
//Draw a pixel
void ST7793_DrawPixel(uint16_t color, uint16_t x, uint16_t y);
//Draw a line
void ST7793_DrawLine(uint16_t color, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
//Draw a rectangle
void ST7793_DrawRect(uint16_t color, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
//Draw a circle
void ST7793_DrawCircle(uint16_t color, uint16_t x0, uint16_t y0, uint16_t r);
//Set font size
void ST7793_SetFontSize(uint8_t size);
//Draw a character
void ST7793_DrawChar(uint16_t color, uint16_t bgcolor, char c, uint16_t x, uint16_t y);
//Draw a thin number character
void ST7793_DrawTNChar(uint16_t color, uint16_t bgcolor, char c, uint16_t x, uint16_t y);
//Draw a string of characters
void ST7793_DrawString(uint16_t color, uint16_t bgcolor, char *str, uint16_t x, uint16_t y);
//Draw a string of characters using constant (typed between quotes) string
void ST7793_DrawStringC(uint16_t color, uint16_t bgcolor, const char *str, uint16_t x, uint16_t y);
//Draw a string of thin number characters
void ST7793_DrawTNString(uint16_t color, uint16_t bgcolor, char *str, uint16_t x, uint16_t y);
//Draw a key
void ST7793_DrawButton(uint8_t btn_type, uint16_t color, uint16_t txtcolor, uint16_t x, uint16_t y, uint16_t x2, uint16_t y2, const char *data, uint8_t func);
//Draw 1-bit bitmap
void ST7793_Draw1bBitmap(uint16_t color, uint16_t bgcolor, uint16_t x, uint16_t y, const char *data);

#ifdef HAS_TOUCHSCREEN
	//Check touch screen. Returns x and y of the touch and z as the pressure value. If z == 0 then we have no touch.
	TouchPoint ST7793_CheckTouch(void);
	//Check if the touch is in rect boundaries
	uint8_t ST7793_TouchInRect(TouchPoint *t, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
#endif


#endif /* ST7793_8BIT_H_ */
