- CVI 정보나눔(www.cvi.kr)
- 스터디
- 개발메모
개발메모(형식에 매이지 않는 짤막한 개발 낙서)
글 수 174
타이머에 의해 날짜와 시간을 표시하는 Timer Callback의 예
출력할 스트링 컨트롤의 ID는 다음과 같다고 가정합니다.
날짜는 PANEL_strDate
시각은 PANEL_strTime
// Time 관련 전역변수-시작
long g_ltime;
struct tm *p_newtime;
// Time 관련 전역변수-종료
void Func_Generate_Date_string(struct tm *newtime, char *StrDate);
void Func_Generate_Time_string(struct tm *newtime, char *StrTime);
int CVICALLBACK TimerCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
static char Buffer_Date[50], Buffer_Time[20];
static int prev_tm_mday; /// 날짜 갱신을 위한 조건 검사를 위해...
switch (event)
{
case EVENT_TIMER_TICK:
// 1. 날짜 요일 정보 획득
time( &g_ltime );
p_newtime = localtime( &g_ltime );
// 2. 날짜 정보 출력
if(prev_tm_mday != p_newtime->tm_mday) /// 날짜를 출력하기 위한 조치가 필요한지를 체크
{
// 날짜 문자열 제작
Func_Generate_Date_string(p_newtime, Buffer_Date);
prev_tm_mday = p_newtime->tm_mday;
SetCtrlVal(panelHandle, PANEL_strDate, Buffer_Date);
}
// 3. 시각 정보 출력
// 시간은 매초마다 바뀌므로 수정
Func_Generate_Time_string(p_newtime, Buffer_Time);
SetCtrlVal(panelHandle, PANEL_strTime, Buffer_Time);
break;
}
return 0;
}
void Func_Generate_Date_string(struct tm *newtime, char *StrDate)
// 날짜 정보를 이용하여 날짜 문자열을 만듬
// 예) 2003년 2월 14일 금요일
{
sprintf(StrDate, "%4d년 %d월 %d일(",
newtime->tm_year+1900, newtime->tm_mon+1, newtime->tm_mday);
switch(newtime->tm_wday)
{
case 0:
strcat(StrDate, "일)");
break;
case 1:
strcat(StrDate, "월)");
break;
case 2:
strcat(StrDate, "화)");
break;
case 3:
strcat(StrDate, "수)");
break;
case 4:
strcat(StrDate, "목)");
break;
case 5:
strcat(StrDate, "금)");
break;
case 6:
strcat(StrDate, "토)");
break;
}
}
void Func_Generate_Time_string(struct tm *newtime, char *StrTime)
// 시각 정보를 이용하여 시각 문자열을 만듬
// 예) 18:28:35
{
sprintf(StrTime, "%02d:%02d:%02d", newtime->tm_hour, newtime->tm_min, newtime->tm_sec);
}
출력할 스트링 컨트롤의 ID는 다음과 같다고 가정합니다.
날짜는 PANEL_strDate
시각은 PANEL_strTime
// Time 관련 전역변수-시작
long g_ltime;
struct tm *p_newtime;
// Time 관련 전역변수-종료
void Func_Generate_Date_string(struct tm *newtime, char *StrDate);
void Func_Generate_Time_string(struct tm *newtime, char *StrTime);
int CVICALLBACK TimerCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
static char Buffer_Date[50], Buffer_Time[20];
static int prev_tm_mday; /// 날짜 갱신을 위한 조건 검사를 위해...
switch (event)
{
case EVENT_TIMER_TICK:
// 1. 날짜 요일 정보 획득
time( &g_ltime );
p_newtime = localtime( &g_ltime );
// 2. 날짜 정보 출력
if(prev_tm_mday != p_newtime->tm_mday) /// 날짜를 출력하기 위한 조치가 필요한지를 체크
{
// 날짜 문자열 제작
Func_Generate_Date_string(p_newtime, Buffer_Date);
prev_tm_mday = p_newtime->tm_mday;
SetCtrlVal(panelHandle, PANEL_strDate, Buffer_Date);
}
// 3. 시각 정보 출력
// 시간은 매초마다 바뀌므로 수정
Func_Generate_Time_string(p_newtime, Buffer_Time);
SetCtrlVal(panelHandle, PANEL_strTime, Buffer_Time);
break;
}
return 0;
}
void Func_Generate_Date_string(struct tm *newtime, char *StrDate)
// 날짜 정보를 이용하여 날짜 문자열을 만듬
// 예) 2003년 2월 14일 금요일
{
sprintf(StrDate, "%4d년 %d월 %d일(",
newtime->tm_year+1900, newtime->tm_mon+1, newtime->tm_mday);
switch(newtime->tm_wday)
{
case 0:
strcat(StrDate, "일)");
break;
case 1:
strcat(StrDate, "월)");
break;
case 2:
strcat(StrDate, "화)");
break;
case 3:
strcat(StrDate, "수)");
break;
case 4:
strcat(StrDate, "목)");
break;
case 5:
strcat(StrDate, "금)");
break;
case 6:
strcat(StrDate, "토)");
break;
}
}
void Func_Generate_Time_string(struct tm *newtime, char *StrTime)
// 시각 정보를 이용하여 시각 문자열을 만듬
// 예) 18:28:35
{
sprintf(StrTime, "%02d:%02d:%02d", newtime->tm_hour, newtime->tm_min, newtime->tm_sec);
}