void EP1_IN_Callback(void)
{
? ? static uint8_t state = 0;
? ? uint8_t keyboard_report[8] = {0};? // 初始化鍵盤報(bào)告
? ? // 根據(jù)當(dāng)前狀態(tài)設(shè)置鍵盤報(bào)告中的按鍵碼
? ? switch (state)
? ? {
? ? ? ? // 字母鍵 A-Z
? ? ? ? case 0: keyboard_report[2] = KEY_A; break;
? ? ? ? case 1: keyboard_report[2] = KEY_B; break;
? ? ? ? case 2: keyboard_report[2] = KEY_C; break;
? ? ? ? case 3: keyboard_report[2] = KEY_D; break;
? ? ? ? case 4: keyboard_report[2] = KEY_E; break;
? ? ? ? case 5: keyboard_report[2] = KEY_F; break;
? ? ? ? case 6: keyboard_report[2] = KEY_G; break;
? ? ? ? case 7: keyboard_report[2] = KEY_H; break;
? ? ? ? case 8: keyboard_report[2] = KEY_I; break;
? ? ? ? case 9: keyboard_report[2] = KEY_J; break;
? ? ? ? case 10: keyboard_report[2] = KEY_K; break;
? ? ? ? case 11: keyboard_report[2] = KEY_L; break;
? ? ? ? case 12: keyboard_report[2] = KEY_M; break;
? ? ? ? case 13: keyboard_report[2] = KEY_N; break;
? ? ? ? case 14: keyboard_report[2] = KEY_O; break;
? ? ? ? case 15: keyboard_report[2] = KEY_P; break;
? ? ? ? case 16: keyboard_report[2] = KEY_Q; break;
? ? ? ? case 17: keyboard_report[2] = KEY_R; break;
? ? ? ? case 18: keyboard_report[2] = KEY_S; break;
? ? ? ? case 19: keyboard_report[2] = KEY_T; break;
? ? ? ? case 20: keyboard_report[2] = KEY_U; break;
? ? ? ? case 21: keyboard_report[2] = KEY_V; break;
? ? ? ? case 22: keyboard_report[2] = KEY_W; break;
? ? ? ? case 23: keyboard_report[2] = KEY_X; break;
? ? ? ? case 24: keyboard_report[2] = KEY_Y; break;
? ? ? ? case 25: keyboard_report[2] = KEY_Z; break;
? ? ? ? // 數(shù)字鍵 0-9
? ? ? ? case 26: keyboard_report[2] = KEY_1; break;
? ? ? ? case 27: keyboard_report[2] = KEY_2; break;
? ? ? ? case 28: keyboard_report[2] = KEY_3; break;
? ? ? ? case 29: keyboard_report[2] = KEY_4; break;
? ? ? ? case 30: keyboard_report[2] = KEY_5; break;
? ? ? ? case 31: keyboard_report[2] = KEY_6; break;
? ? ? ? case 32: keyboard_report[2] = KEY_7; break;
? ? ? ? case 33: keyboard_report[2] = KEY_8; break;
? ? ? ? case 34: keyboard_report[2] = KEY_9; break;
? ? ? ? case 35: keyboard_report[2] = KEY_0; break;
? ? ? ? // 功能鍵 F1-F12
? ? ? ? case 36: keyboard_report[2] = KEY_F1; break;
? ? ? ? case 37: keyboard_report[2] = KEY_F2; break;
? ? ? ? case 38: keyboard_report[2] = KEY_F3; break;
? ? ? ? case 39: keyboard_report[2] = KEY_F4; break;
? ? ? ? case 40: keyboard_report[2] = KEY_F5; break;
? ? ? ? case 41: keyboard_report[2] = KEY_F6; break;
? ? ? ? case 42: keyboard_report[2] = KEY_F7; break;
? ? ? ? case 43: keyboard_report[2] = KEY_F8; break;
? ? ? ? case 44: keyboard_report[2] = KEY_F9; break;
? ? ? ? case 45: keyboard_report[2] = KEY_F10; break;
? ? ? ? case 46: keyboard_report[2] = KEY_F11; break;
? ? ? ? case 47: keyboard_report[2] = KEY_F12; break;
? ? ? ? // 符號鍵和特殊功能鍵
? ? ? ? case 48: keyboard_report[2] = KEY_ENTER; break;
? ? ? ? case 49: keyboard_report[2] = KEY_ESC; break;
? ? ? ? case 50: keyboard_report[2] = KEY_BACKSPACE; break;
? ? ? ? case 51: keyboard_report[2] = KEY_TAB; break;
? ? ? ? case 52: keyboard_report[2] = KEY_SPACE; break;
? ? ? ? case 53: keyboard_report[2] = KEY_MINUS; break;
? ? ? ? case 54: keyboard_report[2] = KEY_EQUAL; break;
? ? ? ? case 55: keyboard_report[2] = KEY_LEFTBRACE; break;
? ? ? ? case 56: keyboard_report[2] = KEY_RIGHTBRACE; break;
? ? ? ? case 57: keyboard_report[2] = KEY_BACKSLASH; break;
? ? ? ? case 58: keyboard_report[2] = KEY_SEMICOLON; break;
? ? ? ? case 59: keyboard_report[2] = KEY_APOSTROPHE; break;
? ? ? ? case 60: keyboard_report[2] = KEY_GRAVE; break;
? ? ? ? case 61: keyboard_report[2] = KEY_COMMA; break;
? ? ? ? case 62: keyboard_report[2] = KEY_DOT; break;
? ? ? ? case 63: keyboard_report[2] = KEY_SLASH; break;
? ? ? ? case 64: keyboard_report[2] = KEY_CAPSLOCK; break;
? ? ? ? // 方向鍵
? ? ? ? case 65: keyboard_report[2] = KEY_RIGHT; break;
? ? ? ? case 66: keyboard_report[2] = KEY_LEFT; break;
? ? ? ? case 67: keyboard_report[2] = KEY_DOWN; break;
? ? ? ? case 68: keyboard_report[2] = KEY_UP; break;
? ? ? ? // 控制鍵
? ? ? ? case 69: keyboard_report[2] = KEY_LEFTCTRL; break;
? ? ? ? case 70: keyboard_report[2] = KEY_LEFTSHIFT; break;
? ? ? ? case 71: keyboard_report[2] = KEY_LEFTALT; break;
? ? ? ? case 72: keyboard_report[2] = KEY_LEFTMETA; break;
? ? ? ? case 73: keyboard_report[2] = KEY_RIGHTCTRL; break;
? ? ? ? case 74: keyboard_report[2] = KEY_RIGHTSHIFT; break;
? ? ? ? case 75: keyboard_report[2] = KEY_RIGHTALT; break;
? ? ? ? case 76: keyboard_report[2] = KEY_RIGHTMETA; break;
? ? ? ? // 音量和媒體控制鍵
? ? ? ? case 77: keyboard_report[2] = KEY_MEDIA_PLAYPAUSE; break;
? ? ? ? case 78: keyboard_report[2] = KEY_MEDIA_STOPCD; break;
? ? ? ? case 79: keyboard_report[2] = KEY_MEDIA_PREVIOUSSONG; break;
? ? ? ? case 80: keyboard_report[2] = KEY_MEDIA_NEXTSONG; break;
? ? ? ? case 81: keyboard_report[2] = KEY_MEDIA_VOLUMEUP; break;
? ? ? ? case 82: keyboard_report[2] = KEY_MEDIA_VOLUMEDOWN; break;
? ? ? ? case 83: keyboard_report[2] = KEY_MEDIA_MUTE; break;
? ? ? ? default: state = 0; break;? // 重置狀態(tài)
? ? }
? ? // 將鍵盤報(bào)告復(fù)制到 USB 緩沖區(qū)并設(shè)置數(shù)據(jù)包大小
? ? UserToPMABufferCopy(keyboard_report, GetEPTxAddr(ENDP1), 8);
? ? SetEPTxCount(ENDP1, 8);? // 設(shè)置傳輸?shù)臄?shù)據(jù)包大小
? ? SetEPTxValid(ENDP1);? ? ?// 使能端點(diǎn)傳輸
? ? state++;? // 切換到下一個(gè)鍵碼
? ? if (state > 83)? // 總共84個(gè)按鍵,循環(huán)回到初始狀態(tài)
? ? {
? ? ? ? state = 0;
? ? }
}
/*********************************************************************
?* @fn? ? ? EP2_IN_Callback
?*
?* @brief? ?Callback for Mouse IN endpoint (EP2)
?*
?* @return? none
?*/
void EP2_IN_Callback(void)
{
? ? static uint8_t state = 0;
? ? uint8_t mouse_report[3] = {0};? // 初始化鼠標(biāo)報(bào)告
? ? switch (state)
? ? {
? ? ? ? case 0:
? ? ? ? ? ? // 模擬鼠標(biāo)左鍵按下
? ? ? ? ? ? mouse_report[0] = 0x01;? // 左鍵按下
? ? ? ? ? ? break;
? ? ? ? case 1:
? ? ? ? ? ? // 模擬鼠標(biāo)右鍵按下
? ? ? ? ? ? mouse_report[0] = 0x02;? // 右鍵按下
? ? ? ? ? ? break;
? ? ? ? case 2:
? ? ? ? ? ? // 模擬鼠標(biāo)左鍵和右鍵同時(shí)按下
? ? ? ? ? ? mouse_report[0] = 0x03;? // 左鍵 + 右鍵按下
? ? ? ? ? ? break;
? ? ? ? case 3:
? ? ? ? ? ? // 模擬鼠標(biāo)向右移動 20 個(gè)單位
? ? ? ? ? ? mouse_report[0] = 0x00;? // 無按鍵
? ? ? ? ? ? mouse_report[1] = 20;? ? // X 軸移動 20
? ? ? ? ? ? mouse_report[2] = 0;? ? ?// Y 軸不移動
? ? ? ? ? ? break;
? ? ? ? case 4:
? ? ? ? ? ? // 模擬鼠標(biāo)向左移動 20 個(gè)單位
? ? ? ? ? ? mouse_report[0] = 0x00;? // 無按鍵
? ? ? ? ? ? mouse_report[1] = -20;? ?// X 軸移動 -20
? ? ? ? ? ? mouse_report[2] = 0;? ? ?// Y 軸不移動
? ? ? ? ? ? break;
? ? ? ? case 5:
? ? ? ? ? ? // 模擬鼠標(biāo)向上移動 20 個(gè)單位
? ? ? ? ? ? mouse_report[0] = 0x00;? // 無按鍵
? ? ? ? ? ? mouse_report[1] = 0;? ? ?// X 軸不移動
? ? ? ? ? ? mouse_report[2] = -20;? ?// Y 軸移動 -20
? ? ? ? ? ? break;
? ? ? ? case 6:
? ? ? ? ? ? // 模擬鼠標(biāo)向下移動 20 個(gè)單位
? ? ? ? ? ? mouse_report[0] = 0x00;? // 無按鍵
? ? ? ? ? ? mouse_report[1] = 0;? ? ?// X 軸不移動
? ? ? ? ? ? mouse_report[2] = 20;? ? // Y 軸移動 20
? ? ? ? ? ? break;
? ? ? ? default:
? ? ? ? ? ? state = 0;
? ? ? ? ? ? break;
? ? }
? ? // 將鼠標(biāo)報(bào)告復(fù)制到 USB 緩沖區(qū)
? ? UserToPMABufferCopy(mouse_report, GetEPTxAddr(ENDP2), 3);
? ? SetEPTxCount(ENDP2, 3);? // 設(shè)置傳輸?shù)臄?shù)據(jù)包大小
? ? SetEPTxValid(ENDP2);? ? ?// 使能端點(diǎn)傳輸
? ? state++;? // 切換到下一個(gè)鼠標(biāo)操作狀態(tài)
? ? if (state > 6)
? ? {
? ? ? ? state = 0;? // 循環(huán)回到初始狀態(tài)
? ? }
}
USB鍵鼠通訊的兩個(gè)Call? Back 設(shè)置是否正確 要想實(shí)現(xiàn)與上位機(jī)的通訊 用鍵鼠進(jìn)行操作 還需要哪些設(shè)置
可否提供一下與上位機(jī)通信的例程 謝謝