 /* *****************************************************************************
 * File:      lcd_ili9341.h
 * Date:      21-03-2026
 * Author:    Andrew Levido
 *
 * Copyright 2026 Imbrius Pty Ltd - All Rights Reserved
 *
 ******************************************************************************/

 
#ifndef ILI9341_H
#define ILI9341_H

#include "main.h"
#ifdef	__cplusplus
extern "C" {
#endif



/* Description -----------------------------------------------------------------
 * 
 *  This driver provides interface to the ILI9341 320 x 240 TFT LCD display 
 *  driver. It configures the driver to use 16-bit colour in RGB 5:6:5 format
 *
 *  The driver supports 4 display orientations defined by the LCD_ORIENTATION 
 *  macros
 * 
 * 		     o---> x
 *  		 |  +-----+    X--------+   
 *  		 V  |  A  |    |   A    |   
 *   		 y  |     |    |        |   
 *  		    |     |    +--------+  
 *    		  X-----+                 
 *                 0           90
 * 
 *    		    +-----X    +--------+
 *    		    |     |    |   A    |  
 *    		    |  A  |    |        |
 *     		    |     |    +--------X
 *     		    +-----+  
 *                180         270
 *
 *  @breif Dependencies 
 * 
 *  In board.h the hardware mapping must be defined according to the example
 *  below. The bus width must be 8 or 16. Orientation must be 0 or 1. 
 *  Chip select must be 1 or 2
 *
 * @code
 *      #define LCD_ORIENTATION				0
 *      #define LCD_BUS_WIDTH               16    
 *      #define LCD_CS_PIN					LATDbits.LATD11
 *      #define LCD_BKLT_PWR_PIN            LATDbits.LATD9
 *      #define LCD_RESET_PIN               LATGbits.LATG15    
 * @endcode
 *  
 *  In app.h the peripherals must be specified according to the example
 * @code
 *      #define LCD_BKLT_TxCONbits          T2CONbits
 *      #define LCD_BKLT_TMRx               TMR2
 *      #define LCD_BKLT_PRx                PR2
 *      #define LCD_BKLT_OCxCONbits         OC2CONbits
 *      #define LCD_BKLT_OCxR               OC2R
 *      #define LCD_BKLT_OCxRS              OC2RS
 * @endcode 
 *  
 */

/* Includes ------------------------------------------------------------------*/
#include "app.h"

/* Defines -------------------------------------------------------------------*/
/* LCD Orientation
 *
 * 		     o---> x                  
 *  		 |  +-----+    X--------+      +-----X    +--------+
 *  		 V  |  A  |    |   A    |      |     |    |   A    |  
 *   		 y  |     |    |        |      |  A  |    |        |
 *  		    |     |    +--------+  		 |     |    +--------X
 *    		  X-----+                    +-----+  
 *                 0           90        180         270
*/
#define LCD_ORIENTATION 0

// GPIO for Cip Select
#define LCD_CS_PORT LCD_nCS_GPIO_Port
#define LCD_CS_PIN LCD_nCS_Pin

// GPIO for Command/Data
#define LCD_DC_PORT LCD_DC_GPIO_Port
#define LCD_DC_PIN LCD_DC_Pin

// SPI port
extern SPI_HandleTypeDef hspi1;
#define LCD_SPI &hspi1

/* Typedefs & Enumerations ---------------------------------------------------*/

/* Public Functions Declarations ---------------------------------------------*/

/** Initialise LCD & backlight and set the initial background colour
*/
void lcd_init(void);


/* Turn LCD on
*/
void lcd_on(void);

/* Turn LCD off
*/
void lcd_off(void);

void lcd_start_write(int16_t x, uint16_t y, uint16_t width, uint16_t height);

void lcd_write_pixel(uint16_t colour);

void lcd_stop_write(void);

void lcd_write_block(int16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t colour);



#ifdef	__cplusplus
}
#endif

#endif /* ILI9341_H */
