Use the framework provided GlobalInterruptEnable() function instead of the AVR8/XMEGA...
[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 .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 = 1,
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 /* Disable watchdog if enabled by bootloader/fuses */
110 MCUSR &= ~(1 << WDRF);
111 wdt_disable();
112
113 /* Disable clock division */
114 clock_prescale_set(clock_div_1);
115
116 /* Hardware Initialization */
117 Joystick_Init();
118 LEDs_Init();
119 USB_Init();
120 }
121
122 /** Event handler for the library USB Connection event. */
123 void EVENT_USB_Device_Connect(void)
124 {
125 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
126 }
127
128 /** Event handler for the library USB Disconnection event. */
129 void EVENT_USB_Device_Disconnect(void)
130 {
131 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
132 }
133
134 /** Event handler for the library USB Configuration Changed event. */
135 void EVENT_USB_Device_ConfigurationChanged(void)
136 {
137 bool ConfigSuccess = true;
138
139 ConfigSuccess &= HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface);
140 ConfigSuccess &= HID_Device_ConfigureEndpoints(&Mouse_HID_Interface);
141
142 USB_Device_EnableSOFEvents();
143
144 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
145 }
146
147 /** Event handler for the library USB Control Request reception event. */
148 void EVENT_USB_Device_ControlRequest(void)
149 {
150 HID_Device_ProcessControlRequest(&Keyboard_HID_Interface);
151 HID_Device_ProcessControlRequest(&Mouse_HID_Interface);
152 }
153
154 /** Event handler for the USB device Start Of Frame event. */
155 void EVENT_USB_Device_StartOfFrame(void)
156 {
157 HID_Device_MillisecondElapsed(&Keyboard_HID_Interface);
158 HID_Device_MillisecondElapsed(&Mouse_HID_Interface);
159 }
160
161 /** HID class driver callback function for the creation of HID reports to the host.
162 *
163 * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
164 * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
165 * \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
166 * \param[out] ReportData Pointer to a buffer where the created report should be stored
167 * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
168 *
169 * \return Boolean true to force the sending of the report, false to let the library determine if it needs to be sent
170 */
171 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
172 uint8_t* const ReportID,
173 const uint8_t ReportType,
174 void* ReportData,
175 uint16_t* const ReportSize)
176 {
177 uint8_t JoyStatus_LCL = Joystick_GetStatus();
178 uint8_t ButtonStatus_LCL = Buttons_GetStatus();
179
180 /* Determine which interface must have its report generated */
181 if (HIDInterfaceInfo == &Keyboard_HID_Interface)
182 {
183 USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;
184
185 /* If first board button not being held down, no keyboard report */
186 if (!(ButtonStatus_LCL & BUTTONS_BUTTON1))
187 return 0;
188
189 KeyboardReport->Modifier = HID_KEYBOARD_MODIFIER_LEFTSHIFT;
190
191 if (JoyStatus_LCL & JOY_UP)
192 KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_A;
193 else if (JoyStatus_LCL & JOY_DOWN)
194 KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_B;
195
196 if (JoyStatus_LCL & JOY_LEFT)
197 KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_C;
198 else if (JoyStatus_LCL & JOY_RIGHT)
199 KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_D;
200
201 if (JoyStatus_LCL & JOY_PRESS)
202 KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_E;
203
204 *ReportSize = sizeof(USB_KeyboardReport_Data_t);
205 return false;
206 }
207 else
208 {
209 USB_MouseReport_Data_t* MouseReport = (USB_MouseReport_Data_t*)ReportData;
210
211 /* If first board button being held down, no mouse report */
212 if (ButtonStatus_LCL & BUTTONS_BUTTON1)
213 return 0;
214
215 if (JoyStatus_LCL & JOY_UP)
216 MouseReport->Y = -1;
217 else if (JoyStatus_LCL & JOY_DOWN)
218 MouseReport->Y = 1;
219
220 if (JoyStatus_LCL & JOY_LEFT)
221 MouseReport->X = -1;
222 else if (JoyStatus_LCL & JOY_RIGHT)
223 MouseReport->X = 1;
224
225 if (JoyStatus_LCL & JOY_PRESS)
226 MouseReport->Button |= (1 << 0);
227
228 *ReportSize = sizeof(USB_MouseReport_Data_t);
229 return true;
230 }
231 }
232
233 /** HID class driver callback function for the processing of HID reports from the host.
234 *
235 * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
236 * \param[in] ReportID Report ID of the received report from the host
237 * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
238 * \param[in] ReportData Pointer to a buffer where the received report has been stored
239 * \param[in] ReportSize Size in bytes of the received HID report
240 */
241 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
242 const uint8_t ReportID,
243 const uint8_t ReportType,
244 const void* ReportData,
245 const uint16_t ReportSize)
246 {
247 if (HIDInterfaceInfo == &Keyboard_HID_Interface)
248 {
249 uint8_t LEDMask = LEDS_NO_LEDS;
250 uint8_t* LEDReport = (uint8_t*)ReportData;
251
252 if (*LEDReport & HID_KEYBOARD_LED_NUMLOCK)
253 LEDMask |= LEDS_LED1;
254
255 if (*LEDReport & HID_KEYBOARD_LED_CAPSLOCK)
256 LEDMask |= LEDS_LED3;
257
258 if (*LEDReport & HID_KEYBOARD_LED_SCROLLLOCK)
259 LEDMask |= LEDS_LED4;
260
261 LEDs_SetAllLEDs(LEDMask);
262 }
263 }
264