📄 graphics.c
字号:
// ----------------------- Graphic Library of LCD ------------------------------
/* File Name: graphics.c
Version: 0.7
Last Updated: 2007-3-10
Description:
This library contains the necessary functions for drawing on the LCD screen.
for example, drawing a straight line, a rectangle, displaying a picture and
so on.
Program Coder: Seeseawe
*/
// Display a picture on the specific position of the Screen
void disp_image(uint8 code *ptrImage, uint8 StartP, StartC)
{
uint8 i,k;
uint8 width = *ptrImage++; // Image width
uint8 height = *ptrImage++; // Image height
uint8 PageHold; // Pages the image holds
uint8 EndPage;
// Calc PageHold
if (height%8 != 0)
PageHold = height/8 + 1;
else
PageHold = height/8;
// Calc EndPage
if (StartP - PageHold > StartP)
EndPage = 0;
else
EndPage = StartP - PageHold + 1;
// Display Picture
for (k = StartP; k >= EndPage; k--)
{
Gotoxy(k, StartC);
for (i = 0; i < width; i++)
{
if (i + StartC < LCD_CMAX)
//SendByte(iDat, *ptrImage++);
LCD_WriteData(*ptrImage++);
else
ptrImage++;
}
if (k == 0)
break; // Jump out when StartPage is 0.
}
}
// Display a picture on the upleft corner of LCD Screen
void disp_upleft(uint8 code *ptrImage)
{
disp_image(ptrImage, LCD_PMAX - 1, 0);
}
// Display a picture on the center of LCD Screen
void disp_center(uint8 code *ptrImage)
{
uint8 width = *ptrImage; // Image width
uint8 height = *(ptrImage + 1); // Image height
uint8 PageHold; // Pages the image holds
uint8 StartP; // Start Page
uint8 StartC; // Start Column
if (height%8 != 0)
PageHold = height/8 + 1;
else
PageHold = height/8;
if ((LCD_PMAX + PageHold)%2 != 0)
StartP = (LCD_PMAX + PageHold)/2 + 1;
else
StartP = (LCD_PMAX + PageHold)/2;
StartC = (LCD_CMAX - width)/2;
disp_image(ptrImage, StartP, StartC);
}
// Tile screen with a small picture
void gTile(uint8 code *ptrImage)
{
uint8 i, j;
uint8 width = *ptrImage; // Image width
uint8 height = *(ptrImage + 1); // Image height
uint8 PageHold; // Pages the image holds
// Calc PageHold
if (height%8 != 0)
PageHold = height/8 + 1;
else
PageHold = height/8;
for (i = 0; i < LCD_PMAX/PageHold; i++)
{
for (j = 0; j < LCD_CMAX/width; j++)
{
disp_image(ptrImage, LCD_PMAX - i*PageHold - 1, j*width);
}
}
}
// Draw a rectangle using the two given points
void disp_frame(uint8 X0,Y0,X1,Y1)
{
uint8 i, StartP, EndP;
uint8 LstDot[2];
StartP = Y1/8;
EndP = Y0/8;
LstDot[0] = 1 << Y0%8;
LstDot[1] = 1 << Y1%8;
Gotoxy(StartP, X0);
SendByte(iDat, 0x80|((~(2*LstDot[1] - 1))>>1));
SetCA(X1);
SendByte(iDat, 0x80|((~(2*LstDot[1] - 1))>>1));
Gotoxy(EndP, X0);
SendByte(iDat, 2*LstDot[0] - 1);
SetCA(X1);
SendByte(iDat, 2*LstDot[0] - 1);
// Draw the vertical lines
for (i = StartP + 1; i < EndP; i++)
{
Gotoxy(i, X0);
SendByte(iDat, 0xFF);
SetCA(X1);
SendByte(iDat, 0xFF);
}
// Draw the horizontal lines
for (i = X0 + 1; i < X1; i++)
{
Gotoxy(StartP, i);
SendByte(iDat, LstDot[1]);
Gotoxy(EndP, i);
SendByte(iDat, LstDot[0]);
}
}
#ifdef LCD_READABLE
// Get the status of a point at specific coordinate
uint8 ReadPoint(uint8 Xpoint, uint8 Ypoint)
{
uint8 Page;
uint8 status;
Page = Ypoint/8;
//Page = Ypoint>>3;
Gotoxy(Page, Xpoint);
status = LCD_ReadData();
status &= (1 << (Ypoint % 8));
return status;
}
// Draw a point at specific coordinate
void Point(uint8 Xpoint, uint8 Ypoint)
{
uint8 Page;
uint8 OldData;
uint8 NewData;
Page = Ypoint/8;
//Page = Ypoint>>3;
Gotoxy(Page, Xpoint);
OldData = LCD_ReadData();
Gotoxy(Page, Xpoint);
if (iPoint)
{
// Turn on a point
NewData = 0x01 << (Ypoint%8);
SendByte(iDat, OldData|NewData);
}
else
{
// Turn off a point
NewData = ~(0x01 << (Ypoint%8));
SendByte(iDat, OldData & NewData);
}
}
// Reverse the status of a point
void RvsPoint(uint8 Xpoint, uint8 Ypoint)
{
if (ReadPoint(Xpoint, Ypoint) != 0x00)
{
iPoint = 0;
}
else
{
iPoint = 1;
}
Point(Xpoint, Ypoint);
}
// Reverse the status of an area
void RvsArea(tRect Area)
{
uint8 i, j;
for (i = Area.X0; i <= Area.X1; i++)
{
for (j = Area.Y1; j <= Area.Y0; j++)
{
RvsPoint(i, j);
}
}
}
// Draw a horizontal line
void LineH(uint8 X0, uint8 Y0, uint8 Length)
{
uint8 i;
uint8 EndPoint;
EndPoint = X0 + Length;
if (EndPoint >= LCD_CMAX)
{
EndPoint = LCD_CMAX;
}
for (i = X0; i < EndPoint;)
{
switch (LineStyle)
{
case RealLine:
{
// Real Line
Point(i++, Y0);
break;
}
case BrokenLine0:
{
// Broken line
iPoint = 0;
Point(i++, Y0);
iPoint = 1;
Point(i++, Y0);
break;
}
case BrokenLine1:
{
// Broken line
iPoint = 1;
Point(i++, Y0);
iPoint = 0;
Point(i++, Y0);
break;
}
default:
// Real line
Point(i++, Y0);
}
}
}
// Draw a Virtical line
void LineV(uint8 X0, uint8 Y0, uint8 Height)
{
uint8 i;
uint8 EndPoint;
EndPoint = Y0 + Height;
if (EndPoint >= LCD_LMAX)
{
EndPoint = LCD_LMAX;
}
for (i = Y0; i < EndPoint;)
{
switch (LineStyle)
{
case RealLine:
// Real line
Point(X0, i++);
break;
case BrokenLine0:
// Broken line
iPoint = 0;
Point(X0, i++);
iPoint = 1;
Point(X0, i++);
break;
case BrokenLine1:
// Broken line
iPoint = 1;
Point(X0, i++);
iPoint = 0;
Point(X0, i++);
break;
default:
// Real line
Point(X0, i++);
}
}
}
/* ---------------------------------------------------------------------------
Drawing a circle with the equation: (X - Ox)^2 + (Y - Oy)^2 = Rx^2
Original Coder: XiaoQi
Modifier: Seeseawe
Release Date: 2007-3-2
// ---------------------------------------------------------------------------*/
void Circle(uint8 Ox, uint8 Oy, uint8 Rx)
{
uint8 col, row; // Current Col and Row address
uint16 xx, rr, xt, yt, rs;
yt = Rx;
rr = Rx*Rx+1; //补偿 1 修正方形
rs = (yt+(yt>>1))>>1; //(*0.75)分开1/8圆弧来画
for (xt = 0; xt <= rs; xt++)
{
xx = xt*xt;
while ((yt*yt)>(rr-xx)) yt--;
col = Ox+xt; //第一象限
row = Oy-yt;
Point(col, row);
col = Ox-xt; //第二象限
Point(col, row);
row = Oy+yt; //第三象限
Point(col, row);
col = Ox+xt; //第四象限
Point(col, row);
// ---------------- 45度镜象画另一半 ---------------
col = Ox+yt; //第一象限
row = Oy-xt;
Point(col, row);
col = Ox-yt; //第二象限
Point(col, row);
row = Oy+xt; //第三象限
Point(col, row);
col = Ox+yt; //第四象限
Point(col, row);
}
}
// Draw a table with specific rows and cols
void Table(uint8 Rows, Cols, width, height)
{
uint8 i;
uint8 k;
uint8 PN;
tRect fstRect;
fstRect.X0 = 0;
fstRect.Y0 = LCD_LMAX - 1;
fstRect.X1 = fstRect.X0 + width - 1;
fstRect.Y1 = fstRect.Y0 - height + 1;
for (i = 0; i < Rows; i++)
{
for (k = 0; k < Cols; k++)
{
Rectangle(fstRect);
fstRect.X0 = fstRect.X1;
fstRect.X1 += width - 1;
}
fstRect.X0 = 0;
fstRect.X1 = fstRect.X0 + width - 1;
fstRect.Y0 = fstRect.Y1;
PN = fstRect.Y1;
fstRect.Y1 = fstRect.Y0 - height + 1;
if (PN < fstRect.Y1)
break;
}
}
// Scan Vertical line
void ScanV(uint16 Tscan)
{
uint8 i;
for (i = 0; i < LCD_CMAX; i += 2)
{
LineV(i,0, LCD_LMAX);
DelayMs(Tscan);
}
}
// Scan Horizontal line
void ScanH(uint16 Tscan)
{
uint8 i;
for (i = 0; i < LCD_LMAX; i += 2)
{
LineH(0,i, LCD_CMAX);
DelayMs(Tscan);
}
}
// Draw a Rectangle
void Rectangle(tRect Points)
{
LineH(Points.X0, Points.Y0, Points.X1 - Points.X0 + 1);
LineH(Points.X0, Points.Y1, Points.X1 - Points.X0 + 1);
LineV(Points.X0, Points.Y1, Points.Y0 - Points.Y1 + 1);
LineV(Points.X1, Points.Y1, Points.Y0 - Points.Y1 + 1);
}
// Show x concentric rectangles
void frameShow(uint8 xframes)
{
tRect OriginRect;
OriginRect.X0 = LCD_CMAX/2 - 10;
OriginRect.Y0 = LCD_LMAX/2 + 5;
OriginRect.X1 = LCD_CMAX/2 + 10;
OriginRect.Y1 = LCD_LMAX/2 - 5;
while(xframes-- != 0)
{
Rectangle(OriginRect);
OriginRect.X0 -= 4;
OriginRect.Y0 += 2;
OriginRect.X1 += 4;
OriginRect.Y1 -= 2;
}
}
// Render 8 dots on vertical direction
void RenderV8(uint8 dByte, xPos, yPos)
{
uint8 i;
for (i = 0; i < 8; i++)
{
if (((dByte<<i) & 0x80) != 0x80)
iPoint = 0;
else
iPoint = 1;
if (i <= yPos)
Point(xPos, yPos - i);
else
break;
}
iPoint = 1;
}
// Display a picture anywhere(exactly a ponit)
void PictureAnywhere(uint8 code *ptrImage, uint8 xPos, yPos)
{
uint8 i,k;
uint8 width = *ptrImage++; // Image width
uint8 height = *ptrImage++; // Image height
uint8 StartPage; // The page that point(xPos, yPos) belong to
uint8 PageHold; // Pages the image holds
StartPage = yPos/8;
// Calc PageHold
if (height%8 != 0)
PageHold = height/8 + 1;
else
PageHold = height/8;
// Calc Effective display area
if (PageHold > StartPage)
PageHold = StartPage + 1;
for (k = 0; k < PageHold; k++)
{
for (i = 0; i < width; i++)
{
RenderV8(*ptrImage++, xPos + i, yPos - k*8);
}
}
}
#endif
// Draw a fullscreen rectangle
void RectFullscreen(void)
{
#ifndef LCD_READABLE
disp_frame(0, LCD_LMAX - 1, LCD_CMAX - 1, 0);
#else
LineH(0, 0, LCD_CMAX);
LineH(0, LCD_LMAX - 1, LCD_CMAX);
LineV(0, 0, LCD_LMAX);
LineV(LCD_CMAX - 1, 0, LCD_LMAX);
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -