bwlcddrv.c

来自「一个操作系统源代码 用于嵌入式设备 在Vc++环境下仿真 成功移植到多款处理器上」· C语言 代码 · 共 503 行

C
503
字号
/***************************************************************************************\
 *
 * Copyright (c) 2001 National ASIC Center, All Rights Reserved
 *
 * File Name:	lcddrv.c
 * Programmer:	longn_qi
 * 
 * Date of Creation:	2001/10/23
 * Date of Last Modify:	2001/11/01
 *
 * Description:	This is an application file for simulating LCD.
 *
 * Local Function List:
 * ER InitialLCD(  )
 * ER RefreshLCD(  )
 * ER Write2LCD( UH x1, UH x2, UH y1, UH y2, PIXEL *initAd, UH VRAMwidth )
 *
 * ER InitialPalette(  )
 * BYTE LinearColorMap( BYTE *color, BYTE index, BYTE maxColor, BYTE maxIndex)
 * GRAMPIXEL IndexToRGB( PIXEL index )
 *
 * Global Variable List:
 * RGN		 		GRAMRgn
 * LPBITMAPINFO 	pbmi
 * GRAMPIXEL 		palette[MAX_COLOR]
 *
 * Note:		Further file description refers to simdrv.txt
 *
 ***************************************************************************************
 *
 * Modification History
 * 
 * 2001/11/01		by longn_qi		remove function "Logo". rename file from "SimLCD.c"
 *									to "lcddrv.c".
 * 2001/10/25		by longn_qi		add function "Write2LCD",remove function "DirectDraw"
 *									and function "VRAMToGRAM".
 * 2001/10/24		by longn_qi		add function "DirectDraw", remove function "RegionCheck"
 * 2001/10/23		by longn_qi		create file.
 *
\***************************************************************************************/

/* System or Standard Header */
#define _SIZE_T_DEFINED
#include <windows.h>

#include <kernel\ros33\itron.h>
#include <sys\cdevice.h>
#include <sys\lmalloc.h>
#include <sys\vramop.h>


/* Application Header */

/* Local Data Type Definition */
//typedef UH			PIXEL;		//12bits per pixel (occupied 2 bytes)
//typedef UH			GRAMPIXEL;	//16bits per pixel

/* Local Macro Definition */
//color macro definition
/*
#define MAX_COLOR_BITS			12		// 12 bits color
#define MAX_RED_COLOR_BITS		4		// red color bits in index
#define MAX_GREEN_COLOR_BITS	4		// green color bits in index
#define MAX_BLUE_COLOR_BITS		4		// blue color bits in index

#define MAX_GRAM_COLOR			32		// color in GRAM
#define MAX_RED_COLOR			16		// red color number in index
#define MAX_GREEN_COLOR			16		// green color number in index
#define MAX_BLUE_COLOR			16		// blue color number in index
*/

#define MAX_GRAM_COLOR			255		// color in GRAM

//#define GRAM(X,Y)					*( GRAMinitAd + (Y)*LCD_WIDTH + (X) )
 

/* Extern Variables */
// import
__declspec( dllimport )HWND hwndScreen;
__declspec( dllimport )HWND hwndClient;


/* Extern Variables */
static GRAMPIXEL	*GRAMinitAd = NULL;	// LCD buffer using to store graph in bitmap form
static LPBITMAPINFO pbmi;				// pointer of bitmap header information structure

GRAMPALETTE *GRAMPalette = NULL;

//GRAMPALETTE *GRAMPalette;			// palette for convertion from color index to RGB color


/* Extern Declared Function */
extern void InitialLCD( void );
extern BOOL Write2LCD( WORD x1, WORD x2, WORD y1, WORD y2, VRAM *initAd );
extern BOOL RefreshLCD( void );
extern BOOL FreeLCD( void );
extern BOOL InitialPalette( void );
extern void FreePalette( void );
extern void LinearColorMap( BYTE *color, BYTE index, BYTE maxColor, BYTE maxIndex);

//---------test functions-----------------
extern void testLCD( void );

/* Local Declared Function */
static GRAMPIXEL IndexToRGB( PIXEL index );

/* Local Defined Function */
//////////////////////////////////////////////////////////////
//  Function Name: InitialLCD
//  Param In:					Param Description	
//		None
//								
//  Return Type:				Result Code Description
//		ER						E_OK	initial succeed				
//								E_NOMEM no memory	
//								E_OACV	display fail
//  Description: Initial LCD
//////////////////////////////////////////////////////////////
void InitialLCD( )
{
	DWORD bufSize;
//	UB BmpHdr[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*MAX_COLOR];
	BITMAPINFOHEADER bmi = {
		0x28,           // biSize; 
		LCD_WIDTH,	// biWidth; 
		-LCD_HEIGHT,	// biHeight; 
		1,              // biPlanes; 
		sizeof(GRAMPIXEL)*8,// biBitCount 
		0,              // biCompression; 
		0,              // biSizeImage; 
		0,              // biXPelsPerMeter; 
		0,              // biYPelsPerMeter; 
		0,				// biClrUsed; 
		0               // biClrImportant; 
	};
	
    //Initial bmp header
	pbmi = (LPBITMAPINFO)malloc( sizeof( BITMAPINFO ) );
	memcpy( &pbmi->bmiHeader, &bmi, sizeof( BITMAPINFOHEADER ) );
    pbmi->bmiHeader.biWidth  = LCD_WIDTH;
    pbmi->bmiHeader.biHeight = -LCD_HEIGHT;
    
	//Initial color palette
#if ( BITS_PER_PIXEL <= 8 )
	InitialPalette( );
#else
	GRAMPalette = NULL;
#endif
	
	//Initial LCD buffer
	bufSize = LCD_WIDTH * LCD_HEIGHT * sizeof( GRAMPIXEL );
	GRAMinitAd = ( GRAMPIXEL * )malloc( bufSize );
	memset( GRAMinitAd, 0xf0, bufSize);
	
	//Display
	RefreshLCD(  );

	return; 
}

//////////////////////////////////////////////////////////////
//  Function Name: InitialPalette
//  Param In:					Param Description	
//		RGBQUAD palette				palette recording RGB color.
//								
//  Return Type:				Result Code Description
//		ER							E_OK	intial succeed
//	
//  Description: build the map from index color to RGB color 
//////////////////////////////////////////////////////////////
BOOL InitialPalette(  )
{
	PIXEL index;
	GRAMPIXEL color;
	DWORD i, palSize = 1;

	palSize = ( palSize << BITS_PER_PIXEL );
	GRAMPalette = ( GRAMPALETTE * )SysLmalloc( 	palSize * sizeof(GRAMPALETTE) );
	if( GRAMPalette == NULL )
		return FALSE;
	
	for( index=0, i = 1; i <= palSize; i++, index++ )
	{
#if ( BITS_PER_PIXEL >= 8 )
		color = IndexToRGB( index );		
#else
		{
			DWORD	grayIndex;

			grayIndex = ( i * i * 2550 ) >> ( BITS_PER_PIXEL <<1 );
			color = PixelMask - index;
			GRAMPalette[index].threshold = grayIndex;
		}
#endif
		GRAMPalette[index].rgb = color;
//		GRAMPalette[index].rgb = IndexToRGB( index );
	}

	return TRUE;
}

void FreePalette(  )
{
	SysLfree( GRAMPalette );
}

/*
//////////////////////////////////////////////////////////////
//  Function Name: LinearColorMap
//  Param In:					Param Description	
//		BYTE *color				RGB color ( 0~maxColor )
//		BYTE index				color index ( 0~maxIndex )
//		BYTE maxColor			maximum color
//		BYTE maxIndex			maximum color index
//								
//  Return Value:				Result Code Description
//		ER						E_OK	transform succeed
//								E_PAR	following parameter error:
//										Index is larger than maximum index;
//										Maximum index is zero;
//										Maximum color is zero;
//
//  Description: Linear map from index to color 
//////////////////////////////////////////////////////////////
BOOL LinearColorMap( BYTE *color, BYTE index, BYTE maxColor, BYTE maxIndex)
{
	//if( index > maxIndex || !maxIndex || !maxColor )
	//	return E_PAR;
	
	if( maxIndex == maxColor )
		*color = index;
	else
		*color = index * maxColor / maxIndex;
		
	return TRUE;
}
*/

//////////////////////////////////////////////////////////////
//  Function Name: RefreshLCD
//  Param In:					Param Description	
//		None
//								
//  Return Value:				Result Code Description
//		ER						E_OK	refresh LCD OK
//								E_NOEXS	LCD is not ready
//
//  Description: Refresh LCD with the bitmap in the LCD buffer
//////////////////////////////////////////////////////////////
BOOL RefreshLCD( )
{
    HDC hdc;
	
	if( GRAMinitAd == NULL ) return FALSE;

	hdc = GetDC( hwndScreen );	// get device handle of server screen
	//if( !hdc ) return E_NOEXS;	// device is not available
	
	// display the bitmap in LCD buffer
	if( !SetDIBitsToDevice(
            hdc,
            0,
            0,
            LCD_WIDTH,
            LCD_HEIGHT,
            0,
            0,
            0,
            LCD_HEIGHT,
            GRAMinitAd,
            pbmi,
            DIB_RGB_COLORS ) )
	{
		ReleaseDC( hwndScreen, hdc );	// release device handle
		return FALSE;
	}
	else
	{
		ReleaseDC( hwndScreen, hdc );
		return TRUE;
	}
}


//////////////////////////////////////////////////////////////
//  Function Name: IndexToRGB
//  Param In:					Param Description	
//		PIXEL index				color index ( 0~MAX_COLOR-1)
//								
//  Return Value:				Result Code Description
//		GRAMPIXEL				16 bits RGB color
//
//  Description: Convert 12 bits index to 16 bits RGB color
//////////////////////////////////////////////////////////////
GRAMPIXEL IndexToRGB( PIXEL index )
{
	GRAMPIXEL color;
	BYTE red, green, blue;

//	index &= ( MAX_COLOR-1 );	// only lower 12 bits is available in  the index
	index &= PixelMask;
	
#if BITS_PER_PIXEL >= 8
	{
		UB redIndex, greenIndex, blueIndex;

//		index = index >> 4;
		
//		redIndex = index >> ( MAX_GREEN_COLOR_BITS+MAX_BLUE_COLOR_BITS );
		redIndex = ( index >> INDEX_RED_OFFSET ) & INDEX_RED_MASK;
		LinearColorMap( &red, redIndex, MAX_GRAM_COLOR, (UB)INDEX_RED_MASK );
		
//		greenIndex = ( index >> MAX_BLUE_COLOR_BITS ) & ( MAX_GREEN_COLOR-1 );
		greenIndex = ( index >> INDEX_GREEN_OFFSET ) & INDEX_GREEN_MASK;
		LinearColorMap( &green, greenIndex, MAX_GRAM_COLOR, (UB)INDEX_GREEN_MASK );
		
//		blueIndex = index & ( MAX_BLUE_COLOR-1 );
		blueIndex = ( index >> INDEX_BLUE_OFFSET ) & INDEX_BLUE_MASK;
		LinearColorMap( &blue, blueIndex, MAX_GRAM_COLOR, (UB)INDEX_BLUE_MASK );
	}
#else
	{
		BYTE gray = (BYTE) index;

		LinearColorMap( &gray, gray, MAX_GRAM_COLOR, (BYTE)PixelMask );
		blue = green = red = gray;
	}
#endif

	color = RGB( blue, green, red );
	
	return color;
}

//////////////////////////////////////////////////////////////
//  Function Name: Wrtite2LCD
//  Param In:					Param Description	
//		UH x1					left top x logic coordiante
//		UH y1					left top y logic coordinate
//		UH x2					right bottom x logic coordiante
//		UH y2					right bottom y logic coordinate
//		PIXEL *initAd			rectangle intial address in virtual ram 
//		UH VRAMwidth			VRAM width
//								
//  Return Value:				Result Code Description
//		ER						E_OK	display succeed
//
//  Description: Draw a pixel or write a graph to LCD.
//////////////////////////////////////////////////////////////

BOOL Write2LCD( WORD x1, WORD y1, WORD x2, WORD y2, VRAM *vram )
{
	WORD i, j, width = x2 - x1 + 1;
	PIXEL index;
	GRAMPIXEL	*lineAd = GRAMinitAd + y1 * LCD_WIDTH + x1;
	
	dis_dsp();

	if( x1 == x2 && y1 == y2 )		// direct draw
	{
		HDC hdc;
		PIXEL *pixelPos;
		CHAR bitOffset;
		GRAMPIXEL color;
		
		hdc = GetDC( hwndScreen );
		
		GetPixelPosition( &pixelPos, &bitOffset, vram, x1, y1 );
		newGetPixel( &index, pixelPos, bitOffset );
		color = IndexToRGB( index );
		
		*lineAd = color;
		SetPixelV( hdc, x1, y1, (COLORREF) color );
		ReleaseDC( hwndScreen, hdc );
	}
	else		// load graph
	{
		PIXEL *linePos, *nextPos;
		CHAR bitOffset, nextBitOffset;

		GetPixelPosition( &linePos, &bitOffset, vram, (WORD)(vram->lcdx + x1), (WORD)(vram->lcdy + y1 ) );

		for( j = y1; j <= y2; j++ )
		{	
			nextPos = linePos;
			nextBitOffset = bitOffset;
			for( i = 0; i < width; i++)
			{
				newGetPixel( &index, nextPos, nextBitOffset );
#if ( BITS_PER_PIXEL <= 8 )
				*( lineAd + i ) = IndexToRGB( (PIXEL)( GRAMPalette[ index & PixelMask ].rgb ) );
#else
				*( lineAd + i ) = IndexToRGB( index );
#endif
				GetNextPixelPosition( &nextPos, &nextBitOffset, nextPos, nextBitOffset );
			}
//			initAd += VRAMwidth;
			GetNextLine( &linePos, &bitOffset, linePos, bitOffset, vram );
			lineAd += LCD_WIDTH;
		}
		RefreshLCD( );
		// display
	}
	
	ena_dsp();

	return TRUE;
}

void seWrite2LCD( VRAM *vram, BYTE bitOffset )
{
	WORD i, j, width = LCD_WIDTH, height = LCD_HEIGHT;
	PIXEL index;
	GRAMPIXEL	*lineAd = GRAMinitAd;
	PIXEL *linePos, *nextPos;
	CHAR nextBitOffset;
	
	dis_dsp();


	GetPixelPosition( &linePos, &bitOffset, vram, vram->lcdx, vram->lcdy );

	for( j = 0; j < height; j++ )
	{	
		nextPos = linePos;
		nextBitOffset = bitOffset;
		for( i = 0; i < width; i++)
		{
			newGetPixel( &index, nextPos, nextBitOffset );
#if ( BITS_PER_PIXEL <= 8 )
			*( lineAd + i ) = IndexToRGB( (PIXEL)( GRAMPalette[ index & PixelMask ].rgb ) );
#else
			*( lineAd + i ) = IndexToRGB( index );
#endif
			GetNextPixelPosition( &nextPos, &nextBitOffset, nextPos, nextBitOffset );
		}
//			initAd += VRAMwidth;
		GetNextLine( &linePos, &bitOffset, linePos, bitOffset, vram );
		lineAd += LCD_WIDTH;
	}
	RefreshLCD( );
	// display
	
	ena_dsp();

	return;
}

BOOL FreeLCD( )
{
	free( GRAMinitAd );
	FreePalette(  );
	free( pbmi );
	
	GRAMinitAd = NULL;
	GRAMPalette = NULL;
	pbmi = NULL;
	
	return TRUE;
}
/*
void testLCD()
{
	VRAM *vram;
	PIXEL *initAd, *lineAd;
	CHAR bitOffset;
	WORD width, height;
	WORD x1, y1, x2, y2;

	vram = GetBlock( 125, 173 );
	if( vram == NULL )
		return;
	
	vram->lcdx = 0;
	vram->lcdy = -40;
	
	x1 = 0;
	y1 = 0;
	width = 120;
	height = 160;
	x2 = x1 + width -1;
	y2 = y1 + height -1;
	GetPixelPosition( &initAd, &bitOffset, vram, x1, y1 );
	BlockUnicolorOp( initAd, 0, width, height, 0xf, GPC_REPLACE_STYLE, vram );
	GetPixelPosition( &lineAd, &bitOffset, vram, 30, 30 );
//	SetLine( lineAd, bitOffset, 20, 0xf0 );
	InvertLine( lineAd, bitOffset, 20 );
	InvertBlock( initAd, 0, width, height, vram );
	
	{
		WORD dx1, dy1, dx2, dy2;

		VRAMMap( &dx1, &dy1, x1, y1, vram );
		VRAMMap( &dx2, &dy2, x2, y2, vram );
		
		Write2LCD( dx1, dx2, dy1, dy2, vram );
	}

	FreeBlock( vram );

	return;
}
*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?