Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MouseHost.c 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 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. * Main source file for the MouseHost demo. This file contains the main tasks of
  29. * the demo and is responsible for the initial application hardware configuration.
  30. */
  31. #include "MouseHost.h"
  32. /** Main program entry point. This routine configures the hardware required by the application, then
  33. * enters a loop to run the application tasks in sequence.
  34. */
  35. int main(void)
  36. {
  37. SetupHardware();
  38. puts_P(PSTR(ESC_FG_CYAN "Mouse HID Host Demo running.\r\n" ESC_FG_WHITE));
  39. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  40. GlobalInterruptEnable();
  41. for (;;)
  42. {
  43. MouseHost_Task();
  44. USB_USBTask();
  45. }
  46. }
  47. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  48. void SetupHardware(void)
  49. {
  50. #if (ARCH == ARCH_AVR8)
  51. /* Disable watchdog if enabled by bootloader/fuses */
  52. MCUSR &= ~(1 << WDRF);
  53. wdt_disable();
  54. /* Disable clock division */
  55. clock_prescale_set(clock_div_1);
  56. #endif
  57. /* Hardware Initialization */
  58. Serial_Init(9600, false);
  59. LEDs_Init();
  60. USB_Init();
  61. /* Create a stdio stream for the serial port for stdin and stdout */
  62. Serial_CreateStream(NULL);
  63. }
  64. /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
  65. * starts the library USB task to begin the enumeration and USB management process.
  66. */
  67. void EVENT_USB_Host_DeviceAttached(void)
  68. {
  69. puts_P(PSTR(ESC_FG_GREEN "Device Attached.\r\n" ESC_FG_WHITE));
  70. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  71. }
  72. /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
  73. * stops the library USB task management process.
  74. */
  75. void EVENT_USB_Host_DeviceUnattached(void)
  76. {
  77. puts_P(PSTR(ESC_FG_GREEN "Device Unattached.\r\n" ESC_FG_WHITE));
  78. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  79. }
  80. /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
  81. * enumerated by the host and is now ready to be used by the application.
  82. */
  83. void EVENT_USB_Host_DeviceEnumerationComplete(void)
  84. {
  85. puts_P(PSTR("Getting Config Data.\r\n"));
  86. uint8_t ErrorCode;
  87. /* Get and process the configuration descriptor data */
  88. if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
  89. {
  90. if (ErrorCode == ControlError)
  91. puts_P(PSTR(ESC_FG_RED "Control Error (Get Configuration).\r\n"));
  92. else
  93. puts_P(PSTR(ESC_FG_RED "Invalid Device.\r\n"));
  94. printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
  95. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  96. return;
  97. }
  98. /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
  99. if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)
  100. {
  101. printf_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n"
  102. " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
  103. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  104. return;
  105. }
  106. /* HID class request to set the mouse protocol to the Boot Protocol */
  107. USB_ControlRequest = (USB_Request_Header_t)
  108. {
  109. .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
  110. .bRequest = HID_REQ_SetProtocol,
  111. .wValue = 0,
  112. .wIndex = 0,
  113. .wLength = 0,
  114. };
  115. /* Select the control pipe for the request transfer */
  116. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  117. /* Send the request, display error and wait for device detach if request fails */
  118. if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
  119. {
  120. printf_P(PSTR(ESC_FG_RED "Control Error (Set Protocol).\r\n"
  121. " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
  122. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  123. USB_Host_SetDeviceConfiguration(0);
  124. return;
  125. }
  126. puts_P(PSTR("Mouse Enumerated.\r\n"));
  127. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  128. }
  129. /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
  130. void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
  131. {
  132. USB_Disable();
  133. printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
  134. " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
  135. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  136. for(;;);
  137. }
  138. /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
  139. * enumerating an attached USB device.
  140. */
  141. void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
  142. const uint8_t SubErrorCode)
  143. {
  144. printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
  145. " -- Error Code %d\r\n"
  146. " -- Sub Error Code %d\r\n"
  147. " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
  148. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  149. }
  150. /** Reads in and processes the next report from the attached device, displaying the report
  151. * contents on the board LEDs and via the serial port.
  152. */
  153. void MouseHost_Task(void)
  154. {
  155. if (USB_HostState != HOST_STATE_Configured)
  156. return;
  157. USB_MouseReport_Data_t MouseReport;
  158. uint8_t LEDMask = LEDS_NO_LEDS;
  159. /* Select mouse data pipe */
  160. Pipe_SelectPipe(MOUSE_DATA_IN_PIPE);
  161. /* Unfreeze mouse data pipe */
  162. Pipe_Unfreeze();
  163. /* Check to see if a packet has been received */
  164. if (!(Pipe_IsINReceived()))
  165. {
  166. /* No packet received (no movement), turn off LEDs */
  167. LEDs_SetAllLEDs(LEDS_NO_LEDS);
  168. /* Refreeze HID data IN pipe */
  169. Pipe_Freeze();
  170. return;
  171. }
  172. /* Ensure pipe contains data before trying to read from it */
  173. if (Pipe_IsReadWriteAllowed())
  174. {
  175. /* Read in mouse report data */
  176. Pipe_Read_Stream_LE(&MouseReport, sizeof(MouseReport), NULL);
  177. /* Alter status LEDs according to mouse X movement */
  178. if (MouseReport.X > 0)
  179. LEDMask |= LEDS_LED1;
  180. else if (MouseReport.X < 0)
  181. LEDMask |= LEDS_LED2;
  182. /* Alter status LEDs according to mouse Y movement */
  183. if (MouseReport.Y > 0)
  184. LEDMask |= LEDS_LED3;
  185. else if (MouseReport.Y < 0)
  186. LEDMask |= LEDS_LED4;
  187. /* Alter status LEDs according to mouse button position */
  188. if (MouseReport.Button)
  189. LEDMask = LEDS_ALL_LEDS;
  190. LEDs_SetAllLEDs(LEDMask);
  191. /* Print mouse report data through the serial port */
  192. printf_P(PSTR("dX:%2d dY:%2d Button:%d\r\n"), MouseReport.X,
  193. MouseReport.Y,
  194. MouseReport.Button);
  195. }
  196. /* Clear the IN endpoint, ready for next data packet */
  197. Pipe_ClearIN();
  198. /* Refreeze mouse data pipe */
  199. Pipe_Freeze();
  200. }