?? clk_demo.c
字號:
/************************************************************************/
/* lck_demo.c
程序是利用C語言開發環境下的作圖函數和時間讀取和設置函數編寫而成的,實現了
時間的時鐘顯示。
(Display the time with hands, the time also could be changed.)
Copyright (c) 2006 by Thinta Co.LTD
All rights reserved */
/************************************************************************/
#include "..\inc\Bio_Func.h"
#include "..\inc\key.h"
#include <STDIO.H>
#include <STRING.H>
#include <MATH.H>
#define PI 3.1415926
#define SCALENUM 60 //表盤被劃分的精確度
#define INTERVAL (SCALENUM / 12) //每5分鐘的表盤格數
#define HOURRADIUS 0.35 //小時指針的長度與表盤半徑的比值
#define MINRADIUS 0.65 //分針長度與表盤半徑的比值
#define CRDRADIUS 1 //刻度盤半徑與表盤半徑的比值
#define NORMALCOLOR 1
#define NONECOLOR 0
typedef struct Point
{
U8 pos_x;
U8 pos_y;
}Point;
void _PutString( U8 x,U8 y,U8 *str,U8 color )
{
_setdispcolor(color);
_gotoxy(x,y);
_putstr(str);
}
void Settime(void);
void Clk_help(void);
void drawclk(Point Center, U8 nRadius, U8 LenRect);
S16 drawclkAdvance(Point Center, U8 nRadius, U8 LenRect) ;
void main(void)
{
U8 nRadius, LenRect;
S16 Key_return;
Point Center ;
_sysinit();
Center.pos_x = 80 ;
Center.pos_y = 50 ;
nRadius = 30 ;
LenRect = 80 ;
while(1)
{
_cls();
_gotoxy(0, (U8)(((Center.pos_y + LenRect / 2)/16) +3));
_putstr(" ESC 鍵: 退出\n“1” 鍵: 設置時間 ");
Key_return = drawclkAdvance(Center, nRadius, LenRect);
if (Key_return == ESC)
break; //按ESC鍵退出
else if (Key_return == NUMBER_1) //若按“1”鍵則進入修改時間
Settime();
else if (Key_return == KEY_HELP) //按HELP鍵獲得幫助
Clk_help();
}
}
void drawclk(Point Center, U8 nRadius, U8 LenRect)
{
U8 i ;
U8 nClk_time[9] = {0} ;
U8 nSeq_hour, nSeq_min; //時間的大小
U8 nRadius_hour, nRadius_min, nRadius_crd ; //指針的半徑
float nPos_AngleX[SCALENUM] , nPos_AngleY[SCALENUM] ;
Point CenterPoint, LeftUp , RightBottom ; //時鐘的外形尺寸
Point nPos_clk_min[SCALENUM], nPos_clk_hour[SCALENUM], nPos_clk_crd[SCALENUM] ;
//初始化外形尺寸及指針半徑
CenterPoint = Center ;
LeftUp.pos_x = (U8)(Center.pos_x - LenRect / 2 + 0.5) ;
LeftUp.pos_y = (U8)(Center.pos_y - LenRect / 2 + 0.5) ;
RightBottom.pos_x = (U8)(Center.pos_x + LenRect / 2 + 0.5) ;
RightBottom.pos_y = (U8)(Center.pos_y + LenRect / 2 + 0.5) ;
nRadius_hour = (U8)(HOURRADIUS * nRadius) ;
nRadius_min = (U8)(MINRADIUS * nRadius) ;
nRadius_crd = (U8)(CRDRADIUS * nRadius) ;
//初始化表盤及指針位置,將360度分為SCALENUM等分,計算相應長度指針
//及表盤的準確位置,當畫指針時,只需要在計算得到的數組中取相應的值
//即可,不必再計算
for (i = 0; i < SCALENUM; i++)
{
nPos_AngleX[i] = sin(2 * PI / SCALENUM * i) ;
nPos_AngleY[i] = -cos(2 * PI / SCALENUM * i) ;
nPos_clk_min[i].pos_x = (U8)(nPos_AngleX[i] * nRadius_min + CenterPoint.pos_x) ;
nPos_clk_min[i].pos_y = (U8)(nPos_AngleY[i] * nRadius_min + CenterPoint.pos_y) ; //分針位置
nPos_clk_hour[i].pos_x = (U8)(nPos_AngleX[i] * nRadius_hour + CenterPoint.pos_x) ;
nPos_clk_hour[i].pos_y = (U8)(nPos_AngleY[i] * nRadius_hour + CenterPoint.pos_y) ; //時針位置
nPos_clk_crd[i].pos_x = (U8)(nPos_AngleX[i] * nRadius_crd + CenterPoint.pos_x) ;
nPos_clk_crd[i].pos_y = (U8)(nPos_AngleY[i] * nRadius_crd + CenterPoint.pos_y) ; //坐標位置
if (i % INTERVAL == 0)
_drawdot(nPos_clk_crd[i].pos_x, nPos_clk_crd[i].pos_y, NORMALCOLOR);
}
//初始化刻度線及邊框,畫外方形邊框以及刻鐘表盤位置
_drawrect(LeftUp.pos_x, LeftUp.pos_y, RightBottom.pos_x, RightBottom.pos_y, NORMALCOLOR);
_drawline(CenterPoint.pos_x, nPos_clk_crd[0].pos_y, CenterPoint.pos_x, nPos_clk_crd[0].pos_y + 4, NORMALCOLOR);
_drawline(nPos_clk_crd[15].pos_x, CenterPoint.pos_y, nPos_clk_crd[15].pos_x - 4, CenterPoint.pos_y, NORMALCOLOR);
_drawline(nPos_clk_crd[45].pos_x, CenterPoint.pos_y, nPos_clk_crd[45].pos_x + 4, CenterPoint.pos_y, NORMALCOLOR);
_drawline(CenterPoint.pos_x, nPos_clk_crd[30].pos_y, CenterPoint.pos_x, nPos_clk_crd[30].pos_y - 4, NORMALCOLOR);
while(1)
{
//清除上次指針
_drawline(CenterPoint.pos_x, CenterPoint.pos_y, nPos_clk_min[nSeq_min].pos_x, nPos_clk_min[nSeq_min].pos_y, NONECOLOR) ;
_drawline(CenterPoint.pos_x, CenterPoint.pos_y, nPos_clk_hour[nSeq_hour].pos_x, nPos_clk_hour[nSeq_hour].pos_y, NONECOLOR) ;
_gettime(nClk_time);
//計算分鐘及時鐘對應的指針位置數組中的序號
nSeq_min = (U8) (((nClk_time[3] - '0') * 10 + (nClk_time[4] - '0')) * 60 / SCALENUM) ;
nSeq_hour = (U8) (((nClk_time[0] - '0') * 10 + (nClk_time[1] - '0')) % 12 * INTERVAL + nSeq_min * INTERVAL / SCALENUM ) ;
//畫出當前指針
_drawline(CenterPoint.pos_x, CenterPoint.pos_y, nPos_clk_min[nSeq_min].pos_x, nPos_clk_min[nSeq_min].pos_y, NORMALCOLOR) ;
_drawline(CenterPoint.pos_x, CenterPoint.pos_y, nPos_clk_hour[nSeq_hour].pos_x, nPos_clk_hour[nSeq_hour].pos_y, NORMALCOLOR) ;
_gotoxy(1, (U8)(RightBottom.pos_y / 16 + 1));
_putstr("當前時間: ");
_putstr(nClk_time);
if (_bioskey(1) != 0 )
{
if(_bioskey(0) == ESC)
break;
}
_delay(80);
}
}
/************************************************************************/
/* S16 drawclkAdvance(Point Center, U8 nRadius, U8 LenRect)
畫指針模式時間函數
輸入:時鐘的中心點 Center; 時鐘表盤半徑:nRadius; 時鐘外邊框長度:LenRect
輸出:時鐘顯示時的按鍵值
*/
/************************************************************************/
S16 drawclkAdvance(Point Center, U8 nRadius, U8 LenRect)
{
U8 i ;
U8 nClk_time[9] = {0} ;
U8 nSeq_hour, nSeq_min; //時間的大小
U8 nRadius_hour, nRadius_min, nRadius_crd ; //指針的半徑
float nPos_AngleX[SCALENUM] , nPos_AngleY[SCALENUM] ;
Point CenterPoint, LeftUp , RightBottom ; //時鐘的外形尺寸
Point nPos_clk_min[SCALENUM], nPos_clk_hour[SCALENUM], nPos_clk_crd[SCALENUM] ;
//初始化外形尺寸及指針半徑
CenterPoint = Center ;
LeftUp.pos_x = (U8)(Center.pos_x - LenRect / 2 + 0.5) ;
LeftUp.pos_y = (U8)(Center.pos_y - LenRect / 2 + 0.5) ;
RightBottom.pos_x = (U8)(Center.pos_x + LenRect / 2 + 0.5) ;
RightBottom.pos_y = (U8)(Center.pos_y + LenRect / 2 + 0.5) ;
nRadius_hour = (U8)(HOURRADIUS * nRadius) ;
nRadius_min = (U8)(MINRADIUS * nRadius) ;
nRadius_crd = (U8)(CRDRADIUS * nRadius) ;
//初始化表盤及指針位置,將360度分為SCALENUM等分,計算相應長度指針
//及表盤的準確位置,當畫指針時,只需要在計算得到的數組中取相應的值
//即可,不必再計算
for (i = 0; i < SCALENUM; i++)
{
nPos_AngleX[i] = sin(2 * PI / SCALENUM * i) ;
nPos_AngleY[i] = -cos(2 * PI / SCALENUM * i) ;
nPos_clk_min[i].pos_x = (U8)(nPos_AngleX[i] * nRadius_min + CenterPoint.pos_x) ;
nPos_clk_min[i].pos_y = (U8)(nPos_AngleY[i] * nRadius_min + CenterPoint.pos_y) ; //分針位置
nPos_clk_hour[i].pos_x = (U8)(nPos_AngleX[i] * nRadius_hour + CenterPoint.pos_x) ;
nPos_clk_hour[i].pos_y = (U8)(nPos_AngleY[i] * nRadius_hour + CenterPoint.pos_y) ; //時針位置
nPos_clk_crd[i].pos_x = (U8)(nPos_AngleX[i] * nRadius_crd + CenterPoint.pos_x) ;
nPos_clk_crd[i].pos_y = (U8)(nPos_AngleY[i] * nRadius_crd + CenterPoint.pos_y) ; //坐標位置
if (i % INTERVAL == 0)
_drawdot(nPos_clk_crd[i].pos_x, nPos_clk_crd[i].pos_y, NORMALCOLOR);
}
//初始化刻度線及邊框,畫外方形邊框以及刻鐘表盤位置
_drawrect(LeftUp.pos_x, LeftUp.pos_y, RightBottom.pos_x, RightBottom.pos_y, NORMALCOLOR);
_drawline(CenterPoint.pos_x, nPos_clk_crd[0].pos_y, CenterPoint.pos_x, nPos_clk_crd[0].pos_y + 4, NORMALCOLOR);
_drawline(nPos_clk_crd[15].pos_x, CenterPoint.pos_y, nPos_clk_crd[15].pos_x - 4, CenterPoint.pos_y, NORMALCOLOR);
_drawline(nPos_clk_crd[45].pos_x, CenterPoint.pos_y, nPos_clk_crd[45].pos_x + 4, CenterPoint.pos_y, NORMALCOLOR);
_drawline(CenterPoint.pos_x, nPos_clk_crd[30].pos_y, CenterPoint.pos_x, nPos_clk_crd[30].pos_y - 4, NORMALCOLOR);
while(1)
{
//清除上次指針
_drawline(CenterPoint.pos_x, CenterPoint.pos_y, nPos_clk_min[nSeq_min].pos_x, nPos_clk_min[nSeq_min].pos_y, NONECOLOR) ;
_drawline(CenterPoint.pos_x, CenterPoint.pos_y, nPos_clk_hour[nSeq_hour].pos_x, nPos_clk_hour[nSeq_hour].pos_y, NONECOLOR) ;
_gettime(nClk_time);
////計算分鐘及時鐘對應的指針位置數組中的序號
nSeq_min = (U8) ((nClk_time[3] - '0') * 10 + (nClk_time[4] - '0')) ;
nSeq_hour = (U8) (((nClk_time[0] - '0') * 10 + (nClk_time[1] - '0')) % 12 * INTERVAL + nSeq_min * INTERVAL / SCALENUM ) ;
//畫出當前指針
_drawline(CenterPoint.pos_x, CenterPoint.pos_y, nPos_clk_min[nSeq_min].pos_x, nPos_clk_min[nSeq_min].pos_y, NORMALCOLOR) ;
_drawline(CenterPoint.pos_x, CenterPoint.pos_y, nPos_clk_hour[nSeq_hour].pos_x, nPos_clk_hour[nSeq_hour].pos_y, NORMALCOLOR) ;
_gotoxy(1, (U8)(RightBottom.pos_y / 16 + 1));
_putstr("當前時間: ");
_putstr(nClk_time);
if (_bioskey(1) != 0 )
break;
_delay(80);
}
return _bioskey(0) ;
}
/************************************************************************/
/* 依據輸入鍵值進行時間設置,并判斷設置是否成功 */
/************************************************************************/
void Settime(void)
{
U8 i = 0;
U8 nTime[9] = {0};
S16 Key_in;
_cls();
_PutString(0, 1, " 修改時間 ", NORMALCOLOR);
_PutString(0, 3, " 小時: 分: 秒 ", NORMALCOLOR);
_gotoxy(5, 4);
while(1)
{
//使用_gettime ()函數得到的時間字符串中,第3個與第5個字符是“:”
if (2 == i | 5 == i)
{
i++;
_putstr(":");
}
else if(i < 8)
{
Key_in = _bioskey(0);
if (Key_in == ESC || Key_in == ENTER)
break;
_putch((U8)Key_in);
nTime[i] = Key_in;
i++;
}
else
break;
}
if (_settime(nTime) == 0)
_PutString(0, 7, "時間設置成功!",NORMALCOLOR);
else
_PutString(0, 7, "時間設置失敗!",NORMALCOLOR);
_bioskey(0);
}
/*幫助信息*/
void Clk_help(void)
{
_cls();
_PutString(0, 0, " -時鐘操作幫助- ", NORMALCOLOR);
_PutString(2, 2, "修改時間請按“1”鍵, 退出時鐘顯示程序請按“ESC”鍵", NORMALCOLOR);
_bioskey(0);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -