📄 wdtsampledlg.cpp
字号:
{
// No free IRQ resource in system
MessageBox("Not searched IRQ resource!","Error",MB_OK);
return false;
}
}
else
{ // user give irq
if(m_nEdit_irq < 0 || m_nEdit_irq > 15)
{
// irq error
MessageBox("IRQ number must between 0 to 15","IRQ Setting",MB_OK);
return false;
}
else
{
// irq ok
result = WDTCreate(WDT_MODE_INTERRUPT,m_nEdit_irq);
if(result == -1)
{
// Invalid parameters
MessageBox("Create WDT parameters error!","Error",MB_OK);
return false;
}
else if(result == -3)
{
// IRQ resourc conflict
MessageBox("IRQ used by others device","IRQ Setting",MB_OK);
return false;
}
}
}
return true;
}
BOOL CWDTSampleDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
OnButtonStop();
if(m_bWDTCreated)
{
WDTDestroy();
}
if(m_hMyEvent != NULL)
{
m_ThreadExit = true;
CloseHandle(m_hMyEvent);
m_hMyEvent = NULL;
}
if(m_Initialized)
{
ShutdownEVOCIO();
}
NOTIFYICONDATA nd;
nd.cbSize = sizeof (NOTIFYICONDATA);
nd.hWnd = m_hWnd;
nd.uID = IDR_MAINFRAME;
nd.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
nd.uCallbackMessage = WM_NOTIFYICON;
nd.hIcon = m_hIcon;
Shell_NotifyIcon(NIM_DELETE, &nd);
return CDialog::DestroyWindow();
}
// While press "Enter","Check Box" can get the "key press" message.
BOOL CWDTSampleDlg::PreTranslateMessage(MSG* pMsg)
{
int nFocus;
if(pMsg->message==WM_KEYDOWN) // Is keydown.
{
switch(pMsg->wParam)
{
case VK_RETURN: // Is key "Enter" .
nFocus=GetFocus()->GetDlgCtrlID();
switch(nFocus)
{
case IDC_BUTTON_MANUAL:
OnButtonManual();
break;
case IDC_BUTTON_STOP:
OnButtonStop();
break;
case IDC_BUTTON_HELP:
OnButtonHelp();
break;
}
return TRUE;
case VK_F1: // Is key "F1".
OnButtonHelp();
break;
return TRUE;
}
}
if(pMsg->message == WM_WDT_OVERFLOW_MESSAGE) // Message from "WDTDll" when WDT overflow.
{
MessageBox("WDT overflow message!","Dll Message",MB_OK);
}
return CDialog::PreTranslateMessage(pMsg);
}
// Thread function,when thread create,this function will be called.
DWORD WINAPI SetEventThreadFunc( LPVOID lpParam )
{
CWDTSampleDlg * pDlg = (CWDTSampleDlg * )(lpParam);
while(1)
{
WaitForSingleObject(pDlg->m_hMyEvent,INFINITE);// waitting for MyEvent signaled.
if(pDlg->m_ThreadExit)
{
pDlg->m_ThreadExit = false;
return 0;
}
pDlg->MessageBox("WDT overflow event!","Dll event",MB_OK);
}
return 0;
}
void CWDTSampleDlg::OnButtonWdtCreate()
{
// TODO: Add your control notification handler code here
HANDLE hSetEventThread;
DWORD dwSetEventThreadId;
CString strTemp;
UpdateData(TRUE);
if(!m_Initialized)
{
return;
}
// Destroy created WDT at first
if(m_bWDTCreated)
{
WDTDestroy();
}
// Get WDT overflow event type
switch(m_nRadio_event)
{
case -1: // Doesnot select the event.
MessageBox("Please selecte WDT overflow event","Event Setting",MB_OK);
break;
case 0: // WDT work in hardware reset mode.
if(WDTCreate(WDT_MODE_RESET,0) != 0)
{
m_bWDTCreated = false;
MessageBox("Create WDT failed!");
return;
}
m_bWDTCreated = true;
break;
case 1: // WDT work in interrupt mode,and then shutdown system.
if(!CreateIntWDT())
{
m_bWDTCreated = false;
return;
}
else
{
WDTRegisterOverflowEvent(OVERFLOW_EVENT_SHUTDOWN,NULL,NULL,NULL);
m_bWDTCreated = true;
}
break;
case 2: // WDT work in interrupt mode,and then reboot system.
if(!CreateIntWDT())
{
m_bWDTCreated = false;
return;
}
else
{
WDTRegisterOverflowEvent(OVERFLOW_EVENT_REBOOT,NULL,NULL,NULL);
m_bWDTCreated = true;
}
break;
case 3: // WDT work in interrupt mode,and then set event.
if(!CreateIntWDT())
{
m_bWDTCreated = false;
return;
}
else
{
// Creat event,while WDT overflow,"WDTDll.dll" will be set event signaled.
m_hMyEvent = CreateEvent( NULL,
FALSE,
FALSE,
"MyEvent"
);
if(m_hMyEvent == NULL)
{
// Destroy WDT
WDTDestroy();
MessageBox("Create event error!","Set event",MB_OK);
break;
}
hSetEventThread = CreateThread(NULL, // default security attributes.
0, // use default stack size.
SetEventThreadFunc, // thread function.
this, // argument to thread function.
0, // use default creation flags.
&dwSetEventThreadId); // returns the thread identifier.
// Check the return value for success.
if (hSetEventThread == NULL)
{
// Destroy WDT
WDTDestroy();
MessageBox("Create waitting event thread failed!","Set event",MB_OK);
break;
}
else
{
CloseHandle( hSetEventThread );
}
WDTRegisterOverflowEvent(OVERFLOW_EVENT_SET_EVENT,m_hMyEvent,NULL,NULL);
m_bWDTCreated = true;
}
break;
case 4: // WDT work in interrupt mode,and then send message.
if(!CreateIntWDT())
{
m_bWDTCreated = false;
return;
}
else
{
WDTRegisterOverflowEvent(OVERFLOW_EVENT_SEND_MESSAGE,NULL,NULL,NULL);
m_bWDTCreated = true;
}
break;
case 5: // WDT work in interrupt mode,and then call user function.
if(!CreateIntWDT())
{
m_bWDTCreated = false;
return;
}
else
{
WDTRegisterOverflowEvent(OVERFLOW_EVENT_CALL_FUNC,NULL,MyFunc,NULL);
m_bWDTCreated = true;
}
break;
default:
break;
}
GetDlgItem(IDC_BUTTON_WDT_CREATE)->EnableWindow(false);
}
void CWDTSampleDlg::OnCheckAuto()
{
// TODO: Add your control notification handler code here
if(!m_Initialized)
{
return;
}
if(m_CheckAuto.GetCheck())
{
GetDlgItem(IDC_CHECK_AUTO)->SetWindowText("Stop Auto Start");
GetDlgItem(IDC_BUTTON_MANUAL)->EnableWindow(FALSE);
OnButtonManual();
if(m_bWDTStart)
{
if(m_unit==WDT_COUNTER_UNIT_SECOND)
{
if(m_time==1)
SetTimer(2,500,NULL);
else
SetTimer(2,(m_time-1)*500,NULL); // After (m_time1-1)second,start WDT.
}
else if(m_unit==WDT_COUNTER_UNIT_SECOND)
{
if(m_time==1)
SetTimer(2,10000,NULL);
else
SetTimer(2,(m_time-1)*60000,NULL); // After (m_time1-1)second,start WDT.
}
m_bTimer2 = TRUE;
}
}
else
{
WDTDisable();
GetDlgItem(IDC_CHECK_AUTO)->SetWindowText("Start WDT Auto");
GetDlgItem(IDC_BUTTON_MANUAL)->EnableWindow(TRUE);
if(m_bTimer2)
{
KillTimer(2);
m_bTimer2 = FALSE;
}
}
}
void CWDTSampleDlg::OnButtonManual()
{
// TODO: Add your control notification handler code here
CString strTemp;
UpdateData(TRUE);
if(!m_Initialized)
{
return;
}
// Get unit.
if(m_nRadio_unit == -1)
{
MessageBox("Please selecte time unit!","Time Setting",MB_OK);
return;
}
else if(m_nRadio_unit == 0)
{
m_unit = WDT_COUNTER_UNIT_MINUTE; // minutes
}
else if(m_nRadio_unit == 1)
{
m_unit = WDT_COUNTER_UNIT_SECOND; // seconds
}
// Get time.
if((m_nEdit_time <1) || (m_nEdit_time > 255))
{
MessageBox("Time value must between 1 to 255","Time Setting",MB_OK);
return;
}
else
{
m_time = m_nEdit_time;
}
int ret = WDTEnable(m_unit,m_time);
if(ret == -1)
{
MessageBox("Start WDT failed! Invalid parameters.");
return;
}
else if(ret == -2)
{
MessageBox("Start WDT failed! please create WDT at first.");
return;
}
// Set timer,call "OnTimer" once per second.
SetTimer(1,500,NULL);
m_bTimer1 = TRUE;
m_bWDTStart = TRUE;
}
void CWDTSampleDlg::OnButtonStop()
{
// TODO: Add your control notification handler code here
if(!m_Initialized)
{
return;
}
WDTDisable();
if(m_CheckAuto.GetCheck())
{
m_CheckAuto.SetCheck(0);
OnCheckAuto();
}
if(m_bTimer1)
{
KillTimer(1);
m_bTimer1 = FALSE;
}
if(m_bTimer2)
{
KillTimer(2);
m_bTimer2 = FALSE;
}
if(m_bTimer3)
{
KillTimer(3);
m_bTimer3 = FALSE;
}
GetDlgItem(IDC_STATICTEXT_VALUE)->SetWindowText("0");
m_bWDTStart = FALSE;
}
void _stdcall CWDTSampleDlg::MyFunc(void *pContext)
{
// Get CWDTSampleDlg class object pointer.
CWDTSampleApp * pApp=(CWDTSampleApp * )AfxGetApp();
CWDTSampleDlg* pDlg=(CWDTSampleDlg * )pApp->m_pMainWnd;
pDlg->MessageBox("Call register function!","MyFunc",MB_OK);
}
void CWDTSampleDlg::OnNotifyIcon(WPARAM wParam, LPARAM lParam)
{
// click system salver icon
//wParam icon message,lParam is Windows message
if ((wParam == IDR_MAINFRAME)&&(lParam == WM_LBUTTONDOWN))
{
ShowWindow(SW_SHOW);
ShowWindow(SW_SHOWDEFAULT);
}
}
void CWDTSampleDlg::OnButtonHelp()
{
// TODO: Add your control notification handler code here
CString appPath;
// Get "WDTSampleHelp.chm" path.
GetModuleFileName(NULL,appPath.GetBuffer(MAX_PATH),MAX_PATH);
appPath.ReleaseBuffer();
int n = appPath.ReverseFind('\\');
CString helpFile;
helpFile = appPath.Left(n);
//TCHAR c = (TCHAR)helpFile;
if(helpFile == '\\')
helpFile += "WDTHelp.chm";
else
helpFile += "\\WDTHelp.chm";
HtmlHelp(NULL,(LPCSTR)helpFile,HH_DISPLAY_TOPIC,0);
}
void CWDTSampleDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if((cx == 0) && (cy == 0))
{
ShowWindow(SW_HIDE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -