lcddrv.c

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

C
352
字号
/***************************************************************************************\
 *
 * 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 */
#include <stdlib.h>
#include <string.h>
#include <windows.h>

#include <sys\cdevice.h>
#include <kernel\ros33\itron.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 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
static GRAMPIXEL	palette[MAX_COLOR];	// palette for convertion from color index to RGB color


/* Extern Declared Function */
extern void InitialLCD( void );
extern ER Write2LCD( UH x1, UH x2, UH y1, UH y2, PIXEL *initAd, UH VRAMwidth );
extern ER RefreshLCD( void );
extern ER FreeLCD( void );

/* Local Declared Function */
static ER InitialPalette( );
static ER LinearColorMap( UB *color, UB index, UB maxColor, UB maxIndex );
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( )
{
	UW 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)BmpHdr;
	memcpy( &pbmi->bmiHeader, &bmi, sizeof( BITMAPINFOHEADER ) );
    pbmi->bmiHeader.biWidth  = LCD_WIDTH;
    pbmi->bmiHeader.biHeight = -LCD_HEIGHT;
    
	//Initial color palette
	InitialPalette( );
    
	//Initial LCD buffer
	bufSize = LCD_WIDTH * LCD_HEIGHT * sizeof( GRAMPIXEL );
	GRAMinitAd = ( GRAMPIXEL * )malloc( bufSize );
	memset( GRAMinitAd, 0xff, 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 
//////////////////////////////////////////////////////////////
ER InitialPalette(  )
{
	PIXEL index;
	GRAMPIXEL color;

	for( index=0; index < MAX_COLOR; index++ )
	{
		color = IndexToRGB( index );
		palette[index] = color;
	}

	return E_OK;
}

//////////////////////////////////////////////////////////////
//  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 
//////////////////////////////////////////////////////////////
ER 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 E_OK;
}

//////////////////////////////////////////////////////////////
//  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
//////////////////////////////////////////////////////////////
ER RefreshLCD( )
{
    HDC hdc;
	
	if( GRAMinitAd == NULL ) return E_NOEXS;

	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 E_NOEXS;
	}
	else
	{
		ReleaseDC( hwndScreen, hdc );
		return E_OK;
	}
}


//////////////////////////////////////////////////////////////
//  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;
	UB redIndex, greenIndex, blueIndex;
	UB red, green, blue;

	index &= ( MAX_COLOR-1 );	// only lower 12 bits is available in  the index
	
	redIndex = index >> ( MAX_GREEN_COLOR_BITS+MAX_BLUE_COLOR_BITS );
	LinearColorMap( &red, redIndex, MAX_GRAM_COLOR-1, MAX_RED_COLOR-1 );
	
	greenIndex = ( index >> MAX_BLUE_COLOR_BITS ) & ( MAX_GREEN_COLOR-1 );
	LinearColorMap( &green, greenIndex, MAX_GRAM_COLOR-1, MAX_GREEN_COLOR-1 );
	
	blueIndex = index & ( MAX_BLUE_COLOR-1 );
	LinearColorMap( &blue, blueIndex, MAX_GRAM_COLOR-1, MAX_BLUE_COLOR-1 );

	color = ( red << 10 )|( green << 5 )| ( blue );
	
	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.
//////////////////////////////////////////////////////////////
ER Write2LCD( UH x1, UH x2, UH y1, UH y2, PIXEL *initAd, UH VRAMwidth )
{
	UH i, j, width = x2 - x1 + 1;
	PIXEL index;
	HDC hdc;
	COLORREF color;
	UB red, green, blue;
	GRAMPIXEL	*lineAd = GRAMinitAd + y1 * LCD_WIDTH + x1;
	
	dis_dsp();

	if( x1 == x2 && y1 == y2 )
	{
		// direct draw
		hdc = GetDC( hwndScreen );
		
		index = *initAd;
		index = ( index >> 4 );

		LinearColorMap( &blue, ( UB )( index & 0xf ), 0xff, 0x0f );
		LinearColorMap( &green, ( UB )( ( index & 0xf0 ) >> 4 ), 0xff, 0x0f );
		LinearColorMap( &red, ( UB )( ( index & 0xf00 ) >> 8 ), 0xff, 0x0f );

		color = ( COLORREF )red | ( ( COLORREF )green ) << 8 | ( ( COLORREF )blue ) << 16;
		*lineAd = palette[ index ];
		SetPixelV( hdc, x1, y1, color );
		ReleaseDC( hwndScreen, hdc );
	}
	else
	{
		// load graph

		for( j = y1; j <= y2; j++ )
		{
			for( i = 0; i < width; i++)
			{
				index = *( initAd + i );
				index = ( index >> 4 );

				if ( index >= MAX_COLOR )
					index = MAX_COLOR-1;
				*( lineAd + i ) = palette[ index ];
			}
			initAd += VRAMwidth;
			lineAd += LCD_WIDTH;
		}
		// display
		RefreshLCD( );
	}
	
	ena_dsp();

	return E_OK;
}

ER FreeLCD( )
{
	free( GRAMinitAd );
	
	GRAMinitAd = NULL;
	
	return E_OK;
}

⌨️ 快捷键说明

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