LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Mouse.c
Go to the documentation of this file.
1 /*
2  * @brief Make your board becomes a USB mouse
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 "Mouse.h"
34 
35 /*****************************************************************************
36  * Private types/enumerations/variables
37  ****************************************************************************/
38 
41 
47  .Config = {
48  .InterfaceNumber = 0,
49 
50  .ReportINEndpointNumber = MOUSE_EPNUM,
51  .ReportINEndpointSize = MOUSE_EPSIZE,
52  .ReportINEndpointDoubleBank = false,
53 
54  .PrevReportINBuffer = PrevMouseHIDReportBuffer,
55  .PrevReportINBufferSize = sizeof(PrevMouseHIDReportBuffer),
56  .PortNumber = 0,
57  },
58 };
59 
60 /*****************************************************************************
61  * Public types/enumerations/variables
62  ****************************************************************************/
63 
64 /*****************************************************************************
65  * Private functions
66  ****************************************************************************/
67 
72 static void SetupHardware(void)
73 {
74  Board_Init();
77  USB_Init(Mouse_HID_Interface.Config.PortNumber, USB_MODE_Device);
78 #if defined(USB_DEVICE_ROM_DRIVER)
79  UsbdHid_Init();
80 #endif
81 }
82 
83 /*****************************************************************************
84  * Public functions
85  ****************************************************************************/
86 
87 #if defined(USB_DEVICE_ROM_DRIVER)
88 extern void CALLBACK_UsbdHid_SetReportChange(bool newstate);
89 #endif
90 
97 int main(void)
98 {
99  SetupHardware();
100 
101  for (;; ) {
102  #if defined(USB_DEVICE_ROM_DRIVER)
103  USB_MouseReport_Data_t report;
104  uint16_t reportsize;
105  uint8_t reportID = 0;
106 
107  memset(&report, 0, sizeof(USB_MouseReport_Data_t));
108  CALLBACK_HID_Device_CreateHIDReport(&Mouse_HID_Interface, &reportID, HID_REPORT_ITEM_In, &report, &reportsize);
109  if (memcmp(&report, Mouse_HID_Interface.Config.PrevReportINBuffer,
110  Mouse_HID_Interface.Config.PrevReportINBufferSize)) {
111  memcpy(Mouse_HID_Interface.Config.PrevReportINBuffer,
112  &report,
113  Mouse_HID_Interface.Config.PrevReportINBufferSize);
115  }
116  #else
117  HID_Device_USBTask(&Mouse_HID_Interface);
118  USB_USBTask(Mouse_HID_Interface.Config.PortNumber, USB_MODE_Device);
119  #endif
120  }
121 }
122 
128 {
129 }
130 
136 {}
137 
143 {
144  bool ConfigSuccess = true;
145 
146  ConfigSuccess &= HID_Device_ConfigureEndpoints(&Mouse_HID_Interface);
147 
149 }
150 
156 {
157  HID_Device_ProcessControlRequest(&Mouse_HID_Interface);
158 }
159 
165 {
166  HID_Device_MillisecondElapsed(&Mouse_HID_Interface);
167 }
168 
169 /* HID class driver callback function for the creation of HID reports to the host */
171  uint8_t *const ReportID,
172  const uint8_t ReportType,
173  void *ReportData,
174  uint16_t *const ReportSize)
175 {
177 
178  uint8_t JoyStatus_LCL = Joystick_GetStatus();
179  uint8_t ButtonStatus_LCL = Buttons_GetStatus();
180  bool press = false;
181 
182  if (JoyStatus_LCL & JOY_UP) {
183  MouseReport->Y = -1;
184  press = true;
185  }
186  else if (JoyStatus_LCL & JOY_DOWN) {
187  MouseReport->Y = 1;
188  press = true;
189  }
190 
191  if (JoyStatus_LCL & JOY_LEFT) {
192  MouseReport->X = -1;
193  press = true;
194  }
195  else if (JoyStatus_LCL & JOY_RIGHT) {
196  MouseReport->X = 1;
197  press = true;
198  }
199 
200  if (JoyStatus_LCL & JOY_PRESS) {
201  MouseReport->Button |= (1 << 0);
202  press = true;
203  }
204 
205  if (ButtonStatus_LCL & BUTTONS_BUTTON1) {
206  MouseReport->Button |= (1 << 1);
207  press = true;
208  }
209 
210  *ReportSize = sizeof(USB_MouseReport_Data_t);
211  return press;
212 }
213 
214 /* HID class driver callback function for the processing of HID reports from the host */
216  const uint8_t ReportID,
217  const uint8_t ReportType,
218  const void *ReportData,
219  const uint16_t ReportSize)
220 {
221  /* Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports */
222 }