]> git.donarmstrong.com Git - qmk_firmware.git/blob - lib/lufa/Demos/Device/ClassDriver/Keyboard/Keyboard.c
Merge commit '60b30c036397cb5627fa374bb930794b225daa29' as 'lib/lufa'
[qmk_firmware.git] / lib / lufa / Demos / Device / ClassDriver / Keyboard / Keyboard.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 Keyboard demo. This file contains the main tasks of
34  *  the demo and is responsible for the initial application hardware configuration.
35  */
36
37 #include "Keyboard.h"
38
39 /** Buffer to hold the previously generated Keyboard HID report, for comparison purposes inside the HID class driver. */
40 static uint8_t PrevKeyboardHIDReportBuffer[sizeof(USB_KeyboardReport_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 Keyboard_HID_Interface =
47         {
48                 .Config =
49                         {
50                                 .InterfaceNumber              = INTERFACE_ID_Keyboard,
51                                 .ReportINEndpoint             =
52                                         {
53                                                 .Address              = KEYBOARD_EPADDR,
54                                                 .Size                 = KEYBOARD_EPSIZE,
55                                                 .Banks                = 1,
56                                         },
57                                 .PrevReportINBuffer           = PrevKeyboardHIDReportBuffer,
58                                 .PrevReportINBufferSize       = sizeof(PrevKeyboardHIDReportBuffer),
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(&Keyboard_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         Buttons_Init();
106         USB_Init();
107 }
108
109 /** Event handler for the library USB Connection event. */
110 void EVENT_USB_Device_Connect(void)
111 {
112         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
113 }
114
115 /** Event handler for the library USB Disconnection event. */
116 void EVENT_USB_Device_Disconnect(void)
117 {
118         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
119 }
120
121 /** Event handler for the library USB Configuration Changed event. */
122 void EVENT_USB_Device_ConfigurationChanged(void)
123 {
124         bool ConfigSuccess = true;
125
126         ConfigSuccess &= HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface);
127
128         USB_Device_EnableSOFEvents();
129
130         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
131 }
132
133 /** Event handler for the library USB Control Request reception event. */
134 void EVENT_USB_Device_ControlRequest(void)
135 {
136         HID_Device_ProcessControlRequest(&Keyboard_HID_Interface);
137 }
138
139 /** Event handler for the USB device Start Of Frame event. */
140 void EVENT_USB_Device_StartOfFrame(void)
141 {
142         HID_Device_MillisecondElapsed(&Keyboard_HID_Interface);
143 }
144
145 /** HID class driver callback function for the creation of HID reports to the host.
146  *
147  *  \param[in]     HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced
148  *  \param[in,out] ReportID    Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
149  *  \param[in]     ReportType  Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
150  *  \param[out]    ReportData  Pointer to a buffer where the created report should be stored
151  *  \param[out]    ReportSize  Number of bytes written in the report (or zero if no report is to be sent)
152  *
153  *  \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
154  */
155 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
156                                          uint8_t* const ReportID,
157                                          const uint8_t ReportType,
158                                          void* ReportData,
159                                          uint16_t* const ReportSize)
160 {
161         USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;
162
163         uint8_t JoyStatus_LCL    = Joystick_GetStatus();
164         uint8_t ButtonStatus_LCL = Buttons_GetStatus();
165
166         uint8_t UsedKeyCodes = 0;
167
168         if (JoyStatus_LCL & JOY_UP)
169           KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_A;
170         else if (JoyStatus_LCL & JOY_DOWN)
171           KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_B;
172
173         if (JoyStatus_LCL & JOY_LEFT)
174           KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_C;
175         else if (JoyStatus_LCL & JOY_RIGHT)
176           KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_D;
177
178         if (JoyStatus_LCL & JOY_PRESS)
179           KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_E;
180
181         if (ButtonStatus_LCL & BUTTONS_BUTTON1)
182           KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_F;
183
184         if (UsedKeyCodes)
185           KeyboardReport->Modifier = HID_KEYBOARD_MODIFIER_LEFTSHIFT;
186
187         *ReportSize = sizeof(USB_KeyboardReport_Data_t);
188         return false;
189 }
190
191 /** HID class driver callback function for the processing of HID reports from the host.
192  *
193  *  \param[in] HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced
194  *  \param[in] ReportID    Report ID of the received report from the host
195  *  \param[in] ReportType  The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
196  *  \param[in] ReportData  Pointer to a buffer where the received report has been stored
197  *  \param[in] ReportSize  Size in bytes of the received HID report
198  */
199 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
200                                           const uint8_t ReportID,
201                                           const uint8_t ReportType,
202                                           const void* ReportData,
203                                           const uint16_t ReportSize)
204 {
205         uint8_t  LEDMask   = LEDS_NO_LEDS;
206         uint8_t* LEDReport = (uint8_t*)ReportData;
207
208         if (*LEDReport & HID_KEYBOARD_LED_NUMLOCK)
209           LEDMask |= LEDS_LED1;
210
211         if (*LEDReport & HID_KEYBOARD_LED_CAPSLOCK)
212           LEDMask |= LEDS_LED3;
213
214         if (*LEDReport & HID_KEYBOARD_LED_SCROLLLOCK)
215           LEDMask |= LEDS_LED4;
216
217         LEDs_SetAllLEDs(LEDMask);
218 }
219