Advertisement
winston1777

Untitled

Oct 1st, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.56 KB | None | 0 0
  1. /**
  2. * |----------------------------------------------------------------------
  3. * | Copyright (C) Tilen Majerle, 2014
  4. * |
  5. * | This program is free software: you can redistribute it and/or modify
  6. * | it under the terms of the GNU General Public License as published by
  7. * | the Free Software Foundation, either version 3 of the License, or
  8. * | any later version.
  9. * |
  10. * | This program is distributed in the hope that it will be useful,
  11. * | but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * | GNU General Public License for more details.
  14. * |
  15. * | You should have received a copy of the GNU General Public License
  16. * | along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. * |----------------------------------------------------------------------
  18. */
  19. #include "tm_stm32f4_usb_hid_device.h"
  20.  
  21. extern USB_OTG_CORE_HANDLE USB_OTG_dev;
  22. extern TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_INT_Status;
  23.  
  24. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_Init(void) {
  25. /* Initialize HID device */
  26. USBD_Init(&USB_OTG_dev,
  27. #ifdef USE_USB_OTG_HS
  28. USB_OTG_HS_CORE_ID,
  29. #else
  30. USB_OTG_FS_CORE_ID,
  31. #endif
  32. &USR_desc,
  33. &USBD_HID_cb,
  34. &USR_cb);
  35.  
  36. /* Set not connected */
  37. TM_USB_HIDDEVICE_INT_Status = TM_USB_HIDDEVICE_Status_Disconnected;
  38.  
  39. /* Device not connected */
  40. return TM_USB_HIDDEVICE_INT_Status;
  41. }
  42.  
  43. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_GetStatus(void) {
  44. /* Return status */
  45. return TM_USB_HIDDEVICE_INT_Status;
  46. }
  47.  
  48. /* Mouse */
  49. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_MouseStructInit(TM_USB_HIDDEVICE_Mouse_t* Mouse_Data) {
  50. /* Set defaults */
  51. Mouse_Data->LeftButton = TM_USB_HIDDEVICE_Button_Released;
  52. Mouse_Data->RightButton = TM_USB_HIDDEVICE_Button_Released;
  53. Mouse_Data->MiddleButton = TM_USB_HIDDEVICE_Button_Released;
  54. Mouse_Data->XAxis = 0;
  55. Mouse_Data->YAxis = 0;
  56. Mouse_Data->Wheel = 0;
  57.  
  58. /* Return currect status */
  59. return TM_USB_HIDDEVICE_INT_Status;
  60. }
  61.  
  62. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_MouseSend(TM_USB_HIDDEVICE_Mouse_t* Mouse_Data) {
  63. uint8_t buff[5]; /* 5 bytes long report */
  64.  
  65. /* Check status */
  66. if (TM_USB_HIDDEVICE_INT_Status != TM_USB_HIDDEVICE_Status_Connected) {
  67. return TM_USB_HIDDEVICE_Status_Disconnected;
  68. }
  69.  
  70. /* Report ID */
  71. buff[0] = 0x02; /* Mouse */
  72.  
  73. /* Set buttons */
  74. buff[1] = 0;
  75. buff[1] |= Mouse_Data->LeftButton << 0; /* Bit 0 */
  76. buff[1] |= Mouse_Data->RightButton << 1; /* Bit 1 */
  77. buff[1] |= Mouse_Data->MiddleButton << 2; /* Bit 2 */
  78.  
  79. /* Set X and Y offset */
  80. buff[2] = Mouse_Data->XAxis;
  81. buff[3] = Mouse_Data->YAxis;
  82.  
  83. /* Set wheel */
  84. buff[4] = Mouse_Data->Wheel;
  85.  
  86. /* Send to USB */
  87. USBD_HID_SendReport(&USB_OTG_dev, buff, 5);
  88.  
  89. /* Return connected */
  90. return TM_USB_HIDDEVICE_Status_Connected;
  91. }
  92.  
  93. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_MouseReleaseAll(void) {
  94. uint8_t buff[5] = {0, 0, 0, 0, 0}; /* 4 bytes long report */
  95.  
  96. /* Check status */
  97. if (TM_USB_HIDDEVICE_INT_Status != TM_USB_HIDDEVICE_Status_Connected) {
  98. return TM_USB_HIDDEVICE_Status_Disconnected;
  99. }
  100.  
  101. /* Report ID */
  102. buff[0] = 0x02; /* Mouse */
  103.  
  104. /* Send to USB to release all buttons and axes */
  105. USBD_HID_SendReport(&USB_OTG_dev, buff, 5);
  106.  
  107. /* Return connected */
  108. return TM_USB_HIDDEVICE_Status_Connected;
  109. }
  110.  
  111. /* Gamepad */
  112. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_GamepadStructInit(TM_USB_HIDDEVICE_Gamepad_t* Gamepad_Data) {
  113. /* Set defaults */
  114. Gamepad_Data->Button1 = TM_USB_HIDDEVICE_Button_Released;
  115. Gamepad_Data->Button2 = TM_USB_HIDDEVICE_Button_Released;
  116. Gamepad_Data->Button3 = TM_USB_HIDDEVICE_Button_Released;
  117. Gamepad_Data->Button4 = TM_USB_HIDDEVICE_Button_Released;
  118. Gamepad_Data->Button5 = TM_USB_HIDDEVICE_Button_Released;
  119. Gamepad_Data->Button6 = TM_USB_HIDDEVICE_Button_Released;
  120. Gamepad_Data->Button7 = TM_USB_HIDDEVICE_Button_Released;
  121. Gamepad_Data->Button8 = TM_USB_HIDDEVICE_Button_Released;
  122. Gamepad_Data->Button9 = TM_USB_HIDDEVICE_Button_Released;
  123. Gamepad_Data->Button10 = TM_USB_HIDDEVICE_Button_Released;
  124. Gamepad_Data->Button11 = TM_USB_HIDDEVICE_Button_Released;
  125. Gamepad_Data->Button12 = TM_USB_HIDDEVICE_Button_Released;
  126. Gamepad_Data->Button13 = TM_USB_HIDDEVICE_Button_Released;
  127. Gamepad_Data->Button14 = TM_USB_HIDDEVICE_Button_Released;
  128. Gamepad_Data->Button15 = TM_USB_HIDDEVICE_Button_Released;
  129. Gamepad_Data->Button16 = TM_USB_HIDDEVICE_Button_Released;
  130. Gamepad_Data->LeftXAxis = 0;
  131. Gamepad_Data->LeftYAxis = 0;
  132. Gamepad_Data->RightXAxis = 0;
  133. Gamepad_Data->RightYAxis = 0;
  134.  
  135. /* Return currect status */
  136. return TM_USB_HIDDEVICE_INT_Status;
  137. }
  138.  
  139. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_GamepadSend(TM_USB_HIDDEVICE_Gamepad_Number_t gamepad_id, TM_USB_HIDDEVICE_Gamepad_t* Gamepad_Data) {
  140. uint8_t buff[7]; /* 7 bytes long report */
  141.  
  142. /* Check status */
  143. if (TM_USB_HIDDEVICE_INT_Status != TM_USB_HIDDEVICE_Status_Connected) {
  144. return TM_USB_HIDDEVICE_Status_Disconnected;
  145. }
  146.  
  147. /* Report ID */
  148. buff[0] = gamepad_id; /* gamepad id */
  149.  
  150. /* Buttons pressed, byte 1 */
  151. buff[1] = 0;
  152. buff[1] |= Gamepad_Data->Button1 << 0; /* Bit 0 */
  153. buff[1] |= Gamepad_Data->Button2 << 1; /* Bit 1 */
  154. buff[1] |= Gamepad_Data->Button3 << 2; /* Bit 2 */
  155. buff[1] |= Gamepad_Data->Button4 << 3; /* Bit 3 */
  156. buff[1] |= Gamepad_Data->Button5 << 4; /* Bit 4 */
  157. buff[1] |= Gamepad_Data->Button6 << 5; /* Bit 5 */
  158. buff[1] |= Gamepad_Data->Button7 << 6; /* Bit 6 */
  159. buff[1] |= Gamepad_Data->Button8 << 7; /* Bit 7 */
  160.  
  161. /* Buttons pressed, byte 2 */
  162. buff[2] = 0;
  163. buff[2] |= Gamepad_Data->Button9 << 0; /* Bit 0 */
  164. buff[2] |= Gamepad_Data->Button10 << 1; /* Bit 1 */
  165. buff[2] |= Gamepad_Data->Button11 << 2; /* Bit 2 */
  166. buff[2] |= Gamepad_Data->Button12 << 3; /* Bit 3 */
  167. buff[2] |= Gamepad_Data->Button13 << 4; /* Bit 4 */
  168. buff[2] |= Gamepad_Data->Button14 << 5; /* Bit 5 */
  169. buff[2] |= Gamepad_Data->Button15 << 6; /* Bit 6 */
  170. buff[2] |= Gamepad_Data->Button16 << 7; /* Bit 7 */
  171.  
  172. /* Left joystick */
  173. buff[3] = Gamepad_Data->LeftXAxis;
  174. buff[4] = Gamepad_Data->LeftYAxis;
  175.  
  176. /* Right joystick */
  177. buff[5] = Gamepad_Data->RightXAxis;
  178. buff[6] = Gamepad_Data->RightYAxis;
  179.  
  180. /* Send to USB gamepad data */
  181. USBD_HID_SendReport(&USB_OTG_dev, buff, 7);
  182.  
  183. /* Return connected */
  184. return TM_USB_HIDDEVICE_Status_Connected;
  185. }
  186.  
  187. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_GamepadReleaseAll(TM_USB_HIDDEVICE_Gamepad_Number_t gamepad_id) {
  188. uint8_t buff[7] = {0, 0, 0, 0, 0, 0, 0}; /* 7 bytes long report */
  189.  
  190. /* Check status */
  191. if (TM_USB_HIDDEVICE_INT_Status != TM_USB_HIDDEVICE_Status_Connected) {
  192. return TM_USB_HIDDEVICE_Status_Disconnected;
  193. }
  194.  
  195. /* Report ID */
  196. buff[0] = gamepad_id;
  197.  
  198. /* Send to USB gamepad release all buttons and joysticks */
  199. USBD_HID_SendReport(&USB_OTG_dev, buff, 7);
  200.  
  201. /* Return connected */
  202. return TM_USB_HIDDEVICE_Status_Connected;
  203. }
  204.  
  205. /* Keyboard */
  206. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_KeyboardStructInit(TM_USB_HIDDEVICE_Keyboard_t* Keyboard_Data) {
  207. /* Set defaults */
  208. Keyboard_Data->L_CTRL = TM_USB_HIDDEVICE_Button_Released;
  209. Keyboard_Data->L_ALT = TM_USB_HIDDEVICE_Button_Released;
  210. Keyboard_Data->L_SHIFT = TM_USB_HIDDEVICE_Button_Released;
  211. Keyboard_Data->L_GUI = TM_USB_HIDDEVICE_Button_Released;
  212. Keyboard_Data->R_CTRL = TM_USB_HIDDEVICE_Button_Released;
  213. Keyboard_Data->R_ALT = TM_USB_HIDDEVICE_Button_Released;
  214. Keyboard_Data->R_SHIFT = TM_USB_HIDDEVICE_Button_Released;
  215. Keyboard_Data->R_GUI = TM_USB_HIDDEVICE_Button_Released;
  216. Keyboard_Data->Key1 = 0;
  217. Keyboard_Data->Key2 = 0;
  218. Keyboard_Data->Key3 = 0;
  219. Keyboard_Data->Key4 = 0;
  220. Keyboard_Data->Key5 = 0;
  221. Keyboard_Data->Key6 = 0;
  222. Keyboard_Data->Key7 = 0;
  223. /* Return currect status */
  224. return TM_USB_HIDDEVICE_INT_Status;
  225. }
  226.  
  227. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_KeyboardSend(TM_USB_HIDDEVICE_Keyboard_t* Keyboard_Data) {
  228. uint8_t buff[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0,0};; /* 9 bytes long report */
  229.  
  230. /* Check status */
  231. if (TM_USB_HIDDEVICE_INT_Status != TM_USB_HIDDEVICE_Status_Connected) {
  232. return TM_USB_HIDDEVICE_Status_Disconnected;
  233. }
  234.  
  235. /* Report ID */
  236. buff[0] = 0x01; /* Keyboard */
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244. /* Control buttons */
  245. buff[1] = 0;
  246. buff[1] |= Keyboard_Data->L_CTRL << 0; /* Bit 0 */
  247. buff[1] |= Keyboard_Data->L_SHIFT << 1; /* Bit 1 */
  248. buff[1] |= Keyboard_Data->L_ALT << 2; /* Bit 2 */
  249. buff[1] |= Keyboard_Data->L_GUI << 3; /* Bit 3 */
  250. buff[1] |= Keyboard_Data->R_CTRL << 4; /* Bit 4 */
  251. buff[1] |= Keyboard_Data->R_SHIFT << 5; /* Bit 5 */
  252. buff[1] |= Keyboard_Data->R_ALT << 6; /* Bit 6 */
  253. buff[1] |= Keyboard_Data->R_GUI << 7; /* Bit 7 */
  254.  
  255. /* Padding */
  256. buff[2] = 0x00;
  257.  
  258. /* Keys */
  259. buff[3] = Keyboard_Data->Key1;
  260. buff[4] = Keyboard_Data->Key2;
  261. buff[5] = Keyboard_Data->Key3;
  262. buff[6] = Keyboard_Data->Key4;
  263. buff[7] = Keyboard_Data->Key5;
  264. buff[8] = Keyboard_Data->Key6;
  265. buff[9] = Keyboard_Data->Key7;
  266. /* Send to USB */
  267. USBD_HID_SendReport(&USB_OTG_dev, buff, 9);
  268.  
  269. /* Return connected */
  270. return TM_USB_HIDDEVICE_Status_Connected;
  271. }
  272.  
  273. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_KeyboardReleaseAll(void) {
  274. uint8_t buff[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0,0}; /* 9 bytes long report */
  275.  
  276. /* Check status */
  277. if (TM_USB_HIDDEVICE_INT_Status != TM_USB_HIDDEVICE_Status_Connected) {
  278. return TM_USB_HIDDEVICE_Status_Disconnected;
  279. }
  280.  
  281. /* Report ID */
  282. buff[0] = 0x01; /* Keyboard */
  283.  
  284. /* Send to USB */
  285. USBD_HID_SendReport(&USB_OTG_dev, buff, 10);
  286.  
  287. /* Return connected */
  288. return TM_USB_HIDDEVICE_Status_Connected;
  289. }
  290.  
  291.  
  292.  
  293. /* Consumer */
  294. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_ConsumerStructInit(TM_USB_HIDDEVICE_Consumer_t* Consumer_Data) {
  295. /* Set defaults */
  296. Consumer_Data->Mute = TM_USB_HIDDEVICE_Button_Released;
  297. Consumer_Data->VolumeUP = TM_USB_HIDDEVICE_Button_Released;
  298. Consumer_Data->VolumeDown = TM_USB_HIDDEVICE_Button_Released;
  299. // Consumer_Data->L_GUI = TM_USB_HIDDEVICE_Button_Released;
  300. // Consumer_Data->R_CTRL = TM_USB_HIDDEVICE_Button_Released;
  301. // Consumer_Data->R_ALT = TM_USB_HIDDEVICE_Button_Released;
  302. // Consumer_Data->R_SHIFT = TM_USB_HIDDEVICE_Button_Released;
  303. // Consumer_Data->R_GUI = TM_USB_HIDDEVICE_Button_Released;
  304. // Consumer_Data->Key1 = 0;
  305. // Consumer_Data->Key2 = 0;
  306. // Consumer_Data->Key3 = 0;
  307. // Consumer_Data->Key4 = 0;
  308. // Consumer_Data->Key5 = 0;
  309. // Consumer_Data->Key6 = 0;
  310. // Consumer_Data->Key7 = 0;
  311. /* Return currect status */
  312. return TM_USB_HIDDEVICE_INT_Status;
  313. }
  314.  
  315. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_ConsumerSend(TM_USB_HIDDEVICE_Consumer_t* Consumer_Data) {
  316. uint8_t buff[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0,0};; /* 9 bytes long report */
  317.  
  318. /* Check status */
  319. if (TM_USB_HIDDEVICE_INT_Status != TM_USB_HIDDEVICE_Status_Connected) {
  320. return TM_USB_HIDDEVICE_Status_Disconnected;
  321. }
  322.  
  323. /* Report ID */
  324. buff[0] = 0x05; /* Keyboard */
  325.  
  326.  
  327.  
  328. /* Control buttons */
  329. buff[1] = 0;
  330. buff[1] |= Consumer_Data->Mute << 0; /* Bit 0 */
  331. buff[1] |= Consumer_Data->VolumeUP << 1; /* Bit 1 */
  332. buff[1] |= Consumer_Data->VolumeDown << 2; /* Bit 2 */
  333. //buff[1] |= Keyboard_Data->L_GUI << 3; /* Bit 3 */
  334. // buff[1] |= Keyboard_Data->R_CTRL << 4; /* Bit 4 */
  335. // buff[1] |= Keyboard_Data->R_SHIFT << 5; /* Bit 5 */
  336. // buff[1] |= Keyboard_Data->R_ALT << 6; /* Bit 6 */
  337. // buff[1] |= Keyboard_Data->R_GUI << 7; /* Bit 7 */
  338.  
  339. /* Padding */
  340. buff[2] = 0x00;
  341.  
  342. /* Keys */
  343. // buff[3] = Keyboard_Data->Key1;
  344. // buff[4] = Keyboard_Data->Key2;
  345. // buff[5] = Keyboard_Data->Key3;
  346. // buff[6] = Keyboard_Data->Key4;
  347. // buff[7] = Keyboard_Data->Key5;
  348. // buff[8] = Keyboard_Data->Key6;
  349. // buff[9] = Keyboard_Data->Key7;
  350. /* Send to USB */
  351. USBD_HID_SendReport(&USB_OTG_dev, buff, 1);
  352.  
  353. /* Return connected */
  354. return TM_USB_HIDDEVICE_Status_Connected;
  355. }
  356.  
  357. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_ConsumerReleaseAll(void) {
  358. uint8_t buff[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0,0}; /* 9 bytes long report */
  359.  
  360. /* Check status */
  361. if (TM_USB_HIDDEVICE_INT_Status != TM_USB_HIDDEVICE_Status_Connected) {
  362. return TM_USB_HIDDEVICE_Status_Disconnected;
  363. }
  364.  
  365. /* Report ID */
  366. buff[0] = 0x05; /* Keyboard */
  367.  
  368. /* Send to USB */
  369. USBD_HID_SendReport(&USB_OTG_dev, buff, 10);
  370.  
  371. /* Return connected */
  372. return TM_USB_HIDDEVICE_Status_Connected;
  373. }
  374.  
  375.  
  376.  
  377.  
  378.  
  379. /* Custom report */
  380. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_SendCustom(uint8_t* buff, uint8_t count) {
  381. /* Check status */
  382. if (TM_USB_HIDDEVICE_INT_Status != TM_USB_HIDDEVICE_Status_Connected) {
  383. return TM_USB_HIDDEVICE_Status_Disconnected;
  384. }
  385.  
  386. /* Send to USB */
  387. USBD_HID_SendReport(&USB_OTG_dev, buff, count);
  388.  
  389. /* Return connected */
  390. return TM_USB_HIDDEVICE_Status_Connected;
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement