📄 datetime.c
字号:
/*************************************************************************
*
* Copyright 2002 National ASIC Center, All rights Reserved
*
* FILE NAME: DateTime.c
* PROGRAMMER: longn_qi
* Date of Creation: 2002/10/11
*
* DESCRIPTION:
*
* NOTE:
*
*
* FUNCTIONS LIST:
* -------------------------------------------------------------------------
*
* GLOBAL VARS LIST:
*
*
**************************************************************************
*
* MODIFICATION HISTORY
*
*
* 2002/10/11 by longn_qi Create the file
*
*************************************************************************/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <asixwin.h>
#include <asixapp.h>
#include <sys\systmr.h>
#include <asixwin\asix_tb.h>
#include <resource\bitmap.h>
extern void Calendar( void );
// 0 deg ~ 180 deg, point per 6 deg
WORD SinValue[30] = {
0, 428, 851, 1265, 1665, 2047,
2407, 2740, 3043, 3313, 3547, 3741,
3895, 4006, 4073, 4096, 4073, 4006,
3895, 3741, 3547, 3313, 3043, 2740,
2407, 2047, 1665, 1265, 851, 428,
};
extern void SetDateTimeTsk( void );
extern void SetSystemDateTime( void );
TASKDESCRIPTION CnfgDTTskDesp =
{"时间设置", ASIX_APP, 2, zi_time, 2048, 1, LCD_WIDTH, LCD_HEIGHT, SetDateTimeTsk, APP_PRI};
static void DrawClockPanel( void );
static void DrawClockHands( ASIX_TIME *time, U16 *pos_x, U16 *pos_y, U8 *update );
void SetDateTimeTsk( void )
{
SetSystemDateTime();
EndofTask();
}
void SetSystemDateTime( void )
{
MSG msg;
// U32 hGC;
// 窗口和控件ID
U32 mainwin; // 主窗口
U32 bt_adjust[2][3]; //
U32 bt_op[3]; //
U32 st_value[3];//
U32 tskbar; // 任务栏
U32 timer; // 刷新定时器
S8 *btcap[3] = { "保存设置", "日期设置", "恢复设置" };
char time_str[3][3];
U8 i, quit = 0;
U16 temp;
U16 *value[3];
U16 value_max[3] = { 23, 59, 59 };
U16 pos_x[3], pos_y[3]; // 时针、分针、秒针的位置
U8 update_flag[3] = { 0, 0, 0 }; // 时、分、秒的更新标志
U8 update_all[3] = { 1, 1, 1 }; // 完全更新标志
ASIX_TIME cur_tim;
// WINDOW ARRANGEMENT
const U16 form_w = PHY_LCD_W, form_h = PHY_LCD_H, form_x = 0, form_y = 0;
//const U16 bt_w = 32, bt_h = 12, bt_gap = ( form_w - bt_w * 3 )/4, bt_x = form_x + bt_gap, bt_y = PHY_LCD_H - 94;
const U16 bt_w = PHY_LCD_W/6, bt_h = 12, bt_gap = ( form_w - bt_w * 3 )/4, bt_x = form_x + bt_gap, bt_y = PHY_LCD_H*9/10 - 20 - 12 - PHY_LCD_H/12 - 12; //former bt_y = PHY_LCD_H - 94;
const U16 st_w = bt_w, st_h = PHY_LCD_H/12, st_x = bt_x , st_y = PHY_LCD_H*9/10 - 20 - 12 - PHY_LCD_H/12;
const U16 bt_y2 = PHY_LCD_H*9/10 - 20 -12 ;
/*
const U16 bt_w3[3] = { 52, 54, 52 },bt_x3[3] = { 1, 53, 107 };
const U16 bt_h3 = 20, bt_y3 = form_y + PHY_LCD_H - 20 - bt_h3;
*/
const U16 bt_w3[3] = { PHY_LCD_W/3-1, PHY_LCD_W/3, PHY_LCD_W/3-1 },bt_x3[3] = { 1, PHY_LCD_W/3, PHY_LCD_W*2/3 };
const U16 bt_h3 = PHY_LCD_H/10, bt_y3 = PHY_LCD_H*9/10 - 20 ;
mainwin = CreateWindow( WNDCLASS_WIN,
"时间设置",
WS_OVERLAPPEDWINDOW,
form_x,form_y,
form_w,form_h,
0,
0,
NULL);
tskbar = CreateWindow(WNDCLASS_TSKBAR, NULL, WS_CHILD|TBS_TYPICAL, 0, 0, 0, 0,mainwin, 0, NULL);
GetTime( &cur_tim );
sprintf( time_str[0], "%02.2d", cur_tim.hour );
sprintf( time_str[1], "%02.2d", cur_tim.minute );
sprintf( time_str[2], "%02.2d", cur_tim.second );
value[0] = &cur_tim.hour;
value[1] = &cur_tim.minute;
value[2] = &cur_tim.second;
temp = bt_x;
for( i = 0; i < 3; i++ )
{
// 增加键
bt_adjust[0][i] = CreateWindow( WNDCLASS_BUTTON,
NULL,
WS_CHILD | BS_ICON | BS_TRANSPARENT,
temp, bt_y,
bt_w,bt_h,
mainwin,0,(char *)up10X10);
// 时间
st_value[i] = CreateWindow( WNDCLASS_STATIC,
time_str[i],
WS_CHILD | SS_TEXT | SS_BOARD,
temp, st_y,
st_w,st_h,
mainwin,0,NULL);
if( i < 2 ) // 时间显示分隔符
CreateWindow( WNDCLASS_STATIC,
":",
WS_CHILD | SS_TEXT,
(U16)(temp+st_w), st_y,
bt_gap,st_h,
mainwin,0,NULL);
// 减少键
bt_adjust[1][i] = CreateWindow( WNDCLASS_BUTTON,
NULL,
WS_CHILD | BS_ICON | BS_TRANSPARENT,
temp, bt_y2,
bt_w,bt_h,
mainwin,0,(char *)down10X10);
// 功能键
bt_op[i] = CreateWindow( WNDCLASS_BUTTON,
btcap[i],
WS_CHILD | BS_REGULAR | BS_FLAT | BS_BOARD,
bt_x3[i], bt_y3,
bt_w3[i], bt_h3,
mainwin,0,NULL);
temp += bt_w + bt_gap;
}
EnableWindow( bt_op[0], FALSE );
EnableWindow( bt_op[2], FALSE );
// SetFocus( bt[0] );
pos_x[0] = 0;
pos_y[0] = 0;
DrawClockPanel();
DrawClockHands( &cur_tim, pos_x, pos_y, update_all );
CreateTimer( &timer, 1000, NULL, NULL, CYC_MODE|AUTO_START_MODE );
StartTimer( timer );
while( !quit )
{
ASIXGetMessage( &msg, NULL, 0, 0 );
switch( msg.message )
{
case WM_COMMAND:
for( i = 0; i < 3; i++ )
{
if( msg.lparam == bt_adjust[0][i] )
{
update_flag[i] = 1;
StopTimer( timer );
if( *value[i] == value_max[i] )
*value[i] = 0;
else
(*value[i])++;
sprintf( time_str[i], "%02d", *value[i] );
SetWindowText( st_value[i], time_str[i], NULL );
DrawClockHands( &cur_tim, pos_x, pos_y, update_flag );
update_flag[i] = 0;
EnableWindow( bt_op[0], TRUE );
EnableWindow( bt_op[2], TRUE );
break;
}
else if( msg.lparam == bt_adjust[1][i] )
{
update_flag[i] = 1;
StopTimer( timer );
if( *value[i] == 0 )
*value[i] = value_max[i];
else
(*value[i])--;
sprintf( time_str[i], "%02d", *value[i] );
SetWindowText( st_value[i], time_str[i], NULL );
DrawClockHands( &cur_tim, pos_x, pos_y, update_flag );
update_flag[i] = 0;
EnableWindow( bt_op[0], TRUE );
EnableWindow( bt_op[2], TRUE );
break;
}
}
if( i == 3 )
{
if( msg.lparam == bt_op[0] ) // 按下确定键
{
// 保存设置
StartTimer( timer );
SetTime( &cur_tim );
AdjustTskBarTime( tskbar );
EnableWindow( bt_op[0], FALSE );
EnableWindow( bt_op[2], FALSE );
quit =1;
}
else if( msg.lparam == bt_op[2] ) // 按下取消键
{
// 恢复设置
GetTime( &cur_tim );
for( i = 0; i < 3; i++ )
{
sprintf( time_str[i], "%02d", *value[i] );
SetWindowText( st_value[i], time_str[i], NULL );
}
DrawClockHands( &cur_tim, pos_x, pos_y, update_all );
StartTimer( timer );
EnableWindow( bt_op[0], FALSE );
EnableWindow( bt_op[2], FALSE );
quit = 1;
}
else if( msg.lparam == bt_op[1] ) // 切换到日期设置
{
StopTimer( timer );
Calendar();
}
}
break;
case WM_KEYDOWN:
case WM_KEYUP:
break;
case WM_TIMER:
if( msg.lparam == timer )
{
ASIX_TIME temp_tim;
U8 flag[3];
// 更新时间和界面
GetTime( &temp_tim );
flag[0] = ( temp_tim.hour != cur_tim.hour );
flag[1] = ( temp_tim.minute != cur_tim.minute );
flag[2] = ( temp_tim.second != cur_tim.second );
memcpy( &cur_tim, &temp_tim, sizeof( ASIX_TIME ) );
for( i = 0; i < 3; i++ )
{
if( flag[i] )
{
sprintf( time_str[i], "%02d", *value[i] );
SetWindowText( st_value[i], time_str[i], NULL );
}
}
DrawClockHands( &cur_tim, pos_x, pos_y, flag );
}
break;
case WM_REPAINT:
GetTime( &cur_tim );
for( i = 0; i < 3; i++ )
{
sprintf( time_str[i], "%02d", *value[i] );
SetWindowText( st_value[i], time_str[i], NULL );
}
pos_x[0] = 0;
pos_y[0] = 0;
DrawClockPanel();
DrawClockHands( &cur_tim, pos_x, pos_y, update_all );
StartTimer( timer );
break;
case WM_QUIT:
quit = 1;
break;
}
DefWindowProc(msg.message, msg.lparam, msg.data, msg.wparam);
}
FreeTimer( timer );
DestroyWindow( mainwin );
return;
}
void DrawClockPanel( void )
{
U32 hGC;
// U16 old_dot_w, new_dot_w = 3;
// const U16 clock_cx = 80, clock_cy = 85, clock_r = 55;
const U16 clock_cx = PHY_LCD_W/2, clock_cy = 20 + (PHY_LCD_H*9/10 - 20 - 12 - PHY_LCD_H/12 - 12 -20)/2, clock_r = (PHY_LCD_H*9/10 - 20 - 12 - PHY_LCD_H/12 - 12 -20)/2 - 15;
U32 off_x, off_y;
U16 pos_x, pos_y;
U8 i, j;
S8 sign_x[4] = { 0, 1, 1, 0 }, sign_y[4] = { 1, 1, 0, 0 };
hGC = GetGC();
GroupOn( hGC );
// SetDotWidth( hGC, new_dot_w, &old_dot_w );
ClearRec( hGC, ColorTheme.form_client, 20, 25, PHY_LCD_W/2-1, PHY_LCD_W/2-1, GPC_REPLACE_STYLE );
// ClearRec( hGC, ColorTheme.form_client, 24, 30, 143, 143, GPC_REPLACE_STYLE );
for( j = 0; j < 4; j++ )
{
for( i = 0; i < 16; i += 5 )
{
off_x = ( clock_r * SinValue[i] ) >> 12;
off_y = ( clock_r * SinValue[15-i] ) >> 12;
pos_x = sign_x[j] ? clock_cx + off_x : clock_cx - off_x;
pos_y = sign_y[j] ? clock_cy + off_y : clock_cy - off_y;
// DrawDot( hGC, GPC_BLACK, pos_x, pos_y, GPC_REPLACE_STYLE );
ClearRec( hGC, GPC_BLACK, pos_x-1, pos_y-1, 3, 3, GPC_REPLACE_STYLE );
}
}
// new_dot_w = old_dot_w;
// SetDotWidth( hGC, new_dot_w, &old_dot_w );
GroupOff( hGC, 20, 25, PHY_LCD_W - 20, PHY_LCD_W - 20 );
return;
}
void DrawClockHands( ASIX_TIME *time, U16 *pos_x, U16 *pos_y, U8 *update )
{
U32 hGC;
const U16 clock_cx = PHY_LCD_W/2, clock_cy = 20 + (PHY_LCD_H*9/10 - 20 - 12 - PHY_LCD_H/12 - 12 -20)/2 ;
U16 hour_r = (PHY_LCD_H*9/10 - 20 - 12 - PHY_LCD_H/12 - 12 -20)/2 - 15 - 20, minute_r = (PHY_LCD_H*9/10 - 20 - 12 - PHY_LCD_H/12 - 12 -20)/2 - 15 - 15, second_r = (PHY_LCD_H*9/10 - 20 - 12 - PHY_LCD_H/12 - 12 -20)/2 - 15 - 5;
U32 off_x, off_y;
U8 i, j;
S8 sign_x[4] = { 1, 1, 0, 0 }, sign_y[4] = { 0, 1, 1, 0 };
hGC = GetGC();
if( pos_x[0] || pos_y[0] )
{
// 清除
if( update[0] )
DrawLine( hGC, GPC_YELLOW, clock_cx, clock_cy, pos_x[0], pos_y[0], GPC_SOLID_LINE, GPC_XOR_STYLE );
if( update[1] )
DrawLine( hGC, GPC_YELLOW, clock_cx, clock_cy, pos_x[1], pos_y[1], GPC_SOLID_LINE, GPC_XOR_STYLE );
if( update[2] )
DrawLine( hGC, GPC_YELLOW, clock_cx, clock_cy, pos_x[2], pos_y[2], GPC_SOLID_LINE, GPC_XOR_STYLE );
}
if( update[0] )
{
// 画时针
j = (U8)( ( time->hour %12*5 + time->minute /12 ) /15 );
i = (U8)( ( time->hour %12*5 + time->minute /12 ) %30 );
off_x = ( hour_r * SinValue[i] ) >> 12;
if( i > 15 )
i = i -15;
else
i = 15 - i;
off_y = ( hour_r * SinValue[i] ) >> 12;
pos_x[0] = sign_x[j] ? clock_cx + off_x : clock_cx - off_x;
pos_y[0] = sign_y[j] ? clock_cy + off_y : clock_cy - off_y;
DrawLine( hGC, GPC_YELLOW, clock_cx, clock_cy, pos_x[0], pos_y[0], GPC_SOLID_LINE, GPC_XOR_STYLE );
}
if( update[1] )
{
// 画分针
j = (U8)( time->minute / 15 );
i = (U8)( time->minute % 30 );
off_x = ( minute_r * SinValue[i] ) >> 12;
if( i > 15 )
i = i -15;
else
i = 15 - i;
off_y = ( minute_r * SinValue[i] ) >> 12;
pos_x[1] = sign_x[j] ? clock_cx + off_x : clock_cx - off_x;
pos_y[1] = sign_y[j] ? clock_cy + off_y : clock_cy - off_y;
DrawLine( hGC, GPC_YELLOW, clock_cx, clock_cy, pos_x[1], pos_y[1], GPC_SOLID_LINE, GPC_XOR_STYLE );
}
if( update[2] )
{
// 画秒针
j = (U8)( time->second / 15 );
i = (U8)( time->second % 30 );
off_x = ( second_r * SinValue[i] ) >> 12;
if( i > 15 )
i = i -15;
else
i = 15 - i;
off_y = ( second_r * SinValue[i] ) >> 12;
pos_x[2] = sign_x[j] ? clock_cx + off_x : clock_cx - off_x;
pos_y[2] = sign_y[j] ? clock_cy + off_y : clock_cy - off_y;
DrawLine( hGC, GPC_YELLOW, clock_cx, clock_cy, pos_x[2], pos_y[2], GPC_SOLID_LINE, GPC_XOR_STYLE );
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -