/*
******************************************************************************
* Date: 11st July 2008
* This demonstration for QA example for the hardware platform
* PIC18LF4550-Eval-Rev4a + TY280T-240320-BO Rev 1B
* Microchip Graphics Library port to PIC18. Only Primitive functions ported
* with Font and Picture Drawn function removed just now.
*
* FileName:        ILI9325P_LE.h
* Processor:       PIC18LF4550
* Compiler:        MPLAB C18 student version
* Programmer : 	   John Leung (www.TechToys.com.hk)
* Note:		       Please refer to the original Software License Agreement
*				   from Microchip for details concerning Copyright
******************************************************************************
*/

/*
******************************************************************************
* Date: 17th April 2008
* This demonstration for QA example for the hardware platform
* PIC18LF4550-Eval-Rev4a + TY320T-240320-BO Rev 2C
* Microchip Graphics Library port to PIC18. Only Primitive functions ported
* with Font and Picture Drawn function removed just now.
*
* FileName:        ILI9320_LE.h
* Processor:       PIC18LF4550
* Compiler:        MPLAB C18 student version
* Programmer : 	   John Leung (www.TechToys.com.hk)
* Note:		       Please refer to the original Software License Agreement
*				   from Microchip for details concerning Copyright
******************************************************************************
*/

/*****************************************************************************
 *  Module for Microchip Graphics Library
 *  ILITEK ILI9320 LCD controller driver
 *  Portrait orientation
 *****************************************************************************
 * FileName:        ILI9320P_8BIT.h
 * Dependencies:    p24Fxxxx.h
 * Processor:       PIC24
 * Compiler:       	MPLAB C30
 * Linker:          MPLAB LINK30
 * Company:         TechToys Company
 * Web presence:	www.TechToys.com.hk
 *
 * Note:			Original source code from Microchip Graphic Library 
 *					ILI9320P_8BIT.h with Software License Agreement below
 *
 * Software License Agreement
 *
 * Copyright ?2007 Microchip Technology Inc.  All rights reserved.
 * Microchip licenses to you the right to use, modify, copy and distribute
 * Software only when embedded on a Microchip microcontroller or digital
 * signal controller, which is integrated into your product or third party
 * product (pursuant to the sublicense terms in the accompanying license
 * agreement).  
 *
 * You should refer to the license agreement accompanying this Software
 * for additional information regarding your rights and obligations.
 *
 * SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS?WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY
 * OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR
 * PURPOSE. IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR
 * OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION,
 * BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT
 * DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL,
 * INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA,
 * COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY
 * CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF),
 * OR OTHER SIMILAR COSTS.

 * Author               Date          Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * John Leung     		5 Dec 2007	  Version 1.0 release
 *****************************************************************************/

#ifndef _ILI9325P_LE_H
#define _ILI9325P_LE_H

#include <p18cxxx.h>

// Additional hardware-accelerated functions implemented in the driver.
// These definitions exclude from compilation the PutPixel()-based functions
// in the graphic primitive layer (Primitive.c file).

//#define USE_DRV_FONT 			//not implemented under the Low Level Display driver yet
//#define USE_DRV_LINE			//not implemented under the Low Level Display driver yet
//#define USE_DRV_CIRCLE		//not implemented under the Low Level Display driver yet
//#define USE_DRV_FILLCIRCLE	//not implemented under the Low Level Display driver yet
#define USE_DRV_BAR
#define USE_DRV_CLEARDEVICE
//#define USE_DRV_PUTIMAGE		//not implemented under the Low Level Display driver yet


// Horizontal and vertical screen size
#define SCREEN_HOR_SIZE    240
#define SCREEN_VER_SIZE    320

// Memory pitch for line
#define LINE_MEM_PITCH     0x100 

/*
*****************************************************************************
* Use PMP module for interface
* Function		mcu pins		LCD pins
* ==========	========		========
* DATA LINES	RD[7:0]			DB[7:0]
* LE			RC6				LE of 74HC573 onboard of the TY280T LCD module
* RESET			RA5				/RESET
* CHIP SELECT	RE1				/CS
* COMMAND/DATA	RE2				RS 
* WR STROBE		RE0				/WR
* RD STROBE		RA4				/RD
* Baclight 		RC2				EN

* Remarks: JP6 on TY280T-240320-BO should be CLOSED 
* for we are using 74HC573 for 16-bit mode
*****************************************************************************
*/

//Definitions for LATCH pin of 74HC573
#define LE_TRIS_BIT			TRISCbits.TRISC6
#define LE_LAT_BIT			LATCbits.LATC6

// Definitions for reset pin
#define RST_TRIS_BIT       TRISAbits.TRISA5
#define RST_LAT_BIT        LATAbits.LATA5

// Definitions for CS pin
#define CS_TRIS_BIT        TRISEbits.TRISE1
#define CS_LAT_BIT         LATEbits.LATE1

//Definition for RS pin
#define RS_TRIS_BIT			TRISEbits.TRISE2
#define RS_LAT_BIT			LATEbits.LATE2

//Definition for RD pin
#define RD_TRIS_BIT			TRISAbits.TRISA4
#define RD_LAT_BIT			LATAbits.LATA4

//Definition for WR pin
#define WR_TRIS_BIT			TRISEbits.TRISE0
#define WR_LAT_BIT			LATEbits.LATE0

//Definition for data BUS
#define DAT_TRIS_BIT		TRISD
#define DAT_LAT_BIT			LATD

//Defintion for backlight control
#define LED_TRIS_BIT		TRISCbits.TRISC2
#define LED_LAT_BIT			LATCbits.LATC2

// Clipping region control codes
#define CLIP_DISABLE       0 
#define CLIP_ENABLE        1

// Color codes
                                 //RRRRRGGGGGGBBBBB 
#define BLACK              (WORD)0b0000000000000000
#define BRIGHTBLUE         (WORD)0b0000000000011111
#define BRIGHTGREEN        (WORD)0b0000011111100000
#define BRIGHTCYAN         (WORD)0b0000011111111111
#define BRIGHTRED          (WORD)0b1111100000000000
#define BRIGHTMAGENTA      (WORD)0b1111100000011111
#define BRIGHTYELLOW       (WORD)0b1111111111100000
#define BLUE               (WORD)0b0000000000010000
#define GREEN              (WORD)0b0000010000000000
#define CYAN               (WORD)0b0000010000010000
#define RED                (WORD)0b1000000000000000
#define MAGENTA            (WORD)0b1000000000010000
#define BROWN              (WORD)0b1111110000000000
#define LIGHTGRAY          (WORD)0b1000010000010000
#define DARKGRAY           (WORD)0b0100001000001000
#define LIGHTBLUE          (WORD)0b1000010000011111
#define LIGHTGREEN         (WORD)0b1000011111110000
#define LIGHTCYAN          (WORD)0b1000011111111111
#define LIGHTRED           (WORD)0b1111110000010000
#define LIGHTMAGENTA       (WORD)0b1111110000011111
#define YELLOW             (WORD)0b1111111111110000
#define WHITE              (WORD)0b1111111111111111

#define GRAY0       	   (WORD)0b1110011100011100
#define GRAY1         	   (WORD)0b1100011000011000   
#define GRAY2              (WORD)0b1010010100010100   
#define GRAY3              (WORD)0b1000010000010000
#define GRAY4              (WORD)0b0110001100001100
#define GRAY5              (WORD)0b0100001000001000
#define GRAY6	           (WORD)0b0010000100000100


// Color
extern WORD_VAL _color;

// Clipping region control
extern SHORT _clipRgn;

// Clipping region borders
extern SHORT _clipLeft;
extern SHORT _clipTop;
extern SHORT _clipRight;
extern SHORT _clipBottom;

/*********************************************************************
* Macros:  PMPDelay()
*
* PreCondition: none
*
* Input: none
*
* Output: none
*
* Side Effects: none
*
* Overview: delays execution for LCD data write
*
* Note: none
*
********************************************************************/
#define PMPDelay()  Nop();

/*********************************************************************
* Macros:  SetIndex(index)
*
* PreCondition: none
*
* Input: index - index register
*
* Output: none
*
* Side Effects: none
*
* Overview: writes index register
*
* Note: This macro modified for discrete I/O method
*
********************************************************************/
#define SetIndex(index)  RS_LAT_BIT = 0; LE_LAT_BIT = 1; DAT_LAT_BIT = 0; LE_LAT_BIT = 0; DAT_LAT_BIT = index; WR_LAT_BIT = 0; WR_LAT_BIT = 1;

/*********************************************************************
* Macros:  WriteData(byte1, byte0)
*
* PreCondition: none
*
* Input: byte1,byte0 - data bytes
*
* Output: none
*
* Side Effects: none
*
* Overview: writes data
*
* Note: This macro modified for discrete I/O method
*
********************************************************************/
#define WriteData(byte1,byte0) RS_LAT_BIT = 1; LE_LAT_BIT = 1; DAT_LAT_BIT = byte1; LE_LAT_BIT = 0; DAT_LAT_BIT = byte0; WR_LAT_BIT = 0; WR_LAT_BIT = 1;

/*********************************************************************
* Macros:  SetAddress(addr2,addr1,addr0)
*
* PreCondition: none
*
* Input: addr0,addr1,addr2 - address bytes
*
* Output: none
*
* Side Effects: none
*
* Overview: writes address pointer
*
* Note: none
*
********************************************************************/
#define SetAddress(addr2,addr1,addr0)  \
SetIndex(0x20);WriteData(0,addr0);\
SetIndex(0x21);WriteData(addr2,addr1);\
SetIndex(0x22);

/*********************************************************************
* Function:  void ResetDevice()
*
* PreCondition: none
*
* Input: none
*
* Output: none
*
* Side Effects: none
*
* Overview: resets device, initialize PMP
*
* Note: none
*
********************************************************************/
void ResetDevice(void);

/*********************************************************************
* Macros:  GetMaxX()
*
* PreCondition: none
*
* Input: none
*
* Output: maximum horizontal coordinate
*
* Side Effects: none
*
* Overview: returns maximum horizontal coordinate
*
* Note: none
*
********************************************************************/
#define GetMaxX() (SCREEN_HOR_SIZE-1)

/*********************************************************************
* Macros:  GetMaxY()
*
* PreCondition: none
*
* Input: none
*
* Output: maximum vertical coordinate
*
* Side Effects: none
*
* Overview: returns maximum vertical coordinate
*
* Note: none
*
********************************************************************/
#define GetMaxY() (SCREEN_VER_SIZE-1)

/*********************************************************************
* Macros:  SetColor(color)
*
* PreCondition: none
*
* Input: color coded in format:
*           bits 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00 
*          color  R  R  R  R  R  G  G  G  G  G  G  B  B  B  B  B
*
* Output: none
*
* Side Effects: none
*
* Overview: sets current color
*
* Note: none
*
********************************************************************/
#define SetColor(color) _color.Val = color;

/*********************************************************************
* Macros:  GetColor()
*
* PreCondition: none
*
* Input: none
*
* Output:  color coded in format:
*           bits 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00 
*          color  R  R  R  R  R  G  G  G  G  G  G  B  B  B  B  B
*
* Side Effects: none
*
* Overview: returns current color
*
* Note: none
*
********************************************************************/
#define GetColor() _color.Val

/*********************************************************************
* Macros:  SetActivePage(page)
*
* PreCondition: none
*
* Input: graphic page number
*
* Output: none
*
* Side Effects: none
*
* Overview: sets active graphic page 
*
* Note: the controller has only page
*
********************************************************************/
#define SetActivePage(page)

/*********************************************************************
* Macros: SetVisualPage(page)
*
* PreCondition: none
*
* Input: graphic page number
*
* Output: none
*
* Side Effects: none
*
* Overview: sets graphic page to display
*
* Note: the controller has only page
*
********************************************************************/
#define SetVisualPage(page)

/*********************************************************************
* Function: void PutPixel(SHORT x, SHORT y)
*
* PreCondition: none
*
* Input: x,y - pixel coordinates 
*
* Output: none
*
* Side Effects: none
*
* Overview: puts pixel
*
* Note: none
*
********************************************************************/
void PutPixel(SHORT x, SHORT y);

/*********************************************************************
* Function: WORD GetPixel(SHORT x, SHORT y)
*
* PreCondition: none
*
* Input: x,y - pixel coordinates 
*
* Output: pixel color
*
* Side Effects: none
*
* Overview: returns pixel color at x,y position
*
* Note: none
*
********************************************************************/
WORD GetPixel(SHORT x, SHORT y);

/*********************************************************************
* Macros: SetClipRgn(left, top, right, bottom)
*
* PreCondition: none
*
* Input: left,top,right,bottom - clipping region borders
*
* Output: none
*
* Side Effects: none
*
* Overview: sets clipping region 
*
* Note: none
*
********************************************************************/
#define SetClipRgn(left,top,right,bottom) _clipLeft=left; _clipTop=top; _clipRight=right; _clipBottom=bottom;

/*********************************************************************
* Macros: GetClipLeft()
*
* PreCondition: none
*
* Input: none
*
* Output: left clipping border
*
* Side Effects: none
*
* Overview: returns left clipping border
*
* Note: none
*
********************************************************************/
#define GetClipLeft() _clipLeft

/*********************************************************************
* Macros: GetClipRight()
*
* PreCondition: none
*
* Input: none
*
* Output: right clipping border
*
* Side Effects: none
*
* Overview: returns right clipping border
*
* Note: none
*
********************************************************************/
#define GetClipRight() _clipRight

/*********************************************************************
* Macros: GetClipTop()
*
* PreCondition: none
*
* Input: none
*
* Output: top clipping border
*
* Side Effects: none
*
* Overview: returns top clipping border
*
* Note: none
*
********************************************************************/
#define GetClipTop() _clipTop

/*********************************************************************
* Macros: GetClipBottom()
*
* PreCondition: none
*
* Input: none
*
* Output: bottom clipping border
*
* Side Effects: none
*
* Overview: returns bottom clipping border
*
* Note: none
*
********************************************************************/
#define GetClipBottom() _clipBottom

/*********************************************************************
* Macros: SetClip(control)
*
* PreCondition: none
*
* Input: control - 0 disable/ 1 enable
*
* Output: none
*
* Side Effects: none
*
* Overview: enables/disables clipping 
*
* Note: none
*
********************************************************************/
#define SetClip(control) _clipRgn=control;

/*********************************************************************
* Macros: IsDeviceBusy()
*
* PreCondition: none
*
* Input: none
*
* Output: busy status
*
* Side Effects: none
*
* Overview:  returns non-zero if LCD controller is busy 
* (previous drawing operation is not complete)
*
* Note: the controller is always accessible
*
********************************************************************/
#define IsDeviceBusy()  0

/*********************************************************************
* Macros: SetPalette(colorNum, color)
*
* PreCondition: none
*
* Input: colorNum - register number, color - color
*
* Output: none
*
* Side Effects: none
*
* Overview:  sets palette register
*
* Note: the controller has no palette
*
********************************************************************/
#define SetPalette(colorNum, color)

/*********************************************************************
* Function:  void  DelayMs(WORD time)
*
* PreCondition: none
*
* Input: time - delay in ms
*
* Output: none
*
* Side Effects: none
*
* Overview: delays execution on time specified in ms
*
* Note: 
*
********************************************************************/
void DelayMs(WORD time);


#endif // _S6D0129P_H
