📄 tft_api.c
字号:
//====================================================================================
//File Name: TFT_API.c
//Description: TFT API
//Update: 2007.01.17 V0.1 by wangtao <wangtao@sunnorth.com.cn>
//====================================================================================
#include "TFT_API.h"
short Window_TLx = 0; // x-coordinate at the top left corner of window
short Window_TLy = 0; // y-coordinate at the top left corner of window
short Window_BRx = TFT_WIDTH - 1; // x-coordinate at the bottom right corner of window
short Window_BRy = TFT_HEIGHT - 1; // y-coordinate at the bottom right corner of window
short CurTextx = 0; // Relative position of the text to be displayed
short CurTexty = 0;
unsigned short Transparency = 0; // Transparency
unsigned short Color = COLOR_WHITE; // Foreground color
unsigned short BGColor = COLOR_BLACK; // Background color
short gAsciiFontType = 0; // ASCII font type
short gChineseFontType = 0; // Chinese font type
STR_FONT gAsciiFont; // Information of current ASCII font
STR_FONT gChineseFont; // Information of current Chinese font
extern void TFT_CallBack_GetCharBuf(unsigned short CharCode, STR_FONT *Font_Char);
extern void TFT_CallBack_UpdateFont(void);
//=============================================================================
// Prototype: void TFT_Init(void);
// Description: Initialize TFT driver.
// Arguments: None
// Return Value: None
//=============================================================================
void TFT_Init(void)
{
TFT_InitHardware();
Window_TLx = 0;
Window_TLy = 0;
Window_BRx = TFT_WIDTH - 1;
Window_BRy = TFT_HEIGHT - 1;
CurTextx = 0;
CurTexty = 0;
Transparency = 0;
Color = COLOR_WHITE;
BGColor = COLOR_BLACK;
gAsciiFontType = 0;
gChineseFontType = 0;
TFT_CallBack_UpdateFont();
}
//=============================================================================
// Prototype: void TFT_SetWindow(short TLx, short TLy, short BRx, short BRy);
// Description: Set a workspace (window).
// Arguments:
// TLx - x-coordinate at the top left corner of window
// TLy - y-coordinate at the top left corner of window
// BRx - x-coordinate at the bottom right corner of window
// BRy - y-coordinate at the bottom right corner of window
// Return Value:
// None
//=============================================================================
void TFT_SetWindow(short TLx, short TLy, short BRx, short BRy)
{
short i, j;
short BoundX, BoundY;
unsigned short *p_Buf;
Window_TLx = TLx;
Window_TLy = TLy;
if(BRx >= Window_TLx) Window_BRx = BRx;
else Window_BRx = Window_TLx;
if(BRy >= Window_TLy) Window_BRy = BRy;
else Window_BRy = Window_TLy;
if(BGColor!=COLOR_BLACK) // If background color is not 0x0000, color the window in the background color
{
BoundX = (Window_BRx<TFT_WIDTH) ? Window_BRx : TFT_WIDTH-1;
BoundY = (Window_BRy<TFT_HEIGHT) ? Window_BRy : TFT_HEIGHT-1;
for(i=(Window_TLy>=0?Window_TLy:0); i<=BoundY; i++)
{
p_Buf = pTFT_WorkBuf + i * TFT_WIDTH;
for(j=(Window_TLx>=0?Window_TLx:0); j<=BoundX; j++)
{
*(p_Buf+j) = BGColor;
}
}
}
}
//=============================================================================
// Prototype: void TFT_GetWindow(short *TLx, short *TLy, short *BRx, short *BRy);
// Description: Get coordintes of current window.
// Arguments:
// TLx - x-coordinate at the top left corner of window
// TLy - y-coordinate at the top left corner of window
// BRx - x-coordinate at the bottom right corner of window
// BRy - y-coordinate at the bottom right corner of window
// Return Value:
// None
//=============================================================================
void TFT_GetWindow(short *TLx, short *TLy, short *BRx, short *BRy)
{
*TLx = Window_TLx;
*TLy = Window_TLy;
*BRx = Window_BRx;
*BRy = Window_BRy;
}
//=============================================================================
// Prototype: void TFT_SetTransparency(unsigned short TransparencySet);
// Description: Set transparency for new content.
// Arguments: TransparencySet - Transparency, 0~100 (0: solid ; 100: full transparent)
// Return Value: None
//=============================================================================
void TFT_SetTransparency(unsigned short TransparencySet)
{
Transparency = TransparencySet * 128 / 100; // 128 levels for reducing the amount of calculation
if(Transparency>128) Transparency = 128;
}
//=============================================================================
// Prototype: unsigned short TFT_GetTransparency(void);
// Description: Get current transparency.
// Arguments: None
// Return Value: Transparency, 0~100 (0: solid ; 100: full transparent)
//=============================================================================
unsigned short TFT_GetTransparency(void)
{
return (Transparency * 100)>>7;
}
//=============================================================================
// Prototype: void TFT_SetColor(unsigned short ColorSet);
// Description: Set foreground color.
// Arguments: ColorSet - 16-bit color value, RRRRR-GGGGGG-BBBBB format
// Return Value: None
//=============================================================================
void TFT_SetColor(unsigned short ColorSet)
{
Color = ColorSet;
}
//=============================================================================
// Prototype: unsigned short TFT_GetColor(void);
// Description: Get current foreground color.
// Arguments: None
// Return Value: 16-bit color value, RRRRR-GGGGGG-BBBBB format
//=============================================================================
unsigned short TFT_GetColor(void)
{
return Color;
}
//=============================================================================
// Prototype: unsigned short TFT_PickColor(short x, short y);
// Description: Get the color value of a specified pixel in window.
// Arguments:
// x - Relative x-coordinate in window
// y - Relative y-coordinate in window
// Return Value:
// 16-bit color value, RRRRR-GGGGGG-BBBBB format
//=============================================================================
unsigned short TFT_PickColor(short x, short y)
{
short AbsX, AbsY;
AbsX = Window_TLx + x;
AbsY = Window_TLy + y;
if((AbsX<0)||(AbsY<0)||(AbsX>=TFT_WIDTH)||(AbsY>=TFT_HEIGHT))
return 0x0000;
return *(pTFT_WorkBuf+AbsY*TFT_WIDTH+AbsX);
}
//=============================================================================
// Prototype: unsigned short TFT_PickColorAbsolute(short AbsX, short AbsY);
// Description: Get the color value of a specified pixel in display buffer.
// Arguments:
// AbsX - Absolute x-coordinate (relative to screen)
// AbsY - Absolute y-coordinate (relative to screen)
// Return Value:
// 16-bit color value, RRRRR-GGGGGG-BBBBB format
//=============================================================================
unsigned short TFT_PickColorAbsolute(short AbsX, short AbsY)
{
if((AbsX<0)||(AbsY<0)||(AbsX>=TFT_WIDTH)||(AbsY>=TFT_HEIGHT))
return 0x0000;
return *(pTFT_WorkBuf+AbsY*TFT_WIDTH+AbsX);
}
//=============================================================================
// Prototype: void TFT_SetBGColor(unsigned short ColorSet);
// Description: Set background color for window and text.
// Arguments: ColorSet - 16-bit background color value, RRRRR-GGGGGG-BBBBB format.
// If ColorSet=0x0000(COLOR_BLACK), the background color will be of no effect.
// Return Value: None
//=============================================================================
void TFT_SetBGColor(unsigned short ColorSet)
{
BGColor = ColorSet;
}
//=============================================================================
// Prototype: unsigned short TFT_GetBGColor(void);
// Description: Get the current background color.
// Arguments: None
// Return Value: 16-bit background color value, RRRRR-GGGGGG-BBBBB format
//=============================================================================
unsigned short TFT_GetBGColor(void)
{
return BGColor;
}
//=============================================================================
// Prototype: unsigned short TFT_CalcTransparent(unsigned short OldColor, unsigned short MaskColor);
// Description: Calculate the color value after blending at current transparency.
// Arguments:
// OldColor - Original color
// MaskColor - Color to be added
// Return Value:
// 16-bit color value after blending, RRRRR-GGGGGG-BBBBB format
//=============================================================================
unsigned short TFT_CalcTransparent(unsigned short OldColor, unsigned short MaskColor)
{
int R, G, B;
int dR, dG, dB;
// The formula is New = Old * K + Mask * (1-K). Where, K is transparency (0%~100%)
dR = (int)(OldColor&0xF800) - (int)(MaskColor&0xF800); // Calculate R component
dR = ((dR * Transparency)>>7)&0xF800;
R = ((MaskColor&0xF800)+dR)&0xF800;
dG = (int)(OldColor&0x07E0) - (int)(MaskColor&0x07E0); // Calculate G component
dG = ((dG * Transparency)>>7)&0x07E0;
G = ((MaskColor&0x07E0)+dG)&0x07E0;
dB = (int)(OldColor&0x001F) - (int)(MaskColor&0x001F); // Calculate B component
dB = ((dB * Transparency)>>7)&0x001F;
B = ((MaskColor&0x001F)+dB)&0x001F;
return R + G + B;
}
//=============================================================================
// Prototype: void TFT_PutPixelAbsolute(short AbsX, short AbsY);
// Description: Draw a point at the specified position in display buffer.
// Arguments:
// AbsX - Absolut x-coordinate (relative to screen)
// AbsY - Absolut y-coordinate (relative to screen)
// Return Value:
// None
//=============================================================================
void TFT_PutPixelAbsolute(short AbsX, short AbsY)
{
unsigned short *pBuf;
if((AbsX>=0) && (AbsY>=0) && (AbsX<TFT_WIDTH) && (AbsY<TFT_HEIGHT))
{
pBuf = pTFT_WorkBuf + AbsY * TFT_WIDTH + AbsX;
if(Transparency==0)
*pBuf = Color;
else
*pBuf = TFT_CalcTransparent(*pBuf, Color);
}
}
//=============================================================================
// Prototype: void TFT_PutPixel(short x, short y);
// Description: Draw a point at the specified position in window.
// Arguments:
// x - Relative x-coordinate in window
// y - Relative y-coordinate in window
// Return Value:
// None
//=============================================================================
void TFT_PutPixel(short x, short y)
{
short AbsX, AbsY;
AbsX = Window_TLx+x;
AbsY = Window_TLy+y;
if(AbsX<Window_TLx || AbsX>Window_BRx)return;
if(AbsY<Window_TLy || AbsY>Window_BRy)return;
TFT_PutPixelAbsolute(AbsX, AbsY);
}
//=============================================================================
// Prototype: void TFT_Line(short x1, short y1, short x2, short y2);
// Description: Draw a line in window.
// Arguments:
// x1 - Relative x-coordinate of the start point of a line
// y1 - Relative y-coordinate of the start point of a line
// x2 - Relative x-coordinate of the end point of a line
// y2 - Relative y-coordinate of the end point of a line
// Return Value:
// None
//=============================================================================
void TFT_Line(short x1, short y1, short x2, short y2)
{
short Sx, Sy, Ex, Ey;
short LB, RB, TB, BB;
short Offset_x,Offset_y,Offset_k = 0;
short Err_d = 1;
LB = Window_TLx>=0 ? Window_TLx : 0; // Left bound
RB = Window_BRx<TFT_WIDTH ? Window_BRx : TFT_WIDTH-1; // Right bound
TB = Window_TLy>=0 ? Window_TLy : 0; // Top bound
BB = Window_BRy<TFT_HEIGHT ? Window_BRy : TFT_HEIGHT-1; // Bottom bound
Sx = Window_TLx + x1;
Sy = Window_TLy + y1;
Ex = Window_TLx + x2;
Ey = Window_TLy + y2;
if((Sx<LB && Ex<LB) || (Sx>RB && Ex>RB))return; // If line is outside window area, return
if((Sy<TB && Ey<TB) || (Sy>BB && Ey>BB))return;
if(Sx<LB) Sx = LB; // Actual drawing area
if(Sx>RB) Sx = RB;
if(Sy<TB) Sy = TB;
if(Sy>BB) Sy = BB;
if(Ex<LB) Ex = LB;
if(Ex>RB) Ex = RB;
if(Ey<TB) Ey = TB;
if(Ey>BB) Ey = BB;
if(Sy>Ey)
{
Offset_x = Sx;
Sx = Ex;
Ex = Offset_x;
Offset_y = Sy;
Sy = Ey;
Ey = Offset_y;
}
Offset_x = Ex-Sx;
Offset_y = Ey-Sy;
TFT_PutPixelAbsolute(Sx, Sy);
if(Offset_x<=0)
{
Offset_x = Sx-Ex;
Err_d = -1;
}
if(Offset_x>Offset_y)
{
while(Sx!=Ex)
{
if(Offset_k>0)
{
Sy += 1;
Offset_k += (Offset_y-Offset_x);
}
else Offset_k += Offset_y;
Sx += Err_d;
TFT_PutPixelAbsolute(Sx,Sy);
}
}
else
{
while(Sy!=Ey)
{
if(Offset_k>0)
{
Sx += Err_d;
Offset_k += (Offset_x-Offset_y);
}
else Offset_k += Offset_x;
Sy += 1;
TFT_PutPixelAbsolute(Sx, Sy);
}
}
}
//=============================================================================
// Prototype: void TFT_Circle(unsigned x,unsigned y,unsigned r,unsigned Mode);
// Description: Draw a circle in window.
// Arguments:
// x - Relative x-coordinate of the centre of a circle
// y - Relative y-coordinate of the centre of a circle
// r - The radius of a circle
// Mode - 0(PAINT_HOLLOW): hollow circle
// 1(PAINT_SOLID): solid circle
// Return Value:
// None
//=============================================================================
void TFT_Circle(unsigned x,unsigned y,unsigned r,unsigned Mode)
{
int dx,dy,ref;
int Temp;
if(r == 0)return;
dx = r;
dy = 1;
ref = 707 * r / 1000; // Reference value: 0.707r
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -