/*********************************************************************
 *                    Flowcode LED Sign Code
 *
 * File: Custom_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 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
 * -------+----+-----------------------------------------------------
 * 250609 | BR | Created
 * 260609 | ST | Modified so that each command uses a specific CS assertion
 *        |    |
 *        |    |
 *        |    |
 *
 *
 *
 ********************************************************************/

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

 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 = ??

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

[Settings]
CLSID={4B174520-75AB-4B14-A0F7-2AA6C203F1DC}
IsAnalogue=0
MultipleAllowed=0
Description=Custom Flowcode Component

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

[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=Initialize
3=ClearDisplay
4=PrintText
5=ScrollDisplay
6=LightPixel
7=ClearPixel
8=WriteData
9=ReadData
10=ConfigMode
11=SetBrightness
12=SetFlash

[MacroReturns]
1=void
2=void
3=void
4=void
5=void
6=void
7=void
8=void
9=char
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_Initialize]
Count=0

[MacroParamTypes_Initialize]


[MacroParameters_ClearDisplay]
Count=1
1=cs

[MacroParamTypes_ClearDisplay]
1=char

[MacroParameters_PrintText]
Count=2
1=Cursor
2=String

[MacroParamTypes_PrintText]
1=char
2=char*

[MacroParameters_ScrollDisplay]
Count=1
1=direction

[MacroParamTypes_ScrollDisplay]
1=char

[MacroParameters_LightPixel]
Count=3
1=cs
2=x
3=y

[MacroParamTypes_LightPixel]
1=char
2=char
3=char

[MacroParameters_ClearPixel]
Count=3
1=cs
2=x
3=y

[MacroParamTypes_ClearPixel]
1=char
2=char
3=char

[MacroParameters_WriteData]
Count=3
1=cs
2=address
3=data

[MacroParamTypes_WriteData]
1=char
2=char
3=char

[MacroParameters_ReadData]
Count=2
1=cs
2=address

[MacroParamTypes_ReadData]
1=char
2=char

[MacroParameters_ConfigMode]
Count=2
1=cs
2=mode

[MacroParamTypes_ConfigMode]
1=char
2=char

[MacroParameters_SetBrightness]
Count=2
1=cs
2=brightness

[MacroParamTypes_SetBrightness]
1=char
2=char

[MacroParameters_SetFlash]
Count=2
1=cs
2=flash

[MacroParamTypes_SetFlash]
1=char
2=char


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

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

/*InitialisationCode_Start*/

	//Initialise the chip select lines
	clear_bit(MX_LED_TRIS, MX_LED_CS1);
	set_bit(MX_LED_PORT, MX_LED_CS1);
	clear_bit(MX_LED_TRIS, MX_LED_CS2);
	set_bit(MX_LED_PORT, MX_LED_CS2);
	clear_bit(MX_LED_TRIS, MX_LED_CS3);
	set_bit(MX_LED_PORT, MX_LED_CS3);
	clear_bit(MX_LED_TRIS, MX_LED_CS4);
	set_bit(MX_LED_PORT, MX_LED_CS4);

	//Initialise the Control lines
	clear_bit(MX_LED_TRIS, MX_LED_WR);
	set_bit(MX_LED_PORT, MX_LED_WR);
	clear_bit(MX_LED_TRIS, MX_LED_RD);
	set_bit(MX_LED_PORT, MX_LED_RD);
	clear_bit(MX_LED_TRIS, MX_LED_DATA);
	set_bit(MX_LED_PORT, MX_LED_DATA);

/*InitialisationCode_End*/

/*InterruptCode_Start*/
/*InterruptCode_End*/




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


void GetDefines()
{
/*Macro_GetDefines_Start*/

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


//common defines
#define MX_LED_TRIS		trisd
#define MX_LED_PORT		portd

#define MX_LED_CS1		2
#define MX_LED_CS2		0
#define MX_LED_CS3		1
#define MX_LED_CS4		3
#define MX_LED_WR		4
#define MX_LED_RD		5
#define MX_LED_DATA		6


//global variables
char CUSTOM_A;
char CUSTOM_B;



//ASCII Pixel data
//(Generated by gLCD component)

rom char* ASCII1 = {0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,       // 32 = <space>
					0x00 , 0x06 , 0x5F , 0x06 , 0x00 ,       // 33 = !
					0x07 , 0x03 , 0x00 , 0x07 , 0x03 ,       // 34 = "
					0x24 , 0x7E , 0x24 , 0x7E , 0x24 ,       // 35 = #
					0x24 , 0x2B , 0x6A , 0x12 , 0x00 ,       // 36 = $
					0x63 , 0x13 , 0x08 , 0x64 , 0x63 ,       // 37 = %
					0x36 , 0x49 , 0x56 , 0x20 , 0x50 ,       // 38 = &
					0x00 , 0x07 , 0x03 , 0x00 , 0x00 ,       // 39 = '
					0x00 , 0x3E , 0x41 , 0x00 , 0x00 ,       // 40 = (
					0x00 , 0x41 , 0x3E , 0x00 , 0x00 ,       // 41 = )
					0x08 , 0x3E , 0x1C , 0x3E , 0x08 ,       // 42 = *
					0x08 , 0x08 , 0x3E , 0x08 , 0x08};       // 43 = +
rom char* ASCII2 = {0x00 , 0xE0 , 0x60 , 0x00 , 0x00 ,       // 44 = ,
					0x08 , 0x08 , 0x08 , 0x08 , 0x08 ,       // 45 = -
					0x00 , 0x60 , 0x60 , 0x00 , 0x00 ,       // 46 = .
					0x20 , 0x10 , 0x08 , 0x04 , 0x02 ,       // 47 = /
					0x3E , 0x51 , 0x49 , 0x45 , 0x3E ,       // 48 = 0
					0x00 , 0x42 , 0x7F , 0x40 , 0x00 ,       // 49 = 1
					0x62 , 0x51 , 0x49 , 0x49 , 0x46 ,       // 50 = 2
					0x22 , 0x49 , 0x49 , 0x49 , 0x36 ,       // 51 = 3
					0x18 , 0x14 , 0x12 , 0x7F , 0x10 ,       // 52 = 4
					0x2F , 0x49 , 0x49 , 0x49 , 0x31 ,       // 53 = 5
					0x3C , 0x4A , 0x49 , 0x49 , 0x30 ,       // 54 = 6
					0x01 , 0x71 , 0x09 , 0x05 , 0x03};       // 55 = 7
rom char* ASCII3 = {0x36 , 0x49 , 0x49 , 0x49 , 0x36 ,       // 56 = 8
					0x06 , 0x49 , 0x49 , 0x29 , 0x1E ,       // 57 = 9
					0x00 , 0x6C , 0x6C , 0x00 , 0x00 ,       // 58 = :
					0x00 , 0xEC , 0x6C , 0x00 , 0x00 ,       // 59 = ;
					0x08 , 0x14 , 0x22 , 0x41 , 0x00 ,       // 60 = <
					0x24 , 0x24 , 0x24 , 0x24 , 0x24 ,       // 61 = =
					0x00 , 0x41 , 0x22 , 0x14 , 0x08 ,       // 62 = >
					0x02 , 0x01 , 0x59 , 0x09 , 0x06 ,       // 63 = ?
					0x3E , 0x41 , 0x5D , 0x55 , 0x1E ,       // 64 = @
					0x7E , 0x09 , 0x09 , 0x09 , 0x7E ,       // 65 = A
					0x7F , 0x49 , 0x49 , 0x49 , 0x36 ,       // 66 = B
					0x3E , 0x41 , 0x41 , 0x41 , 0x22};       // 67 = C
rom char* ASCII4 = {0x7F , 0x41 , 0x41 , 0x41 , 0x3E ,       // 68 = D
					0x7F , 0x49 , 0x49 , 0x49 , 0x41 ,       // 69 = E
					0x7F , 0x09 , 0x09 , 0x09 , 0x01 ,       // 70 = F
					0x3E , 0x41 , 0x49 , 0x49 , 0x7A ,       // 71 = G
					0x7F , 0x08 , 0x08 , 0x08 , 0x7F ,       // 72 = H
					0x00 , 0x41 , 0x7F , 0x41 , 0x00 ,       // 73 = I
					0x30 , 0x40 , 0x40 , 0x40 , 0x3F ,       // 74 = J
					0x7F , 0x08 , 0x14 , 0x22 , 0x41 ,       // 75 = K
					0x7F , 0x40 , 0x40 , 0x40 , 0x40 ,       // 76 = L
					0x7F , 0x02 , 0x04 , 0x02 , 0x7F ,       // 77 = M
					0x7F , 0x02 , 0x04 , 0x08 , 0x7F ,       // 78 = N
					0x3E , 0x41 , 0x41 , 0x41 , 0x3E};       // 79 = O
rom char* ASCII5 = {0x7F , 0x09 , 0x09 , 0x09 , 0x06 ,       // 80 = P
					0x3E , 0x41 , 0x51 , 0x21 , 0x5E ,       // 81 = Q
					0x7F , 0x09 , 0x09 , 0x19 , 0x66 ,       // 82 = R
					0x26 , 0x49 , 0x49 , 0x49 , 0x32 ,       // 83 = S
					0x01 , 0x01 , 0x7F , 0x01 , 0x01 ,       // 84 = T
					0x3F , 0x40 , 0x40 , 0x40 , 0x3F ,       // 85 = U
					0x1F , 0x20 , 0x40 , 0x20 , 0x1F ,       // 86 = V
					0x3F , 0x40 , 0x3C , 0x40 , 0x3F ,       // 87 = W
					0x63 , 0x14 , 0x08 , 0x14 , 0x63 ,       // 88 = X
					0x07 , 0x08 , 0x70 , 0x08 , 0x07 ,       // 89 = Y
					0x71 , 0x49 , 0x45 , 0x43 , 0x00 ,       // 90 = Z
					0x00 , 0x7F , 0x41 , 0x41 , 0x00};       // 91 = [
rom char* ASCII6 = {0x02 , 0x04 , 0x08 , 0x10 , 0x20 ,       // 92 = <backslash>
					0x00 , 0x41 , 0x41 , 0x7F , 0x00 ,       // 93 = ]
					0x04 , 0x02 , 0x01 , 0x02 , 0x04 ,       // 94 = ^
					0x80 , 0x80 , 0x80 , 0x80 , 0x80 ,       // 95 = _
					0x00 , 0x03 , 0x07 , 0x00 , 0x00 ,       // 96 = `
					0x20 , 0x54 , 0x54 , 0x54 , 0x78 ,       // 97 = a
					0x7F , 0x44 , 0x44 , 0x44 , 0x38 ,       // 98 = b
					0x38 , 0x44 , 0x44 , 0x44 , 0x28 ,       // 99 = c
					0x38 , 0x44 , 0x44 , 0x44 , 0x7F ,       // 100 = d
					0x38 , 0x54 , 0x54 , 0x54 , 0x18 ,       // 101 = e
					0x08 , 0x7E , 0x09 , 0x09 , 0x00 ,       // 102 = f
					0x18 , 0xA4 , 0xA4 , 0xA4 , 0x7C};       // 103 = g
rom char* ASCII7 = {0x7F , 0x04 , 0x04 , 0x78 , 0x00 ,       // 104 = h
					0x00 , 0x00 , 0x7D , 0x00 , 0x00 ,       // 105 = i
					0x40 , 0x80 , 0x84 , 0x7D , 0x00 ,       // 106 = j
					0x7F , 0x10 , 0x28 , 0x44 , 0x00 ,       // 107 = k
					0x00 , 0x00 , 0x7F , 0x40 , 0x00 ,       // 108 = l
					0x7C , 0x04 , 0x18 , 0x04 , 0x78 ,       // 109 = m
					0x7C , 0x04 , 0x04 , 0x78 , 0x00 ,       // 110 = n
					0x38 , 0x44 , 0x44 , 0x44 , 0x38 ,       // 111 = o
					0xFC , 0x44 , 0x44 , 0x44 , 0x38 ,       // 112 = p
					0x38 , 0x44 , 0x44 , 0x44 , 0xFC ,       // 113 = q
					0x44 , 0x78 , 0x44 , 0x04 , 0x08 ,       // 114 = r
					0x08 , 0x54 , 0x54 , 0x54 , 0x20};       // 115 = s
rom char* ASCII8 = {0x04 , 0x3E , 0x44 , 0x24 , 0x00 ,       // 116 = t
					0x3C , 0x40 , 0x20 , 0x7C , 0x00 ,       // 117 = u
					0x1C , 0x20 , 0x40 , 0x20 , 0x1C ,       // 118 = v
					0x3C , 0x60 , 0x30 , 0x60 , 0x3C ,       // 119 = w
					0x6C , 0x10 , 0x10 , 0x6C , 0x00 ,       // 120 = x
					0x9C , 0xA0 , 0x60 , 0x3C , 0x00 ,       // 121 = y
					0x64 , 0x54 , 0x54 , 0x4C , 0x00 ,       // 122 = z
					0x08 , 0x3E , 0x41 , 0x41 , 0x00 ,       // 123 = {
					0x00 , 0x00 , 0x7F , 0x00 , 0x00 ,       // 124 = |
					0x00 , 0x41 , 0x41 , 0x3E , 0x08 ,       // 125 = }
					0x02 , 0x01 , 0x02 , 0x01 , 0x00};       // 126 = ~


void send(char in);
char receive();
void cs_enable(char cs);
void cs_disable(char cs);
void command();
void data();
void read();


void send(char in)
{
	//Configure data bit
	if (in)
		set_bit(MX_LED_PORT, MX_LED_DATA);
	else
		clear_bit(MX_LED_PORT, MX_LED_DATA);

	//Clock through data bit
	clear_bit(MX_LED_PORT, MX_LED_WR);				// WR 0
	delay_10us(1);
	set_bit(MX_LED_PORT, MX_LED_WR);				// WR 1
	delay_10us(1);
}



char receive()
{
	char in;

	//Clock through data bit
	clear_bit(MX_LED_PORT, MX_LED_RD);				// RD 0
	delay_10us(1);

	set_bit(MX_LED_PORT, MX_LED_RD);				// RD 1
	delay_10us(1);

	//Read data bit
	in = test_bit(MX_LED_PORT, MX_LED_DATA);

	return(in);
}



void cs_enable(char cs)
{
	if(cs == 1)
	{
		clear_bit(MX_LED_PORT, MX_LED_CS1);
	}
	else if(cs == 2)
	{
		clear_bit(MX_LED_PORT, MX_LED_CS2);
	}
	else if(cs == 3)
	{
		clear_bit(MX_LED_PORT, MX_LED_CS3);
	}
	else if(cs == 4)
	{
		clear_bit(MX_LED_PORT, MX_LED_CS4);
	}
}


void cs_disable(char cs)
{
	if(cs == 1)
	{
		set_bit(MX_LED_PORT, MX_LED_CS1);
	}
	else if(cs == 2)
	{
		set_bit(MX_LED_PORT, MX_LED_CS2);
	}
	else if(cs == 3)
	{
		set_bit(MX_LED_PORT, MX_LED_CS3);
	}
	else if(cs == 4)
	{
		set_bit(MX_LED_PORT, MX_LED_CS4);
	}
}


void command()
{
	send(1);
	send(0);
	send(0);
}


void data()
{
	send(1);
	send(0);
	send(1);
}


void read()
{
	send(1);
	send(1);
	send(0);
}


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

/*Macro_GetDefines_End*/
}



char Initialize()
{
/*Macro_Initialize_Start*/


	char master = 1;
	char slave1 = 2;
	char slave2 = 3;
	char slave3 = 4;

//Sys Dis

	cs_enable(master);
	command();
	send(0);
	send(0);
	send(0);
	send(0);
	send(0);
	send(0);
	send(0);
	send(0);
	send(0);
	cs_disable(master);
	delay_10us(5);

//Commons Option

	cs_enable(master);
	command();
	send(0);
	send(0);
	send(1);
	send(0);
	send(1);
	send(0);
	send(0);
	send(0);
	send(0);
	cs_disable(master);
	delay_10us(1);

//Master Mode CS1

	ConfigMode(master,1);		//Make device 1 the Master
	delay_10us(1);
	ConfigMode(slave1,0);		//Make device 2 a Slave
	delay_10us(1);
	ConfigMode(slave2,0);		//Make device 3 a Slave
	delay_10us(1);
	ConfigMode(slave3,0);		//Make device 4 a Slave
	delay_10us(1);

//SYS ON

	cs_enable(master);
	command();
	send(0);
	send(0);
	send(0);
	send(0);
	send(0);
	send(0);
	send(0);
	send(1);
	send(0);
	cs_disable(master);
	delay_10us(1);

//LED duty cycle on

	cs_enable(master);
	command();
	send(0);
	send(0);
	send(0);
	send(0);
	send(0);
	send(0);
	send(1);
	send(1);
	send(0);
	cs_disable(master);
	delay_10us(1);

//LED PWM 16/16

	cs_enable(master);
	command();
	send(1);
	send(0);
	send(1);
	send(0);
	send(1);
	send(1);
	send(0);
	send(0);
	send(0);
	cs_disable(master);

	ClearDisplay(master);
	ClearDisplay(slave1);
	ClearDisplay(slave2);
	ClearDisplay(slave3);

/*Macro_Initialize_End*/
}


void ClearDisplay(char cs)
{
/*Macro_ClearDisplay_Start*/

	char address = 0;

	for (address = 0; address < 0x40; address++)
	{
		WriteData(cs, address, 0);
	}

/*Macro_ClearDisplay_End*/
}


void PrintText(char Cursor, char* String, char MSZ_String)
{
/*Macro_PrintText_Start*/

	char idx, count, pos_Str, chipselect, temp, address;

	Cursor = Cursor * 2;

	for (idx=0; idx<MSZ_String; idx++)         	//Start at beginning of string and work along
    {
		//Pull out ASCII data for character
        pos_Str = String[idx] - 32;     		//Calculate place in ASCII memory

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

			if(Cursor < 64)
			{
				chipselect = 1;
				address = Cursor;
			}

			else if(Cursor < 128)
			{
				chipselect = 2;
				address = Cursor - 64;
			}

			else if(Cursor < 192)
			{
				chipselect = 3;
				address = Cursor - 128;
			}

			else
			{
				chipselect = 4;
				address = Cursor - 192;
			}

			//Send ASCII character data to LEDs
			WriteData (chipselect, Cursor, temp & 0x0f);

			WriteData (chipselect, Cursor+1, (temp & 0xf0) >> 4);

			Cursor = Cursor + 2;	//Move to next column
        }
        Cursor = Cursor + 2;		//Leave a letter spacing
	}

/*Macro_PrintText_End*/
}



void ScrollDisplay(char direction)
{
/*Macro_ScrollDisplay_Start*/

	char idx, idx2, cs, data, address;

	switch(direction)
	{

		case '0':
		case 'l':
		case 'L':

			for(idx = 0; idx < 254; idx++)
			{

				idx2 = idx + 2;			//Source Address

				if(idx2 < 64)
				{
					cs = 1;
					address = idx2;
				}
				else if(idx2 < 128)
				{
					cs = 2;
					address = idx2 - 64;
				}
				else if(idx2 < 192)
				{
					cs = 3;
					address = idx2 - 128;
				}
				else
				{
					cs = 4;
					address = idx2 - 192;
				}

				data = ReadData (cs, address);

				if(idx < 64)
				{
					cs = 1;
					address = idx;
				}
				else if(idx < 128)
				{
					cs = 2;
					address = idx - 64;
				}
				else if(idx < 192)
				{
					cs = 3;
					address = idx - 128;
				}
				else
				{
					cs = 4;
					address = idx - 192;
				}

				WriteData(cs, address, data);
			}
			break;


		case '1':
		case 'r':
		case 'R':

			for(idx = 255; idx > 1; idx--)
			{

				idx2 = idx - 2;			//Source Address

				if(idx2 < 64)
				{
					cs = 1;
					address = idx2;
				}
				else if(idx2 < 128)
				{
					cs = 2;
					address = idx2 - 64;
				}
				else if(idx2 < 192)
				{
					cs = 3;
					address = idx2 - 128;
				}
				else
				{
					cs = 4;
					address = idx2 - 192;
				}

				data = ReadData (cs, address);

				if(idx < 64)
				{
					cs = 1;
					address = idx;
				}
				else if(idx < 128)
				{
					cs = 2;
					address = idx - 64;
				}
				else if(idx < 192)
				{
					cs = 3;
					address = idx - 128;
				}
				else
				{
					cs = 4;
					address = idx - 192;
				}

				WriteData(cs, address, data);
			}
			break;
	}

/*Macro_ScrollDisplay_End*/
}


void LightPixel(char cs, char x, char y)
{
/*Macro_LightPixel_Start*/

	char address;
	char curr_data;

	address = x * 2;

	if (y > 3)
	{
		address = address + 1;
		y = y - 4;
	}

	curr_data = ReadData (cs, address);

	set_bit(curr_data, y);

	WriteData(cs, address, curr_data);

/*Macro_LightPixel_End*/
}

void ClearPixel(char cs, char x, char y)
{
/*Macro_ClearPixel_Start*/

	char address;
	char curr_data;

	address = x * 2;

	if (y > 3)
	{
		address = address + 1;
		y = y - 4;
	}

	curr_data = ReadData (cs, address);

	clear_bit(curr_data, y);

	WriteData(cs, address, curr_data);

/*Macro_ClearPixel_End*/
}


void WriteData(char cs, char address, char data)
{
/*Macro_WriteData_Start*/

	char idx;

	cs_enable(cs);

	data();

	for (idx = 0x40; idx > 0; idx=idx>>1)
	{
		if (address & idx)
			send(1);
		else
			send(0);
	}

	//for (idx = 0x08; idx > 0; idx=idx>>1)
	for (idx = 0x01; idx < 0x10; idx=idx<<1)
	{
		if (data & idx)
			send(1);
		else
			send(0);
	}

	cs_disable(cs);

/*Macro_WriteData_End*/
}


char ReadData(char cs, char address)
{
/*Macro_ReadData_Start*/

	char idx;
	char data;
	char detect;

	cs_enable(cs);

	read();

	for (idx = 0x40; idx > 0; idx=idx>>1)
	{
		if (address & idx)
			send(1);
		else
			send(0);
	}

	set_bit(MX_LED_TRIS, MX_LED_DATA);

	data = 0;

	for (idx = 0x01; idx < 0x10; idx=idx<<1)
	{
		detect = receive();
		if (detect)
			data = data | idx;
	}

	clear_bit(MX_LED_TRIS, MX_LED_DATA);
	set_bit(MX_LED_PORT, MX_LED_DATA);

	cs_disable(cs);

	return(data);

/*Macro_ReadData_End*/
}


char ConfigMode(char cs, char mode)
{
/*Macro_ConfigMode_Start*/

	cs_enable(cs);
	command();

	if(mode)			//Master Mode
	{
		send(0);
		send(0);
		send(0);
		send(1);
		send(0);
		send(1);
		send(0);
		send(0);
		send(0);
	}
	else				//Slave Mode
	{
		send(0);
		send(0);
		send(0);
		send(1);
		send(0);
		send(0);
		send(0);
		send(0);
		send(0);
	}

	cs_disable(cs);

/*Macro_ConfigMode_End*/
}


void SetBrightness(char cs, char brightness)
{
/*Macro_SetBrightness_Start*/

	char idx;

	cs_enable(cs);
	command();

	send(1);
	send(0);
	send(1);
	send(0);

	for(idx = 0x08; idx > 0x00; idx=idx>>1)
	{
		if (idx & brightness)
			send(1);
		else
			send(0);
	}

	send(0);

	cs_disable(cs);

/*Macro_SetBrightness_End*/
}


void SetFlash(char cs, char flash)
{
/*Macro_SetFlash_End*/

	cs_enable(cs);
	command();

	send(0);
	send(0);
	send(0);
	send(0);
	send(1);
	send(0);
	send(0);

	if(flash)
		send(1);
	else
		send(0);

	send(0);

	cs_disable(cs);

/*Macro_SetFlash_End*/
}