本帖最后由 muyichuan2012 于 2021-1-25 19:44 编辑
HID mouse sample code 1. 鼠标自动移动 2. 增加 remote wakeup
增加PA0控制是否要移动鼠标功能。 接上PC 之后鼠标会自动移动 PC进入休眠之后可以通过按键PA0 使能Remotewakeup功能
#include "usb_lib.h" #include "hw_config.h" #include "usb_pwr.h" #include "at32_board.h"
/** @addtogroup AT32F413_StdPeriph_Examples * @{ */
/** @addtogroup USB_Mouse * @{ */ void Mouse_send(uint8_t operation) { uint8_t Mouse_Buffer[4] = {0, 0, 0, 0}; int8_t X = 0, Y = 0,MouseButton=0;
switch (operation) { case LEFT: X -= CURSOR_STEP; break;
case RIGHT: X += CURSOR_STEP; break; case UP: Y -= CURSOR_STEP; break;
case DOWN: Y += CURSOR_STEP; break;
default: return; } Mouse_Buffer[0] = MouseButton; Mouse_Buffer[1] = X; Mouse_Buffer[2] = Y; Joystick_Send(Mouse_Buffer[0], Mouse_Buffer[1], Mouse_Buffer[2], Mouse_Buffer[3]); } /** * @param None * @retval None */ int main(void) { BUTTON_Type Button_Press; u8 keysta = 0, i; static uint8_t s_flag = 0; /*AT START F4xx board initialize * Include LED, Button */ AT32_Board_Init(); /*USB GPIO configure*/ AT32_USB_GPIO_init(); /*Enable USB Interrut*/ USB_Interrupts_Config(); /*Set USB Clock, USB Clock must 48MHz*/ Set_USBClock(); /*if use USB SRAM_Size = 768 Byte, default is 512 Byte*/ //Set_USB768ByteMode(); /* USB protocol and register initialize*/ USB_Init(); // while(!(GPIOA->IPTDT & 1)); while(1) { if ( AT32_BUTTON_Press() == BUTTON_WAKEUP ) { // Delay_ms(100); if ( s_flag ) s_flag = 0; else s_flag = 1; } if ( bDeviceState == CONFIGURED && s_flag) { for (i = 1; i <= 10; i++) { Mouse_send(LEFT); Delay_ms(20); Mouse_send(UP); Delay_ms(20); } for (i = 1; i <= 10; i++) {
Mouse_send(RIGHT); Delay_ms(20); Mouse_send(UP); Delay_ms(20); } for (i = 1; i <= 10; i++) { Mouse_send(RIGHT); Delay_ms(20); Mouse_send(DOWN); Delay_ms(20); } for (i = 1; i <= 10; i++) { Mouse_send(LEFT); Delay_ms(20); Mouse_send(DOWN); Delay_ms(20); } }else if ( bDeviceState == SUSPENDED) { if (AT32_BUTTON_Press() == BUTTON_WAKEUP && ((pInformation->Current_Feature & 0x20) != 0)) Resume(RESUME_INTERNAL); } #if 0 Button_Press = AT32_BUTTON_Press(); switch (Button_Press) {
case BUTTON_WAKEUP: Joystick_Send(2, 0, 0, 0); /*send mouse right button*/ keysta = 1; break; default: break; } if ( keysta != 0 ) { Delay_ms(30); keysta=0; Joystick_Send(0,0,0,0); /*release the button*/ } Delay_ms(40); AT32_LEDn_Toggle(LED4); #endif }
}
|