Library 34- STM32F4 as USB HID Device
With USB HID Device library, you can turn STM32F4 to be a keyboard, mouse or gamepad device. It also supports all three settings at the same time. Device is shown to computer as “Keyboard; Mouse; Game controller“. This library allows you to use 2 gamepads at the same time, one keyboard and one mouse.
For mouse you can use left, middle and right buttons, X and Y cursor axes and wheel vertical rotation.
For gamepads, you can implement 16 buttons and 2 joysticks, like on PS2 gamepad device.
Also, if you use keyboard, you have enabled all special keys (SHIFT, CTRL, ALT and GUI), in combination with other keys.
You can of cource make a device, which implement everything listed here in one piece. Your imagination is the limit.
Library
Features
- Interface STM32F4 with computer as keyboard, mouse or gamepads
- Supports up to 2 gamepads, 1 keyboard and 1 mouse
- Keyboard supports all special keys
- Mouse supports 3 main buttons, cursor movement and wheel vertical rotation
- Each of 2 gamepads supports up to 16 buttons and 2 joysticks
- Library works in USB FS or USB HS in FS mode
- Just plug USB in computer and works. No need for drivers
Dependencies
- CMSIS
- STM32F4xx
- STM32F4xx RCC
- STM32F4xx GPIO
- STM32F4xx EXTI
- misc.h
- TM
- defines.h
- defines.h
- USB HID Device stack provided by STMicroelectronics (included in library)
USB | FS MODE | HS IN FS MODE | DESCRIPTION |
---|---|---|---|
Data + | PA12 | PB15 | USB Data+ line |
Data – | PA11 | PB14 | USB Data- line |
ID | PA10 | PB12 | USB ID pin |
VBUS | PA9 | PB13 | USB activate |
By default, USB FS mode is used, also used on STM32F4-Discovery board. If you want to enable USB HS in FS mode for STM32F429 Discovery board, open project’s defines.h file and add lines below:
1 2 3 4 5 6 |
/* defines.h configuration file */ /* Uncomment if you want to enable USB HS in FS mode */ /* Used on STM32F429 Discovery board */ /* By default, USB FS mode is used */ //#define USE_USB_OTG_HS |
That’s all. You are now ready to work as USB HID device.
Clock was set down to 168MHz for STM32F429 because you can not get 48MHz for USB from 180MHz core clock.
Description
USB HID Device library is configured to support keyboard, mouse and 2 gamepads at the same time. You can send HID report for for mouse and keyboard one by one at the same time. Also, this library allows you to create a device to support 2 gamepads and create a console where you and your friend can play games with one STM32F4 device.
You have to know that when you send a report when some button is pressed, computer will detect button pressed. But after you release the button or everything, you have to also send HID report that you have released button. In one word:Â You have to send HID report every time each button is changed (pressed or released) or joysticks are moved.
Everywhere with joystick and movement (mouse, gamepad) where you have X and Y axis, you send each time a relative movement. So if you send one time cursor movement, and if you next time send the same value, cursor will be moved for twice length. You always sends relative movement according to position right now. There is no absolute movement for cursor.
Below, you have structure on how reports looks for USB HID report. Everywhere you have buttons, you have to set bit to 1 when button is pressed or 0 when button is released.
Also, keyboard buttons ARE NOT ascii values. To get proper HEX value for pressed key, you should look here.
- Keyboard
Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Byte 0 Report ID = 0x01 Byte 1 Right GUI Right ALT Right SHIFT Right CTRL Left GUI Left ALT Left SHIFT Left CTRL Byte 2 Padding = Always 0x00 BYTE 3 Key 1 BYTE 4 Key 2 BYTE 5 Key 3 BYTE 6 Key 4 BYTE 7 Key 5 BYTE 8 Key 6 - Mouse
Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Byte 0 Report ID = 0x02 Byte 1 Middle button Right button Left button Byte 2 Cursor movement X axis BYTE 3 Cursor movement YÂ axis BYTE 4 Wheel vertical movement - Gamepad 1
Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Byte 0 Report ID = 0x03 Byte 1 Button 8 Button 7 Button 6 Button 5 Button 4 Button 3 Button 2 Button 1 Byte 1 Button 16 Button 15 Button 14 Button 13 Button 12 Button 11 Button 10 Button 9 BYTE 3 Left joystick X axis BYTE 4 Left joystick Y axis BYTE 5 Right joystick X axis BYTE 6 Right joystick Y axis - Gamepad 2
Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Byte 0 Report ID = 0x04 Byte 1 Button 8 Button 7 Button 6 Button 5 Button 4 Button 3 Button 2 Button 1 Byte 1 Button 16 Button 15 Button 14 Button 13 Button 12 Button 11 Button 10 Button 9 BYTE 3 Left joystick X axis BYTE 4 Left joystick Y axis BYTE 5 Right joystick X axis BYTE 6 Right joystick Y axis
Functions and enumerations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
/** * Current USB HID status * * Parameters: * - TM_USB_HIDDEVICE_Status_LibraryNotInitialized * Library is not initialized yet * - TM_USB_HIDDEVICE_Status_Connected * Device is connected and ready to use * - TM_USB_HIDDEVICE_Status_Disconnected * Device is not connected * - TM_USB_HIDDEVICE_Status_IdleMode * Device is is IDLE mode * - TM_USB_HIDDEVICE_Status_SuspendMode * Device is in suspend mode */ typedef enum { TM_USB_HIDDEVICE_Status_LibraryNotInitialized, TM_USB_HIDDEVICE_Status_Connected, TM_USB_HIDDEVICE_Status_Disconnected, TM_USB_HIDDEVICE_Status_IdleMode, TM_USB_HIDDEVICE_Status_SuspendMode } TM_USB_HIDDEVICE_Status_t; /** * Button status typedef * * Parameters: * - TM_USB_HIDDEVICE_Button_Released * Button is not pressed * - TM_USB_HIDDEVICE_Button_Pressed * Button is pressed */ typedef enum { TM_USB_HIDDEVICE_Button_Released = 0, TM_USB_HIDDEVICE_Button_Pressed = 1 } TM_USB_HIDDEVICE_Button_t; /** * 2 Gamepads are supported simultaneously to work with * * Parameters: * - TM_USB_HIDDEVICE_Gamepad_Number_1 * Send data to computer for gamepad 1 * - TM_USB_HIDDEVICE_Gamepad_Number_2 * Send data to computer for gamepad 2 */ typedef enum { TM_USB_HIDDEVICE_Gamepad_Number_1 = 0x03, TM_USB_HIDDEVICE_Gamepad_Number_2 = 0x04 } TM_USB_HIDDEVICE_Gamepad_Number_t; /** * Mouse struct, to work with mouse * * Parameters: * - TM_USB_HIDDEVICE_Button_t LeftButton: * Detect if left button is pressed and set this to send command to computer * - TM_USB_HIDDEVICE_Button_t RightButton: * Detect if right button is pressed and set this to send command to computer * - TM_USB_HIDDEVICE_Button_t MiddleButton: * Detect if middle button is pressed and set this to send command to computer * - int8_t XAxis: * Cursor's X axis relative movement * - int8_t YAxis: * Cursor's Y axis relative movement * - int8_t Wheel: * Wheel movement */ typedef struct { TM_USB_HIDDEVICE_Button_t LeftButton; TM_USB_HIDDEVICE_Button_t RightButton; TM_USB_HIDDEVICE_Button_t MiddleButton; int8_t XAxis; int8_t YAxis; int8_t Wheel; } TM_USB_HIDDEVICE_Mouse_t; /** * Gamepad struct for 2 gamepads available * * Parameters: * - TM_USB_HIDDEVICE_Button_t Button[1:16]: * Set value, according to if button on your project is pressed * Gamepad supports 16 buttons * - int8_t LeftXAxis: * Left joystick X axis * - int8_t LeftYAxis: * Left joystick Y axis * - int8_t RightXAxis: * Right joystick Y axis * - int8_t RightYAxis: * Right joystick Y axis */ typedef struct { TM_USB_HIDDEVICE_Button_t Button1; TM_USB_HIDDEVICE_Button_t Button2; TM_USB_HIDDEVICE_Button_t Button3; TM_USB_HIDDEVICE_Button_t Button4; TM_USB_HIDDEVICE_Button_t Button5; TM_USB_HIDDEVICE_Button_t Button6; TM_USB_HIDDEVICE_Button_t Button7; TM_USB_HIDDEVICE_Button_t Button8; TM_USB_HIDDEVICE_Button_t Button9; TM_USB_HIDDEVICE_Button_t Button10; TM_USB_HIDDEVICE_Button_t Button11; TM_USB_HIDDEVICE_Button_t Button12; TM_USB_HIDDEVICE_Button_t Button13; TM_USB_HIDDEVICE_Button_t Button14; TM_USB_HIDDEVICE_Button_t Button15; TM_USB_HIDDEVICE_Button_t Button16; int8_t LeftXAxis; int8_t LeftYAxis; int8_t RightXAxis; int8_t RightYAxis; } TM_USB_HIDDEVICE_Gamepad_t; /** * Keyboard typedef * * Keyboard has special 8 buttons (CTRL, ALT, SHIFT, GUI (or WIN), all are left and right). * It also supports 6 others keys to be pressed at same time, let's say Key1 = 'a', Key2 = 'b', and you will get "ab" result on the screen. * If key is not used, then 0x00 value should be set * * Parameters: * - TM_USB_HIDDEVICE_Button_t L_CTRL: * Left CTRL button * - TM_USB_HIDDEVICE_Button_t L_ALT: * Left ALT button * - TM_USB_HIDDEVICE_Button_t L_SHIFT: * Left SHIFT button * - TM_USB_HIDDEVICE_Button_t L_GUI: * Left GUI (or Win) button * - TM_USB_HIDDEVICE_Button_t R_CTRL: * Right CTRL button * - TM_USB_HIDDEVICE_Button_t R_ALT: * Right ALT button * - TM_USB_HIDDEVICE_Button_t R_SHIFT: * Right SHIFT button * - TM_USB_HIDDEVICE_Button_t R_GUI: * Right GUI (or Win) button * - uint8_t Key[1:6]: * Key used with keyboard. * This can be whatever. Like numbers, letters, everything. * Also, 6 Key parameters you have. It means you can set 6 * different numbers, letters, whatever in one keyboard update. */ typedef struct { TM_USB_HIDDEVICE_Button_t L_CTRL; TM_USB_HIDDEVICE_Button_t L_ALT; TM_USB_HIDDEVICE_Button_t L_SHIFT; TM_USB_HIDDEVICE_Button_t L_GUI; TM_USB_HIDDEVICE_Button_t R_CTRL; TM_USB_HIDDEVICE_Button_t R_ALT; TM_USB_HIDDEVICE_Button_t R_SHIFT; TM_USB_HIDDEVICE_Button_t R_GUI; uint8_t Key1; uint8_t Key2; uint8_t Key3; uint8_t Key4; uint8_t Key5; uint8_t Key6; } TM_USB_HIDDEVICE_Keyboard_t; /** * Initialize USB HID Device library for work * * This library always returns TM_USB_HIDDEVICE_Status_Disconnected */ extern TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_Init(void); /** * Get USB status * * Returns TM_USB_HIDDEVICE_Status_Connected, if library is ready to use with USB, * otherwise one of members TM_USB_HIDDEVICE_Status_t is returned */ extern TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_GetStatus(void); /** * Initialize structure for mouse * * Set default values, before you start working * * Parameters: * - TM_USB_HIDDEVICE_Mouse_t* Mouse_Data * Pointer to mouse struct * * Returns member of TM_USB_HIDDEVICE_Status_t */ extern TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_MouseStructInit(TM_USB_HIDDEVICE_Mouse_t* Mouse_Data); /** * Sends mouse data over USB * * Parameters: * - Pointer to mouse struct for data to be send * * Returns member of TM_USB_HIDDEVICE_Status_t */ extern TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_MouseSend(TM_USB_HIDDEVICE_Mouse_t* Mouse_Data); /** * Sends command to release all mouse data in computer. * This will release all button in computer for mouse and cursor will stop * * Returns member of TM_USB_HIDDEVICE_Status_t */ extern TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_MouseReleaseAll(void); /** * Initialize structure for gamepad * * Set default values, before you start working with gamepads * * Parameters: * - TM_USB_HIDDEVICE_Gamepad_t* Gamepad_Data * Pointer to Gamepad struct * * Returns member of TM_USB_HIDDEVICE_Status_t */ extern TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_GamepadStructInit(TM_USB_HIDDEVICE_Gamepad_t* Gamepad_Data); /** * Sends Gamepad report over USB * * Parameters: * - TM_USB_HIDDEVICE_Gamepad_Number_t gamepad_id: * Gamepad number. 2 of them are supported * - TM_USB_HIDDEVICE_Gamepad_t* Gamepad_Data: * Pointer to Gamepad data to be send over USB * * Returns member of TM_USB_HIDDEVICE_Status_t */ extern TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_GamepadSend(TM_USB_HIDDEVICE_Gamepad_Number_t gamepad_id, TM_USB_HIDDEVICE_Gamepad_t* Gamepad_Data); /** * Release all buttons and joysticks over USB * * If you press a button, and don't release it, computer will recognize as long pressed button * * Parameters: * - TM_USB_HIDDEVICE_Gamepad_Number_t gamepad_id * Gamepad number. 2 of them are supported * * Returns member of TM_USB_HIDDEVICE_Status_t */ extern TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_GamepadReleaseAll(TM_USB_HIDDEVICE_Gamepad_Number_t gamepad_id); /** * Set default values to work with keyboard * * Parameters: * - TM_USB_HIDDEVICE_Keyboard_t* Keyboard_Data: * Pointer to keyboard struct * * Returns member of TM_USB_HIDDEVICE_Status_t */ extern TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_KeyboardStructInit(TM_USB_HIDDEVICE_Keyboard_t* Keyboard_Data); /** * Sends keyboard report over USB * * Parameters: * - TM_USB_HIDDEVICE_Keyboard_t* Keyboard_Data: * Pointer to keyboard struct to be sent over USB * * Returns member of TM_USB_HIDDEVICE_Status_t */ extern TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_KeyboardSend(TM_USB_HIDDEVICE_Keyboard_t* Keyboard_Data); /** * Release all buttons from keyboard * * If you press a button and don't release it, computer will detect like long pressed button * * Returns member of TM_USB_HIDDEVICE_Status_t */ extern TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_KeyboardReleaseAll(void); /** * Sends custom HID report over USB * * Parameters: * - uint8_t* buff: * Pointer for data to be sent * - uint8_t count: * number of bytes to be sent * * Returns member of TM_USB_HIDDEVICE_Status_t */ extern TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_SendCustom(uint8_t* buff, uint8_t count); |
Example
This is one simple example. If you press the blue button, then “WIN+R” command will be send to computer. In picture below, new device is found under Devices and Printers section.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
/** * Keil project for USB HID Device * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_usb_hid_device.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_disco.h" int main(void) { uint8_t already = 0; /* Set structs for all examples */ TM_USB_HIDDEVICE_Keyboard_t Keyboard; TM_USB_HIDDEVICE_Gamepad_t Gamepad1, Gamepad2; TM_USB_HIDDEVICE_Mouse_t Mouse; /* Initialize system */ SystemInit(); /* Initialize leds */ TM_DISCO_LedInit(); /* Initialize button */ TM_DISCO_ButtonInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize USB HID Device */ TM_USB_HIDDEVICE_Init(); /* Set default values for mouse struct */ TM_USB_HIDDEVICE_MouseStructInit(&Mouse); /* Set default values for keyboard struct */ TM_USB_HIDDEVICE_KeyboardStructInit(&Keyboard); /* Set default values for gamepad structs */ TM_USB_HIDDEVICE_GamepadStructInit(&Gamepad1); TM_USB_HIDDEVICE_GamepadStructInit(&Gamepad2); while (1) { /* If we are connected and drivers are OK */ if (TM_USB_HIDDEVICE_GetStatus() == TM_USB_HIDDEVICE_Status_Connected) { /* Turn on green LED */ TM_DISCO_LedOn(LED_GREEN); /* Simple sketch start */ /* If you pressed button right now and was not already pressed */ if (TM_DISCO_ButtonPressed() && already == 0) { /* Button on press */ already = 1; /* Set pressed keys = WIN + R */ Keyboard.L_GUI = TM_USB_HIDDEVICE_Button_Pressed; /* Win button */ Keyboard.Key1 = 0x15; /* R */ /* Result = "Run" command */ /* Send keyboard report */ TM_USB_HIDDEVICE_KeyboardSend(&Keyboard); } else if (!TM_DISCO_ButtonPressed() && already == 1) { /* Button on release */ already = 0; /* Release all buttons */ Keyboard.L_GUI = TM_USB_HIDDEVICE_Button_Released; /* No button */ Keyboard.Key1 = 0x00; /* No key */ /* Result = Released everything */ /* Send keyboard report */ TM_USB_HIDDEVICE_KeyboardSend(&Keyboard); } /* Simple sketch end */ } else { /* Turn off green LED */ TM_DISCO_LedOff(LED_GREEN); } } } |
Sketches
This sketches are made for copy/paste inside project, where you have comments “Simple sketch start” and “Simple sketch end“.
Everything you can test, if you open Control panel and the Devices and printers. If you have properly set values you will see a new device. If you use sketch for gamepad simulation, then make a right click on new device and hit “Game Controller settings“. You will get a new windows when you have 2 devices with same name. First is gamepad 1 and second is gamepad 2. Select one and click “Properties“. You will get a new window, where you can look, which buttons are pressed when you press the button on discovery board.
- This sketch is used in main example. If you press the button od Discovery board, it will send command “WIN + R” and when you release the button, it will send command that all buttons are released.
12345678910111213141516171819202122/* If you pressed button right now and was not already pressed */if (TM_DISCO_ButtonPressed() && already == 0) { /* Button on press */already = 1;/* Set pressed keys = WIN + R */Keyboard.L_GUI = TM_USB_HIDDEVICE_Button_Pressed; /* Win button */Keyboard.Key1 = 0x15; /* R *//* Result = "Run" command *//* Send keyboard report */TM_USB_HIDDEVICE_KeyboardSend(&Keyboard);} else if (!TM_DISCO_ButtonPressed() && already == 1) { /* Button on release */already = 0;/* Release all buttons */Keyboard.L_GUI = TM_USB_HIDDEVICE_Button_Released; /* No button */Keyboard.Key1 = 0x00; /* No key *//* Result = Released everything *//* Send keyboard report */TM_USB_HIDDEVICE_KeyboardSend(&Keyboard);} - If you press the button on discovery board, then you simulate left mouse key. If you hold button, and move your mouse around, you will see option “Select“.
123456789101112131415161718/* If you pressed button right now and was not already pressed */if (TM_DISCO_ButtonPressed() && already == 0) { /* Button on press */already = 1;/* Left button on mouse is pressed */Mouse.LeftButton = TM_USB_HIDDEVICE_Button_Pressed;/* Send mouse report */TM_USB_HIDDEVICE_MouseSend(&Mouse);} else if (!TM_DISCO_ButtonPressed() && already == 1) { /* Button on release */already = 0;/* Left button on mouse is released */Mouse.LeftButton = TM_USB_HIDDEVICE_Button_Released;/* Send mouse report */TM_USB_HIDDEVICE_MouseSend(&Mouse);} - Each time you press the button, you will write “Tilen” to the some file or somewhere else in your computer.
1234567891011121314151617181920212223242526272829303132333435/* If you pressed button right now and was not already pressed */if (TM_DISCO_ButtonPressed() && already == 0) { /* Button on press */already = 1;/* Send "Tilen" to computer *//* First we need upper "T" */Keyboard.L_SHIFT = TM_USB_HIDDEVICE_Button_Pressed; /* Left shift is pressed */Keyboard.Key1 = 0x17; /* 0x17 = T *//* Send keyboard report */TM_USB_HIDDEVICE_KeyboardSend(&Keyboard);/* After first letter, we can send all 4 other characters in one piece */Keyboard.L_SHIFT = TM_USB_HIDDEVICE_Button_Released; /* Left shift is released */Keyboard.Key1 = 0x0C; /* 0x0C = I *//* Send keyboard report */TM_USB_HIDDEVICE_KeyboardSend(&Keyboard);Keyboard.Key1 = 0x0F; /* 0x0F = L *//* Send keyboard report */TM_USB_HIDDEVICE_KeyboardSend(&Keyboard);Keyboard.Key1 = 0x08; /* 0x08 = E *//* Send keyboard report */TM_USB_HIDDEVICE_KeyboardSend(&Keyboard);Keyboard.Key1 = 0x11; /* 0x11 = N *//* Send keyboard report */TM_USB_HIDDEVICE_KeyboardSend(&Keyboard);} else if (!TM_DISCO_ButtonPressed() && already == 1) { /* Button on release */already = 0;/* Send keyboard report to release all buttons */TM_USB_HIDDEVICE_KeyboardReleaseAll();} - Next example simulates 2 gamepads. If you press the button, you activate buttons 3, 10 on gamepad 1 and left joystick rotation. Also, if you set window for gamepad 2, you will activate button 5 and 13 with left and right joysticks.
12345678910111213141516171819202122232425262728293031323334353637/* If you pressed button right now and was not already pressed */if (TM_DISCO_ButtonPressed() && already == 0) { /* Button on press */already = 1;/* Gamepad 1 *//* Simulate button 3 and button 10 on gamepad 1 */Gamepad1.Button3 = TM_USB_HIDDEVICE_Button_Pressed;Gamepad1.Button10 = TM_USB_HIDDEVICE_Button_Pressed;/* Simulate left stick rotation */Gamepad1.LeftXAxis = 10; /* X axis */Gamepad1.LeftYAxis = 30; /* Y axis *//* Send report for gamepad 1 */TM_USB_HIDDEVICE_GamepadSend(TM_USB_HIDDEVICE_Gamepad_Number_1, &Gamepad1);/* Gamepad 2 *//* Simulate button 5 and button 13 on gamepad 2 */Gamepad2.Button5 = TM_USB_HIDDEVICE_Button_Pressed;Gamepad2.Button13 = TM_USB_HIDDEVICE_Button_Pressed;/* Simulate left stick rotation */Gamepad2.LeftXAxis = -10; /* X axis */Gamepad2.LeftYAxis = -30; /* Y axis *//* Simulate right stick rotation */Gamepad2.RightXAxis = -8; /* X axis */Gamepad2.RightYAxis = 23; /* Y axis *//* Send report for gamepad 2 */TM_USB_HIDDEVICE_GamepadSend(TM_USB_HIDDEVICE_Gamepad_Number_2, &Gamepad2);} else if (!TM_DISCO_ButtonPressed() && already == 1) { /* Button on release */already = 0;/* Send command to release all buttons on both gamepads */TM_USB_HIDDEVICE_GamepadReleaseAll(TM_USB_HIDDEVICE_Gamepad_Number_1);TM_USB_HIDDEVICE_GamepadReleaseAll(TM_USB_HIDDEVICE_Gamepad_Number_2);}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061/* Example below works THE SAME as example above *//* If you pressed button right now and was not already pressed */if (TM_DISCO_ButtonPressed() && already == 0) { /* Button on press */already = 1;/* Gamepad 1 *//* Simulate button 3 and button 10 on gamepad 1 */Gamepad1.Button3 = TM_USB_HIDDEVICE_Button_Pressed;Gamepad1.Button10 = TM_USB_HIDDEVICE_Button_Pressed;/* Simulate left stick rotation */Gamepad1.LeftXAxis = 10; /* X axis */Gamepad1.LeftYAxis = 30; /* Y axis *//* Send report for gamepad 1 */TM_USB_HIDDEVICE_GamepadSend(TM_USB_HIDDEVICE_Gamepad_Number_1, &Gamepad1);/* Gamepad 2 *//* Simulate button 5 and button 13 on gamepad 2 */Gamepad2.Button5 = TM_USB_HIDDEVICE_Button_Pressed;Gamepad2.Button13 = TM_USB_HIDDEVICE_Button_Pressed;/* Simulate left stick rotation */Gamepad2.LeftXAxis = -10; /* X axis */Gamepad2.LeftYAxis = -30; /* Y axis *//* Simulate right stick rotation */Gamepad2.RightXAxis = -8; /* X axis */Gamepad2.RightYAxis = 23; /* Y axis *//* Send report for gamepad 2 */TM_USB_HIDDEVICE_GamepadSend(TM_USB_HIDDEVICE_Gamepad_Number_2, &Gamepad2);} else if (!TM_DISCO_ButtonPressed() && already == 1) { /* Button on release */already = 0;/* Gamepad 1 = released everything *//* Simulate button 3 and button 10 on gamepad 1 */Gamepad1.Button3 = TM_USB_HIDDEVICE_Button_Released;Gamepad1.Button10 = TM_USB_HIDDEVICE_Button_Released;/* Simulate left stick rotation */Gamepad1.LeftXAxis = 0; /* X axis */Gamepad1.LeftYAxis = 0; /* Y axis *//* Send report for gamepad 1 */TM_USB_HIDDEVICE_GamepadSend(TM_USB_HIDDEVICE_Gamepad_Number_1, &Gamepad1);/* Gamepad 2 *//* Simulate button 5 and button 13 on gamepad 2 */Gamepad2.Button5 = TM_USB_HIDDEVICE_Button_Released;Gamepad2.Button13 = TM_USB_HIDDEVICE_Button_Released;/* Simulate left stick rotation */Gamepad2.LeftXAxis = 0; /* X axis */Gamepad2.LeftYAxis = 0; /* Y axis *//* Simulate right stick rotation */Gamepad2.RightXAxis = 0; /* X axis */Gamepad2.RightYAxis = 0; /* Y axis *//* Send report for gamepad 2 */TM_USB_HIDDEVICE_GamepadSend(TM_USB_HIDDEVICE_Gamepad_Number_2, &Gamepad2);} - If you press the button, you will move file, or website, or everything used with mouse wheel for 10 units.
123456789101112131415161718/* If you pressed button right now and was not already pressed */if (TM_DISCO_ButtonPressed() && already == 0) { /* Button on press */already = 1;/* Rotate wheel on mouse */Mouse.Wheel = 10; /* Move 10 units = 10 * 3 lines on text editor in windows *//* Send mouse report */TM_USB_HIDDEVICE_MouseSend(&Mouse);} else if (!TM_DISCO_ButtonPressed() && already == 1) { /* Button on release */already = 0;/* Stop vertical wheel */Mouse.Wheel = 00; /* Reset *//* Send mouse report */TM_USB_HIDDEVICE_MouseSend(&Mouse);}
Project is available at my Github account, download library below.
Recent comments