?? 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 + -