/*********************************************************************
 *           Flowcode Graphical LCD Type 5 Component Code
 *
 * File: gLCD_5.c
 *
 * (c) 2009 Matrix Multimedia Ltd.
 * http://www.matrixmultimedia.com
 *
 * Software License Agreement
 *
 * The software supplied herewith by Matrix Multimedia Ltd (the
 * “Company”) for its Flowcode graphical programming language is
 * intended and supplied to you, the Company’s customer, for use
 * solely and exclusively on the Company's products. The software
 * is owned by the Company, and is protected under applicable
 * copyright laws. All rights are reserved. Any use in violation
 * of the foregoing restrictions may subject the user to criminal
 * sanctions under applicable laws, as well as to civil liability
 * for the breach of the terms and conditions of this licence.
 *
 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 * Changelog:
 *
 *  date  | by | description
 * -------+----+-----------------------------------------------------
 * 240407 | ST | Created
 * 220108 | BR | Edited for GfxLCD
 * 300108 | ST | Minor edits to function names, etc.
 * 300108 | ST | Addition of pin info
 * 300108 | BR | Edited to include global color functions
 * 300108 | ST | Removed tabs, fixed a few errors & added extra properties
 * 010208 | BR | Edited Print fonts, Added DrawBox fill functionality
 * 040208 | ST | Removed "BackPlot" function and renamed "ForePlot" to "Plot"
 * 080408 | BR | Added PrintNumber Function
 * 230309 | BR | Moves private functions from Add Defines section
 * 130509 | BR | Added % definitions to DefinesCode section
 * 100909 | BR | Added HiTECH compatible defines
 * 200909 | BR | Fixed small bug with print string routine for HITECH
 * 280110 | BR | Updated to work with the EB057 GLCD module.
 * 290110 | BR | Fixed a window size bug with the EB043.
 * 040210 | BR | Added a S65 type display controller
 * 300410 | BR | fixed a problem where the ports were swapped on the EB057 module.
 * 020211 | BR | Fixed a bug where EB057 colour data was being output incorrectly
 * 020311 | BR | Added bit banged SPI support for the EB057 gLCD
 * 230311 | BR | Changed code to work for hitech and boostc without ifdefs
 * 190511 | BR | Added 4D Display Compatibility to standard functions
 * 201011 | BR | Converted to universal gLCD component
 * 011211 | BR | Updated FC_CAL_Bit functions
 * 120312 | BR | UART Send CAL function needs to send 16-bit value as param to allow 9-bit data mode
 * 140312 | BR | Removed DrawEllipse function from 4D displays - Unknown compilation error
 * 090412 | BR | Fixed faulty startup code causing displays to not always work straight off
 * 160412 | BR | Added baud mod function to allow device to run 12x faster
 * 010512 | BR | New Baud too fast - corruption using touchscreen
 * 010512 | BR | Baud can go faster without corruption, baud update only available in HW mode
 * 140812 | BR | Added definition to allow gLCD to work with Zigbee Verbose mode
 * 260912 | BR | Bug Fix - Added missing definition %a_bus_type
 ********************************************************************/

/*********************************************************************

 Return & Parameter types:
   void
   char
   short
   char*

 Pin directions:
   0 = OUTPUT
   2 = INPUT
   3 = BIDIRECTIONAL

**********************************************************************

[Settings]
CLSID={2606B13A-2EA4-48AE-8A94-AE55337E6284}
IsAnalogue=0
MultipleAllowed=1
Description=Graphical LCD Flowcode Component

**********************************************************************

[Port]
DesiredPort=0
PortMandatory=0
SamePortAsID=0

[Pins]
Count=3
1=RX
2=TX
3=Reset

[PinPort]
# PORTA = 0
# PORTB = 1
# PORTC = 2
# PORTD = 3
# PORTE = 4
1=0
2=0
3=0

[PinDesiredBit]
1=7
2=6
3=5

[PinDirection]
# DIR_INPUT = 2
# DIR_OUTPUT = 0
# DIR_BIDIRECTIONAL = 3
1=0
2=0
3=0

[PinMustUsePort]
# boolean
1=0
2=0
3=1

[SamePortAsPinX]
# -1 = any pin
# otherwise, the pin number
1=-1
2=-1
3=-1

[PinMustUsePin]
# boolean
1=0
2=0
3=1

[PinValue]
# boolean (0 = off, 1 = on)
1=0
2=0
3=0

**********************************************************************

NOTE: For simulation to work, these macros _MUST_ remain at the indicated index:
	3=Init
	4=Clear
	5=Print
	6=PrintNumber
	7=Plot
	8=DrawLine
	9=DrawBox
	10=SetForeColor
	11=SetBackColor


[MacroNames]
Count=27
1=gLCD_Window
2=gLCD_SendByte
3=Init
4=Clear
5=Print
6=PrintNumber
7=Plot
8=DrawLine
9=DrawBox
10=SetForeColor
11=SetBackColor
12=G4D_DrawCircle
13=G4D_InitialiseCard
14=G4D_DumpScreenToCard
15=G4D_DumpScreenToFilePicaso
16=G4D_DisplayImageFromCard
17=G4D_DisplayImageFromFilePicaso
18=G4D_DisplayVideoFromCard
19=G4D_PlayAudioFromFilePicaso
20=G4D_SetVolumePicaso
21=G4D_EnableTouchscreenPicaso
22=G4D_DisableTouchscreenPicaso
23=G4D_SetTouchRegionPicaso
24=G4D_GetTouchCoordinatesPicaso
25=G4D_ReadTouchCoordinatesPicaso
26=G4D_SetVGADisplaySizePicaso
27=DisplayControl

[MacroReturns]
1=void
2=void
3=void
4=void
5=void
6=void
7=void
8=void
9=void
10=void
11=void
12=void
13=char
14=char
15=char
16=char
17=char
18=char
19=char
20=char
21=char
22=char
23=char
24=char
25=short
26=char
27=void

[MacroIsPrivate]
1=1
2=1
3=0
4=0
5=0
6=0
7=0
8=0
9=0
10=0
11=0
12=0
13=0
14=0
15=0
16=0
17=0
18=0
19=0
20=0
21=0
22=0
23=0
24=0
25=0
26=0
27=0


[MacroParameters_gLCD_Window]
Count=4
1=X1
2=Y1
3=X2
4=Y2

[MacroParamTypes_gLCD_Window]
1=char
2=char
3=char
4=char

[MacroParameters_gLCD_SendByte]
Count=2
1=Command
2=Lcdout

[MacroParamTypes_gLCD_SendByte]
1=char
2=unsigned int


[MacroParameters_Init]
Count=0

[MacroParamTypes_Init]


[MacroParameters_Clear]
Count=0

[MacroParamTypes_Clear]

[MacroParameters_Print]
Count=5
1=String
2=X
3=Y
4=Font
5=Transparent

[MacroParamTypes_Print]
1=char*
2=short
3=short
4=char
5=char

[MacroParameters_PrintNumber]
Count=5
1=Number
2=X
3=Y
4=Font
5=Transparent

[MacroParamTypes_PrintNumber]
1=short
2=short
3=short
4=char
5=char

[MacroParameters_Plot]
Count=2
1=X1
2=Y1

[MacroParamTypes_Plot]
1=short
2=short

[MacroParameters_DrawLine]
Count=4
1=X1
2=Y1
3=X2
4=Y2

[MacroParamTypes_DrawLine]
1=short
2=short
3=short
4=short

[MacroParameters_DrawBox]
Count=6
1=X1
2=Y1
3=X2
4=Y2
5=Transparent
6=Solid

[MacroParamTypes_DrawBox]
1=short
2=short
3=short
4=short
5=char
6=char

[MacroParameters_SetForeColor]
Count=3
1=Red
2=Green
3=Blue

[MacroParamTypes_SetForeColor]
1=char
2=char
3=char

[MacroParameters_SetBackColor]
Count=3
1=Red
2=Green
3=Blue

[MacroParamTypes_SetBackColor]
1=char
2=char
3=char

[MacroParameters_G4D_DrawCircle]
Count=4
1=X
2=Y
3=Radius
4=Solid

[MacroParamTypes_G4D_DrawCircle]
1=short
2=short
3=short
4=char

[MacroParameters_G4D_DrawElipsePicaso]
Count=5
1=X
2=Y
3=RX
4=RY
5=Solid

[MacroParamTypes_G4D_DrawElipsePicaso]
1=short
2=short
3=short
4=short
5=char

[MacroParameters_G4D_InitialiseCard]
Count=0

[MacroParamTypes_G4D_InitialiseCard]

[MacroParameters_G4D_DumpScreenToCard]
Count=7
1=X
2=Y
3=Width
4=Height
5=AddH
6=AddM
7=AddL

[MacroParamTypes_G4D_DumpScreenToCard]
1=short
2=short
3=short
4=short
5=char
6=char
7=char

[MacroParameters_G4D_DumpScreenToFilePicaso]
Count=5
1=X
2=Y
3=Width
4=Height
5=Filename

[MacroParamTypes_G4D_DumpScreenToFilePicaso]
1=short
2=short
3=short
4=short
5=char*

[MacroParameters_G4D_DisplayImageFromCard]
Count=8
1=X
2=Y
3=Width
4=Height
5=ColourMode
6=AddH
7=AddM
8=AddL

[MacroParamTypes_G4D_DisplayImageFromCard]
1=short
2=short
3=short
4=short
5=char
6=char
7=char
8=char

[MacroParameters_G4D_DisplayImageFromFilePicaso]
Count=5
1=X
2=Y
3=Filename
4=PosH
5=PosL

[MacroParamTypes_G4D_DisplayImageFromFilePicaso]
1=short
2=short
3=char*
4=short
5=short

[MacroParameters_G4D_DisplayVideoFromCard]
Count=10
1=X
2=Y
3=Width
4=Height
5=ColourMode
6=Delay
7=Frames
8=AddH
9=AddM
10=AddL

[MacroParamTypes_G4D_DisplayVideoFromCard]
1=short
2=short
3=short
4=short
5=char
6=char
7=short
8=char
9=char
10=char

[MacroParameters_G4D_PlayAudioFromFilePicaso]
Count=2
1=Play_Option
2=Filename

[MacroParamTypes_G4D_PlayAudioFromFilePicaso]
1=char
2=char*

[MacroParameters_G4D_SetVolumePicaso]
Count=1
1=Volume

[MacroParamTypes_G4D_SetVolumePicaso]
1=char

[MacroParameters_G4D_EnableTouchscreenPicaso]
Count=0

[MacroParamTypes_G4D_EnableTouchscreenPicaso]

[MacroParameters_G4D_DisableTouchscreenPicaso]
Count=0

[MacroParamTypes_G4D_DisableTouchscreenPicaso]

[MacroParameters_G4D_SetTouchRegionPicaso]
Count=4
1=X1
2=Y1
3=X2
4=Y2

[MacroParamTypes_G4D_SetTouchRegionPicaso]
1=short
2=short
3=short
4=short

[MacroParameters_G4D_GetTouchCoordinatesPicaso]
Count=0

[MacroParamTypes_G4D_GetTouchCoordinatesPicaso]

[MacroParameters_G4D_ReadTouchCoordinatesPicaso]
Count=1
1=Axis

[MacroParamTypes_G4D_ReadTouchCoordinatesPicaso]
1=char

[MacroParameters_G4D_SetVGADisplaySizePicaso]
Count=1
1=Size

[MacroParamTypes_G4D_SetVGADisplaySizePicaso]
1=char

[MacroParameters_DisplayControl]
Count=3
1=Backlight
2=Display
3=Contrast


[MacroParamTypes_DisplayControl]
1=char
2=char
3=char


 ********************************************************************/

/********************************************************************
 * ADDITIONAL CODE
 ********************************************************************/

/*DefinesCode_Start*/

/**** Macro Substitutions ****
a = unique ID
b = CON0 Port (RX)
c = CON0 PIN (RX)
d = CON1 Port (TX)
e = CON1 PIN (TX)
f = CON2 Port (RESET)
g = CON2 PIN (RESET)
h = CON3 Port (Not Connected)
i = CON3 PIN (Not Connected)
j = Default Background Colour
k = Default Foreground Colour
l = SPI or UART channel
m, n, o, p, q, r, s, t = ASCII Pixel data
******************************/

//EB07x Picaso gLCD Driver Code
//Display Part Number -
//Driver IC Part Number -

#include <string.h>

//Definitions for Reset control line
#define %a_MX_GFXLCD_PORT_RS		port%f
#define %a_MX_GFXLCD_TRIS_RS		tris%f
#define %a_MX_GFXLCD_PIN_RS			%g

//Common Defines
#define %a_MX_GFXLCD_BCOL       	%j
#define %a_MX_GFXLCD_FCOL       	%k

#define %a_MX_GLCD_BUSTYPE			%l

#ifndef MX_GFXLCD_TYPE5
 #define MX_GFXLCD_TYPE5
 #define MX_GFXLCD_CMD       		0
#endif

//Definitions for RS232 UART slot allocation
#ifndef	MX_UART_REF1
	#define MX_UART_REF1
	#define %a_MX_UART_UREF			1
	#define MX_UART_CHANNEL_1		%l
	#define MX_UART_TX_PIN_1		%e
	#define MX_UART_TX_PORT_1		port%d
	#define MX_UART_TX_TRIS_1		tris%d
	#define MX_UART_RX_PIN_1		%c
	#define MX_UART_RX_PORT_1		port%b
	#define MX_UART_RX_TRIS_1		tris%b
	#define MX_UART_FLOWEN_1		0
	#define MX_UART_CTS_PIN_1		0
	#define MX_UART_CTS_PORT_1		0
	#define MX_UART_RTS_PIN_1		0
	#define MX_UART_RTS_PORT_1		0
	#define MX_UART_BAUD_1			9600
	#define MX_UART_DBITS_1			8
	#define MX_UART_RETURN_1		0
	#define MX_UART_ECHO_1			0
	#define MX_UART_INT_1			0
#else
 #ifndef	MX_UART_REF2
	#define MX_UART_REF2
	#define %a_MX_UART_UREF			2
	#define MX_UART_CHANNEL_2		%l
	#define MX_UART_TX_PIN_2		%e
	#define MX_UART_TX_PORT_2		port%d
	#define MX_UART_TX_TRIS_2		tris%d
	#define MX_UART_RX_PIN_2		%c
	#define MX_UART_RX_PORT_2		port%b
	#define MX_UART_RX_TRIS_2		tris%b
	#define MX_UART_FLOWEN_2		0
	#define MX_UART_CTS_PIN_2		0
	#define MX_UART_CTS_PORT_2		0
	#define MX_UART_RTS_PIN_2		0
	#define MX_UART_RTS_PORT_2		0
	#define MX_UART_BAUD_2			9600
	#define MX_UART_DBITS_2			8
	#define MX_UART_RETURN_2		0
	#define MX_UART_ECHO_2			0
	#define MX_UART_INT_2			0
 #else
  #ifndef	MX_UART_REF3
	#define MX_UART_REF3
	#define %a_MX_UART_UREF			3
	#define MX_UART_CHANNEL_3		%l
	#define MX_UART_TX_PIN_3		%e
	#define MX_UART_TX_PORT_3		port%d
	#define MX_UART_TX_TRIS_3		tris%d
	#define MX_UART_RX_PIN_3		%c
	#define MX_UART_RX_PORT_3		port%b
	#define MX_UART_RX_TRIS_3		tris%b
	#define MX_UART_FLOWEN_3		0
	#define MX_UART_CTS_PIN_3		0
	#define MX_UART_CTS_PORT_3		0
	#define MX_UART_RTS_PIN_3		0
	#define MX_UART_RTS_PORT_3		0
	#define MX_UART_BAUD_3			9600
	#define MX_UART_DBITS_3			8
	#define MX_UART_RETURN_3		0
	#define MX_UART_ECHO_3			0
	#define MX_UART_INT_3			0
  #else
   #ifndef	MX_UART_REF4
	#define MX_UART_REF4
	#define %a_MX_UART_UREF			4
	#define MX_UART_CHANNEL_4		%l
	#define MX_UART_TX_PIN_4		%e
	#define MX_UART_TX_PORT_4		port%d
	#define MX_UART_TX_TRIS_4		tris%d
	#define MX_UART_RX_PIN_4		%c
	#define MX_UART_RX_PORT_4		port%b
	#define MX_UART_RX_TRIS_4		tris%b
	#define MX_UART_FLOWEN_4		0
	#define MX_UART_CTS_PIN_4		0
	#define MX_UART_CTS_PORT_4		0
	#define MX_UART_RTS_PIN_4		0
	#define MX_UART_RTS_PORT_4		0
	#define MX_UART_BAUD_4			9600
	#define MX_UART_DBITS_4			8
	#define MX_UART_RETURN_4		0
	#define MX_UART_ECHO_4			0
	#define MX_UART_INT_4			0
   #endif
  #endif
 #endif
#endif


#define %a_UART_Init			CAL_APPEND(FC_CAL_UART_Init_, %a_MX_UART_UREF)
#define %a_UART_Send			CAL_APPEND(FC_CAL_UART_Send_, %a_MX_UART_UREF)
#define %a_UART_Receive			CAL_APPEND(FC_CAL_UART_Receive_, %a_MX_UART_UREF)
#define %a_UART_Update_Baud		CAL_APPEND(FC_CAL_UART_Update_Baud_, %a_MX_UART_UREF)


extern void %a_UART_Init();
extern void %a_UART_Send(MX_UINT16 nChar);
extern MX_SINT16 %a_UART_Receive(MX_UINT8 nTimeout);
extern void %a_UART_Update_Baud (MX_UINT8 new_baud);


short %a_GFX_Fore_Color;
short %a_GFX_Back_Color;
short %a_Touch_X;
short %a_Touch_Y;

/*DefinesCode_End*/

/*InitialisationCode_Start*/
/*InitialisationCode_End*/

/*InterruptCode_Start*/
/*InterruptCode_End*/

/********************************************************************
 * FUNCTIONS
 ********************************************************************/


//-------------------------------------------------------------------------------
//Unused function
//-------------------------------------------------------------------------------
void gLCD_Window (short X1, short Y1, short X2, short Y2)
{
/*Macro_gLCD_Window_Start*/

//This macro is not used in this gLCD type

/*Macro_gLCD_Window_End*/
}


//-------------------------------------------------------------------------------
//Unused function
//-------------------------------------------------------------------------------
void gLCD_SendByte (char Command, unsigned int Lcdout)
{
/*Macro_gLCD_SendByte_Start*/

//This macro is not used in this gLCD type

/*Macro_gLCD_SendByte_End*/
}


//-------------------------------------------------------------------------------
//Initializing of the display
//-------------------------------------------------------------------------------
void Init (void)
{
/*Macro_Init_Start*/

	char Red, Green, Blue;
	%a_GFX_Fore_Color = %a_MX_GFXLCD_FCOL;
	%a_GFX_Back_Color = %a_MX_GFXLCD_BCOL;
	Green = (%a_GFX_Fore_Color >> 2) & 0x07;			//Convert Foreground to 16-bit colour
	Green = Green << 2;
	Red = (%a_GFX_Fore_Color >> 5) & 0x07;
	Red = Red << 3;
	Blue = (%a_GFX_Fore_Color) & 0x03;
	Blue = Blue << 3;
	%a_GFX_Fore_Color = Blue | (Green << 5);
	%a_GFX_Fore_Color = %a_GFX_Fore_Color | (Red << 11);
	Green = (%a_GFX_Back_Color >> 2) & 0x07;			//Convert Background to 16-bit colour
	Green = Green << 2;
	Red = (%a_GFX_Back_Color >> 5) & 0x07;
	Red = Red << 3;
	Blue = (%a_GFX_Back_Color) & 0x03;
	Blue = Blue << 3;
	%a_GFX_Back_Color = Blue | (Green << 5);
	%a_GFX_Back_Color = %a_GFX_Back_Color | (Red << 11);

	FC_CAL_Bit_Low_DDR (%a_MX_GFXLCD_PORT_RS, %a_MX_GFXLCD_TRIS_RS, %a_MX_GFXLCD_PIN_RS);			//Reset LCD
	%a_UART_Init();  //Call initialise function
	delay_us(100);                                  												//Wait for Reset
	FC_CAL_Bit_In_DDR (%a_MX_GFXLCD_PORT_RS, %a_MX_GFXLCD_TRIS_RS, %a_MX_GFXLCD_PIN_RS);			//Startup LCD

	delay_s(3);                                  												//Wait for Reset

	%a_UART_Receive(0);
	%a_UART_Receive(0);
	%a_UART_Receive(0);

	%a_UART_Send(0x55);								// Send autobaud command
	%a_UART_Receive(255);							//Try to collect Ack

	#if (%a_MX_GLCD_BUSTYPE != 0)

		%a_UART_Send(0x51);								//Update Baud Command
		%a_UART_Send(0x0C);								//Update to 57600bps
		%a_UART_Receive(255);							//Try to collect Ack
		%a_UART_Update_Baud(6);							//Update UART baud to 57600bps

	#endif

	%a_UART_Receive(0);
	%a_UART_Receive(0);

	Clear();

	%a_UART_Receive(0);
	%a_UART_Receive(0);

/*Macro_Init_End*/
}


//------------------------------------------------------------------------------
//Configures the color of the foreground
//------------------------------------------------------------------------------
void SetForeColor (char Red, char Green, char Blue)
{
/*Macro_SetForeColor_Start*/

	char msb, lsb;
	Red = Red >> 3;
	Green = Green >> 2;
	Blue = Blue >> 3;
	lsb = Blue | (Green << 5);
	msb = (Green >> 3) | (Red << 3);
	%a_GFX_Fore_Color = lsb | (msb << 8);

/*Macro_SetForeColor_End*/
}


//------------------------------------------------------------------------------
//Configures the color of the background
//------------------------------------------------------------------------------
void SetBackColor (char Red, char Green, char Blue)
{
/*Macro_SetBackColor_Start*/

	char msb, lsb;
	Red = Red >> 3;
	Green = Green >> 2;
	Blue = Blue >> 3;
	lsb = Blue | (Green << 5);
	msb = (Green >> 3) | (Red << 3);
	%a_UART_Send(msb);							// Send msb
	%a_UART_Send(lsb);							// Send lsb
	%a_UART_Receive(255);						// Try to collect Ack
	%a_GFX_Back_Color = lsb | (msb << 8);

/*Macro_SetBackColor_End*/
}


//------------------------------------------------------------------------------
//Defines window area and fills with color
//------------------------------------------------------------------------------
void DrawBox (short X1, short Y1, short X2, short Y2, char Transparent, char Solid)
{
/*Macro_DrawBox_Start*/

	if (Transparent == 0 && Solid == 0)									// In this case we need to draw solid shape in bg colour and then overlay with wire frame
	{
		%a_UART_Send(0x70);						// Configure Pen Size
		%a_UART_Send(0x00);						// Solid
		%a_UART_Receive(255);						// Try to collect Ack

		%a_UART_Send(0x72);						// CMD - Draw Rectangle
		%a_UART_Send((X1 >> 8));					// X1
		%a_UART_Send(X1);							// X1
		%a_UART_Send((Y1 >> 8));					// X1
		%a_UART_Send(Y1);							// Y1
		%a_UART_Send((X2 >> 8));					// X1
		%a_UART_Send(X2);							// X2
		%a_UART_Send((Y2 >> 8));					// X1
		%a_UART_Send(Y2);							// Y2
		%a_UART_Send((%a_GFX_Back_Color >> 8));	// BGND Colour
		%a_UART_Send(%a_GFX_Back_Color);			// BGND Colour
		%a_UART_Receive(255);						// Try to collect Ack
	}

	%a_UART_Send(0x70);							// Configure Pen Size
	if (Solid)
		%a_UART_Send(0x00);						// Solid
	else
		%a_UART_Send(0x01);						// Wire Frame
	%a_UART_Receive(255);							// Try to collect Ack

	%a_UART_Send(0x72);							// CMD - Draw Rectangle
	%a_UART_Send((X1 >> 8));						// X1
	%a_UART_Send(X1);								// X1
	%a_UART_Send((Y1 >> 8));						// X1
	%a_UART_Send(Y1);								// Y1
	%a_UART_Send((X2 >> 8));						// X1
	%a_UART_Send(X2);								// X2
	%a_UART_Send((Y2 >> 8));						// X1
	%a_UART_Send(Y2);								// Y2
	%a_UART_Send((%a_GFX_Fore_Color >> 8));		// FGND Colour
	%a_UART_Send(%a_GFX_Fore_Color);				// FGND Colour
	%a_UART_Receive(255);							// Try to collect Ack

/*Macro_DrawBox_End*/
}


//-------------------------------------------------------------------------------
//Clear screen
//-------------------------------------------------------------------------------
void Clear (void)
{
/*Macro_Clear_Start*/

	%a_UART_Send(0x42);						// Replace Background colour
	%a_UART_Send((%a_GFX_Back_Color>>8));
	%a_UART_Send(%a_GFX_Back_Color);
	%a_UART_Receive(255);						// Try to collect Ack

	%a_UART_Send(0x45);						// Clear Command
	%a_UART_Receive(255);						// Try to collect Ack

/*Macro_Clear_End*/
}


//-------------------------------------------------------------------------------
//Routine for displaying characters
//-------------------------------------------------------------------------------
void Print (char* String, short X, short Y, char Font, char Transparent)
{
/*Macro_Print_Start*/

	char idx;

	%a_UART_Send(0x4F);						// Set Text Transparancy command
	if(Transparent)
		%a_UART_Send(0);						// Transparancy on
	else
		%a_UART_Send(1);						// Transparancy off
	%a_UART_Receive(255);						// Try to collect Ack

	%a_UART_Send(0x53);						// Send String Command
	%a_UART_Send((X >> 8));					// X MSB
	%a_UART_Send(X);							// X LSB
	%a_UART_Send((Y >> 8));					// Y MSB
	%a_UART_Send(Y);							// Y LSB

	%a_UART_Send(Font);						// Font
	%a_UART_Send((%a_GFX_Fore_Color >> 8));	// Colour MSB
	%a_UART_Send(%a_GFX_Fore_Color);			// Colour LSB

	%a_UART_Send(1);							// Width
	%a_UART_Send(1);							// Height

	for (idx = 0; idx < MSZ_String; idx++)
	{
		if (String[idx] == 0)
			break;
		%a_UART_Send(String[idx]);			// Send byte from string
	}
	%a_UART_Send(0);							// Send null termination
	%a_UART_Receive(255);						// Try to collect Ack

/*Macro_Print_End*/
}


//-------------------------------------------------------------------------------
//Routine for Printing Numbers
//-------------------------------------------------------------------------------
void PrintNumber (short Number, short X, short Y, char Font, char Transparent)
{
/*Macro_PrintNumber_Start*/

	char temp_str[6];
	FCI_TOSTRING(Number,temp_str,6);					//Convert number to String
	Print (temp_str, 6, X, Y, Font, Transparent);		//Send string to Print function

/*Macro_PrintNumber_End*/
}


//---------------------------------------------------------------------------
//Outputs one pixel onto the screen
//---------------------------------------------------------------------------
void Plot (short X1, short Y1)
{
/*Macro_Plot_Start*/

		%a_UART_Send(0x50);						// Draw Pixel Command
		%a_UART_Send((X1 >> 8));					// X MSB
		%a_UART_Send(X1);							// X LSB
		%a_UART_Send((Y1 >> 8));					// Y MSB
		%a_UART_Send(Y1);							// Y LSB
		%a_UART_Send((%a_GFX_Fore_Color >> 8));	// Colour MSB
		%a_UART_Send(%a_GFX_Fore_Color);			// Colour LSB
		%a_UART_Receive(255);						// Try to collect Ack

/*Macro_Plot_End*/
}


//---------------------------------------------------------------------------
//Draws a line from one point to another
//---------------------------------------------------------------------------
void DrawLine (short X1, short Y1, short X2, short Y2)
{
/*Macro_DrawLine_Start*/

	%a_UART_Send(0x4C);						// Draw Line Command
	%a_UART_Send((X1 >> 8));					// X1 MSB
	%a_UART_Send(X1);							// X1 LSB
	%a_UART_Send((Y1 >> 8));					// Y1 MSB
	%a_UART_Send(Y1);							// Y1 LSB
	%a_UART_Send((X2 >> 8));					// X2 MSB
	%a_UART_Send(X2);							// X2 LSB
	%a_UART_Send((Y2 >> 8));					// Y2 MSB
	%a_UART_Send(Y2);							// Y2 LSB
	%a_UART_Send((%a_GFX_Fore_Color >> 8));	// Colour MSB
	%a_UART_Send(%a_GFX_Fore_Color);			// Colour LSB
	%a_UART_Receive(255);						// Try to collect Ack

/*Macro_DrawLine_End*/
}


void G4D_DrawCircle(short X, short Y, short Radius, char Solid)
{
/*Macro_G4D_DrawCircle_Start*/

	%a_UART_Send(0x70);							//Pen Size Command
	if (Solid)
		%a_UART_Send(0);						//Fill
	else
		%a_UART_Send(1);						//Wire Frame
	%a_UART_Receive(255);						//Wait for ack

	%a_UART_Send(0x43);							//Draw Circle Command
	%a_UART_Send((X >> 8));						// X MSB
	%a_UART_Send(X);							// X LSB
	%a_UART_Send((Y >> 8));						// Y MSB
	%a_UART_Send(Y);							// Y LSB
	%a_UART_Send((Radius >> 8));				// Radius MSB
	%a_UART_Send(Radius);						// Radius LSB
	%a_UART_Send((%a_GFX_Fore_Color >> 8));		// Colour MSB
	%a_UART_Send(%a_GFX_Fore_Color);			// Colour LSB
	%a_UART_Receive(255);						//Wait for ack

/*Macro_G4D_DrawCircle_End*/
}


void G4D_DrawElipsePicaso(short X, short Y, short RX, short RY, char Solid)
{
/*Macro_G4D_DrawElipsePicaso_Start*/

	%a_UART_Send(0x70);							//Pen Size Command
	if (Solid)
		%a_UART_Send(0);						//Fill
	else
		%a_UART_Send(1);						//Wire Frame
	%a_UART_Receive(255);						//Wait for ack

	%a_UART_Send(0x65);							//Draw Elipse Command
	%a_UART_Send(X >> 8);						// X MSB
	%a_UART_Send(X);							// X LSB
	%a_UART_Send(Y >> 8);						// Y MSB
	%a_UART_Send(Y);							// Y LSB
	%a_UART_Send(RX >> 8);						// Radius X MSB
	%a_UART_Send(RX);							// Radius X LSB
	%a_UART_Send(RY >> 8);						// Radius Y MSB
	%a_UART_Send(RY);							// Radius Y LSB
	%a_UART_Send(%a_GFX_Fore_Color >> 8);		// Colour MSB
	%a_UART_Send(%a_GFX_Fore_Color);			// Colour LSB
	%a_UART_Receive(255);						//Wait for ack

/*Macro_G4D_DrawElipsePicaso_End*/
}


char G4D_InitialiseCard (void)
{
/*Macro_G4D_InitialiseCard_Start*/

	char retval;

	%a_UART_Send(0x40);					//Extended Command Set
	%a_UART_Send(0x69);					//Init Card Command
	retval = %a_UART_Receive(255);		//Wait for ack

	if (retval == 6)
		return 0;									//Card initialised correctly
	return 255;

/*Macro_G4D_InitialiseCard_End*/
}


char G4D_DumpScreenToCard (short X, short Y, short Width, short Height, char AddH, char AddM, char AddL)
{
/*Macro_G4D_DumpScreenToCard_Start*/

	char retval;

	%a_UART_Send(0x40);					//Extended Command Set
	%a_UART_Send(0x43);					//Screen Dump Command
	%a_UART_Send((X >> 8));				//X
	%a_UART_Send(X);					//X
	%a_UART_Send((Y >> 8));				//Y
	%a_UART_Send(Y);					//Y
	%a_UART_Send((Width >> 8));			//Width
	%a_UART_Send(Width);				//Width
	%a_UART_Send((Height >> 8));		//Height
	%a_UART_Send(Height);				//Height
	%a_UART_Send(AddH);					//Sector Address
	%a_UART_Send(AddM);					//
	%a_UART_Send(AddL);					//
	retval = %a_UART_Receive(255);		//Wait for ack

	if (retval == 6)
		return 0;									//Command OK
	return 255;

/*Macro_G4D_DumpScreenToCard_End*/
}


char G4D_DumpScreenToFilePicaso (short X, short Y, short Width, short Height, char* Filename, char MSZ_Filename)
{
/*Macro_G4D_DumpScreenToFilePicaso_Start*/

	char idx, retval;

	%a_UART_Send(0x40);					//Extended Command Set
	%a_UART_Send(0x63);					//Screen Dump Command - Picaso only
	%a_UART_Send((X >> 8));				//X
	%a_UART_Send(X);					//X
	%a_UART_Send((Y >> 8));				//Y
	%a_UART_Send(Y);					//Y
	%a_UART_Send((Width >> 8));			//Width
	%a_UART_Send(Width);				//Width
	%a_UART_Send((Height >> 8));		//Height
	%a_UART_Send(Height);				//Height

	for (idx = 0; idx < MSZ_Filename; idx++)
	{
		if(Filename[idx] == 0)
			break;
		%a_UART_Send(Filename[idx]);	//Send character from string
	}
	%a_UART_Send(0);					//Null termination
	retval = %a_UART_Receive(255);		//Wait for ack

	if (retval == 6)
		return 0;									//Command OK
	return 255;

/*Macro_G4D_DumpScreenToFilePicaso_End*/
}


char G4D_DisplayImageFromCard (short X, short Y, short Width, short Height, char ColourMode, char AddH, char AddM, char AddL)
{
/*Macro_G4D_DisplayImageFromCard_Start*/

	char retval;

	%a_UART_Send(0x40);					//Extended Command Set
	%a_UART_Send(0x49);					//Display Image Command
	%a_UART_Send((X >> 8));				//X
	%a_UART_Send(X);					//X
	%a_UART_Send((Y >> 8));				//Y
	%a_UART_Send(Y);					//Y
	%a_UART_Send(AddH);					//Sector Address
	%a_UART_Send(AddM);					//
	%a_UART_Send(AddL);					//
	retval = %a_UART_Receive(255);		//Wait for ack

	if (retval == 6)
		return 0;									//Command OK
	return 255;

/*Macro_G4D_DisplayImageFromCard_End*/
}


char G4D_DisplayImageFromFilePicaso (short X, short Y, char* Filename, char MSZ_Filename, short PosH, short PosL)
{
/*Macro_G4D_DisplayImageFromFilePicaso_Start*/

	char retval, idx;

	%a_UART_Send(0x40);				//Extended Command Set
	%a_UART_Send(0x6D);				//Display Image Command

	for (idx = 0; idx < MSZ_Filename; idx++)
	{
		if(Filename[idx] == 0)
			break;
		%a_UART_Send(Filename[idx]);		//Send character
	}
	%a_UART_Send(0);						//Null termination

	%a_UART_Send((X >> 8));					//X
	%a_UART_Send(X);						//X
	%a_UART_Send((Y >> 8));					//Y
	%a_UART_Send(Y);						//Y
	%a_UART_Send((PosH >> 8));				//PosH
	%a_UART_Send(PosH);						//PosH
	%a_UART_Send((PosL >> 8));				//PosL
	%a_UART_Send(PosL);						//PosL
	retval = %a_UART_Receive(255);			//Wait for ack

	if (retval == 6)
		return 0;									//Command OK
	return 255;

/*Macro_G4D_DisplayImageFromFilePicaso_End*/
}


char G4D_DisplayVideoFromCard (short X, short Y, short Width, short Height, char ColourMode, char Delay, short Frames, char AddH, char AddM, char AddL)
{
/*Macro_G4D_DisplayVideoFromCard_Start*/

	char retval;

	%a_UART_Send(0x40);					//Extended Command Set
	%a_UART_Send(0x56);					//Display Video Command

	%a_UART_Send((X >> 8));				//X
	%a_UART_Send(X);					//X
	%a_UART_Send((Y >> 8));				//Y
	%a_UART_Send(Y);					//Y

	%a_UART_Send((Width >> 8));
	%a_UART_Send(Width);
	%a_UART_Send((Height >> 8));
	%a_UART_Send(Height);

	%a_UART_Send(ColourMode);

	%a_UART_Send(Delay);

	%a_UART_Send((Frames >> 8));
	%a_UART_Send(Frames);

	%a_UART_Send(AddH);
	%a_UART_Send(AddM);
	%a_UART_Send(AddL);

	retval = %a_UART_Receive(255);		//Wait for ack

	if (retval == 6)
		return 0;									//Command OK
	return 255;

/*Macro_G4D_DisplayVideoFromCard_End*/
}


char G4D_PlayAudioFromFilePicaso (char Play_Option, char* Filename, char MSZ_Filename)
{
/*Macro_G4D_PlayAudioFromFilePicaso_Start*/

	char idx, retval;

	%a_UART_Send(0x40);								//Extended Commands
	%a_UART_Send(0x6C);								//Play Audio Command
	%a_UART_Send(Play_Option);						//Play Option

	for (idx = 0; idx < MSZ_Filename; idx++)
	{
		if (Filename[idx] == 0)
			break;
		%a_UART_Send(Filename[idx]);				//Send character from string
	}
	%a_UART_Send(0);								//Null termination
	retval = %a_UART_Receive(255);					//Wait for ack

	if (retval == 6)
		return 0;													//Command OK
	return 255;

/*Macro_G4D_PlayAudioFromFilePicaso_End*/
}


char G4D_SetVolumePicaso (char Volume)
{
/*Macro_G4D_SetVolumePicaso_Start*/

	char retval;

	%a_UART_Send(0x76);								//Set Volume Command
	%a_UART_Send(Volume);							//Set Volume
	retval = %a_UART_Receive(255);					//Wait for ack

	if (retval == 6)
		return 0;													//Command OK
	return 255;

/*Macro_G4D_SetVolumePicaso_End*/
}


char G4D_EnableTouchscreenPicaso ()
{
/*Macro_G4D_EnableTouchscreenPicaso_Start*/

	char retval;

	%a_UART_Send(0x59);								//Display Control Command
	%a_UART_Send(0x05);								//Touchscreen Control
	%a_UART_Send(0x00);								//Enable Touchscreen
	retval = %a_UART_Receive(255);					//Wait for ack

	%a_UART_Send(0x59);								//Display Control Command
	%a_UART_Send(0x05);								//Touchscreen Control
	%a_UART_Send(0x02);								//Reset active region to entire display
	retval = %a_UART_Receive(255);					//Wait for ack

	if (retval == 6)
		return 0;													//Command OK
	return retval;

/*Macro_G4D_EnableTouchscreenPicaso_End*/
}


char G4D_DisableTouchscreenPicaso ()
{
/*Macro_G4D_DisableTouchscreenPicaso_Start*/

	char retval;

	%a_UART_Send(0x59);								//Display Control Command
	%a_UART_Send(0x05);								//Touchscreen Control
	%a_UART_Send(0x01);								//Disable Touchscreen
	retval = %a_UART_Receive(255);					//Wait for ack

	if (retval == 6)
		return 0;													//Command OK
	return 255;

/*Macro_G4D_DisableTouchscreenPicaso_End*/
}


char G4D_SetTouchRegionPicaso (short X1, short Y1, short X2, short Y2)
{
/*Macro_G4D_SetTouchRegionPicaso_Start*/

	char retval;

	%a_UART_Send(0x75);								//Set Touch Region Command
	%a_UART_Send((X1>>8));
	%a_UART_Send(X1);
	%a_UART_Send((Y1>>8));
	%a_UART_Send(Y1);
	%a_UART_Send((X2>>8));
	%a_UART_Send(X2);
	%a_UART_Send((Y2>>8));
	%a_UART_Send(Y2);
	retval = %a_UART_Receive(255);					//Wait for ack

	if (retval == 6)
		return 0;													//Command OK
	return 255;

/*Macro_G4D_SetTouchRegionPicaso_End*/
}


char G4D_GetTouchCoordinatesPicaso (void)
{
/*Macro_G4D_GetTouchCoordinatesPicaso_Start*/

	char mode;

	%a_UART_Send(0x6F);								//Get Touch Command
	%a_UART_Send(4);								//Send Touch Mode - Get Status

	%a_UART_Receive(255);							//Read Touch Mode
	mode = %a_UART_Receive(255);

	%a_UART_Receive(255);
	%a_UART_Receive(255);

	if (mode)
	{
		%a_UART_Send(0x6F);							//Get Touch Command
		%a_UART_Send(5);							//Send Touch Mode - Get Coordinates

		%a_Touch_X = %a_UART_Receive(255) << 8;
		%a_Touch_X = %a_Touch_X | %a_UART_Receive(255);
		%a_Touch_Y = %a_UART_Receive(255) << 8;
		%a_Touch_Y = %a_Touch_Y | %a_UART_Receive(255);
	}
	return mode;

/*Macro_G4D_GetTouchCoordinatesPicaso_End*/
}


short G4D_ReadTouchCoordinatesPicaso (char Axis)
{
/*Macro_G4D_ReadTouchCoordinatesPicaso_Start*/

	switch (Axis)
	{
		case 0:
		case 'x':
		case 'X':
		default:
			return %a_Touch_X;

		case 1:
		case 'y':
		case 'Y':
			return %a_Touch_Y;
	}

/*Macro_G4D_ReadTouchCoordinatesPicaso_End*/
}


char G4D_SetVGADisplaySizePicaso (char Size)
{
/*Macro_G4D_SetVGADisplaySizePicaso_Start*/

	char retval;

	%a_UART_Send(0x59);						//Display Control Command
	%a_UART_Send(0x0C);						//Resolution Control
	%a_UART_Send(Size);						//Set Resolution
	retval = %a_UART_Receive(255);			//Wait for ack

	if (retval == 6)
		return 0;													//Command OK
	return 255;

/*Macro_G4D_SetVGADisplaySizePicaso_End*/
}


void DisplayControl (char Backlight, char Display, char Contrast)
{
/*Macro_DisplayControl_Start*/

	char retval;

	%a_UART_Send(0x59);						//Display Control Command
	%a_UART_Send(0x00);						//Resolution Control
	%a_UART_Send(Backlight);				//Set Resolution
	retval = %a_UART_Receive(255);			//Wait for ack

	delay_ms(1);

	%a_UART_Send(0x59);						//Display Control Command
	%a_UART_Send(0x01);						//Resolution Control
	%a_UART_Send(Display);					//Set Resolution
	retval = %a_UART_Receive(255);			//Wait for ack

	delay_ms(1);

	%a_UART_Send(0x59);						//Display Control Command
	%a_UART_Send(0x02);						//Resolution Control
	%a_UART_Send(Contrast);					//Set Resolution
	retval = %a_UART_Receive(255);			//Wait for ack

/*Macro_DisplayControl_End*/
}

