3 Copyright (C) Dean Camera, 2014.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
33 * Main source file for the KeyboardMouseMultiReport demo. This file contains the main tasks of
34 * the demo and is responsible for the initial application hardware configuration.
37 #include "KeyboardMouseMultiReport.h"
39 /** Buffer to hold the previously generated HID report, for comparison purposes inside the HID class driver. */
40 static uint8_t PrevHIDReportBuffer
[MAX(sizeof(USB_KeyboardReport_Data_t
), sizeof(USB_MouseReport_Data_t
))];
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.
46 USB_ClassInfo_HID_Device_t Device_HID_Interface
=
50 .InterfaceNumber
= INTERFACE_ID_KeyboardAndMouse
,
53 .Address
= HID_IN_EPADDR
,
57 .PrevReportINBuffer
= PrevHIDReportBuffer
,
58 .PrevReportINBufferSize
= sizeof(PrevHIDReportBuffer
),
63 /** Main program entry point. This routine contains the overall program flow, including initial
64 * setup of all components and the main program loop.
70 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
71 GlobalInterruptEnable();
75 HID_Device_USBTask(&Device_HID_Interface
);
80 /** Configures the board hardware and chip peripherals for the demo's functionality. */
83 #if (ARCH == ARCH_AVR8)
84 /* Disable watchdog if enabled by bootloader/fuses */
85 MCUSR
&= ~(1 << WDRF
);
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
);
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
);
99 PMIC
.CTRL
= PMIC_LOLVLEN_bm
| PMIC_MEDLVLEN_bm
| PMIC_HILVLEN_bm
;
102 /* Hardware Initialization */
108 /** Event handler for the library USB Connection event. */
109 void EVENT_USB_Device_Connect(void)
111 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
114 /** Event handler for the library USB Disconnection event. */
115 void EVENT_USB_Device_Disconnect(void)
117 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
120 /** Event handler for the library USB Configuration Changed event. */
121 void EVENT_USB_Device_ConfigurationChanged(void)
123 bool ConfigSuccess
= true;
125 ConfigSuccess
&= HID_Device_ConfigureEndpoints(&Device_HID_Interface
);
127 USB_Device_EnableSOFEvents();
129 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY
: LEDMASK_USB_ERROR
);
132 /** Event handler for the library USB Control Request reception event. */
133 void EVENT_USB_Device_ControlRequest(void)
135 HID_Device_ProcessControlRequest(&Device_HID_Interface
);
138 /** Event handler for the USB device Start Of Frame event. */
139 void EVENT_USB_Device_StartOfFrame(void)
141 HID_Device_MillisecondElapsed(&Device_HID_Interface
);
144 /** HID class driver callback function for the creation of HID reports to the host.
146 * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
147 * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
148 * \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
149 * \param[out] ReportData Pointer to a buffer where the created report should be stored
150 * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
152 * \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 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
,
155 uint8_t* const ReportID
,
156 const uint8_t ReportType
,
158 uint16_t* const ReportSize
)
160 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
161 uint8_t ButtonStatus_LCL
= Buttons_GetStatus();
163 if (!(ButtonStatus_LCL
& BUTTONS_BUTTON1
))
165 USB_KeyboardReport_Data_t
* KeyboardReport
= (USB_KeyboardReport_Data_t
*)ReportData
;
167 KeyboardReport
->Modifier
= HID_KEYBOARD_MODIFIER_LEFTSHIFT
;
169 if (JoyStatus_LCL
& JOY_UP
)
170 KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_A
;
171 else if (JoyStatus_LCL
& JOY_DOWN
)
172 KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_B
;
174 if (JoyStatus_LCL
& JOY_LEFT
)
175 KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_C
;
176 else if (JoyStatus_LCL
& JOY_RIGHT
)
177 KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_D
;
179 if (JoyStatus_LCL
& JOY_PRESS
)
180 KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_E
;
182 *ReportID
= HID_REPORTID_KeyboardReport
;
183 *ReportSize
= sizeof(USB_KeyboardReport_Data_t
);
188 USB_MouseReport_Data_t
* MouseReport
= (USB_MouseReport_Data_t
*)ReportData
;
190 if (JoyStatus_LCL
& JOY_UP
)
192 else if (JoyStatus_LCL
& JOY_DOWN
)
195 if (JoyStatus_LCL
& JOY_LEFT
)
197 else if (JoyStatus_LCL
& JOY_RIGHT
)
200 if (JoyStatus_LCL
& JOY_PRESS
)
201 MouseReport
->Button
|= (1 << 0);
203 *ReportID
= HID_REPORTID_MouseReport
;
204 *ReportSize
= sizeof(USB_MouseReport_Data_t
);
209 /** HID class driver callback function for the processing of HID reports from the host.
211 * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
212 * \param[in] ReportID Report ID of the received report from the host
213 * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
214 * \param[in] ReportData Pointer to a buffer where the received report has been stored
215 * \param[in] ReportSize Size in bytes of the received HID report
217 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
,
218 const uint8_t ReportID
,
219 const uint8_t ReportType
,
220 const void* ReportData
,
221 const uint16_t ReportSize
)
223 uint8_t LEDMask
= LEDS_NO_LEDS
;
224 uint8_t* LEDReport
= (uint8_t*)ReportData
;
226 if (*LEDReport
& HID_KEYBOARD_LED_NUMLOCK
)
227 LEDMask
|= LEDS_LED1
;
229 if (*LEDReport
& HID_KEYBOARD_LED_CAPSLOCK
)
230 LEDMask
|= LEDS_LED3
;
232 if (*LEDReport
& HID_KEYBOARD_LED_SCROLLLOCK
)
233 LEDMask
|= LEDS_LED4
;
235 LEDs_SetAllLEDs(LEDMask
);