1
0

DeviceFunctions.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. * Device Mode USB Mouse functionality for the MouseHostDevice demo. This file contains the Device mode
  29. * USB Mouse related code of the demo and is responsible for all the Device mode Mouse functionality.
  30. */
  31. #include "DeviceFunctions.h"
  32. /** Buffer to hold the previously generated Mouse Device HID report, for comparison purposes inside the HID class driver. */
  33. static uint8_t PrevMouseHIDReportBuffer[sizeof(USB_MouseReport_Data_t)];
  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_Device_t Mouse_HID_Device_Interface =
  39. {
  40. .Config =
  41. {
  42. .InterfaceNumber = INTERFACE_ID_Mouse,
  43. .ReportINEndpoint =
  44. {
  45. .Address = MOUSE_EPADDR,
  46. .Size = MOUSE_EPSIZE,
  47. .Banks = 1,
  48. },
  49. .PrevReportINBuffer = PrevMouseHIDReportBuffer,
  50. .PrevReportINBufferSize = sizeof(PrevMouseHIDReportBuffer),
  51. },
  52. };
  53. /** Event handler for the library USB WakeUp event. */
  54. void EVENT_USB_Device_Connect(void)
  55. {
  56. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  57. }
  58. /** Event handler for the library USB Suspend event. */
  59. void EVENT_USB_Device_Disconnect(void)
  60. {
  61. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  62. }
  63. /** Event handler for the library USB Configuration Changed event. */
  64. void EVENT_USB_Device_ConfigurationChanged(void)
  65. {
  66. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  67. if (!(HID_Device_ConfigureEndpoints(&Mouse_HID_Device_Interface)))
  68. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  69. USB_Device_EnableSOFEvents();
  70. }
  71. /** Event handler for the library USB Control Request reception event. */
  72. void EVENT_USB_Device_ControlRequest(void)
  73. {
  74. HID_Device_ProcessControlRequest(&Mouse_HID_Device_Interface);
  75. }
  76. /** Event handler for the USB device Start Of Frame event. */
  77. void EVENT_USB_Device_StartOfFrame(void)
  78. {
  79. HID_Device_MillisecondElapsed(&Mouse_HID_Device_Interface);
  80. }
  81. /** HID class driver callback function for the creation of HID reports to the host.
  82. *
  83. * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
  84. * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
  85. * \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
  86. * \param[out] ReportData Pointer to a buffer where the created report should be stored
  87. * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
  88. *
  89. * \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
  90. */
  91. bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
  92. uint8_t* const ReportID,
  93. const uint8_t ReportType,
  94. void* ReportData,
  95. uint16_t* const ReportSize)
  96. {
  97. USB_MouseReport_Data_t* MouseReport = (USB_MouseReport_Data_t*)ReportData;
  98. uint8_t JoyStatus_LCL = Joystick_GetStatus();
  99. uint8_t ButtonStatus_LCL = Buttons_GetStatus();
  100. if (JoyStatus_LCL & JOY_UP)
  101. MouseReport->Y = -1;
  102. else if (JoyStatus_LCL & JOY_DOWN)
  103. MouseReport->Y = 1;
  104. if (JoyStatus_LCL & JOY_RIGHT)
  105. MouseReport->X = 1;
  106. else if (JoyStatus_LCL & JOY_LEFT)
  107. MouseReport->X = -1;
  108. if (JoyStatus_LCL & JOY_PRESS)
  109. MouseReport->Button |= (1 << 0);
  110. if (ButtonStatus_LCL & BUTTONS_BUTTON1)
  111. MouseReport->Button |= (1 << 1);
  112. *ReportSize = sizeof(USB_MouseReport_Data_t);
  113. return true;
  114. }
  115. /** HID class driver callback function for the processing of HID reports from the host.
  116. *
  117. * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
  118. * \param[in] ReportID Report ID of the received report from the host
  119. * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
  120. * \param[in] ReportData Pointer to a buffer where the received report has been stored
  121. * \param[in] ReportSize Size in bytes of the received HID report
  122. */
  123. void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
  124. const uint8_t ReportID,
  125. const uint8_t ReportType,
  126. const void* ReportData,
  127. const uint16_t ReportSize)
  128. {
  129. // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
  130. }