]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-git/Demos/Device/ClassDriver/KeyboardMouseMultiReport/KeyboardMouseMultiReport.c
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[qmk_firmware.git] / protocol / lufa / LUFA-git / Demos / Device / ClassDriver / KeyboardMouseMultiReport / KeyboardMouseMultiReport.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2014.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2014  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 KeyboardMouseMultiReport demo. This file contains the main tasks of
34  *  the demo and is responsible for the initial application hardware configuration.
35  */
36
37 #include "KeyboardMouseMultiReport.h"
38
39 /** Buffer to hold the previously generated HID report, for comparison purposes inside the HID class driver. */
40 static uint8_t PrevHIDReportBuffer[MAX(sizeof(USB_KeyboardReport_Data_t), sizeof(USB_MouseReport_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 Device_HID_Interface =
47         {
48                 .Config =
49                         {
50                                 .InterfaceNumber              = INTERFACE_ID_KeyboardAndMouse,
51                                 .ReportINEndpoint             =
52                                         {
53                                                 .Address              = HID_IN_EPADDR,
54                                                 .Size                 = HID_EPSIZE,
55                                                 .Banks                = 1,
56                                         },
57                                 .PrevReportINBuffer           = PrevHIDReportBuffer,
58                                 .PrevReportINBufferSize       = sizeof(PrevHIDReportBuffer),
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(&Device_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 #elif (ARCH == ARCH_XMEGA)
91         /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
92         XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
93         XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
94
95         /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
96         XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
97         XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
98
99         PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
100 #endif
101
102         /* Hardware Initialization */
103         Joystick_Init();
104         LEDs_Init();
105         USB_Init();
106 }
107
108 /** Event handler for the library USB Connection event. */
109 void EVENT_USB_Device_Connect(void)
110 {
111         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
112 }
113
114 /** Event handler for the library USB Disconnection event. */
115 void EVENT_USB_Device_Disconnect(void)
116 {
117         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
118 }
119
120 /** Event handler for the library USB Configuration Changed event. */
121 void EVENT_USB_Device_ConfigurationChanged(void)
122 {
123         bool ConfigSuccess = true;
124
125         ConfigSuccess &= HID_Device_ConfigureEndpoints(&Device_HID_Interface);
126
127         USB_Device_EnableSOFEvents();
128
129         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
130 }
131
132 /** Event handler for the library USB Control Request reception event. */
133 void EVENT_USB_Device_ControlRequest(void)
134 {
135         HID_Device_ProcessControlRequest(&Device_HID_Interface);
136 }
137
138 /** Event handler for the USB device Start Of Frame event. */
139 void EVENT_USB_Device_StartOfFrame(void)
140 {
141         HID_Device_MillisecondElapsed(&Device_HID_Interface);
142 }
143
144 /** HID class driver callback function for the creation of HID reports to the host.
145  *
146  *  \param[in]     HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced
147  *  \param[in,out] ReportID    Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
148  *  \param[in]     ReportType  Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
149  *  \param[out]    ReportData  Pointer to a buffer where the created report should be stored
150  *  \param[out]    ReportSize  Number of bytes written in the report (or zero if no report is to be sent)
151  *
152  *  \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
153  */
154 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
155                                          uint8_t* const ReportID,
156                                          const uint8_t ReportType,
157                                          void* ReportData,
158                                          uint16_t* const ReportSize)
159 {
160         uint8_t JoyStatus_LCL    = Joystick_GetStatus();
161         uint8_t ButtonStatus_LCL = Buttons_GetStatus();
162
163         if (!(ButtonStatus_LCL & BUTTONS_BUTTON1))
164         {
165                 USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;
166
167                 KeyboardReport->Modifier = HID_KEYBOARD_MODIFIER_LEFTSHIFT;
168
169                 if (JoyStatus_LCL & JOY_UP)
170                   KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_A;
171                 else if (JoyStatus_LCL & JOY_DOWN)
172                   KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_B;
173
174                 if (JoyStatus_LCL & JOY_LEFT)
175                   KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_C;
176                 else if (JoyStatus_LCL & JOY_RIGHT)
177                   KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_D;
178
179                 if (JoyStatus_LCL & JOY_PRESS)
180                   KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_E;
181
182                 *ReportID   = HID_REPORTID_KeyboardReport;
183                 *ReportSize = sizeof(USB_KeyboardReport_Data_t);
184                 return false;
185         }
186         else
187         {
188                 USB_MouseReport_Data_t* MouseReport = (USB_MouseReport_Data_t*)ReportData;
189
190                 if (JoyStatus_LCL & JOY_UP)
191                   MouseReport->Y = -1;
192                 else if (JoyStatus_LCL & JOY_DOWN)
193                   MouseReport->Y =  1;
194
195                 if (JoyStatus_LCL & JOY_LEFT)
196                   MouseReport->X = -1;
197                 else if (JoyStatus_LCL & JOY_RIGHT)
198                   MouseReport->X =  1;
199
200                 if (JoyStatus_LCL & JOY_PRESS)
201                   MouseReport->Button |= (1 << 0);
202
203                 *ReportID   = HID_REPORTID_MouseReport;
204                 *ReportSize = sizeof(USB_MouseReport_Data_t);
205                 return true;
206         }
207 }
208
209 /** HID class driver callback function for the processing of HID reports from the host.
210  *
211  *  \param[in] HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced
212  *  \param[in] ReportID    Report ID of the received report from the host
213  *  \param[in] ReportType  The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
214  *  \param[in] ReportData  Pointer to a buffer where the received report has been stored
215  *  \param[in] ReportSize  Size in bytes of the received HID report
216  */
217 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
218                                           const uint8_t ReportID,
219                                           const uint8_t ReportType,
220                                           const void* ReportData,
221                                           const uint16_t ReportSize)
222 {
223         uint8_t  LEDMask   = LEDS_NO_LEDS;
224         uint8_t* LEDReport = (uint8_t*)ReportData;
225
226         if (*LEDReport & HID_KEYBOARD_LED_NUMLOCK)
227           LEDMask |= LEDS_LED1;
228
229         if (*LEDReport & HID_KEYBOARD_LED_CAPSLOCK)
230           LEDMask |= LEDS_LED3;
231
232         if (*LEDReport & HID_KEYBOARD_LED_SCROLLLOCK)
233           LEDMask |= LEDS_LED4;
234
235         LEDs_SetAllLEDs(LEDMask);
236 }
237