]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/Demos/Device/ClassDriver/KeyboardMouse/KeyboardMouse.c
Merge commit 'f6d56675f9f981c5464f0ca7a1fbb0162154e8c5'
[tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / Demos / Device / ClassDriver / KeyboardMouse / KeyboardMouse.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 KeyboardMouse demo. This file contains the main tasks of
34 * the demo and is responsible for the initial application hardware configuration.
35 */
36
37 #include "KeyboardMouse.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 /** Buffer to hold the previously generated Mouse HID report, for comparison purposes inside the HID class driver. */
43 static uint8_t PrevMouseHIDReportBuffer[sizeof(USB_MouseReport_Data_t)];
44
45 /** LUFA HID Class driver interface configuration and state information. This structure is
46 * passed to all HID Class driver functions, so that multiple instances of the same class
47 * within a device can be differentiated from one another. This is for the keyboard HID
48 * interface within the device.
49 */
50 USB_ClassInfo_HID_Device_t Keyboard_HID_Interface =
51 {
52 .Config =
53 {
54 .InterfaceNumber = INTERFACE_ID_Keyboard,
55 .ReportINEndpoint =
56 {
57 .Address = KEYBOARD_IN_EPADDR,
58 .Size = HID_EPSIZE,
59 .Banks = 1,
60 },
61 .PrevReportINBuffer = PrevKeyboardHIDReportBuffer,
62 .PrevReportINBufferSize = sizeof(PrevKeyboardHIDReportBuffer),
63 },
64 };
65
66 /** LUFA HID Class driver interface configuration and state information. This structure is
67 * passed to all HID Class driver functions, so that multiple instances of the same class
68 * within a device can be differentiated from one another. This is for the mouse HID
69 * interface within the device.
70 */
71 USB_ClassInfo_HID_Device_t Mouse_HID_Interface =
72 {
73 .Config =
74 {
75 .InterfaceNumber = INTERFACE_ID_Mouse,
76 .ReportINEndpoint =
77 {
78 .Address = MOUSE_IN_EPADDR,
79 .Size = HID_EPSIZE,
80 .Banks = 1,
81 },
82 .PrevReportINBuffer = PrevMouseHIDReportBuffer,
83 .PrevReportINBufferSize = sizeof(PrevMouseHIDReportBuffer),
84 },
85 };
86
87
88 /** Main program entry point. This routine contains the overall program flow, including initial
89 * setup of all components and the main program loop.
90 */
91 int main(void)
92 {
93 SetupHardware();
94
95 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
96 GlobalInterruptEnable();
97
98 for (;;)
99 {
100 HID_Device_USBTask(&Keyboard_HID_Interface);
101 HID_Device_USBTask(&Mouse_HID_Interface);
102 USB_USBTask();
103 }
104 }
105
106 /** Configures the board hardware and chip peripherals for the demo's functionality. */
107 void SetupHardware()
108 {
109 #if (ARCH == ARCH_AVR8)
110 /* Disable watchdog if enabled by bootloader/fuses */
111 MCUSR &= ~(1 << WDRF);
112 wdt_disable();
113
114 /* Disable clock division */
115 clock_prescale_set(clock_div_1);
116 #elif (ARCH == ARCH_XMEGA)
117 /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
118 XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
119 XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
120
121 /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
122 XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
123 XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
124
125 PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
126 #endif
127
128 /* Hardware Initialization */
129 Joystick_Init();
130 LEDs_Init();
131 USB_Init();
132 }
133
134 /** Event handler for the library USB Connection event. */
135 void EVENT_USB_Device_Connect(void)
136 {
137 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
138 }
139
140 /** Event handler for the library USB Disconnection event. */
141 void EVENT_USB_Device_Disconnect(void)
142 {
143 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
144 }
145
146 /** Event handler for the library USB Configuration Changed event. */
147 void EVENT_USB_Device_ConfigurationChanged(void)
148 {
149 bool ConfigSuccess = true;
150
151 ConfigSuccess &= HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface);
152 ConfigSuccess &= HID_Device_ConfigureEndpoints(&Mouse_HID_Interface);
153
154 USB_Device_EnableSOFEvents();
155
156 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
157 }
158
159 /** Event handler for the library USB Control Request reception event. */
160 void EVENT_USB_Device_ControlRequest(void)
161 {
162 HID_Device_ProcessControlRequest(&Keyboard_HID_Interface);
163 HID_Device_ProcessControlRequest(&Mouse_HID_Interface);
164 }
165
166 /** Event handler for the USB device Start Of Frame event. */
167 void EVENT_USB_Device_StartOfFrame(void)
168 {
169 HID_Device_MillisecondElapsed(&Keyboard_HID_Interface);
170 HID_Device_MillisecondElapsed(&Mouse_HID_Interface);
171 }
172
173 /** HID class driver callback function for the creation of HID reports to the host.
174 *
175 * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
176 * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
177 * \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
178 * \param[out] ReportData Pointer to a buffer where the created report should be stored
179 * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
180 *
181 * \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
182 */
183 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
184 uint8_t* const ReportID,
185 const uint8_t ReportType,
186 void* ReportData,
187 uint16_t* const ReportSize)
188 {
189 uint8_t JoyStatus_LCL = Joystick_GetStatus();
190 uint8_t ButtonStatus_LCL = Buttons_GetStatus();
191
192 /* Determine which interface must have its report generated */
193 if (HIDInterfaceInfo == &Keyboard_HID_Interface)
194 {
195 USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;
196
197 /* If first board button not being held down, no keyboard report */
198 if (!(ButtonStatus_LCL & BUTTONS_BUTTON1))
199 return 0;
200
201 KeyboardReport->Modifier = HID_KEYBOARD_MODIFIER_LEFTSHIFT;
202
203 if (JoyStatus_LCL & JOY_UP)
204 KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_A;
205 else if (JoyStatus_LCL & JOY_DOWN)
206 KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_B;
207
208 if (JoyStatus_LCL & JOY_LEFT)
209 KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_C;
210 else if (JoyStatus_LCL & JOY_RIGHT)
211 KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_D;
212
213 if (JoyStatus_LCL & JOY_PRESS)
214 KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_E;
215
216 *ReportSize = sizeof(USB_KeyboardReport_Data_t);
217 return false;
218 }
219 else
220 {
221 USB_MouseReport_Data_t* MouseReport = (USB_MouseReport_Data_t*)ReportData;
222
223 /* If first board button being held down, no mouse report */
224 if (ButtonStatus_LCL & BUTTONS_BUTTON1)
225 return 0;
226
227 if (JoyStatus_LCL & JOY_UP)
228 MouseReport->Y = -1;
229 else if (JoyStatus_LCL & JOY_DOWN)
230 MouseReport->Y = 1;
231
232 if (JoyStatus_LCL & JOY_LEFT)
233 MouseReport->X = -1;
234 else if (JoyStatus_LCL & JOY_RIGHT)
235 MouseReport->X = 1;
236
237 if (JoyStatus_LCL & JOY_PRESS)
238 MouseReport->Button |= (1 << 0);
239
240 *ReportSize = sizeof(USB_MouseReport_Data_t);
241 return true;
242 }
243 }
244
245 /** HID class driver callback function for the processing of HID reports from the host.
246 *
247 * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
248 * \param[in] ReportID Report ID of the received report from the host
249 * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
250 * \param[in] ReportData Pointer to a buffer where the received report has been stored
251 * \param[in] ReportSize Size in bytes of the received HID report
252 */
253 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
254 const uint8_t ReportID,
255 const uint8_t ReportType,
256 const void* ReportData,
257 const uint16_t ReportSize)
258 {
259 if (HIDInterfaceInfo == &Keyboard_HID_Interface)
260 {
261 uint8_t LEDMask = LEDS_NO_LEDS;
262 uint8_t* LEDReport = (uint8_t*)ReportData;
263
264 if (*LEDReport & HID_KEYBOARD_LED_NUMLOCK)
265 LEDMask |= LEDS_LED1;
266
267 if (*LEDReport & HID_KEYBOARD_LED_CAPSLOCK)
268 LEDMask |= LEDS_LED3;
269
270 if (*LEDReport & HID_KEYBOARD_LED_SCROLLLOCK)
271 LEDMask |= LEDS_LED4;
272
273 LEDs_SetAllLEDs(LEDMask);
274 }
275 }
276
Imprint / Impressum