USD HID

07 Feb 2013

I have an application using mBed LPC1768 which uses USB HID. Actually I have two mBeds connected to 1 PC. Both devices use the same Vid (Vendor ID), but use different Pid (Product ID).

My problem is I need the ability to check an input on the mBed to determine which Pid to use with the USB constructor.

For example, pseudo code:

if (input ==1) {
	USBHID hid(Vid, PID1);   //Use PID1
}
else {
	USBHID hid(vid, PID2);  //Use PID2
}

But the USB constructor is called before Main(). Is there any way to check an input before calling the USB constructor?

Thanks,

Mike

07 Feb 2013

Hi Mike,

You could do something like this inside your main function (don't forget that the first two arguments in the constructor are the lengths of output/input reports (default = 64bytes)):

pid = (input == 1) ? PID1 : PID2;
USBHID hid(64, 64, vid, pid);

Cheers, Sam

07 Feb 2013

Hi Sam -

I thought the constructor needed to be outside the main function? I tried to move it into the main but was getting compile errors do to the scope of the HID and the reports. The HID is used in several functions.

Is it possible to put these in the main function and still be able to access them in other functions?

Thanks for the help,

Mike

07 Feb 2013

Hi Mike,

Quote:

I thought the constructor needed to be outside the main function?

Not necessarily as soon as the USBHID variable is only used inside this function.

Quote:

Is it possible to put these in the main function and still be able to access them in other functions?

Yep, two possibilities:

  • using dynamic allocation (new):

USBHID * hid;

int main(void) {
   pid = (input == 1) ? PID1 : PID2;
   hid = new USBHID(64, 64, vid, pid);
}

In this case hid is a pointer. You will be able to call the USBHID methods like:

hid->send(&send_report);
  • Pass a USBHID pointer to the function that needs a ref to the USBHID object:

void func(USBHID * hid) {
   hid->send(&send_report);
}

int main(void) {
   pid = (input == 1) ? PID1 : PID2;
   USBHID hid(64, 64, vid, pid);
   func(&hid);
}

Hope that helps, Sam

07 Feb 2013

Wow that actualy looks simple,

:) Ceri

07 Feb 2013

Hi Sam -

Thank you so much!

I used the dynamic allocation and it works great!

Thanks,

Mike

15 Jul 2013

I have been trying to get USB HID to work on a project,

The HID part works OK, but when no PC is connected, my program does not run.

I need the MBED to run off batterys, doing some logging (works well)

but I need to be able to plug into PC, and use HID, from NEVER to occasionaly to perminant (almost)

My application cannot go throuh a reset. so I would also like to know if connected,

Poleing would be fine.

Cheers in advance

Lex.

15 Jul 2013

Without much USB experience I wouldn't know if there is a special function for it. But a partial option would be connecting the power line of the USB connector to an input pin. Enable pull-down, if it reads as 1 it is at least plugged in.