/*********************************************************************
 *                Flowcode 128x64 gLCD Component Code
 *
 * File: ssd1289gLCD_Code.c
 *
 * (c) 2007 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 theb 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
 * -------+----+-----------------------------------------------------
 * 010708 | BR | Created
 * 040908 | BR | Fixed Print Number function
 *        |    |
 *        |    |
 *        |    |
 *
 *
 *
 ********************************************************************/

/*********************************************************************
// This is from custom glcd component


 Return & parameter types:
   void
   char
   short
   char*

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

 Macro substitutions:
   %a = ??
   %b = ??
   %c = ??
   %d = ??
   %e = ??
   %f = ??
   %g = ??
   %h = ??
   %i = ??
   %j,= ??
   

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


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

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

[Pins]
Count=0

[PinPort]
# PortA = 0
# PortB = 1
# PortC = 2
# PortD = 3
# PortE = 4

[PinDesiredBit]

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

[PinMustUsePort]
# boolean

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

[PinMustUsePin]
# boolean

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

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

[MacroNames]
Count=12
1=GetDefines
2=LCD_Init
3=LCD_Set_Fore_Color
4=LCD_Set_Back_Color
5=LCD_Draw_Rect
6=LCD_Print_String
7=LCD_Print_Number
8=LCD_Draw_Line
9=LCD_Display_Clear
10=LCD_Send_Cmd
11=LCD_Sent_Data
12=LCD_Set_Pixel


[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

[MacroIsPrivate]
1=1
2=0
3=0
4=0
5=0
6=0
7=0
8=0
9=0
10=0
11=0
12=0

[MacroParameters_GetDefines]
Count=0

[MacroParamTypes_GetDefines]


[MacroParameters_LCD_Init]
Count=0

[MacroParamTypes_LCD_Init]


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


[MacroParameters_LCD_Set_Fore_Color]
1=char
2=har
3char



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


[MacroParameters_LCD_Set_Back_Color]
1=char
2=har
3char


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

[MacroParamTypes_LCD_Draw_Rect]
1=char
2=char
3=char
4=char
5=char
6=char

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

[MacroParamTypes_LCD_Print_String]
1=char*
2=char
3=char
4=char
5=char


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

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

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

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


[MacroParameters_LCD_Display_Clear]
Count=0


[MacroParamTypes_LCD_Display_Clear]




[MacroParameters_LCD_Send_Cmd]
Count=1
1=command


[MacroParamTypes_LCD_Send_Cmd]
1=char

[MacroParameters_LCD_Sent_Data]
Count=1
1=dat


[MacroParamTypes_LCD_Sent_Data]
1=char


[MacroParameters_LCD_Set_Pixel]
Count=3
1=x
2=y
3=color

[MacroParamTypes_LCD_Set_Pixel]
1=char
2=short
3=short





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


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

/*InitialisationCode_Start*/
/*InitialisationCode_End*/


/*InterruptCode_Start*/
/*InterruptCode_End*/


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


void GetDefines()
{
/*Macro_GetDefines_Start*/

}  //Dummy end of function to allow defines to be added correctly

#include <string.h>

//Common defines
#define MX_DATA_PORT 		%a
#define MX_DATA_TRIS		%b
#define MX_CONTROL_PORT		%c
#define MX_CONTROL_TRIS		%d
#define MX_CS	      %e
#define MX_CD		%f
#define MX_RD		%g
#define MX_RW			%h


// This is from custom glcd component
//ASCII Pixel data
#ifdef _BOOSTC
rom char* ASCII1 = {0x00 , 0x00 , 0x00 , 0x00 , 0x00,       // space // 32  - 43
					0x00 , 0x06 , 0x5F , 0x06 , 0x00,       // !
                              0x07 , 0x03 , 0x00 , 0x07 , 0x03,       // ''
					0x07 , 0x05 , 0x07 , 0x00 , 0x00,       // # aste
					0x7D , 0x12 , 0x11 , 0x12 , 0x7D,        // $ Ä
					0x63 , 0x13 , 0x08 , 0x64 , 0x63,       // %
					0x36 , 0x49 , 0x56 , 0x20 , 0x50,       // &
					0x00 , 0x07 , 0x03 , 0x00 , 0x00,       // '
					0x00 , 0x3E , 0x41 , 0x00 , 0x00,       // (
					0x00 , 0x41 , 0x3E , 0x00 , 0x00,       // )
					0x08 , 0x3E , 0x1C , 0x3E , 0x08,       // *
					0x08 , 0x08 , 0x3E , 0x08 , 0x08};      // +
rom char* ASCII2 = {0x00 , 0xE0 , 0x60 , 0x00 , 0x00,       // , // 44 - 55
					0x08 , 0x08 , 0x08 , 0x08 , 0x08,       // -
					0x00 , 0x60 , 0x60 , 0x00 , 0x00,       // .
					0x20 , 0x10 , 0x08 , 0x04 , 0x02,       // /
					0x3E , 0x51 , 0x49 , 0x45 , 0x3E,       // 0
					0x00 , 0x42 , 0x7F , 0x40 , 0x00,       // 1
					0x62 , 0x51 , 0x49 , 0x49 , 0x46,       // 2
					0x22 , 0x49 , 0x49 , 0x49 , 0x36,       // 3
					0x18 , 0x14 , 0x12 , 0x7F , 0x10,       // 4
					0x2F , 0x49 , 0x49 , 0x49 , 0x31,       // 5
					0x3C , 0x4A , 0x49 , 0x49 , 0x30,       // 6
					0x01 , 0x71 , 0x09 , 0x05 , 0x03};      // 7
rom char* ASCII3 = {0x36 , 0x49 , 0x49 , 0x49 , 0x36,       // 8 // 56 - 67
					0x06 , 0x49 , 0x49 , 0x29 , 0x1E,       // 9
					0x00 , 0x6C , 0x6C , 0x00 , 0x00,        // :
					0x00 , 0xEC , 0x6C , 0x00 , 0x00,       // ;
					0x08 , 0x14 , 0x22 , 0x41 , 0x00,       // <
					0x24 , 0x24 , 0x24 , 0x24 , 0x24,       // =
					0x00 , 0x41 , 0x22 , 0x14 , 0x08,       // >
					0x02 , 0x01 , 0x59 , 0x09 , 0x06,       // ?
					0x00 , 0x00 , 0x40 , 0x00 , 0x00,                  // @ piste pieni
					0x7E , 0x09 , 0x09 , 0x09 , 0x7E,       // A
					0x7F , 0x49 , 0x49 , 0x49 , 0x36,       // B
					0x3E , 0x41 , 0x41 , 0x41 , 0x22};      // C
rom char* ASCII4 = {0x7F , 0x41 , 0x41 , 0x41 , 0x3E,       // D // 68 - 79
					0x7F , 0x49 , 0x49 , 0x49 , 0x41,       // E
					0x7F , 0x09 , 0x09 , 0x09 , 0x01,       // F
					0x3E , 0x41 , 0x49 , 0x49 , 0x7A,       // G
					0x7F , 0x08 , 0x08 , 0x08 , 0x7F,       // H
					0x00 , 0x41 , 0x7F , 0x41 , 0x00,       // I
					0x30 , 0x40 , 0x40 , 0x40 , 0x3F,       // J
					0x7F , 0x08 , 0x14 , 0x22 , 0x41,       // K
					0x7F , 0x40 , 0x40 , 0x40 , 0x40,       // L
					0x7F , 0x02 , 0x04 , 0x02 , 0x7F,       // M
					0x7F , 0x02 , 0x04 , 0x08 , 0x7F,       // N
					0x3E , 0x41 , 0x41 , 0x41 , 0x3E};      // O
rom char* ASCII5 = {0x7F , 0x09 , 0x09 , 0x09 , 0x06,       // P // 80 - 91
					0x3E , 0x41 , 0x51 , 0x21 , 0x5E,       // Q
					0x7F , 0x09 , 0x09 , 0x19 , 0x66,       // R
					0x26 , 0x49 , 0x49 , 0x49 , 0x32,       // S
					0x01 , 0x01 , 0x7F , 0x01 , 0x01,       // T
					0x3F , 0x40 , 0x40 , 0x40 , 0x3F,       // U
					0x1F , 0x20 , 0x40 , 0x20 , 0x1F,       // V
					0x3F , 0x40 , 0x3C , 0x40 , 0x3F,       // W
					0x63 , 0x14 , 0x08 , 0x14 , 0x63,       // X
					0x07 , 0x08 , 0x70 , 0x08 , 0x07,       // Y
					0x71 , 0x49 , 0x45 , 0x43 , 0x00,       // Z
					0x38 , 0x45 , 0x44 , 0x45 , 0x38,};      // [ ö
rom char* ASCII6 = {0x02 , 0x04 , 0x08 , 0x10 , 0x20,       // \ // 92 - 103
					0x20 , 0x55 , 0x54 , 0x55 , 0x78,       // ] ä
					0x04 , 0x02 , 0x01 , 0x02 , 0x04,       // ^
					0x80 , 0x80 , 0x80 , 0x80 , 0x80,       // _
					0x00 , 0x03 , 0x07 , 0x00 , 0x00,       // `
					0x20 , 0x54 , 0x54 , 0x54 , 0x78,       // a
					0x7F , 0x44 , 0x44 , 0x44 , 0x38,       // b
					0x38 , 0x44 , 0x44 , 0x44 , 0x28,       // c
					0x38 , 0x44 , 0x44 , 0x44 , 0x7F,       // d
					0x38 , 0x54 , 0x54 , 0x54 , 0x18,       // e
					0x08 , 0x7E , 0x09 , 0x09 , 0x00,       // f
					0x18 , 0xA4 , 0xA4 , 0xA4 , 0x7C};      // g
rom char* ASCII7 = {0x7F , 0x04 , 0x04 , 0x78 , 0x00,       // h // 104 - 115
					0x00 , 0x00 , 0x7D , 0x00 , 0x00,       // i
					0x40 , 0x80 , 0x84 , 0x7D , 0x00,       // j
					0x7F , 0x10 , 0x28 , 0x44 , 0x00,       // k
					0x00 , 0x00 , 0x7F , 0x40 , 0x00,       // l
					0x7C , 0x04 , 0x18 , 0x04 , 0x78,       // m
					0x7C , 0x04 , 0x04 , 0x78 , 0x00,       // n
					0x38 , 0x44 , 0x44 , 0x44 , 0x38,       // o
					0xFC , 0x44 , 0x44 , 0x44 , 0x38,       // p
					0x38 , 0x44 , 0x44 , 0x44 , 0xFC,       // q
					0x44 , 0x78 , 0x44 , 0x04 , 0x08,       // r
					0x08 , 0x54 , 0x54 , 0x54 , 0x20};      // s
rom char* ASCII8 = {0x04 , 0x3E , 0x44 , 0x24 , 0x00,       // t // 116 - 126
					0x3C , 0x40 , 0x20 , 0x7C , 0x00,       // u
					0x1C , 0x20 , 0x40 , 0x20 , 0x1C,       // v
					0x3C , 0x60 , 0x30 , 0x60 , 0x3C,       // w
					0x6C , 0x10 , 0x10 , 0x6C , 0x00,       // x
					0x9C , 0xA0 , 0x60 , 0x3C , 0x00,       // y
					0x64 , 0x54 , 0x54 , 0x4C , 0x00,       // z
					0x08 , 0x3E , 0x41 , 0x41 , 0x00,       // {
					0x00 , 0x00 , 0x7F , 0x00 , 0x00,       // |
					0x00 , 0x41 , 0x41 , 0x3E , 0x08,       // }
					0x02 , 0x01 , 0x02 , 0x01 , 0x00};      // ~

#endif
#ifdef HI_TECH_C
const char ASCII1[] = {0x00 , 0x00 , 0x00 , 0x00 , 0x00,       // space // 32  - 43
					0x00 , 0x06 , 0x5F , 0x06 , 0x00,       // !
                              0x07 , 0x03 , 0x00 , 0x07 , 0x03,       // ''
					0x07 , 0x05 , 0x07 , 0x00 , 0x00,       // # aste
					0x7D , 0x12 , 0x11 , 0x12 , 0x7D,        // $ Ä
					0x63 , 0x13 , 0x08 , 0x64 , 0x63,       // %
					0x36 , 0x49 , 0x56 , 0x20 , 0x50,       // &
					0x00 , 0x07 , 0x03 , 0x00 , 0x00,       // '
					0x00 , 0x3E , 0x41 , 0x00 , 0x00,       // (
					0x00 , 0x41 , 0x3E , 0x00 , 0x00,       // )
					0x08 , 0x3E , 0x1C , 0x3E , 0x08,       // *
					0x08 , 0x08 , 0x3E , 0x08 , 0x08};      // +
const char ASCII2[] = {0x00 , 0xE0 , 0x60 , 0x00 , 0x00,       // , // 44 - 55
					0x08 , 0x08 , 0x08 , 0x08 , 0x08,       // -
					0x00 , 0x60 , 0x60 , 0x00 , 0x00,       // .
					0x20 , 0x10 , 0x08 , 0x04 , 0x02,       // /
					0x3E , 0x51 , 0x49 , 0x45 , 0x3E,       // 0
					0x00 , 0x42 , 0x7F , 0x40 , 0x00,       // 1
					0x62 , 0x51 , 0x49 , 0x49 , 0x46,       // 2
					0x22 , 0x49 , 0x49 , 0x49 , 0x36,       // 3
					0x18 , 0x14 , 0x12 , 0x7F , 0x10,       // 4
					0x2F , 0x49 , 0x49 , 0x49 , 0x31,       // 5
					0x3C , 0x4A , 0x49 , 0x49 , 0x30,       // 6
					0x01 , 0x71 , 0x09 , 0x05 , 0x03};      // 7
const char ASCII3[] = {0x36 , 0x49 , 0x49 , 0x49 , 0x36,       // 8 // 56 - 67
					0x06 , 0x49 , 0x49 , 0x29 , 0x1E,       // 9
					0x00 , 0x6C , 0x6C , 0x00 , 0x00,        // :
					0x00 , 0xEC , 0x6C , 0x00 , 0x00,       // ;
					0x08 , 0x14 , 0x22 , 0x41 , 0x00,       // <
					0x24 , 0x24 , 0x24 , 0x24 , 0x24,       // =
					0x00 , 0x41 , 0x22 , 0x14 , 0x08,       // >
					0x02 , 0x01 , 0x59 , 0x09 , 0x06,       // ?
					0x00 , 0x00 , 0x40 , 0x00 , 0x00,                  // @ piste pieni
					0x7E , 0x09 , 0x09 , 0x09 , 0x7E,       // A
					0x7F , 0x49 , 0x49 , 0x49 , 0x36,       // B
					0x3E , 0x41 , 0x41 , 0x41 , 0x22};      // C
const char ASCII4[] = {0x7F , 0x41 , 0x41 , 0x41 , 0x3E,       // D // 68 - 79
					0x7F , 0x49 , 0x49 , 0x49 , 0x41,       // E
					0x7F , 0x09 , 0x09 , 0x09 , 0x01,       // F
					0x3E , 0x41 , 0x49 , 0x49 , 0x7A,       // G
					0x7F , 0x08 , 0x08 , 0x08 , 0x7F,       // H
					0x00 , 0x41 , 0x7F , 0x41 , 0x00,       // I
					0x30 , 0x40 , 0x40 , 0x40 , 0x3F,       // J
					0x7F , 0x08 , 0x14 , 0x22 , 0x41,       // K
					0x7F , 0x40 , 0x40 , 0x40 , 0x40,       // L
					0x7F , 0x02 , 0x04 , 0x02 , 0x7F,       // M
					0x7F , 0x02 , 0x04 , 0x08 , 0x7F,       // N
					0x3E , 0x41 , 0x41 , 0x41 , 0x3E};      // O
const char ASCII5[] = {0x7F , 0x09 , 0x09 , 0x09 , 0x06,       // P // 80 - 91
					0x3E , 0x41 , 0x51 , 0x21 , 0x5E,       // Q
					0x7F , 0x09 , 0x09 , 0x19 , 0x66,       // R
					0x26 , 0x49 , 0x49 , 0x49 , 0x32,       // S
					0x01 , 0x01 , 0x7F , 0x01 , 0x01,       // T
					0x3F , 0x40 , 0x40 , 0x40 , 0x3F,       // U
					0x1F , 0x20 , 0x40 , 0x20 , 0x1F,       // V
					0x3F , 0x40 , 0x3C , 0x40 , 0x3F,       // W
					0x63 , 0x14 , 0x08 , 0x14 , 0x63,       // X
					0x07 , 0x08 , 0x70 , 0x08 , 0x07,       // Y
					0x71 , 0x49 , 0x45 , 0x43 , 0x00,       // Z
					0x38 , 0x45 , 0x44 , 0x45 , 0x38,};      // [ ö
const char ASCII6[] = {0x02 , 0x04 , 0x08 , 0x10 , 0x20,       // \ // 92 - 103
					0x20 , 0x55 , 0x54 , 0x55 , 0x78,       // ] ä
					0x04 , 0x02 , 0x01 , 0x02 , 0x04,       // ^
					0x80 , 0x80 , 0x80 , 0x80 , 0x80,       // _
					0x00 , 0x03 , 0x07 , 0x00 , 0x00,       // `
					0x20 , 0x54 , 0x54 , 0x54 , 0x78,       // a
					0x7F , 0x44 , 0x44 , 0x44 , 0x38,       // b
					0x38 , 0x44 , 0x44 , 0x44 , 0x28,       // c
					0x38 , 0x44 , 0x44 , 0x44 , 0x7F,       // d
					0x38 , 0x54 , 0x54 , 0x54 , 0x18,       // e
					0x08 , 0x7E , 0x09 , 0x09 , 0x00,       // f
					0x18 , 0xA4 , 0xA4 , 0xA4 , 0x7C};      // g
const char ASCII7[] = {0x7F , 0x04 , 0x04 , 0x78 , 0x00,       // h // 104 - 115
					0x00 , 0x00 , 0x7D , 0x00 , 0x00,       // i
					0x40 , 0x80 , 0x84 , 0x7D , 0x00,       // j
					0x7F , 0x10 , 0x28 , 0x44 , 0x00,       // k
					0x00 , 0x00 , 0x7F , 0x40 , 0x00,       // l
					0x7C , 0x04 , 0x18 , 0x04 , 0x78,       // m
					0x7C , 0x04 , 0x04 , 0x78 , 0x00,       // n
					0x38 , 0x44 , 0x44 , 0x44 , 0x38,       // o
					0xFC , 0x44 , 0x44 , 0x44 , 0x38,       // p
					0x38 , 0x44 , 0x44 , 0x44 , 0xFC,       // q
					0x44 , 0x78 , 0x44 , 0x04 , 0x08,       // r
					0x08 , 0x54 , 0x54 , 0x54 , 0x20};      // s
const char ASCII8[] = {0x04 , 0x3E , 0x44 , 0x24 , 0x00,       // t // 116 - 126
					0x3C , 0x40 , 0x20 , 0x7C , 0x00,       // u
					0x1C , 0x20 , 0x40 , 0x20 , 0x1C,       // v
					0x3C , 0x60 , 0x30 , 0x60 , 0x3C,       // w
					0x6C , 0x10 , 0x10 , 0x6C , 0x00,       // x
					0x9C , 0xA0 , 0x60 , 0x3C , 0x00,       // y
					0x64 , 0x54 , 0x54 , 0x4C , 0x00,       // z
					0x08 , 0x3E , 0x41 , 0x41 , 0x00,       // {
					0x00 , 0x00 , 0x7F , 0x00 , 0x00,       // |
					0x00 , 0x41 , 0x41 , 0x3E , 0x08,       // }
					0x02 , 0x01 , 0x02 , 0x01 , 0x00};      // ~

#endif
// this is from glcd component

	//global variables
		short LCD_Back_Color;
		short LCD_Fore_Color;

	
		//global variables
	

	

// Dummy function to close the defines section off
void CUSTOM_Dummy_Function();
void CUSTOM_Dummy_Function()


	{
/*Macro_GetDefines_End*/
}



/*InitialisationCode_Start*/
/*InitialisationCode_End*/

/*InterruptCode_Start*/
/*InterruptCode_End*/

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





void LCD_Init()
{
/*Macro_LCD_Init_Start*/	

	MX_DATA_TRIS = 0x00;								//Convert data port to output
	MX_DATA_PORT = 0x00;

				
	clear_bit(MX_CONTROL_TRIS, MX_RD);  //Convert control pins to output
	clear_bit(MX_CONTROL_TRIS, MX_CS);
	clear_bit(MX_CONTROL_TRIS, MX_RW);
	clear_bit(MX_CONTROL_TRIS, MX_DC);


	LCD_Send_Cmd(0x00);	//Start Oscillation
	LCD_Sent_Data(0x00);
	LCD_Sent_Data(0x01);

	LCD_Send_Cmd(0x01);	//Driver Output Control
	LCD_Sent_Data(0x23);
	LCD_Sent_Data(0x3F);

	LCD_Send_Cmd(0x02);	//LCD Drive AC Control
	LCD_Sent_Data(0x06);
	LCD_Sent_Data(0x00);

	LCD_Send_Cmd(0x03);	//Power Control (1)
	LCD_Sent_Data(0xA8);
	LCD_Sent_Data(0xA6);

	LCD_Send_Cmd(0x07);	//Display Control
	LCD_Sent_Data(0x00);
	LCD_Sent_Data(0x33);

	LCD_Send_Cmd(0x11);	//Entry Mode (R11h): Set Display color mode for 65k/262k color
	
	LCD_Sent_Data(0x68);	//DFM1:DFM0=1:1 for 65k color (POR)
	
	LCD_Sent_Data(0x30);
	
	LCD_Send_Cmd(0x0C);	//Power Control (2)
	LCD_Sent_Data(0x00);
	LCD_Sent_Data(0x05);

	LCD_Send_Cmd(0x0D);	//Power Control (3)
	LCD_Sent_Data(0x30);
	LCD_Sent_Data(0x0B);

	LCD_Send_Cmd(0x0E);	//Power Control (4)
	LCD_Sent_Data(0x20);
	LCD_Sent_Data(0x00);

	LCD_Send_Cmd(0x10);	//Exit Sleep Mode
	LCD_Sent_Data(0x00);
	LCD_Sent_Data(0x00);
	delay_ms(30);		//delay 30ms

	LCD_Send_Cmd(0x1E);	//Power Control (5)
	LCD_Sent_Data(0x00);
	LCD_Sent_Data(0xA8);

	LCD_Send_Cmd(0x44);	//Horizontal RAM address position start/end setup
	LCD_Sent_Data(0xEF);	//dec 239
	LCD_Sent_Data(0x00);	//dec 0, i.e. horizontal ranges from 0 -> 239
						//POR value is 0xEF00 anyway. This address must be set before RAM write
	
	LCD_Send_Cmd(0x45);	//Vertical RAM address start position setting
	LCD_Sent_Data(0x00);	//0x0000 = dec 0
	LCD_Sent_Data(0x00);

	LCD_Send_Cmd(0x46);	//Vertical RAM address end position setting
	LCD_Sent_Data(0x01);	//0x013F = dec 319
	LCD_Sent_Data(0x3F);

	LCD_Send_Cmd(0x22);	//RAM write command

	LCD_Display_Clear(0XFFFF); // init for white back color
	
/*Macro_LCD_Init_End*/
}
	







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

	

		if (Red > 0x3F)
			Red = 0x3F;

		if (Green > 0x1F)
			Green = 0x1F;

		if (Blue > 0x1F)
			Blue = 0x1F;

		LCD_Fore_Color = Green | (Red << 5);
		LCD_Fore_Color = LCD_Fore_Color | (Blue << 11);

	
/*Macro_LCD_Set_Fore_Color_End*/
}




//------------------------------------------------------------------------------
//Configures the color of the background
//------------------------------------------------------------------------------
//Modified by Mika S
void LCD_Set_Back_Color(char Red, char Green, char Blue)
{
/*Macro_LCD_Set_Back_Color_Start*/

		if (Red > 0x3F)
			Red = 0x3F;

		if (Green > 0x1F)
			Green = 0x1F;

		if (Blue > 0x1F)
			Blue = 0x1F;

		LCD_Back_Color = Green | (Red << 5);
		LCD_Back_Color = LCD_Back_Color | (Blue << 11);
		LCD_Display_Clear();

	



/*Macro_LCD_Set_Back_Color_End*/
}



//------------------------------------------------------------------------------
//Defines window area and fills with color 
//------------------------------------------------------------------------------
//Modified by Mika S
void LCD_Draw_Rect(char X1, char Y1, char X2, char Y2, char Transparent, char Solid)
{
/*Macro_LCD_Draw_Rect_Start*/
    char X,Y;
    short Pixel,Count,Color;

    LCD_Draw_Line(X1, Y1, X2, Y1);    //draw border
    LCD_Draw_Line(X2, Y1, X2, Y2);
    LCD_Draw_Line(X1, Y2, X2, Y2);
    LCD_Draw_Line(X1, Y1, X1, Y2);

    if (!Transparent)
    {
        X1 = X1 + 2;    //srink box to make room for border
        Y1 = Y1 + 2;
        //gLCD_Window(X1,Y1,X2,Y2);        //define window

        X = X2 - X1;
        X++;                            //# of X-Pixel
        Y = Y2 - Y1;
        Y++;                               //# of Y-Pixel
        Pixel = X * Y;                   //# of total pixel

        if (Solid)
            Color = LCD_Fore_Color;
        else
            Color = LCD_Back_Color;

        for (Count=0;Count<Pixel;Count++)
        {
			//ForePlot each pixel in the defined output window
		    LCD_Set_Pixel (X,Y,LCD_Fore_Color);
        }
    }
/*Macro_LCD_Draw_Rect_End*/
}

//-------------------------------------------------------------------------------
//Routine for displaying characters
//Modified by Mika S
//-------------------------------------------------------------------------------
void LCD_Print_String(char* String, char X, char Y, char Font, char Transparent)
{
/*Macro_LCD_Print_String_Start*/
    char xpix, ypix, pos_Str, length_Str, count, xcount, ycount, height, width, i;
    char Fontwidth = 1;                //First we assume small font
    char Fontheight = 1;
	char position;

    char temp[6];
    temp[5] = 0x00;                        //Spacing Line

    length_Str = strlen(String);       //Get length of string

    if (Font == 1)                     //Double Height Sizes //Double Width
    {
        Fontwidth = 2;
    }
    if (Font == 2)                         //Double Height and Width Sizes
    {
        Fontwidth = 2;
        Fontheight = 2;
    }
    if (Font == 3)                         //Double Height Sizes
    {
        Fontheight = 2;
    }

    xcount = 0;
    for (i=0;i<length_Str;i++)         //Start at beginning of string and work along
    {

        pos_Str = *(String + i) - 32;      //Calculate place in ASCII memory

        for (count = 0; count < 5; count++)        //Use correct buffer location
        {
            if (pos_Str < 12)
            {
				position = (pos_Str * 5) + count;
                temp[count]=ASCII1[position];
            }
            else if (pos_Str < 24)
            {
				position = ((pos_Str - 12)*5) + count;
                temp[count]=ASCII2[position];
            }
            else if (pos_Str < 36)
            {
				position = ((pos_Str - 24)*5) + count;
                temp[count]=ASCII3[position];
            }
            else if (pos_Str < 48)
            {
				position = ((pos_Str - 36)*5) + count;
                temp[count]=ASCII4[position];
            }
            else if (pos_Str < 60)
            {
				position = ((pos_Str - 48)*5) + count;
                temp[count]=ASCII5[position];
            }
            else if (pos_Str < 72)
            {
				position = ((pos_Str - 60)*5) + count;
                temp[count]=ASCII6[position];
            }
            else if (pos_Str < 84)
            {
				position = ((pos_Str - 72)*5) + count;
                temp[count]=ASCII7[position];
            }
            else if (pos_Str < 95)
            {
				position = ((pos_Str - 84)*5) + count;
                temp[count]=ASCII8[position];
            }
        }

        for (xpix=0;xpix<6;xpix++)    //For 6 ASCII bytes 0 - 5
        {
            for (width=0;width<Fontwidth;width++)
            {
                ycount = 0;
                for (ypix=0;ypix<8;ypix++)    //For 8 data bits in bytes 0 - 7
                {
                    for (height=0;height<Fontheight;height++)
                    {
                        if (test_bit(temp[xpix],ypix))
                        {
                            LCD_Set_Pixel(X + xcount, Y + ycount, LCD_Fore_Color);
                        }
                        else if (Transparent == 0)
                        {
							
							LCD_Set_Pixel(X,Y,LCD_Back_Color);
							
                        }
                        ycount++;
                    }
                }
                xcount++;
            }
        }
    }
/*Macro_LCD_Print_String_End*/
}


//-------------------------------------------------------------------------------
//Routine for Printing Numbers
//-------------------------------------------------------------------------------
void LCD_Print_Number(short Number, char X, char Y, char Font, char Transparent)
{
/*Macro_LCD_Print_Number_Start*/
	char temp_str[6];
	FCI_TOSTRING(Number,temp_str,6);					//Convert number to String
	LCD_Print_String(temp_str, 6, X, Y, Font, Transparent);		//Send string to Print function
/*Macro_LCD_Print_Number_End*/
}






//---------------------------------------------------------------------------
//Draws a line from one point to another
//Modified by Mika S
//---------------------------------------------------------------------------
void LCD_Draw_Line(char X1, char Y1, char X2, char Y2)
{
/*Macro_LCD_Draw_Line_Start*/
    int C1, M1;
    signed int D1 = 0;
    signed int Pixelx = X2 - X1;    //number of X-pixels;
    signed int Pixely = Y2 - Y1;    //Number of Y-pixels
    signed char Xinc = 1;
    signed char Yinc = 1;

    if (Pixelx < 0)        //If negative X direction
    {
        Xinc = -1;
        Pixelx = Pixelx * -1;
    }
    if (Pixely < 0)        //If negative Y direction
    {
        Yinc = -1;
        Pixely = Pixely * -1;
    }
    if (Pixely <= Pixelx)
    {
        C1 = 2 * Pixelx;
        M1 = 2 * Pixely;
        while (X1 != X2)
        {
            LCD_Set_Pixel(X1, Y1,LCD_Fore_Color);    //ForePlot line
            X1 = X1 + Xinc;
            D1 = D1 + M1;
            if (D1 > Pixelx)
            {
                Y1 = Y1 + Yinc;
                D1 = D1 - C1;
            }
        }
    }
    else
    {
        C1 = 2 * Pixely;
        M1 = 2 * Pixelx;
        while (Y1 != Y2)
        {
            LCD_Set_Pixel(X1, Y1,LCD_Fore_Color);    //ForePlot Line
            Y1 = Y1 + Yinc;
            D1 = D1 + M1;
            if (D1 > Pixely)
            {
                X1 = X1 + Xinc;
                D1 = D1 - C1;
            }
        }
        LCD_Set_Pixel(X1, Y1,LCD_Fore_Color);
    }
/*Macro_LCD_Draw_Line_End*/
}



// End of the glcd component data


// This is compeletely new






void LCD_Display_Clear(void)
{
/*Macro_LCD_Display_Clear_Start*/
	char i,hiByte,lowByte;
	short j;

	hiByte  = LCD_Back_Color >>8; 
	lowByte = char LCD_Back_Color;

	for(j=0;j<320;j++)
	{
		for(i=0;i<240;i++)
		{
			LCD_Sent_Data(hiByte);
			LCD_Sent_Data(lowByte);
		}
	}
/*Macro_LCD_Display_Clear_End*/	
}

void LCD_Send_Cmd(char command)  // command for display
{
/*Macro_LCD_Send_Cmd_Start*/

	MX_DATA_PORT = command;
	clear_bit(MX_CONTROL_PORT,MX_CD);		//CD LOW for display data
	clear_bit(MX_CONTROL_PORT,MX_RW);
	set_bit(MX_CONTROL_PORT,MX_RD);	
	clear_bit(MX_CONTROL_PORT,MX_CS);	  //CS low for read										
	set_bit(MX_CONTROL_PORT,MX_CS);      //CS high
	
	
/*Macro_LCD_Send_Cmd_End*/
}



void LCD_Sent_Data(char dat)  // data for display
{
/*Macro_LCD_Sent_Data_Start*/

	MX_DATA_PORT = dat;
	set_bit(MX_CONTROL_PORT,MX_CD);		//CD HIGH for display data
	clear_bit(MX_CONTROL_PORT,MX_RW);
	set_bit(MX_CONTROL_PORT,MX_RD);	
	clear_bit(MX_CONTROL_PORT,MX_CS);	  //CS low for read										
	set_bit(MX_CONTROL_PORT,MX_CS);      //CS high
	
	
/*Macro_LCD_Sent_Data_End*/
}







void LCD_Set_Pixel(char x, short y, short color) // Pixel data to display macros
{
/*Macro_LCD_Set_Pixel_Start*/
	char hiByte, lowByte;

	LCD_Send_Cmd(0x4E);		//set address in the 240 pixel direction
	LCD_Sent_Data(0x00);		//hiByte first fill all zero
	LCD_Sent_Data(x);			//0<x<239

	hiByte 	= y>>8;
	lowByte = y;

	LCD_Send_Cmd(0x4F);		//set address in the 320 pixel direction
	LCD_Sent_Data(hiByte);
	LCD_Sent_Data(lowByte);
	
	hiByte  = color>>8;
	lowByte = color;

	LCD_Send_Cmd(0x22);		//RAM write command
	LCD_Sent_Data(hiByte);		//write color content here
	LCD_Sent_Data(lowByte);	
	
	
/*Macro_LCD_Set_Pixel_End*/	
}





