📄 tft_api.c
字号:
//====================================================================================
//文 件 名:TFT_API.c
//功能描述: TFT驱动程序(API)
//维护记录:
// 2007.08.23 更新日志 by lijian <lijian@sunnorth.com.cn>
// 1. 增加JPEG支持
// 2. 增加显示图片时的自适应窗口支持
//
// 2007.08.21 更新日志 by lijian <lijian@sunnorth.com.cn>
// 1. 增加TFT_PutImage(), 可以显示RGB565格式的不带有文件头的位图数据
// 2. 增加TFT_PutImageEx(), 可以显示RGB565格式的不带有文件头的位图数据
// 3. 增加行缩放函数TFT_ScaleLine()
// 4. 增加位图缩放函数TFT_ScaleImage()
// 5. 增加位图解压函数TFT_DepressBitMap(), 解压之后形成STR_IMAGE数据
//
// 2007.08.20 更新日志 by lijian <lijian@sunnorth.com.cn>
// 1. 修正TFT_MoveWindow以及TFT_CopyWindow当窗体超出屏幕时屏幕乱掉的bug
// 2. 优化程序, 采用统一的方法实现MoveWindow和CopyWindow
// 3. 增加TFT_CreateWindowEx()函数, 使用width和height指定窗体属性
// 4. 分离窗体和字体控制, 字体独立控制, 不再依赖窗体, 节约存储空间
//
// 2007.08.19 更新日志 by lijian <lijian@sunnorth.com.cn>
// 1. 使用"句柄"模式操作窗体, 不再需要自己定义窗体结构体, 不使用指针
// 2. 窗体增加TFT_CopyWindow()特性, 可以移动并复制窗体到其他位置
// 3. 窗体增加TFT_MoveWindow()特性, 可以移动窗体到其他位置
// 4. 窗体增加TFT_MoveWindowBorder()特性, 可以移动窗体边框(不移动窗体内容)到其他位置
// 5. 窗体增加TFT_ResetWindow()特性, 可以重新设置窗体位置和大小(不改变屏幕内容)
// 6. 窗体增加TFT_CloseWindow()特性, 可以关闭当前窗体以便释放系统资源(不影响屏幕显示)
// 7. 窗体增加TFT_DeleteWindow()特性, 可以关闭当前窗体以便释放系统资源, 并清除窗体内容
// 2007.07.11 初始版本 by wangtao <wangtao@sunnorth.com.cn>
//====================================================================================
#include "TFT_API.h"
#include "MPEG4_Driver.h"
#include "Resource.h"
#include <stdarg.h>
#include <stdio.h>
static STR_WINDOW g_WinList[MAX_WINDOW];
//=============================================================
//语法格式: void TFT_CallBack_GetCharBuf(STR_WINDOW *Window, unsigned short CharCode, STR_FONT *Font_Char);
//实现功能: 获取指定编码的字符(或汉字)的字模信息
//参数: Window - 工作窗口指针
// CharCode - 字符或汉字的编码
// Font_Char - 字模信息结构体地址
//返回值: 无
//=============================================================
static void TFT_CallBack_GetCharBuf(STR_WINDOW *Window, unsigned short CharCode, STR_FONT *Font_Char)
{
if(CharCode<=0x00FF) // ASCII字符
{
Font_Char->CharWidth = TFT_GetAsciiFontWidth(Window->AsciiFont);
Font_Char->CharHeight = TFT_GetAsciiFontHeight(Window->AsciiFont);
Font_Char->FontBuf = TFT_GetAsciiFontBuf(Window->AsciiFont);
Font_Char->FontBuf += CharCode * Font_Char->CharHeight * ((Font_Char->CharWidth + 7) >> 3);
}
else // 汉字
{
Font_Char->CharWidth = TFT_GetChineseFontWidth(Window->ChineseFont);
Font_Char->CharHeight = TFT_GetChineseFontHeight(Window->ChineseFont);
Font_Char->FontBuf = TFT_GetChineseFontBuf(Window->ChineseFont);
Font_Char->FontBuf += (unsigned int)(94 * ((CharCode & 0xFF) - 0xA1) + ((CharCode >> 8) - 0xA1))
* Font_Char->CharHeight * ((Font_Char->CharWidth + 7) >> 3);
}
}
//=============================================================
//语法格式: static unsigned short TFT_CalcTransparent(STR_WINDOW *Window, unsigned short OldColor, unsigned short MaskColor);
//实现功能: 计算当前透明度下的叠加颜色
//参数: Window - 工作窗口指针
// OldColor - 原有颜色
// MaskColor - 待叠加的颜色
//返回值: 叠加后的颜色代码,以16位存储,格式为RRRRR-GGGGGG-BBBBB
//=============================================================
static unsigned short TFT_CalcTransparent(STR_WINDOW *Window, unsigned short OldColor, unsigned short MaskColor)
{
int R, G, B;
int dR, dG, dB;
if((Window == 0) || (Window->Flag == 0))
return 0;
// 计算公式为 New = Old * K + Mask * (1-K) 其中K为透明度(0%~100%)
dR = (int)(OldColor&0xF800) - (int)(MaskColor&0xF800); // 计算R分量
dR = ((dR * Window->Transparency)>>7)&0xF800;
R = ((MaskColor&0xF800)+dR)&0xF800;
dG = (int)(OldColor&0x07E0) - (int)(MaskColor&0x07E0); // 计算G分量
dG = ((dG * Window->Transparency)>>7)&0x07E0;
G = ((MaskColor&0x07E0)+dG)&0x07E0;
dB = (int)(OldColor&0x001F) - (int)(MaskColor&0x001F); // 计算B分量
dB = ((dB * Window->Transparency)>>7)&0x001F;
B = ((MaskColor&0x001F)+dB)&0x001F;
return R + G + B;
}
//=============================================================
//语法格式: static int TFT_AllocWindow(void)
//实现功能: 申请可用的窗口
//参数: 无
//返回值: 窗口句柄
//=============================================================
static WIN_HANDLE TFT_AllocWindow(void)
{
int i;
for(i = 0; i < MAX_WINDOW; i++)
{
if(g_WinList[i].Flag == 0)
{
g_WinList[i].Flag = 1;
return i;
}
}
return NO_FREE_WIN;
}
//=============================================================
//语法格式: static void TFT_FreeWindow(WIN_HANDLE Handle)
//实现功能: 销毁窗口
//参数: Handle - 窗口句柄
//返回值: 无
//=============================================================
static void TFT_FreeWindow(WIN_HANDLE Handle)
{
if((Handle >= 0) && (Handle < MAX_WINDOW))
g_WinList[Handle].Flag = 0;
}
//=============================================================
//语法格式: static int TFT_CopyRectToBuffer(void *Buf, short TLx, short TLy, short BRx, short BRy)
//实现功能: 复制窗体内容到缓冲区
//参数: Buf - 缓冲区首地址
// TLx,TLy - 左上角坐标
// BRx,BRy - 右下角坐标
//返回值: 1: 成功; 0: 失败
//=============================================================
void *memcpy(void *dest, void *src, unsigned int count);
static int TFT_CopyRectToBuffer(void *Buf, short TLx, short TLy, short BRx, short BRy)
{
COLOR *Dest = (COLOR *)Buf;
short i;
short W, H;
TFT_GetWorkBufSize(&W, &H);
for(i = TLy; i < BRy; i++)
{
memcpy(Dest, TFT_SelWorkBuf(-1) + i * W + TLx, (BRx - TLx) * sizeof(COLOR));
Dest += (BRx - TLx);
}
return 1;
}
//=============================================================
//语法格式: static int TFT_CopyBufferToRect(void *Buf, short TLx, short TLy, short BRx, short BRy)
//实现功能: 显示缓冲区内容到窗体
//参数: Buf - 缓冲区首地址
// TLx,TLy - 左上角坐标
// BRx,BRy - 右下角坐标
//返回值: 1: 成功; 0: 失败
//=============================================================
static int TFT_CopyBufferToRect(void *Buf, short TLx, short TLy, short BRx, short BRy)
{
COLOR *Dest = (COLOR *)Buf;
short i;
short W, H;
TFT_GetWorkBufSize(&W, &H);
for(i = TLy; i < BRy; i++)
{
memcpy(TFT_SelWorkBuf(-1) + i * W + TLx, Dest, (BRx - TLx) * sizeof(COLOR));
Dest += (BRx - TLx);
}
return 1;
}
//=============================================================
//语法格式: void TFT_Init(void);
//实现功能: TFT驱动程序初始化
//参数: 无
//返回值: 无
//=============================================================
void TFT_Init(void)
{
//BLNDMA_Init();
int i;
STR_FONT FontInfo;
for(i = 0; i < MAX_WINDOW; i++)
{
g_WinList[i].Flag = 0;
}
TFT_FontInit();
#ifdef RES_ASC16
FontInfo.CharWidth = 8; // 窗口默认ASCII字体列表
FontInfo.CharHeight = 16;
FontInfo.FontBuf = RES_ASC16;
TFT_LoadAsciiFont(&FontInfo);
#endif
#ifdef RES_HZK16
FontInfo.CharWidth = 16; // 窗口默认中文字体列表
FontInfo.CharHeight = 16;
FontInfo.FontBuf = RES_HZK16;
TFT_LoadChineseFont(&FontInfo);
#endif
#ifdef RES_ASC12
FontInfo.CharWidth = 6;
FontInfo.CharHeight = 12;
FontInfo.FontBuf = RES_ASC12;
TFT_LoadAsciiFont(&FontInfo);
#endif
#ifdef RES_HZK12
FontInfo.CharWidth = 12;
FontInfo.CharHeight = 12;
FontInfo.FontBuf = RES_HZK12;
TFT_LoadChineseFont(&FontInfo);
#endif
TFT_InitHardware();
MPEG4_Init();
}
//=============================================================
//语法格式: WIN_HANDLE TFT_CreateWindow(short TLx, short TLy, short BRx, short BRy, COLOR BGColor)
//实现功能: 创建工作窗口
//参数: TLx - 工作窗口相对于LCD工作缓冲区的起始x坐标
// TLy - 工作窗口相对于LCD工作缓冲区的起始y坐标
// width - 工作窗口宽度
// height - 工作窗口高度
// BGColor - 工作窗口的背景色
//返回值: 窗口句柄
//=============================================================
WIN_HANDLE TFT_CreateWindowEx(short TLx, short TLy, short width, short height, COLOR BGColor)
{
return(TFT_CreateWindow(TLx, TLy, TLx + width - 1, TLy + height - 1, BGColor));
}
//=============================================================
//语法格式: WIN_HANDLE TFT_CreateWindow(short TLx, short TLy, short BRx, short BRy, COLOR BGColor)
//实现功能: 创建工作窗口
//参数: TLx - 工作窗口相对于LCD工作缓冲区的起始x坐标
// TLy - 工作窗口相对于LCD工作缓冲区的起始y坐标
// BRx - 工作窗口相对于LCD工作缓冲区的结束x坐标
// BRy - 工作窗口相对于LCD工作缓冲区的结束y坐标
// BGColor - 工作窗口的背景色
//返回值: 窗口句柄
//=============================================================
WIN_HANDLE TFT_CreateWindow(short TLx, short TLy, short BRx, short BRy, COLOR BGColor)
{
WIN_HANDLE Handle;
STR_WINDOW *Window;
short W, H;
short i, j;
unsigned short *p_Buf;
if((Handle = TFT_AllocWindow()) == NO_FREE_WIN)
return NO_FREE_WIN;
Window = g_WinList + Handle;
TFT_GetWorkBufSize(&W, &H);
Window->TLx = TLx < BRx ? TLx : BRx;
if(Window->TLx > W)
{
TFT_FreeWindow(Handle);
return NO_FREE_WIN;
}
Window->TLy = TLy < BRy ? TLy : BRy;
if(Window->TLy > H)
{
TFT_FreeWindow(Handle);
return NO_FREE_WIN;
}
Window->BRx = TLx > BRx ? TLx : BRx;
if(Window->BRx >= W)
Window->BRx = W - 1;
Window->BRy = TLy >= BRy ? TLy : BRy;
if(Window->BRy > H)
Window->BRy = H - 1;
if(((Window->TLx < 0) && (Window->BRx < 0))
|| ((Window->TLy < 0) && (Window->BRy < 0)))
{
TFT_FreeWindow(Handle);
return NO_FREE_WIN;
}
Window->Flag = 1;
Window->CurTextX = Window->CurTextY = 0; // 窗口初始文本绘制坐标
Window->Transparency = 0; // 窗口初始透明度
Window->FGColor = COLOR_WHITE; // 窗口初始前景色
Window->BGColor = BGColor; // 窗口初始背景色
Window->AsciiFont = 0; // 窗口初始使用的ASCII字体
Window->ChineseFont = 0; // 窗口初始使用的中文字体
Window->Width = Window->BRx - Window->TLx + 1;
Window->Height = Window->BRy - Window->TLy + 1;
if(Window->BGColor != COLOR_BLACK) // 如果背景色不是0x0000则为工作区着背景色
{
for(i = (Window->TLy >= 0 ? Window->TLy : 0); i <= Window->BRy; i++)
{
p_Buf = TFT_SelWorkBuf(-1) + i * W;
for(j=(Window->TLx >= 0 ? Window->TLx : 0); j <= Window->BRx; j++)
{
*(p_Buf+j) = Window->BGColor;
}
}
}
return Handle;
}
//=============================================================
//语法格式: void TFT_CloseWindow(WIN_HANDLE Handle)
//实现功能: 释放窗口
//参数: Handle - 窗口句柄
//返回值: 无
//=============================================================
void TFT_CloseWindow(WIN_HANDLE Handle)
{
if((Handle < 0) && (Handle >= MAX_WINDOW))
return;
TFT_FreeWindow(Handle);
}
//=============================================================
//语法格式: void TFT_DeleteWindow(WIN_HANDLE Handle)
//实现功能: 删除窗口
//参数: Handle - 窗口句柄
//返回值: 无
//=============================================================
void TFT_DeleteWindow(WIN_HANDLE Handle)
{
if((Handle < 0) && (Handle >= MAX_WINDOW))
return;
TFT_SetBGColor(Handle, COLOR_BLACK);
TFT_ClearWindow(Handle);
TFT_FreeWindow(Handle);
}
//=============================================================
//语法格式: int TFT_MoveWindowEx(WIN_HANDLE Handle, short TLx, short TLy, int Cut)
//实现功能: 移动窗口
//参数: Handle - 窗口句柄
// TLx,TLy - 新位置左上角坐标
// Cut - 原窗口内容是否清除
//返回值: 1: 成功; 0:失败
//=============================================================
int TFT_MoveWindowEx(WIN_HANDLE Handle, short TLx, short TLy, int Cut)
{
STR_WINDOW *Window = g_WinList + Handle;
// COLOR *TempBuf = (COLOR*)0xa0500000;
COLOR TempBuf[640][480];
short x1, y1, x2, y2;
short Dx1, Dy1, Dx2, Dy2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -