Minor documentation corrections.
[pub/USBasp.git] / Demos / Device / ClassDriver / GenericHID / GenericHID.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 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 GenericHID demo. This file contains the main tasks of
34 * the demo and is responsible for the initial application hardware configuration.
35 */
36
37 #include "GenericHID.h"
38
39 /** Buffer to hold the previously generated HID report, for comparison purposes inside the HID class driver. */
40 static uint8_t PrevHIDReportBuffer[GENERIC_REPORT_SIZE];
41
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.
45 */
46 USB_ClassInfo_HID_Device_t Generic_HID_Interface =
47 {
48 .Config =
49 {
50 .InterfaceNumber = 0,
51
52 .ReportINEndpointNumber = GENERIC_IN_EPNUM,
53 .ReportINEndpointSize = GENERIC_EPSIZE,
54 .ReportINEndpointDoubleBank = false,
55
56 .PrevReportINBuffer = PrevHIDReportBuffer,
57 .PrevReportINBufferSize = sizeof(PrevHIDReportBuffer),
58 },
59 };
60
61
62 /** Main program entry point. This routine contains the overall program flow, including initial
63 * setup of all components and the main program loop.
64 */
65 int main(void)
66 {
67 SetupHardware();
68
69 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
70 sei();
71
72 for (;;)
73 {
74 HID_Device_USBTask(&Generic_HID_Interface);
75 USB_USBTask();
76 }
77 }
78
79 /** Configures the board hardware and chip peripherals for the demo's functionality. */
80 void SetupHardware(void)
81 {
82 /* Disable watchdog if enabled by bootloader/fuses */
83 MCUSR &= ~(1 << WDRF);
84 wdt_disable();
85
86 /* Disable clock division */
87 clock_prescale_set(clock_div_1);
88
89 /* Hardware Initialization */
90 LEDs_Init();
91 USB_Init();
92 }
93
94 /** Event handler for the library USB Connection event. */
95 void EVENT_USB_Device_Connect(void)
96 {
97 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
98 }
99
100 /** Event handler for the library USB Disconnection event. */
101 void EVENT_USB_Device_Disconnect(void)
102 {
103 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
104 }
105
106 /** Event handler for the library USB Configuration Changed event. */
107 void EVENT_USB_Device_ConfigurationChanged(void)
108 {
109 bool ConfigSuccess = true;
110
111 ConfigSuccess &= HID_Device_ConfigureEndpoints(&Generic_HID_Interface);
112
113 USB_Device_EnableSOFEvents();
114
115 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
116 }
117
118 /** Event handler for the library USB Control Request reception event. */
119 void EVENT_USB_Device_ControlRequest(void)
120 {
121 HID_Device_ProcessControlRequest(&Generic_HID_Interface);
122 }
123
124 /** Event handler for the USB device Start Of Frame event. */
125 void EVENT_USB_Device_StartOfFrame(void)
126 {
127 HID_Device_MillisecondElapsed(&Generic_HID_Interface);
128 }
129
130 /** HID class driver callback function for the creation of HID reports to the host.
131 *
132 * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
133 * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
134 * \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
135 * \param[out] ReportData Pointer to a buffer where the created report should be stored
136 * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
137 *
138 * \return Boolean true to force the sending of the report, false to let the library determine if it needs to be sent
139 */
140 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
141 uint8_t* const ReportID,
142 const uint8_t ReportType,
143 void* ReportData,
144 uint16_t* const ReportSize)
145 {
146 uint8_t* Data = (uint8_t*)ReportData;
147 uint8_t CurrLEDMask = LEDs_GetLEDs();
148
149 Data[0] = ((CurrLEDMask & LEDS_LED1) ? 1 : 0);
150 Data[1] = ((CurrLEDMask & LEDS_LED2) ? 1 : 0);
151 Data[2] = ((CurrLEDMask & LEDS_LED3) ? 1 : 0);
152 Data[3] = ((CurrLEDMask & LEDS_LED4) ? 1 : 0);
153
154 *ReportSize = GENERIC_REPORT_SIZE;
155 return false;
156 }
157
158 /** HID class driver callback function for the processing of HID reports from the host.
159 *
160 * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
161 * \param[in] ReportID Report ID of the received report from the host
162 * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
163 * \param[in] ReportData Pointer to a buffer where the received report has been stored
164 * \param[in] ReportSize Size in bytes of the received HID report
165 */
166 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
167 const uint8_t ReportID,
168 const uint8_t ReportType,
169 const void* ReportData,
170 const uint16_t ReportSize)
171 {
172 uint8_t* Data = (uint8_t*)ReportData;
173 uint8_t NewLEDMask = LEDS_NO_LEDS;
174
175 if (Data[0])
176 NewLEDMask |= LEDS_LED1;
177
178 if (Data[1])
179 NewLEDMask |= LEDS_LED1;
180
181 if (Data[2])
182 NewLEDMask |= LEDS_LED1;
183
184 if (Data[3])
185 NewLEDMask |= LEDS_LED1;
186
187 LEDs_SetAllLEDs(NewLEDMask);
188 }
189