]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/Demos/Device/LowLevel/KeyboardMouse/KeyboardMouse.c
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / lufa / LUFA-git / Demos / Device / LowLevel / 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 Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
12
13 Permission to use, copy, modify, distribute, and sell this
14 software and its documentation for any purpose is hereby granted
15 without fee, provided that the above copyright notice appear in
16 all copies and that both that the copyright notice and this
17 permission notice and warranty disclaimer appear in supporting
18 documentation, and that the name of the author not be used in
19 advertising or publicity pertaining to distribution of the
20 software without specific, written prior permission.
21
22 The author disclaims all warranties with regard to this
23 software, including all implied warranties of merchantability
24 and fitness. In no event shall the author be liable for any
25 special, indirect or consequential damages or any damages
26 whatsoever resulting from loss of use, data or profits, whether
27 in an action of contract, negligence or other tortious action,
28 arising out of or in connection with the use or performance of
29 this software.
30 */
31
32 /** \file
33 *
34 * Main source file for the KeyboardMouse demo. This file contains the main tasks of the demo and
35 * is responsible for the initial application hardware configuration.
36 */
37
38 #include "KeyboardMouse.h"
39
40 /** Global structure to hold the current keyboard interface HID report, for transmission to the host */
41 static USB_KeyboardReport_Data_t KeyboardReportData;
42
43 /** Global structure to hold the current mouse interface HID report, for transmission to the host */
44 static USB_MouseReport_Data_t MouseReportData;
45
46
47 /** Main program entry point. This routine configures the hardware required by the application, then
48 * enters a loop to run the application tasks in sequence.
49 */
50 int main(void)
51 {
52 SetupHardware();
53
54 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
55 GlobalInterruptEnable();
56
57 for (;;)
58 {
59 Keyboard_HID_Task();
60 Mouse_HID_Task();
61 USB_USBTask();
62 }
63 }
64
65 /** Configures the board hardware and chip peripherals for the demo's functionality. */
66 void SetupHardware(void)
67 {
68 #if (ARCH == ARCH_AVR8)
69 /* Disable watchdog if enabled by bootloader/fuses */
70 MCUSR &= ~(1 << WDRF);
71 wdt_disable();
72
73 /* Disable clock division */
74 clock_prescale_set(clock_div_1);
75 #elif (ARCH == ARCH_XMEGA)
76 /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
77 XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
78 XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
79
80 /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
81 XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
82 XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
83
84 PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
85 #endif
86
87 /* Hardware Initialization */
88 Joystick_Init();
89 LEDs_Init();
90 USB_Init();
91 }
92
93 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
94 * starts the library USB task to begin the enumeration and USB management process.
95 */
96 void EVENT_USB_Device_Connect(void)
97 {
98 /* Indicate USB enumerating */
99 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
100 }
101
102 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
103 * the status LEDs and stops the USB management task.
104 */
105 void EVENT_USB_Device_Disconnect(void)
106 {
107 /* Indicate USB not ready */
108 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
109 }
110
111 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
112 * of the USB device after enumeration, and configures the keyboard and mouse device endpoints.
113 */
114 void EVENT_USB_Device_ConfigurationChanged(void)
115 {
116 bool ConfigSuccess = true;
117
118 /* Setup Keyboard HID Report Endpoints */
119 ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPADDR, EP_TYPE_INTERRUPT, HID_EPSIZE, 1);
120 ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_OUT_EPADDR, EP_TYPE_INTERRUPT, HID_EPSIZE, 1);
121
122 /* Setup Mouse HID Report Endpoint */
123 ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPADDR, EP_TYPE_INTERRUPT, HID_EPSIZE, 1);
124
125 /* Indicate endpoint configuration success or failure */
126 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
127 }
128
129 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
130 * the device from the USB host before passing along unhandled control requests to the library for processing
131 * internally.
132 */
133 void EVENT_USB_Device_ControlRequest(void)
134 {
135 uint8_t* ReportData;
136 uint8_t ReportSize;
137
138 /* Handle HID Class specific requests */
139 switch (USB_ControlRequest.bRequest)
140 {
141 case HID_REQ_GetReport:
142 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
143 {
144 Endpoint_ClearSETUP();
145
146 /* Determine if it is the mouse or the keyboard data that is being requested */
147 if (!(USB_ControlRequest.wIndex))
148 {
149 ReportData = (uint8_t*)&KeyboardReportData;
150 ReportSize = sizeof(KeyboardReportData);
151 }
152 else
153 {
154 ReportData = (uint8_t*)&MouseReportData;
155 ReportSize = sizeof(MouseReportData);
156 }
157
158 /* Write the report data to the control endpoint */
159 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
160 Endpoint_ClearOUT();
161
162 /* Clear the report data afterwards */
163 memset(ReportData, 0, ReportSize);
164 }
165
166 break;
167 case HID_REQ_SetReport:
168 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
169 {
170 Endpoint_ClearSETUP();
171
172 /* Wait until the LED report has been sent by the host */
173 while (!(Endpoint_IsOUTReceived()))
174 {
175 if (USB_DeviceState == DEVICE_STATE_Unattached)
176 return;
177 }
178
179 /* Read in the LED report from the host */
180 uint8_t LEDStatus = Endpoint_Read_8();
181
182 Endpoint_ClearOUT();
183 Endpoint_ClearStatusStage();
184
185 /* Process the incoming LED report */
186 Keyboard_ProcessLEDReport(LEDStatus);
187 }
188
189 break;
190 }
191 }
192
193 /** Processes a given Keyboard LED report from the host, and sets the board LEDs to match. Since the Keyboard
194 * LED report can be sent through either the control endpoint (via a HID SetReport request) or the HID OUT
195 * endpoint, the processing code is placed here to avoid duplicating it and potentially having different
196 * behavior depending on the method used to sent it.
197 */
198 void Keyboard_ProcessLEDReport(const uint8_t LEDStatus)
199 {
200 uint8_t LEDMask = LEDS_LED2;
201
202 if (LEDStatus & HID_KEYBOARD_LED_NUMLOCK)
203 LEDMask |= LEDS_LED1;
204
205 if (LEDStatus & HID_KEYBOARD_LED_CAPSLOCK)
206 LEDMask |= LEDS_LED3;
207
208 if (LEDStatus & HID_KEYBOARD_LED_SCROLLLOCK)
209 LEDMask |= LEDS_LED4;
210
211 /* Set the status LEDs to the current Keyboard LED status */
212 LEDs_SetAllLEDs(LEDMask);
213 }
214
215 /** Keyboard task. This generates the next keyboard HID report for the host, and transmits it via the
216 * keyboard IN endpoint when the host is ready for more data. Additionally, it processes host LED status
217 * reports sent to the device via the keyboard OUT reporting endpoint.
218 */
219 void Keyboard_HID_Task(void)
220 {
221 uint8_t JoyStatus_LCL = Joystick_GetStatus();
222
223 /* Device must be connected and configured for the task to run */
224 if (USB_DeviceState != DEVICE_STATE_Configured)
225 return;
226
227 /* Check if board button is not pressed, if so mouse mode enabled */
228 if (!(Buttons_GetStatus() & BUTTONS_BUTTON1))
229 {
230 /* Make sent key uppercase by indicating that the left shift key is pressed */
231 KeyboardReportData.Modifier = HID_KEYBOARD_MODIFIER_LEFTSHIFT;
232
233 if (JoyStatus_LCL & JOY_UP)
234 KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_A;
235 else if (JoyStatus_LCL & JOY_DOWN)
236 KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_B;
237
238 if (JoyStatus_LCL & JOY_LEFT)
239 KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_C;
240 else if (JoyStatus_LCL & JOY_RIGHT)
241 KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_D;
242
243 if (JoyStatus_LCL & JOY_PRESS)
244 KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_E;
245 }
246
247 /* Select the Keyboard Report Endpoint */
248 Endpoint_SelectEndpoint(KEYBOARD_IN_EPADDR);
249
250 /* Check if Keyboard Endpoint Ready for Read/Write */
251 if (Endpoint_IsReadWriteAllowed())
252 {
253 /* Write Keyboard Report Data */
254 Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData), NULL);
255
256 /* Finalize the stream transfer to send the last packet */
257 Endpoint_ClearIN();
258
259 /* Clear the report data afterwards */
260 memset(&KeyboardReportData, 0, sizeof(KeyboardReportData));
261 }
262
263 /* Select the Keyboard LED Report Endpoint */
264 Endpoint_SelectEndpoint(KEYBOARD_OUT_EPADDR);
265
266 /* Check if Keyboard LED Endpoint Ready for Read/Write */
267 if (Endpoint_IsReadWriteAllowed())
268 {
269 /* Read in and process the LED report from the host */
270 Keyboard_ProcessLEDReport(Endpoint_Read_8());
271
272 /* Handshake the OUT Endpoint - clear endpoint and ready for next report */
273 Endpoint_ClearOUT();
274 }
275 }
276
277 /** Mouse task. This generates the next mouse HID report for the host, and transmits it via the
278 * mouse IN endpoint when the host is ready for more data.
279 */
280 void Mouse_HID_Task(void)
281 {
282 uint8_t JoyStatus_LCL = Joystick_GetStatus();
283
284 /* Device must be connected and configured for the task to run */
285 if (USB_DeviceState != DEVICE_STATE_Configured)
286 return;
287
288 /* Check if board button is pressed, if so mouse mode enabled */
289 if (Buttons_GetStatus() & BUTTONS_BUTTON1)
290 {
291 if (JoyStatus_LCL & JOY_UP)
292 MouseReportData.Y = 1;
293 else if (JoyStatus_LCL & JOY_DOWN)
294 MouseReportData.Y = -1;
295
296 if (JoyStatus_LCL & JOY_RIGHT)
297 MouseReportData.X = 1;
298 else if (JoyStatus_LCL & JOY_LEFT)
299 MouseReportData.X = -1;
300
301 if (JoyStatus_LCL & JOY_PRESS)
302 MouseReportData.Button |= (1 << 0);
303 }
304
305 /* Select the Mouse Report Endpoint */
306 Endpoint_SelectEndpoint(MOUSE_IN_EPADDR);
307
308 /* Check if Mouse Endpoint Ready for Read/Write */
309 if (Endpoint_IsReadWriteAllowed())
310 {
311 /* Write Mouse Report Data */
312 Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData), NULL);
313
314 /* Finalize the stream transfer to send the last packet */
315 Endpoint_ClearIN();
316
317 /* Clear the report data afterwards */
318 memset(&MouseReportData, 0, sizeof(MouseReportData));
319 }
320 }
321
Imprint / Impressum