LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Keyboard.c
Go to the documentation of this file.
1 /*
2  * @brief Make your board becomes a USB keyboard
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 "Keyboard.h"
34 
37 
43  .Config = {
44  .InterfaceNumber = 0,
45 
46  .ReportINEndpointNumber = KEYBOARD_EPNUM,
47  .ReportINEndpointSize = KEYBOARD_EPSIZE,
48  .ReportINEndpointDoubleBank = false,
49 
50  .PrevReportINBuffer = PrevKeyboardHIDReportBuffer,
51  .PrevReportINBufferSize = sizeof(PrevKeyboardHIDReportBuffer),
52  },
53 };
54 #if defined(USB_DEVICE_ROM_DRIVER)
55 extern void CALLBACK_UsbdHid_SetReportChange(bool newstate);
56 
57 #endif
58 
61 int main(void)
62 {
63  SetupHardware();
64 
65  for (;; ) {
66  #if defined(USB_DEVICE_ROM_DRIVER)
68  uint16_t reportsize;
69  uint8_t reportID = 0;
70 
71  memset(&report, 0, sizeof(USB_KeyboardReport_Data_t));
72  CALLBACK_HID_Device_CreateHIDReport(&Keyboard_HID_Interface,
73  &reportID,
75  &report,
76  &reportsize);
77  if (memcmp(&report, Keyboard_HID_Interface.Config.PrevReportINBuffer,
78  Keyboard_HID_Interface.Config.PrevReportINBufferSize)) {
79  memcpy(Keyboard_HID_Interface.Config.PrevReportINBuffer,
80  &report,
81  Keyboard_HID_Interface.Config.PrevReportINBufferSize);
83  }
84  #else
85  HID_Device_USBTask(&Keyboard_HID_Interface);
86  USB_USBTask();
87  #endif
88  }
89 }
90 
93 {
94  Board_Init();
97  USB_Init();
98 #if defined(USB_DEVICE_ROM_DRIVER)
99  UsbdHid_Init();
100 #endif
101 }
102 
105 {
106  // LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
107 }
108 
111 {
112  // LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
113 }
114 
117 {
118  bool ConfigSuccess = true;
119 
120  ConfigSuccess &= HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface);
121 
123 
124  // LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
125 }
126 
129 {
130  HID_Device_ProcessControlRequest(&Keyboard_HID_Interface);
131 }
132 
135 {
136  HID_Device_MillisecondElapsed(&Keyboard_HID_Interface);
137 }
138 
141 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t *const HIDInterfaceInfo, uint8_t *const ReportID,
142  const uint8_t ReportType, void *ReportData, uint16_t *const ReportSize)
143 {
145 
146  uint8_t JoyStatus_LCL = Joystick_GetStatus();
147  uint8_t ButtonStatus_LCL = Buttons_GetStatus();
148 
149  uint8_t UsedKeyCodes = 0;
150 
151  if (JoyStatus_LCL & JOY_UP) {
152  KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_A;
153  }
154  else if (JoyStatus_LCL & JOY_DOWN) {
155  KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_B;
156  }
157 
158  if (JoyStatus_LCL & JOY_LEFT) {
159  KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_C;
160  }
161  else if (JoyStatus_LCL & JOY_RIGHT) {
162  KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_D;
163  }
164 
165  if (JoyStatus_LCL & JOY_PRESS) {
166  KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_E;
167  }
168 
169  if (ButtonStatus_LCL & BUTTONS_BUTTON1) {
170  KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_F;
171  }
172 
173  if (UsedKeyCodes) {
174  KeyboardReport->Modifier = HID_KEYBOARD_MODIFER_LEFTSHIFT;
175  }
176 
177  *ReportSize = sizeof(USB_KeyboardReport_Data_t);
178  return false;
179 }
180 
184  const uint8_t ReportID,
185  const uint8_t ReportType,
186  const void *ReportData,
187  const uint16_t ReportSize)
188 {
189  uint8_t LEDMask = LEDS_NO_LEDS;
190  uint8_t *LEDReport = (uint8_t *) ReportData;
191 
192  if (*LEDReport & HID_KEYBOARD_LED_NUMLOCK) {
193  LEDMask |= LEDS_LED1;
194  }
195 
196  if (*LEDReport & HID_KEYBOARD_LED_CAPSLOCK) {
197  LEDMask |= LEDS_LED3;
198  }
199 
200  if (*LEDReport & HID_KEYBOARD_LED_SCROLLLOCK) {
201  LEDMask |= LEDS_LED4;
202  }
203 
204  // LEDs_SetAllLEDs(LEDMask);
205 }