#ifndef __LCD_2INCH_H
#define __LCD_2INCH_H

#include <SPI.h>
#include <Wire.h>
#include <Arduino.h>

#define LCD_W 240
#define LCD_H 320

// LCD control pins
#define LCD_CS   10
#define LCD_DC   9
#define LCD_RST  8
#define LCD_BL   6   

// Touch screen pins
#define TP_RST   4
#define TP_INT   2
// TP_SDA -> A4 (hardware I2C)
// TP_SCL -> A5 (hardware I2C)

#define LCD_CS_0    digitalWrite(LCD_CS, LOW)
#define LCD_CS_1    digitalWrite(LCD_CS, HIGH)

#define LCD_RST_0   digitalWrite(LCD_RST, LOW)
#define LCD_RST_1   digitalWrite(LCD_RST, HIGH)

#define LCD_DC_0    digitalWrite(LCD_DC, LOW)
#define LCD_DC_1    digitalWrite(LCD_DC, HIGH)

#define LCD_BL_0    digitalWrite(LCD_BL, LOW)
#define LCD_BL_1    digitalWrite(LCD_BL, HIGH)

#define TP_RST_0    digitalWrite(TP_RST, LOW)
#define TP_RST_1    digitalWrite(TP_RST, HIGH)

// Color definitions (RGB565)
#define WHITE       0xFFFF
#define BLACK       0x0000
#define BLUE        0x001F
#define GREEN       0x07E0
#define RED         0xF800
#define MAGENTA     0xF81F
#define CYAN        0x7FFF
#define YELLOW      0xFFE0
#define ORANGE      0xFBE0
#define PURPLE      0x881F
#define GBLUE       0X07FF

#define BACKGROUND_COLOR WHITE

// Scan direction definitions
#define L2R_U2D  0
#define L2R_D2U  1
#define R2L_U2D  2
#define R2L_D2U  3
#define U2D_L2R  4
#define U2D_R2L  5
#define D2U_L2R  6
#define D2U_R2L  7

#define DFT_SCAN_DIR  D2U_R2L

// LCD parameters structure
typedef struct
{
    uint16_t width;
    uint16_t height;
    uint8_t  dir;
    uint8_t  wramcmd;
    uint8_t  setxcmd;
    uint8_t  setycmd;
}_lcd_dev;

extern _lcd_dev lcddev;
extern uint16_t POINT_COLOR;
extern uint16_t BACK_COLOR;

void Lcd_Gpio_Init(void);
void Lcd_Init(void);
void LCD_Display_Dir(uint8_t dir);
void LCD_Clear(uint16_t color);
void Draw_Point(uint16_t x, uint16_t y, uint16_t color);
void Spi_write_word(uint16_t data);
void Set_Window(uint16_t start_x, uint16_t start_y, uint16_t end_x, uint16_t end_y);

#endif