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.

MouseHostWithParser.c 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 MouseHostWithParser demo. This file contains the main tasks of
  29. * the demo and is responsible for the initial application hardware configuration.
  30. */
  31. #include "MouseHostWithParser.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 Parser 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. printf_P(PSTR("Processing HID Report (Size %d Bytes).\r\n"), HIDReportSize);
  107. /* Get and process the device's first HID report descriptor */
  108. if ((ErrorCode = GetHIDReportData()) != ParseSuccessful)
  109. {
  110. puts_P(PSTR(ESC_FG_RED "Report Parse Error.\r\n"));
  111. if (!(HIDReportInfo.TotalReportItems))
  112. puts_P(PSTR("Not a valid Mouse." ESC_FG_WHITE));
  113. else
  114. printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
  115. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  116. USB_Host_SetDeviceConfiguration(0);
  117. return;
  118. }
  119. puts_P(PSTR("Mouse Enumerated.\r\n"));
  120. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  121. }
  122. /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
  123. void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
  124. {
  125. USB_Disable();
  126. printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
  127. " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
  128. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  129. for(;;);
  130. }
  131. /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
  132. * enumerating an attached USB device.
  133. */
  134. void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
  135. const uint8_t SubErrorCode)
  136. {
  137. printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
  138. " -- Error Code %d\r\n"
  139. " -- Sub Error Code %d\r\n"
  140. " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
  141. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  142. }
  143. /** Task to read and process the HID report descriptor and HID reports from the device and display the
  144. * results onto the board LEDs.
  145. */
  146. void MouseHost_Task(void)
  147. {
  148. if (USB_HostState != HOST_STATE_Configured)
  149. return;
  150. /* Select and unfreeze mouse data pipe */
  151. Pipe_SelectPipe(MOUSE_DATA_IN_PIPE);
  152. Pipe_Unfreeze();
  153. /* Check to see if a packet has been received */
  154. if (Pipe_IsINReceived())
  155. {
  156. /* Check if data has been received from the attached mouse */
  157. if (Pipe_IsReadWriteAllowed())
  158. {
  159. /* Create buffer big enough for the report */
  160. uint8_t MouseReport[Pipe_BytesInPipe()];
  161. /* Load in the mouse report */
  162. Pipe_Read_Stream_LE(MouseReport, Pipe_BytesInPipe(), NULL);
  163. /* Process the read in mouse report from the device */
  164. ProcessMouseReport(MouseReport);
  165. }
  166. /* Clear the IN endpoint, ready for next data packet */
  167. Pipe_ClearIN();
  168. }
  169. /* Freeze mouse data pipe */
  170. Pipe_Freeze();
  171. }
  172. /** Processes a read HID report from an attached mouse, extracting out elements via the HID parser results
  173. * as required and displays movement and button presses on the board LEDs.
  174. *
  175. * \param[in] MouseReport Pointer to a HID report from an attached mouse device
  176. */
  177. void ProcessMouseReport(uint8_t* MouseReport)
  178. {
  179. uint8_t LEDMask = LEDS_NO_LEDS;
  180. /* Check each HID report item in turn, looking for mouse X/Y/button reports */
  181. for (uint8_t ReportNumber = 0; ReportNumber < HIDReportInfo.TotalReportItems; ReportNumber++)
  182. {
  183. /* Create a temporary item pointer to the next report item */
  184. HID_ReportItem_t* ReportItem = &HIDReportInfo.ReportItems[ReportNumber];
  185. bool FoundData;
  186. if ((ReportItem->Attributes.Usage.Page == USAGE_PAGE_BUTTON) &&
  187. (ReportItem->ItemType == HID_REPORT_ITEM_In))
  188. {
  189. /* Get the mouse button value */
  190. FoundData = USB_GetHIDReportItemInfo(MouseReport, ReportItem);
  191. /* For multi-report devices - if the requested data was not in the issued report, continue */
  192. if (!(FoundData))
  193. continue;
  194. /* If button is pressed, all LEDs are turned on */
  195. if (ReportItem->Value)
  196. LEDMask = LEDS_ALL_LEDS;
  197. }
  198. else if ((ReportItem->Attributes.Usage.Page == USAGE_PAGE_GENERIC_DCTRL) &&
  199. (ReportItem->Attributes.Usage.Usage == USAGE_SCROLL_WHEEL) &&
  200. (ReportItem->ItemType == HID_REPORT_ITEM_In))
  201. {
  202. /* Get the mouse wheel value if it is contained within the current
  203. * report, if not, skip to the next item in the parser list
  204. */
  205. if (!(USB_GetHIDReportItemInfo(MouseReport, ReportItem)))
  206. continue;
  207. int16_t WheelDelta = HID_ALIGN_DATA(ReportItem, int16_t);
  208. if (WheelDelta)
  209. LEDMask = (LEDS_LED1 | LEDS_LED2 | ((WheelDelta > 0) ? LEDS_LED3 : LEDS_LED4));
  210. }
  211. else if ((ReportItem->Attributes.Usage.Page == USAGE_PAGE_GENERIC_DCTRL) &&
  212. ((ReportItem->Attributes.Usage.Usage == USAGE_X) ||
  213. (ReportItem->Attributes.Usage.Usage == USAGE_Y)) &&
  214. (ReportItem->ItemType == HID_REPORT_ITEM_In))
  215. {
  216. /* Get the mouse relative position value */
  217. FoundData = USB_GetHIDReportItemInfo(MouseReport, ReportItem);
  218. /* For multi-report devices - if the requested data was not in the issued report, continue */
  219. if (!(FoundData))
  220. continue;
  221. int16_t DeltaMovement = HID_ALIGN_DATA(ReportItem, int16_t);
  222. /* Check to see if a (non-zero) delta movement has been indicated */
  223. if (DeltaMovement)
  224. {
  225. /* Determine if the report is for the X or Y delta movement, light LEDs as appropriate */
  226. if (ReportItem->Attributes.Usage.Usage == USAGE_X)
  227. LEDMask |= ((DeltaMovement > 0) ? LEDS_LED1 : LEDS_LED2);
  228. else
  229. LEDMask |= ((DeltaMovement > 0) ? LEDS_LED3 : LEDS_LED4);
  230. }
  231. }
  232. }
  233. /* Display the button information on the board LEDs */
  234. LEDs_SetAllLEDs(LEDMask);
  235. }