| LPCOpen Platform v1.03 LPCOpen Platform for NXP LPC Microcontrollers

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
GenericHID.c
Go to the documentation of this file.
1 /*
2 * @brief Generic HID Device Example
3 *
4 * @note
5 * Copyright(C) NXP Semiconductors, 2012
6 * Copyright(C) Dean Camera, 2011, 2012
7 * All rights reserved.
8 *
9 * @par
10 * Software that is described herein is for illustrative purposes only
11 * which provides customers with programming information regarding the
12 * LPC products. This software is supplied "AS IS" without any warranties of
13 * any kind, and NXP Semiconductors and its licensor disclaim any and
14 * all warranties, express or implied, including all implied warranties of
15 * merchantability, fitness for a particular purpose and non-infringement of
16 * intellectual property rights. NXP Semiconductors assumes no responsibility
17 * or liability for the use of the software, conveys no license or rights under any
18 * patent, copyright, mask work right, or any other intellectual property rights in
19 * or to any products. NXP Semiconductors reserves the right to make changes
20 * in the software without notification. NXP Semiconductors also makes no
21 * representation or warranty that such application will be suitable for the
22 * specified use without further testing or modification.
23 *
24 * @par
25 * Permission to use, copy, modify, and distribute this software and its
26 * documentation is hereby granted, under NXP Semiconductors' and its
27 * licensor's relevant copyrights in the software, without fee, provided that it
28 * is used in conjunction with NXP Semiconductors microcontrollers. This
29 * copyright, permission, and disclaimer notice must appear in all copies of
30 * this code.
31 */
32
33 #include "GenericHID.h"
34
35 /*****************************************************************************
36 * Private types/enumerations/variables
37 ****************************************************************************/
38
40 static uint8_t PrevHIDReportBuffer[GENERIC_REPORT_SIZE];
41
46 static USB_ClassInfo_HID_Device_t Generic_HID_Interface = {
47 .Config = {
48 .InterfaceNumber = 0,
49
50 .ReportINEndpointNumber = GENERIC_IN_EPNUM,
51 .ReportINEndpointSize = GENERIC_EPSIZE,
52 .ReportINEndpointDoubleBank = false,
53
54 .PrevReportINBuffer = PrevHIDReportBuffer,
55 .PrevReportINBufferSize = sizeof(PrevHIDReportBuffer),
56 .PortNumber = 0,
57 },
58 };
59
60 /*****************************************************************************
61 * Public types/enumerations/variables
62 ****************************************************************************/
63
64 /*****************************************************************************
65 * Private functions
66 ****************************************************************************/
67
68 /* Configures the board hardware and chip peripherals for the demo's
69 functionality */
70 static void SetupHardware(void)
71 {
72 Board_Init();
73 Board_Buttons_Init();
74 Board_Joystick_Init();
75 USB_Init(Generic_HID_Interface.Config.PortNumber, USB_MODE_Device);
76 #if defined(USB_DEVICE_ROM_DRIVER)
77 UsbdHid_Init();
78 #endif
79 }
80
81 /*****************************************************************************
82 * Public functions
83 ****************************************************************************/
84
91 int main(void)
92 {
93 SetupHardware();
94
95 for (;; ) {
96 #if defined(USB_DEVICE_ROM_DRIVER)
97 USB_Descriptor_HIDReport_Datatype_t report;
98 uint16_t reportsize;
99 uint8_t reportID = 0;
100
101 memset(&report, 0, sizeof(USB_Descriptor_HIDReport_Datatype_t));
102 CALLBACK_HID_Device_CreateHIDReport(&Generic_HID_Interface, &reportID, HID_REPORT_ITEM_In, &report, &reportsize);
103 if (memcmp(&report, Generic_HID_Interface.Config.PrevReportINBuffer,
104 Generic_HID_Interface.Config.PrevReportINBufferSize)) {
105 memcpy(Generic_HID_Interface.Config.PrevReportINBuffer,
106 &report,
107 Generic_HID_Interface.Config.PrevReportINBufferSize);
108 CALLBACK_UsbdHid_SetReportChange(true);
109 }
110 #else
111 HID_Device_USBTask(&Generic_HID_Interface);
112 USB_USBTask(Generic_HID_Interface.Config.PortNumber, USB_MODE_Device);
113 #endif
114 }
115 }
116
117 /* Event handler for the library USB Connection event */
118 void EVENT_USB_Device_Connect(void)
119 {}
120
121 /* Event handler for the library USB Disconnection event */
122 void EVENT_USB_Device_Disconnect(void)
123 {}
124
125 /* Event handler for the library USB Configuration Changed event */
126 void EVENT_USB_Device_ConfigurationChanged(void)
127 {
128 bool ConfigSuccess = true;
129
130 ConfigSuccess &= HID_Device_ConfigureEndpoints(&Generic_HID_Interface);
131
132 USB_Device_EnableSOFEvents();
133 }
134
135 /* Event handler for the library USB Control Request reception event */
136 void EVENT_USB_Device_ControlRequest(void)
137 {
138 HID_Device_ProcessControlRequest(&Generic_HID_Interface);
139 }
140
141 /* Event handler for the USB device Start Of Frame event */
142 void EVENT_USB_Device_StartOfFrame(void)
143 {
144 HID_Device_MillisecondElapsed(&Generic_HID_Interface);
145 }
146
147 /* HID class driver callback function for the creation of HID reports to the host */
148 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t *const HIDInterfaceInfo,
149 uint8_t *const ReportID,
150 const uint8_t ReportType,
151 void *ReportData,
152 uint16_t *const ReportSize)
153 {
154 uint8_t *Data = (uint8_t *) ReportData;
155 uint8_t JoyStatus_LCL = Joystick_GetStatus();
156 uint8_t ButtonStatus_LCL = Buttons_GetStatus();
157 uint8_t ret = 0;
158
159 if (JoyStatus_LCL & JOY_UP) {
160 ret |= 0x01;
161 }
162 if (JoyStatus_LCL & JOY_LEFT) {
163 ret |= 0x02;
164 }
165 if (JoyStatus_LCL & JOY_RIGHT) {
166 ret |= 0x04;
167 }
168 if (JoyStatus_LCL & JOY_PRESS) {
169 ret |= 0x08;
170 }
171 if (JoyStatus_LCL & JOY_DOWN) {
172 ret |= 0x10;
173 }
174 if (ButtonStatus_LCL & BUTTONS_BUTTON1) {
175 ret |= 0x20;
176 }
177
178 Data[0] = ret;
179
180 *ReportSize = GENERIC_REPORT_SIZE;
181 return false;
182 }
183
184 /* HID class driver callback function for the processing of HID reports from the host */
185 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t *const HIDInterfaceInfo,
186 const uint8_t ReportID,
187 const uint8_t ReportType,
188 const void *ReportData,
189 const uint16_t ReportSize)
190 {
191 uint8_t *Data = (uint8_t *) ReportData;
192 uint8_t NewLEDMask = LEDS_NO_LEDS;
193
194 if (Data[0] & 0x01) {
195 NewLEDMask |= LEDS_LED1;
196 Board_LED_Set(0, 1);
197 }
198 else {
199 Board_LED_Set(0, 0);
200 }
201 if (Data[0] & 0x02) {
202 NewLEDMask |= LEDS_LED2;
203 Board_LED_Set(1, 1);
204 }
205 else {
206 Board_LED_Set(1, 0);
207 }
208 if (Data[0] & 0x04) {
209 NewLEDMask |= LEDS_LED3;
210 Board_LED_Set(2, 1);
211 }
212 else {
213 Board_LED_Set(2, 0);
214 }
215 if (Data[0] & 0x08) {
216 NewLEDMask |= LEDS_LED4;
217 Board_LED_Set(3, 1);
218 }
219 else {
220 Board_LED_Set(3, 0);
221 }
222
223 }