新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 如何編寫Windows CE.net的usb驅(qū)動(dòng)程序(2)

如何編寫Windows CE.net的usb驅(qū)動(dòng)程序(2)

作者: 時(shí)間:2012-05-02 來(lái)源:網(wǎng)絡(luò) 收藏

m_pInterface->lpEndpoints[0].Descriptor.bmAttributes));

returnFALSE;

}

DEBUGMSG(ZONE_INIT,(TEXT(USBMouse:EP0:MaxPacket:%u,Interval:%urn),

m_pInterface->lpEndpoints[0].Descriptor.wMaxPacketSize,

m_pInterface->lpEndpoints[0].Descriptor.bInterval));

m_hInterruptPipe=(*m_lpUsbFuncs->lpOpenPipe)(m_hDevice,

m_pInterface->lpEndpoints[0].Descriptor);

if(m_hInterruptPipe==NULL){

RETAILMSG(1,(TEXT(Mouse:Erroropeninginterruptpipern)));

return(FALSE);

}

m_hEvent=CreateEvent(NULL,FALSE,FALSE,NULL);

if(m_hEvent==NULL)

{

RETAILMSG(1,(TEXT(USBMouse:ErroronCreateEventforconnecteventrn)));

return(FALSE);

}

//創(chuàng)建數(shù)據(jù)接受線程

m_hThread=CreateThread(0,0,MouseThreadStub,this,0,NULL);

if(m_hThread==NULL)

{

RETAILMSG(1,(TEXT(USBMouse:ErroronCreateThreadrn)));

return(FALSE);

}

return(TRUE);

}

//從USB鼠標(biāo)設(shè)備中讀出數(shù)據(jù),產(chǎn)生相應(yīng)的鼠標(biāo)事件。

BOOLCMouse::SubmitInterrupt()

{

if(m_hInterruptTransfer)

(*m_lpUsbFuncs->lpCloseTransfer)(m_hInterruptTransfer);

//從USB鼠標(biāo)PIPE中讀數(shù)據(jù)

m_hInterruptTransfer=(*m_lpUsbFuncs->lpIssueInterruptTransfer)

(m_hInterruptPipe,MouseTransferCompleteStub,this,

USB_IN_TRANSFER|USB_SHORT_TRANSFER_OK,//表示讀數(shù)據(jù)

min(m_pInterface->lpEndpoints[0].Descriptor.wMaxPacketSize,

sizeof(m_pbDataBuffer)),

m_pbDataBuffer,

NULL);

if(m_hInterruptTransfer==NULL)

{

DEBUGMSG(ZONE_ERROR,(L!USBMouse:ErrorinIssueInterruptTransferrn));

returnFALSE;

}

else

{

DEBUGMSG(ZONE_TRANSFER,(LUSBMouse::SubmitInterrupt,Transfer:0x%Xrn,

m_hInterruptTransfer));

}

returnTRUE;

}

//處理鼠標(biāo)中斷傳輸?shù)臄?shù)據(jù)

BOOLCMouse::HandleInterrupt()

{

DWORDdwError;

DWORDdwBytes;

DWORDdwFlags=0;

INTdx=(signedchar)m_pbDataBuffer[1];

INTdy=(signedchar)m_pbDataBuffer[2];

BOOLfButton1=m_pbDataBuffer[0]0x01?TRUE:FALSE;

BOOLfButton2=m_pbDataBuffer[0]0x02?TRUE:FALSE;

BOOLfButton3=m_pbDataBuffer[0]0x04?TRUE:FALSE;

if(!(*m_lpUsbFuncs->lpGetTransferStatus)(m_hInterruptTransfer,dwBytes,dwError))

{

DEBUGMSG(ZONE_ERROR,(TEXT(!USBMouse:ErrorinGetTransferStatus(0x%X)rn),

m_hInterruptTransfer));

returnFALSE;

}

else

{

DEBUGMSG(ZONE_TRANSFER,(TEXT(USBMouse::HandleInterrupt,hTransfer0x%Xcomplete(%ubytes,Error:%X)rn),

m_hInterruptTransfer,dwBytes,dwError));

}

if(!SubmitInterrupt())

returnFALSE;

if(dwError!=USB_NO_ERROR)

{

DEBUGMSG(ZONE_ERROR,(TEXT(!USBMouse:Error0x%Xininterrupttransferrn),dwError));

returnTRUE;

}

if(dwBytes3)

{

DEBUGMSG(ZONE_ERROR,(TEXT(!USBMouse:Invalidbytecnt%ufrominterrupttransferrn),dwBytes));

returnTRUE;

}

if(dx||dy)

dwFlags|=MOUSEEVENTF_MOVE;

if(fButton1!=m_fPrevButton1)

{

if(fButton1)

dwFlags|=MOUSEEVENTF_LEFTDOWN;

else

dwFlags|=MOUSEEVENTF_LEFTUP;

}

if(fButton2!=m_fPrevButton2)

{

if(fButton2)

dwFlags|=MOUSEEVENTF_RIGHTDOWN;

else

dwFlags|=MOUSEEVENTF_RIGHTUP;

}

if(fButton3!=m_fPrevButton3)

{

if(fButton3)

dwFlags|=MOUSEEVENTF_MIDDLEDOWN;

else

dwFlags|=MOUSEEVENTF_MIDDLEUP;

}

m_fPrevButton1=fButton1;

m_fPrevButton2=fButton2;

m_fPrevButton3=fButton3;

DEBUGMSG(ZONE_EVENTS,

(TEXT(USBMouseevent:dx:%d,dy:%d,dwFlags:0x%X(B1:%u,B2:%u,B3:%u)rn),

dx,dy,dwFlags,fButton1,fButton2,fButton3));

//通知系統(tǒng)產(chǎn)生鼠標(biāo)事件

if(m_fReadyForMouseEvents)

mouse_event(dwFlags,dx,dy,0,0);

else

m_fReadyForMouseEvents=IsAPIReady(SH_WMGR);

returnTRUE;

}

DWORDCALLBACKCMouse::MouseTransferCompleteStub(LPVOIDlpvNotifyParameter)

{

CMouse*pMouse=(CMouse*)lpvNotifyParameter;

return(pMouse->MouseTransferComplete());

}

//數(shù)據(jù)傳輸完畢回調(diào)函數(shù)

DWORDCMouse::MouseTransferComplete()

{

if(m_hEvent)

SetEvent(m_hEvent);

return0;

}

ULONGCALLBACKCMouse::MouseThreadStub(PVOIDcontext)

{

CMouse*pMouse=(CMouse*)context;

return(pMouse->MouseThread());

}

//USB鼠標(biāo)線程

DWORDCMouse::MouseThread()

{

DEBUGMSG(ZONE_INIT,(TEXT(USBMouse:Workerthreadstartedrn)));

SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_HIGHEST);



評(píng)論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉