Add tag for the 120219 release.
[pub/USBasp.git] / Demos / Device / ClassDriver / KeyboardMouse / KeyboardMouse.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 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 disclaim 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 = 0,
55
56 .ReportINEndpointNumber = KEYBOARD_IN_EPNUM,
57 .ReportINEndpointSize = HID_EPSIZE,
58 .ReportINEndpointDoubleBank = false,
59
60 .PrevReportINBuffer = PrevKeyboardHIDReportBuffer,
61 .PrevReportINBufferSize = sizeof(PrevKeyboardHIDReportBuffer),
62 },
63 };
64
65 /** LUFA HID Class driver interface configuration and state information. This structure is
66 * passed to all HID Class driver functions, so that multiple instances of the same class
67 * within a device can be differentiated from one another. This is for the mouse HID
68 * interface within the device.
69 */
70 USB_ClassInfo_HID_Device_t Mouse_HID_Interface =
71 {
72 .Config =
73 {
74 .InterfaceNumber = 1,
75
76 .ReportINEndpointNumber = MOUSE_IN_EPNUM,
77 .ReportINEndpointSize = HID_EPSIZE,
78
79 .PrevReportINBuffer = PrevMouseHIDReportBuffer,
80 .PrevReportINBufferSize = sizeof(PrevMouseHIDReportBuffer),
81 },
82 };
83
84
85 /** Main program entry point. This routine contains the overall program flow, including initial
86 * setup of all components and the main program loop.
87 */
88 int main(void)
89 {
90 SetupHardware();
91
92 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
93 sei();
94
95 for (;;)
96 {
97 HID_Device_USBTask(&Keyboard_HID_Interface);
98 HID_Device_USBTask(&Mouse_HID_Interface);
99 USB_USBTask();
100 }
101 }
102
103 /** Configures the board hardware and chip peripherals for the demo's functionality. */
104 void SetupHardware()
105 {
106 /* Disable watchdog if enabled by bootloader/fuses */
107 MCUSR &= ~(1 << WDRF);
108 wdt_disable();
109
110 /* Disable clock division */
111 clock_prescale_set(clock_div_1);
112
113 /* Hardware Initialization */
114 Joystick_Init();
115 LEDs_Init();
116 USB_Init();
117 }
118
119 /** Event handler for the library USB Connection event. */
120 void EVENT_USB_Device_Connect(void)
121 {
122 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
123 }
124
125 /** Event handler for the library USB Disconnection event. */
126 void EVENT_USB_Device_Disconnect(void)
127 {
128 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
129 }
130
131 /** Event handler for the library USB Configuration Changed event. */
132 void EVENT_USB_Device_ConfigurationChanged(void)
133 {
134 bool ConfigSuccess = true;
135
136 ConfigSuccess &= HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface);
137 ConfigSuccess &= HID_Device_ConfigureEndpoints(&Mouse_HID_Interface);
138
139 USB_Device_EnableSOFEvents();
140
141 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
142 }
143
144 /** Event handler for the library USB Control Request reception event. */
145 void EVENT_USB_Device_ControlRequest(void)
146 {
147 HID_Device_ProcessControlRequest(&Keyboard_HID_Interface);
148 HID_Device_ProcessControlRequest(&Mouse_HID_Interface);
149 }
150
151 /** Event handler for the USB device Start Of Frame event. */
152 void EVENT_USB_Device_StartOfFrame(void)
153 {
154 HID_Device_MillisecondElapsed(&Keyboard_HID_Interface);
155 HID_Device_MillisecondElapsed(&Mouse_HID_Interface);
156 }
157
158 /** HID class driver callback function for the creation of HID reports to the host.
159 *
160 * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
161 * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
162 * \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
163 * \param[out] ReportData Pointer to a buffer where the created report should be stored
164 * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
165 *
166 * \return Boolean true to force the sending of the report, false to let the library determine if it needs to be sent
167 */
168 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
169 uint8_t* const ReportID,
170 const uint8_t ReportType,
171 void* ReportData,
172 uint16_t* const ReportSize)
173 {
174 uint8_t JoyStatus_LCL = Joystick_GetStatus();
175 uint8_t ButtonStatus_LCL = Buttons_GetStatus();
176
177 /* Determine which interface must have its report generated */
178 if (HIDInterfaceInfo == &Keyboard_HID_Interface)
179 {
180 USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;
181
182 /* If first board button not being held down, no keyboard report */
183 if (!(ButtonStatus_LCL & BUTTONS_BUTTON1))
184 return 0;
185
186 KeyboardReport->Modifier = HID_KEYBOARD_MODIFIER_LEFTSHIFT;
187
188 if (JoyStatus_LCL & JOY_UP)
189 KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_A;
190 else if (JoyStatus_LCL & JOY_DOWN)
191 KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_B;
192
193 if (JoyStatus_LCL & JOY_LEFT)
194 KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_C;
195 else if (JoyStatus_LCL & JOY_RIGHT)
196 KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_D;
197
198 if (JoyStatus_LCL & JOY_PRESS)
199 KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_E;
200
201 *ReportSize = sizeof(USB_KeyboardReport_Data_t);
202 return false;
203 }
204 else
205 {
206 USB_MouseReport_Data_t* MouseReport = (USB_MouseReport_Data_t*)ReportData;
207
208 /* If first board button being held down, no mouse report */
209 if (ButtonStatus_LCL & BUTTONS_BUTTON1)
210 return 0;
211
212 if (JoyStatus_LCL & JOY_UP)
213 MouseReport->Y = -1;
214 else if (JoyStatus_LCL & JOY_DOWN)
215 MouseReport->Y = 1;
216
217 if (JoyStatus_LCL & JOY_LEFT)
218 MouseReport->X = -1;
219 else if (JoyStatus_LCL & JOY_RIGHT)
220 MouseReport->X = 1;
221
222 if (JoyStatus_LCL & JOY_PRESS)
223 MouseReport->Button |= (1 << 0);
224
225 *ReportSize = sizeof(USB_MouseReport_Data_t);
226 return true;
227 }
228 }
229
230 /** HID class driver callback function for the processing of HID reports from the host.
231 *
232 * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
233 * \param[in] ReportID Report ID of the received report from the host
234 * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
235 * \param[in] ReportData Pointer to a buffer where the received report has been stored
236 * \param[in] ReportSize Size in bytes of the received HID report
237 */
238 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
239 const uint8_t ReportID,
240 const uint8_t ReportType,
241 const void* ReportData,
242 const uint16_t ReportSize)
243 {
244 if (HIDInterfaceInfo == &Keyboard_HID_Interface)
245 {
246 uint8_t LEDMask = LEDS_NO_LEDS;
247 uint8_t* LEDReport = (uint8_t*)ReportData;
248
249 if (*LEDReport & HID_KEYBOARD_LED_NUMLOCK)
250 LEDMask |= LEDS_LED1;
251
252 if (*LEDReport & HID_KEYBOARD_LED_CAPSLOCK)
253 LEDMask |= LEDS_LED3;
254
255 if (*LEDReport & HID_KEYBOARD_LED_SCROLLLOCK)
256 LEDMask |= LEDS_LED4;
257
258 LEDs_SetAllLEDs(LEDMask);
259 }
260 }
261