//*********************************************************
//*	Dec 26,	2008	Run Length Encoding for RGB
void	DisplayRLE_RGB(unsigned char *rleBuff, COLOR *colorFade, boolean fillBack, int startY)
{
COLOR	bgColor;
COLOR	rleColor;
int		ii, jj;
int		cc;
int		rlePixCount;
int		pixelX, pixelY;
int		rleColorValue;
int		pixlesWide, pixelsTall;
int		rowCount;
int		byte1, byte2, byte3, byte4;
int		adjusted_Red;
int		adjusted_Green;
int		adjusted_Blue;

	cc		=	0;
	
	byte1			=	rleBuff[cc++] & 0x00ff;
	byte2			=	rleBuff[cc++] & 0x00ff;
	byte3			=	rleBuff[cc++] & 0x00ff;
	byte4			=	rleBuff[cc++] & 0x00ff;
	pixlesWide		=	(byte1 << 8) + byte2;
	pixelsTall		=	(byte3 << 8) + byte4;


	adjusted_Red	=	(rleBuff[cc++] & 0x00ff)		- colorFade->red;
	adjusted_Green	=	(rleBuff[cc++] & 0x00ff)	- colorFade->green;
	adjusted_Blue	=	(rleBuff[cc++] & 0x00ff)	- colorFade->blue;
	cc++;			//*	1 filler char

	if (adjusted_Red < 0)	adjusted_Red	=	0;
	if (adjusted_Green < 0)	adjusted_Green	=	0;
	if (adjusted_Blue < 0)	adjusted_Blue	=	0;

	bgColor.red		=	adjusted_Red;
	bgColor.green	=	adjusted_Green;
	bgColor.blue	=	adjusted_Blue;


	pixelX		=	(kSCREEN_X_size / 2) - (pixlesWide / 2);
	pixelY		=	(kSCREEN_Y_size / 2) - (pixelsTall / 2);

	//*	is the background supposed to be filled in
	if (fillBack)
	{
   		dispColor(bgColor);
		for (jj=startY; jj<pixelY; jj++)
		{
			for (ii=0; ii<=kSCREEN_X_size; ii++)
			{
				dispPixel(ii, jj);
			}
		}
	}

	rowCount	=	0;
	
	while ((cc < 5000) && (rowCount < pixelsTall))
	{
		adjusted_Red	=	(rleBuff[cc] & 0x00ff)		- colorFade->red;
		adjusted_Green	=	(rleBuff[cc + 1] & 0x00ff)	- colorFade->green;
		adjusted_Blue	=	(rleBuff[cc + 2] & 0x00ff)	- colorFade->blue;
		if (adjusted_Red < 0)	adjusted_Red	=	0;
		if (adjusted_Green < 0)	adjusted_Green	=	0;
		if (adjusted_Blue < 0)	adjusted_Blue	=	0;

		rleColor.red	=	adjusted_Red;
		rleColor.green	=	adjusted_Green;
		rleColor.blue	=	adjusted_Blue;
		
		rlePixCount		=	rleBuff[cc + 3] & 0x00ff;

		if ((rleColor.red == 0) && (rleColor.green == 0) && (rleColor.blue == 0) && (rlePixCount == 0))
		{
			//*	we have a new line
			rowCount++;
			pixelX	=	(kSCREEN_X_size / 2) - (pixlesWide / 2);

			//*	as good a time as any to fill in the rest of the row
			if (fillBack)
			{
 		  		dispColor(bgColor);
				for (ii=0; ii<pixelX; ii++)
				{
					dispPixel(ii, pixelY);
				}
				for (ii=(pixelX + pixlesWide); ii<kSCREEN_X_size; ii++)
				{
					dispPixel(ii, pixelY);
				}
			}

			pixelY++;
		}
		else if ((rlePixCount > 0) && (rlePixCount < 256))
		{
	   			
   			dispColor(rleColor);
		
			for (jj=0; jj<rlePixCount; jj++)
			{
				dispPixel(pixelX, pixelY);
				pixelX++;
			}
		}
		cc	+=	4;
	}
	if (fillBack)
	{
   		dispColor(bgColor);
		for (jj=pixelY; jj<=kSCREEN_Y_size; jj++)
		{
			for (ii=0; ii<=kSCREEN_X_size; ii++)
			{
				dispPixel(ii, jj);
			}
		}
	}
}
//*******************************************************************************
void	DisplaySplashScreen(void)
{
COLOR	bgColor;
COLOR	fontColor;
int		ii;
int		yTextLoc;

#ifdef _STARTUPSCREEN_VERSION_
	char	startupMsg[128];
#endif

#ifdef _STARTUPSCREEN_VERSION_
	bgColor.red		=	0;
	bgColor.green	=	0;
	bgColor.blue	=	0;

	fontColor.red	=	0;
	fontColor.green	=	255;
	fontColor.blue	=	0;
	
	//*	display the overall library version
	yTextLoc	=	10;
	strcpy(startupMsg, kDisplayHardwareString);
	strcat(startupMsg, " ");
	strcat(startupMsg, kDisplayHardwareVersion);
	dispPutS(startupMsg, 5, yTextLoc, fontColor, bgColor);
	yTextLoc	+=	kLinrSpacing;
	
	
	//*	display the SubProcessing library version
	strcpy(startupMsg, "Arduino Procssing Library ");
	strcat(startupMsg, kSubP_VersionString);
	strcat(startupMsg, " ");

#ifdef _SUBP_OPTION_GAMES_
	strcat(startupMsg, "+G");
#endif
#ifdef _SUBP_OPTION_KEYBOARD_
	strcat(startupMsg, "+K");
#endif

	dispPutS(startupMsg, 5, yTextLoc, fontColor, bgColor);
	yTextLoc	+=	kLinrSpacing;

#endif

#ifdef _DEBUG_RECTS_
 	dispColor(bgColor);
 	ii	=	kSCREEN_Y_size / 2;
 	ii	-=	25;
 	ii	=	kSCREEN_Y_size / 3;
 	while (ii > 30)
 	{
		fill(random(255), random(255), random(255));
		stroke(random(255), random(255), random(255));
		drawrect((kSCREEN_X_size / 2) - ii, (kSCREEN_Y_size / 2) - ii, (ii * 2), (ii * 2));
		
		ii	-=	10;
 	}
	fill(0);
	stroke(255);
#endif

#ifdef _STARTUPSCREEN_LIQUIDWARE_
   	for (ii=0; ii<190; ii+=10)
	{
		//*	this is the FADE color, how much to subtract from the actual colors
		//*	0 means none.
		bgColor.red		=	ii;
		bgColor.green	=	ii;
		bgColor.blue	=	ii;
		DisplayRLE_RGB(gLiquidWareLogo, &bgColor, false, kMatixTopOffset);
#ifdef _STARTUPSCREEN_MATRIX_
		if (ii > 100)
		{
			MatrixDisplay(kMatixTopOffset, 3);
		}
#endif
	}
#endif
#ifdef _STARTUPSCREEN_MATRIX_
	ii	=	0;
	while(!serialAvailable() && (ii < 2000))
	{
		MatrixDisplay(kMatixTopOffset, 2);
		ii++;

		gettouch();
		if ((mouseX > 200) && (mouseY < 100))
		{
			break;
		}
	}
#endif



#ifdef _SUBP_OPTION_7_SEGMENT_dontDisplay
int	xx, yy;
//	dispClearScreen();

	xx	=	10;
	yy	=	50;
	for (ii=5; ii<30; ii += 4)
	{
		Display7SegmentString(xx, yy, "0123456789ABCDEF", ii);
		yy	+=	(ii * 2);
		yy	+=	8;
		if (ii > 230)
		{
			break;
		}
	}
	while (true)
	{
		//*	do nothing
	}
#endif



	bgColor.red		=	0;
	bgColor.green	=	0;
	bgColor.blue	=	0;
 	dispColor(bgColor);
//	drawrect(0, yTextLoc, 320, 240);
	
}
//*******************************************************************************
void	oled_init()
{
	COLOR black = {0,0,0};
	
	volatile unsigned long x,y;
	
	SETBIT(V5_SHUTDOWN_DDR, V5_SHUTDOWN_PNUM);
	CLRBIT(V5_SHUTDOWN_PORT, V5_SHUTDOWN_PNUM);
	
	OLED_DATA_LOW_DDR = 0xFF; //output
	OLED_DATA_MED_DDR = 0xFF; //output
	OLED_DATA_HIGH_DDR = 0xFF; //output
	
	OLED_CTRL_DDR |=  (1<<OLED_DC) | 
					 (1<<OLED_CS) | 
					 (1<<OLED_RD) |
					 (1<<OLED_WR) |
					 (1<<OLED_RESET);

	for (x=0;x<100;x++)
	{
		/* Reset OLED */
		CLRBIT(OLED_CTRL_PORT,OLED_RESET);
	}

	for (x=0;x<100;x++)
	{
		/* OLED ON */
		SETBIT(OLED_CTRL_PORT,OLED_RESET);
	}

	dispCommand(kOLEDcmd_IFselect);
	
	dispCommand(kOLEDcmd_RGBinterfaceControl);
	dispData(0x0000);
	
	dispCommand(kOLEDcmd_EntryMode);
 //  dispData(0x8030);
	dispData(0x8031);
	
	dispCommand(kOLEDcmd_StandbyMode); //standby off
	dispData(0x0000);
	
	//delay
	for(y=0;y<5000;y++)
	for(x=0;x<10; x++)
	{
		asm("nop");
	}

	SETBIT(OLED_SUPPLY_PORT,OLED_SUPPLY_PNUM);
	SETBIT(OLED_SUPPLY_DDR,OLED_SUPPLY_PNUM); //turn on supply

	for(y=0;y<5000;y++)
	for(x=0;x<10; x++)
	{
		asm("nop");
	}

	dispCommand(kOLEDcmd_DisplayControl1); //display on
	dispData(0x0001);
	
	oled_brightness(1); //low brightness
	
	dispCommand(kOLEDcmd_GRAMread_write); //write / read graphics data
	
	dispColor(black);
	dispClearScreen();

}