/*
***********************************************************************************************
*					JHD12864G/J Graphical LCD Display Driver (KS0107/8 controller)
*
* File name		: ks0108.c
* Programmer 	: John Leung, TechToys. Hong Kong
* Web presence  : www.TechToys.com.hk
* Note			: 
* Language		: C18 version 3.6 under MPLAB IDE version 7.60
* Hardware		: 
* Date			:
***********************************************************************************************
*										DESCRIPTION
*
* This module provides an interface to JHD12864J Graphical LCD module of size 128x64 dots
* Display controller chip is KS0107/8
* LCD pinout function summarized as below
* --------- LCD		MCU -----------------
* pin 1		Vss 	- 0V
* pin 2 	Vdd 	- 5.0V
* pin 3		Vo  	- LCD Drive Voltage (adjust contrast, trimmer to Vee in our case)
* pin 4		D/I		- LCD_DI (H:Data, L:Command (data or command control))
* pin 5		R/W 	- LCD_RW (H:Read, L:Write (read status or write data/command))
* pin 6		E   	- LCD_EN (H/H->L (IC Select or chip enable, H->L effective))
* pin 7:14	DB0:DB7	- LCD_DATA
* pin 15	CS1		- LCD_CS1 (left, high effective)
* pin 16	CS2		- LCD_CS2 (right, high effective)
* pin 17	RST		- LCD_RST (reset pin, low effective)
* pin 18	Vee		- -10.0V, LCD driving voltage. There is a Vee generator built-in LCD
*					  module, thus there is no need to use external generator IC
* pin 19	EL1 / A - LCD_BL (Depends on the backlight option.
*						If it's a LED backlight, it's an anode.
*					  	If it is EL type, this is EL controller input)
* pin 20	EL2 / K	- GND (Cathode for LED-backlight. EL input (OR NC) for EL-backlight model)
***********************************************************************************************
*/

/*
***********************************************************************************************
* 28 June 2007
* Add GDisprStrAt() for MCC18 compiler (PIC)
***********************************************************************************************
*/

#ifndef _KS0108_H_
#define _KS0108_H_

typedef unsigned char 	BOOLEAN;


#define MAX_FONT_WIDTH 		8

typedef struct _font
	{
	unsigned char fontWidth;
	unsigned char fontBody[MAX_FONT_WIDTH];
	}font;



#define MAX_PAT_SIZE 		1024
typedef struct _pattern
	{
	unsigned char patWidth;
	unsigned char patHeight;
	unsigned char patBody[MAX_PAT_SIZE];
	}pattern;


/*
***********************************************************************************************
*										PORT DEFINITION
***********************************************************************************************
*/

//I/O port for data definition
#define LCD_DATA		    LATD
#define TRIS_LCD_DATA		TRISD
#define LCD_PORT			PORTD

//Control pin setting (C18 specific for PIC18 series)

#define LCD_RST				LATAbits.LATA0
#define LCD_CS2				LATAbits.LATA1
#define LCD_CS1				LATAbits.LATA2
#define LCD_EN				LATAbits.LATA3
#define LCD_RW				LATAbits.LATA4
#define LCD_DI				LATAbits.LATA5

#define TRIS_LCD_RST		TRISAbits.TRISA0
#define TRIS_LCD_CS2		TRISAbits.TRISA1
#define TRIS_LCD_CS1		TRISAbits.TRISA2
#define TRIS_LCD_EN			TRISAbits.TRISA3
#define TRIS_LCD_RW			TRISAbits.TRISA4
#define TRIS_LCD_DI			TRISAbits.TRISA5

/*
***********************************************************************************************
*										GLOBAL CONSTANTS
***********************************************************************************************
*/

#define MAX_ROW_PIXEL		64		//MAX_ROW_PIXEL the physical matrix length (y direction)									
#define MAX_COL_PIXEL		128		//MAX_COL_PIXEL the physical matrix width (x direction)
#define ENABLE				1
#define DISABLE				0
#define BLACK				1
#define WHITE				0


/*
***********************************************************************************************
*									FUNCTION PROTOTYPES
***********************************************************************************************
*/
void GDispInit(void);
void GDispSwitch(BOOLEAN sw);
void GDispWrByte(unsigned char col, unsigned char page, unsigned char c);
void GDispClr(unsigned char pat);
void GDispSetPixel(unsigned char x, unsigned char y, BOOLEAN color);
void GDispPixPatAt(unsigned char x, unsigned char y, const rom pattern* pPat, BOOLEAN color);
void GDispPixStrAt(unsigned char x, unsigned char y, const rom char *pStr, unsigned char fontSpace, BOOLEAN color);
void GDispCharAt(unsigned char x, unsigned char y, unsigned char c, unsigned char size, BOOLEAN color);

/*
***********************************************************************************************
*									FUNCTION PROTOTYPES
*									 HARDWARE SPECIFIC
***********************************************************************************************
*/
void			GDispInitPort(void);
void 			GDispReset(void);
void			GDispChipSel(unsigned char wing); 
void 			GDispWr(BOOLEAN type, unsigned char dat);
unsigned char 	GDispRd(BOOLEAN type);
#endif
