📄 lcdlib.c
字号:
#include <string.h>
#include "lcdlib.h"
#include "2410addr.h"
#include "2410lib.h"
#include "def.h"
//#include "win_gui.h"
int TextEdit_x = 5,TextEdit_y = 32,TextEdit_id = 0;
int g_xMax = 240;
int g_yMax = 320;
int mGuiOrientation = GUI_VERTICAL;
/*===================STN 7寸微彩屏==========================*/
#ifdef STN
int (*frameBuffer12Bit)[640*6/8];
//STN微彩LCD初始化函数 (640*480)
void LCD_Init(void)
{
rLCDCON1=0x548;
// 8-bit single scan,8bpp CSTN,ENVID=off
rLCDCON2=0x790000;
rLCDCON3=0xef10;
rLCDCON4=0xd00;
rLCDCON5=0;
rLCDSADDR1 = 0x18000000; //显存地址
rLCDSADDR2 = 0x71ac0;
rLCDSADDR3 = 0xf01e0;
rDITHMODE = 0x12210;
rREDLUT =0;
rGREENLUT=0;
rBLUELUT =0;
rLCDCON1|=1; // ENVID=ON
rGPGUP = 0xffff;
rGPGDAT = 0xa8ff;
rGPGCON = 0xff95fdba;
}
//清屏函数
void clrsrc(COLOR mcolor)
{
unsigned int *p;
unsigned int P1,P2,P3;
int R,G,B;
R = (mcolor & 0xf00) >> 8;
G = (mcolor & 0x0f0) >> 4;
B = (mcolor & 0x00f);
P1 = (unsigned int) ((R<<28)|(G<<24)|(B<<20)|(R<<16)|(G<<12)|(B<<8)|(R<<4)|G);
P2 = (unsigned int) ((B<<28)|(R<<24)|(G<<20)|(B<<16)|(R<<12)|(G<<8)|(B<<4)|R);
P3 = (unsigned int) ((G<<28)|(B<<24)|(R<<20)|(G<<16)|(B<<12)|(R<<8)|(G<<4)|B);
for(p=(unsigned int*)VideoAddrStart;p<(unsigned int*)VideoAddrEnd;)
{
*p = P1;
p++;
*p = P2;
p++;
*p = P3;
p++;
}
return ;
}
//画点函数
int setpixel(int x,int y,unsigned short pencolor)
{
_PutCstn12Bit(x,Y_MAX-y,pencolor);
return 1;
}
void _PutCstn12Bit(int x,int y,unsigned short c)
{
U32 z;
frameBuffer12Bit= (int (*)[640*6/8])VideoAddrStart;
z=((x)%8);
if(x<640&& y<485)
{
if((z%3)!=2)
frameBuffer12Bit[(y)][(x)*3/8]=( frameBuffer12Bit[(y)][(x)*3/8] & ~(0xfff00000>>(((z/3)*4)+((z)%3)*12) )|((c&0xfff)<<(20-(((z/3)*4)+((z)%3)*12))) );
else
{
if(z==2)
{
frameBuffer12Bit[(y)][(x)*3/8]=( (frameBuffer12Bit[(y)][(x)*3/8]
& ~(0xff)) | ((c&0xff0)>>4) );
frameBuffer12Bit[(y)][((x)*3/8)+1]=( (frameBuffer12Bit[(y)][((x)*3/8)+1]
& ~(0xf0000000)) | ((c&0xf)<<28) );
}
else if(z==5)
{
frameBuffer12Bit[(y)][(x)*3/8]=( (frameBuffer12Bit[(y)][(x)*3/8]
& ~(0xf)) | ((c&0xf00)>>8) );
frameBuffer12Bit[(y)][((x)*3/8)+1]=( (frameBuffer12Bit[(y)][((x)*3/8)+1]
& ~(0xff000000)) | ((c&0xff)<<24) );
}
}
}
}
//取色函数
COLOR getpixel(int x,int y)
{
int color;
color = _GetCstn12Bit(x,y);
return color;
}
unsigned short _GetCstn12Bit(int x,int y)
{
int z;
int total;
int temp_h;
int temp_l;
frameBuffer12Bit= (int (*)[640*6/8])VideoAddrStart;
z=((x)%8);
if(x<640&& y<485)
{
if((z%3)!=2)
{
temp_h =( frameBuffer12Bit[(y)][(x)*3/8] & (0xfff00000>>(((z/3)*4)+((z)%3)*12) ) );
total = temp_h>>(20-((z/3)*4+(z%3)*12));
}
else
{
if(z==2)
{
temp_h=( (frameBuffer12Bit[(y)][(x)*3/8]& (0xff)) );
temp_l=( (frameBuffer12Bit[(y)][((x)*3/8)+1]& (0xf0000000)));
total = temp_h<<4+temp_l>>28;
}
else if(z==5)
{
temp_h =( (frameBuffer12Bit[(y)][(x)*3/8]
& (0xf)) );
temp_l =( (frameBuffer12Bit[(y)][((x)*3/8)+1]
& (0xff000000)) );
total = temp_h<<8 + temp_l>>24;
}
}
return (unsigned short)total;
}
return 0;
}
//24bit红绿蓝转换为12bit
int RGB(unsigned char Red,unsigned char Green,unsigned char Blue)
{
return ( ((Red&0xf0) << 4) | (Green&0xf0) | ((Blue&0xf0) >> 4) );
}
unsigned short ColorPC(unsigned char Msb, unsigned char Lsb)
{
unsigned short color;
unsigned short red, green, blue;
color = (Msb << 8) | Lsb;
red = (color & 0x3C00) >> 2;
green = (color & 0x1E0) >> 1;
blue = (color & 0xF);
color = red | green | blue;
return color;
}
/*===================TFT 3.4寸真彩屏==========================*/
#else
//TFT真彩LCD初始化函数 (320*240)
void LCD_Init(void)
{
rLCDCON2=0x14FC081;
// rLCDCON3=0x188EF0F; // 旧屏
rLCDCON3=0x6<<19 | 0xEF0F; // 新屏
rLCDCON4=0xD;
rLCDCON5=0xB09;
rLCDSADDR1=0x18000000;
rLCDSADDR2=0x12c00;
rLCDCON1=0x479;
rLCDSADDR3=0x0;
rREDLUT=0x0;
rGREENLUT=0x0;
rBLUELUT=0x0;
rDITHMODE=0x0;
rTPAL=0x0;
rLCDINTPND=0x0;
rLCDSRCPND=0x0;
rLCDINTMSK=0x3;
rLPCSEL=0x4;
}
//画点函数
int setpixel(int x,int y,unsigned short pencolor)
{
int movetop; // THE PIXEL ADDRESS
short *p;
if (x<0||x>=g_xMax) return ERROR;
if (y<0||y>=g_yMax) return ERROR;
if (mGuiOrientation == GUI_HORIZONTAL)
movetop=(x*g_yMax+y)<<1; // 指向所要显示的像素偏移地址(一个像素占两个字节)
else
movetop=((g_yMax-y)*g_xMax-(g_xMax-x))<<1; // 指向所要显示的像素偏移地址(一个像素占两个字节)
p=(short *)(VideoAddrStart|movetop);
*p=pencolor;
return 1;
}
// 取色函数
COLOR getpixel(int x,int y)
{
int movetop; // THE PIXEL ADDRESS
short *p;
if (x<0||x>=g_xMax) return ERROR;
if (y<0||y>=g_yMax) return ERROR;
if (mGuiOrientation == GUI_HORIZONTAL)
movetop=(x*g_yMax+y)<<1; // 指向所要显示的像素偏移地址(一个像素占两个字节)
else
movetop=((g_yMax-y)*g_xMax-(g_xMax-x))<<1; // 指向所要显示的像素偏移地址(一个像素占两个字节)
p=(short *)(VideoAddrStart|movetop);
return *p;
}
// 清屏函数
void clrsrc(COLOR mcolor)
{
short *p;
for(p=(short*)VideoAddrStart;p<(short*)VideoAddrEnd;p++) *p = mcolor;
}
// 24bit红绿蓝转换为16bit
int RGB(unsigned char Red,unsigned char Green,unsigned char Blue)
{
return ( ((Red>>3)<<11) | ((Green>>2)<<5) | (Blue >> 3) );
}
unsigned short ColorPC(unsigned char Msb, unsigned char Lsb)
{
unsigned short color;
unsigned short temp;
temp = (Msb << 8) | Lsb;
color = (temp << 1) & 0xFFC0;
color |= temp & 0x1F;
return color;
}
#endif
/*===================共用函数==========================*/
// 画图函数,图像可以是16位,或24位,但尺寸必需为320*240
int ShowImage(unsigned char *p , int StartX, int StratY, int mode)
{
int x,y;
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned short color;
unsigned short fileType;
unsigned short imageColorBit;
int height;
int width;
// int imageSize;
int patch = 0;
imageColorBit = p[28]; // 取得图片的颜色位数
fileType = LINK_HWORD(p[1], p[0]); // 文件类型
// imageSize = LINK_WORD(p[37], p[36], p[35], p[34]); // 取得图象的总大小
height = LINK_WORD(p[25], p[24], p[23], p[22]); // 取得图象的宽度,单位是象素
width = LINK_WORD(p[21], p[20], p[19], p[18]); // 取得图象的宽度,单位是象素
if(mode == 1) // mode为1时以黑色填充背景
memset((short*)VideoAddrStart, 0, g_xMax*g_yMax*2);
p += 54; // 指向图像内容
if(fileType != 0x4D42) // 判断是否BMP文件
{
Rewriteletter((g_xMax>>1)-32 ,g_yMax>>1,"无效位图",1,1,RGB(255,255,255),RGB(0,0,0));
return FALSE; // 显示BMP位图失败
}
else
{
if(imageColorBit == 16) // 判断图片颜色位数是否16位
{
patch = (4 - width * 2 % 4) % 4; // 补0的个数
for(y=StratY; y<height+StratY; y++)
{
for(x=StartX; x<width+StartX; x++)
{
if(y < g_yMax && x < g_xMax)
{
color = ColorPC(p[1], p[0]);
setpixel(x ,y ,color);
}
p += 2;
}
p += patch; // 跳过每行补上的0
}
}
else
{
patch = (4 - (width * 3) % 4) % 4; // 补0的个数
for(y=StratY; y<height+StratY; y++)
{
for(x=StartX; x<width+StartX; x++)
{
blue = p[0];
green = p[1];
red = p[2];
p += 3;
if(y < g_yMax && x < g_xMax)
{
color = RGB(red ,green ,blue);
setpixel(x ,y ,color);
}
}
p += patch; // 跳过每行补上的0
}
} // end for "if"
} // end for "if"
return TRUE; // 显示BMP位图成功
}
//画线函数
void line(int x0,int y0,int x1,int y1,COLOR mcolor)
{
int x;
int y;
if(x0==x1)
{
if(y0>y1)
{
y=y0;y0=y1;y1=y;
}
for(y=y0;y<=y1;y++)
setpixel(x0,y,mcolor);
return;
}
//swap
if(x0>x1)
{
x=x0;x0=x1;x1=x;
y=y0;y0=y1;y1=y;
}
for(x=x0;x<=x1;x++)
{
y=(int)(((float)(x-x0))*(y1-y0)/(x1-x0) + y0);
setpixel(x,y,mcolor);
}
}
//填色函数
void fillrectangle(int x0,int y0,int x1,int y1,COLOR mcolor)
{
int x,y;
if (x0 > x1)
{
x = x0;
x0 = x1;
x1 = x;
}
if (y0 > y1)
{
y = y0;
y0 = y1;
y1 = y;
}
for(x=x0;x<=x1;x++)
for(y=y0;y<=y1;y++) setpixel(x,y,mcolor);
}
/*
|| 函数名: DrawFrame 画框函数
|| 参数1: int xS, 开始点 X 坐标
|| 参数2: int yS, 开始点 Y 坐标
|| 参数3: int xE, 结束点 X 坐标
|| 参数4: int yE, 结束点 Y 坐标
|| 参数5: int width, 框的厚度
|| 参数6: COLOR mcolor, 框的颜色
|| 返回值: 无
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -