8 years, 3 months ago.

USBKeyboard emulator waiting for USB connection based on position of C code on LPC1768

I'm using an LPC1768 to emulate a USB keyboard together with some HID stuff. All well and good and it works fine using the standard USB lines on p31/p32.

However in my real code I have a lot of functions that use keyboard.printf's say. That is above the main().

If I define:

USBKeyboard keyboard;

above main() the functions work fine. I've produced a simple working example below which calls SayHello() and types something out.

1 * The issue is that main() isn't entered into unless the keyboard emulator is plugged in to my PC from pins p31/p32. It just sticks, i.e. it's blocking.

2 * I can resolve this by having the USBKeyboard keyboard def in main() and then doing it from there. However again once activated it stops (blocking) for a USB lead to be plugged into the PC.

Option 2 would be fine as what I really want to do is enable the keyboard HID at will based on a config file on the mbed flash drive letter. BUT I can't do this as I have a ton of functions above main() that use the keyboard which is no longer defined at the top. So it won't compile.

Anyone any ideas as I really don't want to start modifying the USBHID and USBKeyboard code unless I really have to?

Don't forget at the moment none of my code executes until the USB lead is plugged in. There also doesn't seem to be a timeout for non connection to USB in USBKeyboard.h. Or have I missed something...

Richards simple example

#include "mbed.h"
#include "USBKeyboard.h"

USBKeyboard keyboard;
HID_REPORT report;

DigitalOut DebugLED(LED4);

void SayHello()
{
    keyboard.printf("Hello World from Mbed\r\n");    
}

int main()
{                
    DebugLED = 1;
    wait(4);

    //USBKeyboard keyboard;
    //keyboard.printf("Hello World from Mbed\r\n");

    SayHello();
}

Be the first to answer this question.