LPCOpen Platform  v1.03
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 
35 /*****************************************************************************
36  * Private types/enumerations/variables
37  ****************************************************************************/
38 
41 
47  .Config = {
48  .InterfaceNumber = 0,
49 
50  .ReportINEndpointNumber = KEYBOARD_EPNUM,
51  .ReportINEndpointSize = KEYBOARD_EPSIZE,
52  .ReportINEndpointDoubleBank = false,
53 
54  .PrevReportINBuffer = PrevKeyboardHIDReportBuffer,
55  .PrevReportINBufferSize = sizeof(PrevKeyboardHIDReportBuffer),
56  .PortNumber = 0,
57  },
58 };
59 
60 /*****************************************************************************
61  * Public types/enumerations/variables
62  ****************************************************************************/
63 
64 /*****************************************************************************
65  * Private functions
66  ****************************************************************************/
67 
68 
69 /* Configures the board hardware and chip peripherals for the demo's
70  functionality */
71 static void SetupHardware(void)
72 {
73  Board_Init();
76  USB_Init(Keyboard_HID_Interface.Config.PortNumber, USB_MODE_Device);
77 #if defined(USB_DEVICE_ROM_DRIVER)
78  UsbdHid_Init();
79 #endif
80 }
81 
82 /*****************************************************************************
83  * Public functions
84  ****************************************************************************/
85 
86 #if defined(USB_DEVICE_ROM_DRIVER)
87 extern void CALLBACK_UsbdHid_SetReportChange(bool newstate);
88 #endif
89 
96 int main(void)
97 {
98  SetupHardware();
99  for (;; ) {
100  #if defined(USB_DEVICE_ROM_DRIVER)
102  uint16_t reportsize;
103  uint8_t reportID = 0;
104 
105  memset(&report, 0, sizeof(USB_KeyboardReport_Data_t));
106  CALLBACK_HID_Device_CreateHIDReport(&Keyboard_HID_Interface,
107  &reportID,
109  &report,
110  &reportsize);
111  if (memcmp(&report, Keyboard_HID_Interface.Config.PrevReportINBuffer,
112  Keyboard_HID_Interface.Config.PrevReportINBufferSize)) {
113  memcpy(Keyboard_HID_Interface.Config.PrevReportINBuffer,
114  &report,
115  Keyboard_HID_Interface.Config.PrevReportINBufferSize);
117  }
118  #else
119  HID_Device_USBTask(&Keyboard_HID_Interface);
120  USB_USBTask(Keyboard_HID_Interface.Config.PortNumber, USB_MODE_Device);
121  #endif
122  }
123 }
124 
125 /* Event handler for the library USB Connection event */
127 {
128 }
129 
130 /* Event handler for the library USB Disconnection event */
132 {
133 }
134 
135 /* Event handler for the library USB Configuration Changed event */
137 {
138  bool ConfigSuccess = true;
139 
140  ConfigSuccess &= HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface);
141 
143 }
144 
145 /* Event handler for the library USB Control Request reception event */
147 {
148  HID_Device_ProcessControlRequest(&Keyboard_HID_Interface);
149 }
150 
151 /* Event handler for the USB device Start Of Frame event */
153 {
154  HID_Device_MillisecondElapsed(&Keyboard_HID_Interface);
155 }
156 
157 /* HID class driver callback function for the creation of HID reports to the host */
158 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t *const HIDInterfaceInfo, uint8_t *const ReportID,
159  const uint8_t ReportType, void *ReportData, uint16_t *const ReportSize)
160 {
162 
163  uint8_t JoyStatus_LCL = Joystick_GetStatus();
164  uint8_t ButtonStatus_LCL = Buttons_GetStatus();
165 
166  uint8_t UsedKeyCodes = 0;
167 
168  if (JoyStatus_LCL & JOY_UP) {
169  KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_A;
170  }
171  else if (JoyStatus_LCL & JOY_DOWN) {
172  KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_B;
173  }
174 
175  if (JoyStatus_LCL & JOY_LEFT) {
176  KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_C;
177  }
178  else if (JoyStatus_LCL & JOY_RIGHT) {
179  KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_D;
180  }
181 
182  if (JoyStatus_LCL & JOY_PRESS) {
183  KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_E;
184  }
185 
186  if (ButtonStatus_LCL & BUTTONS_BUTTON1) {
187  KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_F;
188  }
189 
190  if (UsedKeyCodes) {
191  KeyboardReport->Modifier = HID_KEYBOARD_MODIFER_LEFTSHIFT;
192  }
193 
194  *ReportSize = sizeof(USB_KeyboardReport_Data_t);
195  return false;
196 }
197 
198 /* HID class driver callback function for the processing of HID reports from the host */
200  const uint8_t ReportID,
201  const uint8_t ReportType,
202  const void *ReportData,
203  const uint16_t ReportSize)
204 {
205  uint8_t LEDMask = LEDS_NO_LEDS;
206  uint8_t *LEDReport = (uint8_t *) ReportData;
207 
208  if (*LEDReport & HID_KEYBOARD_LED_NUMLOCK) {
209  LEDMask |= LEDS_LED1;
210  }
211 
212  if (*LEDReport & HID_KEYBOARD_LED_CAPSLOCK) {
213  LEDMask |= LEDS_LED3;
214  }
215 
216  if (*LEDReport & HID_KEYBOARD_LED_SCROLLLOCK) {
217  LEDMask |= LEDS_LED4;
218  }
219 }