]> git.donarmstrong.com Git - qmk_firmware.git/blob - lib/lufa/Projects/MediaController/MediaController.c
Merge commit '60b30c036397cb5627fa374bb930794b225daa29' as 'lib/lufa'
[qmk_firmware.git] / lib / lufa / Projects / MediaController / MediaController.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2017.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2017  Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12   Permission to use, copy, modify, distribute, and sell this
13   software and its documentation for any purpose is hereby granted
14   without fee, provided that the above copyright notice appear in
15   all copies and that both that the copyright notice and this
16   permission notice and warranty disclaimer appear in supporting
17   documentation, and that the name of the author not be used in
18   advertising or publicity pertaining to distribution of the
19   software without specific, written prior permission.
20
21   The author disclaims all warranties with regard to this
22   software, including all implied warranties of merchantability
23   and fitness.  In no event shall the author be liable for any
24   special, indirect or consequential damages or any damages
25   whatsoever resulting from loss of use, data or profits, whether
26   in an action of contract, negligence or other tortious action,
27   arising out of or in connection with the use or performance of
28   this software.
29 */
30
31 /** \file
32  *
33  *  Main source file for the MediaControl project. This file contains the main tasks of
34  *  the demo and is responsible for the initial application hardware configuration.
35  */
36
37 #include "MediaController.h"
38
39 /** Buffer to hold the previously generated Media Control HID report, for comparison purposes inside the HID class driver. */
40 static uint8_t PrevMediaControlHIDReportBuffer[sizeof(USB_MediaReport_Data_t)];
41
42 /** LUFA HID Class driver interface configuration and state information. This structure is
43  *  passed to all HID Class driver functions, so that multiple instances of the same class
44  *  within a device can be differentiated from one another.
45  */
46 USB_ClassInfo_HID_Device_t MediaControl_HID_Interface =
47         {
48                 .Config =
49                         {
50                                 .InterfaceNumber              = INTERFACE_ID_HID,
51                                 .ReportINEndpoint             =
52                                         {
53                                                 .Address              = MEDIACONTROL_HID_EPADDR,
54                                                 .Size                 = MEDIACONTROL_HID_EPSIZE,
55                                                 .Banks                = 1,
56                                         },
57                                 .PrevReportINBuffer           = PrevMediaControlHIDReportBuffer,
58                                 .PrevReportINBufferSize       = sizeof(PrevMediaControlHIDReportBuffer),
59                         },
60     };
61
62
63 /** Main program entry point. This routine contains the overall program flow, including initial
64  *  setup of all components and the main program loop.
65  */
66 int main(void)
67 {
68         SetupHardware();
69
70         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
71         GlobalInterruptEnable();
72
73         for (;;)
74         {
75                 HID_Device_USBTask(&MediaControl_HID_Interface);
76                 USB_USBTask();
77         }
78 }
79
80 /** Configures the board hardware and chip peripherals for the demo's functionality. */
81 void SetupHardware()
82 {
83 #if (ARCH == ARCH_AVR8)
84         /* Disable watchdog if enabled by bootloader/fuses */
85         MCUSR &= ~(1 << WDRF);
86         wdt_disable();
87
88         /* Disable clock division */
89         clock_prescale_set(clock_div_1);
90 #endif
91
92         /* Hardware Initialization */
93         Joystick_Init();
94         LEDs_Init();
95         Buttons_Init();
96         USB_Init();
97 }
98
99 /** Event handler for the library USB Connection event. */
100 void EVENT_USB_Device_Connect(void)
101 {
102         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
103 }
104
105 /** Event handler for the library USB Disconnection event. */
106 void EVENT_USB_Device_Disconnect(void)
107 {
108         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
109 }
110
111 /** Event handler for the library USB Configuration Changed event. */
112 void EVENT_USB_Device_ConfigurationChanged(void)
113 {
114         bool ConfigSuccess = true;
115
116         ConfigSuccess &= HID_Device_ConfigureEndpoints(&MediaControl_HID_Interface);
117
118         USB_Device_EnableSOFEvents();
119
120         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
121 }
122
123 /** Event handler for the library USB Control Request reception event. */
124 void EVENT_USB_Device_ControlRequest(void)
125 {
126         HID_Device_ProcessControlRequest(&MediaControl_HID_Interface);
127 }
128
129 /** Event handler for the USB device Start Of Frame event. */
130 void EVENT_USB_Device_StartOfFrame(void)
131 {
132         HID_Device_MillisecondElapsed(&MediaControl_HID_Interface);
133 }
134
135 /** HID class driver callback function for the creation of HID reports to the host.
136  *
137  *  \param[in]     HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced
138  *  \param[in,out] ReportID    Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
139  *  \param[in]     ReportType  Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
140  *  \param[out]    ReportData  Pointer to a buffer where the created report should be stored
141  *  \param[out]    ReportSize  Number of bytes written in the report (or zero if no report is to be sent)
142  *
143  *  \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
144  */
145 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
146                                          uint8_t* const ReportID,
147                                          const uint8_t ReportType,
148                                          void* ReportData,
149                                          uint16_t* const ReportSize)
150 {
151         USB_MediaReport_Data_t* MediaReport = (USB_MediaReport_Data_t*)ReportData;
152
153         uint8_t JoyStatus_LCL    = Joystick_GetStatus();
154         uint8_t ButtonStatus_LCL = Buttons_GetStatus();
155
156         /* Update the Media Control report with the user button presses */
157         MediaReport->Mute          = ((ButtonStatus_LCL & BUTTONS_BUTTON1) ? true : false);
158         MediaReport->PlayPause     = ((JoyStatus_LCL & JOY_PRESS) ? true : false);
159         MediaReport->VolumeUp      = ((JoyStatus_LCL & JOY_UP)    ? true : false);
160         MediaReport->VolumeDown    = ((JoyStatus_LCL & JOY_DOWN)  ? true : false);
161         MediaReport->PreviousTrack = ((JoyStatus_LCL & JOY_LEFT)  ? true : false);
162         MediaReport->NextTrack     = ((JoyStatus_LCL & JOY_RIGHT) ? true : false);
163
164         *ReportSize = sizeof(USB_MediaReport_Data_t);
165         return false;
166 }
167
168 /** HID class driver callback function for the processing of HID reports from the host.
169  *
170  *  \param[in] HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced
171  *  \param[in] ReportID    Report ID of the received report from the host
172  *  \param[in] ReportType  The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
173  *  \param[in] ReportData  Pointer to a buffer where the received report has been stored
174  *  \param[in] ReportSize  Size in bytes of the received HID report
175  */
176 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
177                                           const uint8_t ReportID,
178                                           const uint8_t ReportType,
179                                           const void* ReportData,
180                                           const uint16_t ReportSize)
181 {
182         // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
183 }
184