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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. /** Processed HID report descriptor items structure, containing information on each HID report element */
  33. static HID_ReportInfo_t HIDReportInfo;
  34. /** LUFA HID Class driver interface configuration and state information. This structure is
  35. * passed to all HID Class driver functions, so that multiple instances of the same class
  36. * within a device can be differentiated from one another.
  37. */
  38. USB_ClassInfo_HID_Host_t Mouse_HID_Interface =
  39. {
  40. .Config =
  41. {
  42. .DataINPipe =
  43. {
  44. .Address = (PIPE_DIR_IN | 1),
  45. .Banks = 1,
  46. },
  47. .DataOUTPipe =
  48. {
  49. .Address = (PIPE_DIR_OUT | 2),
  50. .Banks = 1,
  51. },
  52. .HIDInterfaceProtocol = HID_CSCP_NonBootProtocol,
  53. .HIDParserData = &HIDReportInfo
  54. },
  55. };
  56. /** Main program entry point. This routine configures the hardware required by the application, then
  57. * enters a loop to run the application tasks in sequence.
  58. */
  59. int main(void)
  60. {
  61. SetupHardware();
  62. puts_P(PSTR(ESC_FG_CYAN "Mouse Host Demo running.\r\n" ESC_FG_WHITE));
  63. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  64. GlobalInterruptEnable();
  65. for (;;)
  66. {
  67. MouseHost_Task();
  68. HID_Host_USBTask(&Mouse_HID_Interface);
  69. USB_USBTask();
  70. }
  71. }
  72. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  73. void SetupHardware(void)
  74. {
  75. #if (ARCH == ARCH_AVR8)
  76. /* Disable watchdog if enabled by bootloader/fuses */
  77. MCUSR &= ~(1 << WDRF);
  78. wdt_disable();
  79. /* Disable clock division */
  80. clock_prescale_set(clock_div_1);
  81. #endif
  82. /* Hardware Initialization */
  83. Serial_Init(9600, false);
  84. LEDs_Init();
  85. USB_Init();
  86. /* Create a stdio stream for the serial port for stdin and stdout */
  87. Serial_CreateStream(NULL);
  88. }
  89. /** Task to manage an enumerated USB mouse once connected, to display movement
  90. * data as it is received.
  91. */
  92. void MouseHost_Task(void)
  93. {
  94. if (USB_HostState != HOST_STATE_Configured)
  95. return;
  96. if (HID_Host_IsReportReceived(&Mouse_HID_Interface))
  97. {
  98. uint8_t MouseReport[Mouse_HID_Interface.State.LargestReportSize];
  99. HID_Host_ReceiveReport(&Mouse_HID_Interface, &MouseReport);
  100. uint8_t LEDMask = LEDS_NO_LEDS;
  101. for (uint8_t ReportNumber = 0; ReportNumber < HIDReportInfo.TotalReportItems; ReportNumber++)
  102. {
  103. HID_ReportItem_t* ReportItem = &HIDReportInfo.ReportItems[ReportNumber];
  104. /* Update the report item value if it is contained within the current report */
  105. if (!(USB_GetHIDReportItemInfo(MouseReport, ReportItem)))
  106. continue;
  107. /* Determine what report item is being tested, process updated value as needed */
  108. if ((ReportItem->Attributes.Usage.Page == USAGE_PAGE_BUTTON) &&
  109. (ReportItem->ItemType == HID_REPORT_ITEM_In))
  110. {
  111. if (ReportItem->Value)
  112. LEDMask = LEDS_ALL_LEDS;
  113. }
  114. else if ((ReportItem->Attributes.Usage.Page == USAGE_PAGE_GENERIC_DCTRL) &&
  115. (ReportItem->Attributes.Usage.Usage == USAGE_SCROLL_WHEEL) &&
  116. (ReportItem->ItemType == HID_REPORT_ITEM_In))
  117. {
  118. int16_t WheelDelta = HID_ALIGN_DATA(ReportItem, int16_t);
  119. if (WheelDelta)
  120. LEDMask = (LEDS_LED1 | LEDS_LED2 | ((WheelDelta > 0) ? LEDS_LED3 : LEDS_LED4));
  121. }
  122. else if ((ReportItem->Attributes.Usage.Page == USAGE_PAGE_GENERIC_DCTRL) &&
  123. ((ReportItem->Attributes.Usage.Usage == USAGE_X) ||
  124. (ReportItem->Attributes.Usage.Usage == USAGE_Y)) &&
  125. (ReportItem->ItemType == HID_REPORT_ITEM_In))
  126. {
  127. int16_t DeltaMovement = HID_ALIGN_DATA(ReportItem, int16_t);
  128. if (DeltaMovement)
  129. {
  130. if (ReportItem->Attributes.Usage.Usage == USAGE_X)
  131. LEDMask |= ((DeltaMovement > 0) ? LEDS_LED1 : LEDS_LED2);
  132. else
  133. LEDMask |= ((DeltaMovement > 0) ? LEDS_LED3 : LEDS_LED4);
  134. }
  135. }
  136. }
  137. LEDs_SetAllLEDs(LEDMask);
  138. }
  139. }
  140. /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
  141. * starts the library USB task to begin the enumeration and USB management process.
  142. */
  143. void EVENT_USB_Host_DeviceAttached(void)
  144. {
  145. puts_P(PSTR("Device Attached.\r\n"));
  146. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  147. }
  148. /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
  149. * stops the library USB task management process.
  150. */
  151. void EVENT_USB_Host_DeviceUnattached(void)
  152. {
  153. puts_P(PSTR("\r\nDevice Unattached.\r\n"));
  154. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  155. }
  156. /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
  157. * enumerated by the host and is now ready to be used by the application.
  158. */
  159. void EVENT_USB_Host_DeviceEnumerationComplete(void)
  160. {
  161. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  162. uint16_t ConfigDescriptorSize;
  163. uint8_t ConfigDescriptorData[512];
  164. if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
  165. sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
  166. {
  167. puts_P(PSTR("Error Retrieving Configuration Descriptor.\r\n"));
  168. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  169. return;
  170. }
  171. if (HID_Host_ConfigurePipes(&Mouse_HID_Interface,
  172. ConfigDescriptorSize, ConfigDescriptorData) != HID_ENUMERROR_NoError)
  173. {
  174. puts_P(PSTR("Attached Device Not a Valid Mouse.\r\n"));
  175. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  176. return;
  177. }
  178. if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
  179. {
  180. puts_P(PSTR("Error Setting Device Configuration.\r\n"));
  181. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  182. return;
  183. }
  184. if (HID_Host_SetReportProtocol(&Mouse_HID_Interface) != 0)
  185. {
  186. puts_P(PSTR("Error Setting Report Protocol Mode or Not a Valid Mouse.\r\n"));
  187. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  188. USB_Host_SetDeviceConfiguration(0);
  189. return;
  190. }
  191. puts_P(PSTR("Mouse Enumerated.\r\n"));
  192. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  193. }
  194. /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
  195. void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
  196. {
  197. USB_Disable();
  198. printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
  199. " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
  200. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  201. for(;;);
  202. }
  203. /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
  204. * enumerating an attached USB device.
  205. */
  206. void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
  207. const uint8_t SubErrorCode)
  208. {
  209. printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
  210. " -- Error Code %d\r\n"
  211. " -- Sub Error Code %d\r\n"
  212. " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
  213. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  214. }
  215. /** Callback for the HID Report Parser. This function is called each time the HID report parser is about to store
  216. * an IN, OUT or FEATURE item into the HIDReportInfo structure. To save on RAM, we are able to filter out items
  217. * we aren't interested in (preventing us from being able to extract them later on, but saving on the RAM they would
  218. * have occupied).
  219. *
  220. * \param[in] CurrentItem Pointer to the item the HID report parser is currently working with
  221. *
  222. * \return Boolean \c true if the item should be stored into the HID report structure, \c false if it should be discarded
  223. */
  224. bool CALLBACK_HIDParser_FilterHIDReportItem(HID_ReportItem_t* const CurrentItem)
  225. {
  226. bool IsMouse = false;
  227. /* Iterate through the item's collection path, until either the root collection node or a collection with the
  228. * Mouse Usage is found - this prevents Joysticks, which use identical descriptors except for the Joystick usage
  229. * parent node, from being erroneously treated as a mouse by the demo
  230. */
  231. for (HID_CollectionPath_t* CurrPath = CurrentItem->CollectionPath; CurrPath != NULL; CurrPath = CurrPath->Parent)
  232. {
  233. if ((CurrPath->Usage.Page == USAGE_PAGE_GENERIC_DCTRL) &&
  234. (CurrPath->Usage.Usage == USAGE_MOUSE))
  235. {
  236. IsMouse = true;
  237. break;
  238. }
  239. }
  240. /* If a collection with the mouse usage was not found, indicate that we are not interested in this item */
  241. if (!IsMouse)
  242. return false;
  243. /* Check the attributes of the current item - see if we are interested in it or not;
  244. * only store BUTTON and GENERIC_DESKTOP_CONTROL items into the Processed HID Report
  245. * structure to save RAM and ignore the rest
  246. */
  247. return ((CurrentItem->Attributes.Usage.Page == USAGE_PAGE_BUTTON) ||
  248. (CurrentItem->Attributes.Usage.Page == USAGE_PAGE_GENERIC_DCTRL));
  249. }