HostFunctions.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2018.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * Host Mode USB Mouse functionality for the MouseHostDevice demo. This file contains the Host mode
  29. * USB Mouse related code of the demo and is responsible for all the Host mode Mouse functionality.
  30. */
  31. #include "HostFunctions.h"
  32. /** LUFA HID Class driver interface configuration and state information. This structure is
  33. * passed to all HID Class driver functions, so that multiple instances of the same class
  34. * within a device can be differentiated from one another.
  35. */
  36. USB_ClassInfo_HID_Host_t Mouse_HID_Host_Interface =
  37. {
  38. .Config =
  39. {
  40. .DataINPipe =
  41. {
  42. .Address = (PIPE_DIR_IN | 1),
  43. .Banks = 1,
  44. },
  45. .DataINPipe =
  46. {
  47. .Address = (PIPE_DIR_OUT | 2),
  48. .Banks = 1,
  49. },
  50. .HIDInterfaceProtocol = HID_CSCP_MouseBootProtocol,
  51. },
  52. };
  53. /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
  54. * starts the library USB task to begin the enumeration and USB management process.
  55. */
  56. void EVENT_USB_Host_DeviceAttached(void)
  57. {
  58. puts_P(PSTR("Device Attached.\r\n"));
  59. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  60. }
  61. /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
  62. * stops the library USB task management process.
  63. */
  64. void EVENT_USB_Host_DeviceUnattached(void)
  65. {
  66. puts_P(PSTR("\r\nDevice Unattached.\r\n"));
  67. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  68. }
  69. /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
  70. * enumerated by the host and is now ready to be used by the application.
  71. */
  72. void EVENT_USB_Host_DeviceEnumerationComplete(void)
  73. {
  74. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  75. uint16_t ConfigDescriptorSize;
  76. uint8_t ConfigDescriptorData[512];
  77. if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
  78. sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
  79. {
  80. printf("Error Retrieving Configuration Descriptor.\r\n");
  81. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  82. return;
  83. }
  84. if (HID_Host_ConfigurePipes(&Mouse_HID_Host_Interface,
  85. ConfigDescriptorSize, ConfigDescriptorData) != HID_ENUMERROR_NoError)
  86. {
  87. printf("Attached Device Not a Valid Mouse.\r\n");
  88. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  89. return;
  90. }
  91. if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
  92. {
  93. printf("Error Setting Device Configuration.\r\n");
  94. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  95. return;
  96. }
  97. if (HID_Host_SetBootProtocol(&Mouse_HID_Host_Interface) != HOST_SENDCONTROL_Successful)
  98. {
  99. printf("Could not Set Boot Protocol Mode.\r\n");
  100. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  101. return;
  102. }
  103. printf("Mouse Enumerated.\r\n");
  104. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  105. }
  106. /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
  107. void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
  108. {
  109. USB_Disable();
  110. printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
  111. " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
  112. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  113. for(;;);
  114. }
  115. /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
  116. * enumerating an attached USB device.
  117. */
  118. void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
  119. const uint8_t SubErrorCode)
  120. {
  121. printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
  122. " -- Error Code %d\r\n"
  123. " -- Sub Error Code %d\r\n"
  124. " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
  125. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  126. }
  127. /** Host USB management task. This task handles the control of USB Mice while in USB Host mode,
  128. * setting up the appropriate data pipes and processing reports from the attached device.
  129. */
  130. void MouseHost_Task(void)
  131. {
  132. if (USB_HostState != HOST_STATE_Configured)
  133. return;
  134. if (HID_Host_IsReportReceived(&Mouse_HID_Host_Interface))
  135. {
  136. uint8_t LEDMask = LEDS_NO_LEDS;
  137. USB_MouseReport_Data_t MouseReport;
  138. HID_Host_ReceiveReport(&Mouse_HID_Host_Interface, &MouseReport);
  139. printf_P(PSTR("dX:%2d dY:%2d Button:%d\r\n"), MouseReport.X,
  140. MouseReport.Y,
  141. MouseReport.Button);
  142. if (MouseReport.X > 0)
  143. LEDMask |= LEDS_LED1;
  144. else if (MouseReport.X < 0)
  145. LEDMask |= LEDS_LED2;
  146. if (MouseReport.Y > 0)
  147. LEDMask |= LEDS_LED3;
  148. else if (MouseReport.Y < 0)
  149. LEDMask |= LEDS_LED4;
  150. if (MouseReport.Button)
  151. LEDMask = LEDS_ALL_LEDS;
  152. LEDs_SetAllLEDs(LEDMask);
  153. }
  154. }