MediaController.c 6.8 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. * Main source file for the MediaControl project. This file contains the main tasks of
  29. * the demo and is responsible for the initial application hardware configuration.
  30. */
  31. #include "MediaController.h"
  32. /** Buffer to hold the previously generated Media Control HID report, for comparison purposes inside the HID class driver. */
  33. static uint8_t PrevMediaControlHIDReportBuffer[sizeof(USB_MediaReport_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 MediaControl_HID_Interface =
  39. {
  40. .Config =
  41. {
  42. .InterfaceNumber = INTERFACE_ID_HID,
  43. .ReportINEndpoint =
  44. {
  45. .Address = MEDIACONTROL_HID_EPADDR,
  46. .Size = MEDIACONTROL_HID_EPSIZE,
  47. .Banks = 1,
  48. },
  49. .PrevReportINBuffer = PrevMediaControlHIDReportBuffer,
  50. .PrevReportINBufferSize = sizeof(PrevMediaControlHIDReportBuffer),
  51. },
  52. };
  53. /** Main program entry point. This routine contains the overall program flow, including initial
  54. * setup of all components and the main program loop.
  55. */
  56. int main(void)
  57. {
  58. SetupHardware();
  59. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  60. GlobalInterruptEnable();
  61. for (;;)
  62. {
  63. HID_Device_USBTask(&MediaControl_HID_Interface);
  64. USB_USBTask();
  65. }
  66. }
  67. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  68. void SetupHardware()
  69. {
  70. #if (ARCH == ARCH_AVR8)
  71. /* Disable watchdog if enabled by bootloader/fuses */
  72. MCUSR &= ~(1 << WDRF);
  73. wdt_disable();
  74. /* Disable clock division */
  75. clock_prescale_set(clock_div_1);
  76. #endif
  77. /* Hardware Initialization */
  78. Joystick_Init();
  79. LEDs_Init();
  80. Buttons_Init();
  81. USB_Init();
  82. }
  83. /** Event handler for the library USB Connection event. */
  84. void EVENT_USB_Device_Connect(void)
  85. {
  86. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  87. }
  88. /** Event handler for the library USB Disconnection event. */
  89. void EVENT_USB_Device_Disconnect(void)
  90. {
  91. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  92. }
  93. /** Event handler for the library USB Configuration Changed event. */
  94. void EVENT_USB_Device_ConfigurationChanged(void)
  95. {
  96. bool ConfigSuccess = true;
  97. ConfigSuccess &= HID_Device_ConfigureEndpoints(&MediaControl_HID_Interface);
  98. USB_Device_EnableSOFEvents();
  99. LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
  100. }
  101. /** Event handler for the library USB Control Request reception event. */
  102. void EVENT_USB_Device_ControlRequest(void)
  103. {
  104. HID_Device_ProcessControlRequest(&MediaControl_HID_Interface);
  105. }
  106. /** Event handler for the USB device Start Of Frame event. */
  107. void EVENT_USB_Device_StartOfFrame(void)
  108. {
  109. HID_Device_MillisecondElapsed(&MediaControl_HID_Interface);
  110. }
  111. /** HID class driver callback function for the creation of HID reports to the host.
  112. *
  113. * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
  114. * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
  115. * \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
  116. * \param[out] ReportData Pointer to a buffer where the created report should be stored
  117. * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
  118. *
  119. * \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
  120. */
  121. bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
  122. uint8_t* const ReportID,
  123. const uint8_t ReportType,
  124. void* ReportData,
  125. uint16_t* const ReportSize)
  126. {
  127. USB_MediaReport_Data_t* MediaReport = (USB_MediaReport_Data_t*)ReportData;
  128. uint8_t JoyStatus_LCL = Joystick_GetStatus();
  129. uint8_t ButtonStatus_LCL = Buttons_GetStatus();
  130. /* Update the Media Control report with the user button presses */
  131. MediaReport->Mute = ((ButtonStatus_LCL & BUTTONS_BUTTON1) ? true : false);
  132. MediaReport->PlayPause = ((JoyStatus_LCL & JOY_PRESS) ? true : false);
  133. MediaReport->VolumeUp = ((JoyStatus_LCL & JOY_UP) ? true : false);
  134. MediaReport->VolumeDown = ((JoyStatus_LCL & JOY_DOWN) ? true : false);
  135. MediaReport->PreviousTrack = ((JoyStatus_LCL & JOY_LEFT) ? true : false);
  136. MediaReport->NextTrack = ((JoyStatus_LCL & JOY_RIGHT) ? true : false);
  137. *ReportSize = sizeof(USB_MediaReport_Data_t);
  138. return false;
  139. }
  140. /** HID class driver callback function for the processing of HID reports from the host.
  141. *
  142. * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
  143. * \param[in] ReportID Report ID of the received report from the host
  144. * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
  145. * \param[in] ReportData Pointer to a buffer where the received report has been stored
  146. * \param[in] ReportSize Size in bytes of the received HID report
  147. */
  148. void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
  149. const uint8_t ReportID,
  150. const uint8_t ReportType,
  151. const void* ReportData,
  152. const uint16_t ReportSize)
  153. {
  154. // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
  155. }