Embedded Artists
We are the leading providers of products and services around prototyping, evaluation and OEM platforms using NXP's ARM-based microcontrollers.
LPC4088DM Using USBDevice
The display modules can be used in either USB Host or USB Device mode.
The DMSupport library contains mbed's official USBDevice library.
The USB Device stack contains support for the following USB classes:
Class Name | Status |
---|---|
Audio | Untested & Unsupported |
HID | Tested OK as both mouse and keyboard |
MIDI | Untested & Unsupported |
MSD (MassStorage) | Tested OK |
Serial | Tested OK |
Using USBDevice¶
Unlike the USB Host stack there is no way to get notified when the display module has been connected to an external host (e.g. a PC). This means that the user program either have to keep attempting to connect over and over until successful or only attempt after being instructed by an external action like the user pressing a physical button or interacting with the touch controller.
For the mouse, keyboard and serial classes this is no problem. They can be used as if they are connected to the PC even if they are not. All data sent to the class is discarded while not connected.
USB Mass Storage¶
See this wiki page describing how to use USB MassStorage.
USB Keyboard and Mouse¶
The HID class (Human Interface Device) in the USB Device stack allows the display module to appear as a mouse and/or keyboard when connected to e.g. a PC. The USBDevice library has different classes depending on what you want to do:
Class Name | Description |
---|---|
USBMouse | Only USB mouse |
USBKeyboard | Only USB keyboard |
USBMouseKeyboard | Both mouse and keyboard |
It is not possible to use both USBMouse and USBKeyboard in the same program.
The USBMouse class can be set up to use either relative or absolute coordinates.
The example below shows how to use the touch display to move the cursor on the PC's monitor. It is based on the code for reading touch events that you find here. The example uses relative coordinates (as the PC monitor has much higher resolution than the display module) and this requires an extra set of touch coordinates to calculate the mouse data:
#include "mbed.h" #include "DMBoard.h" #include "USBMouse.h" // A selected signal ID. Can be any 2^x that the sender and receiver agrees upon #define MY_SIGID_TOUCH (1<<0) void MyClass::onTouchEvent() { osSignalSet(_threadId, MY_SIGID_TOUCH); } void MyClass::doStuff() { // save this thread's identifier so that it may be signalled _threadId = osThreadGetId(); // register a listener for touch events FunctionPointer* fp = new FunctionPointer(this, &MyClass::onTouchEvent()); TouchPanel* touch = DMBoard::instance().touchPanel(); touch->setListener(fp); // initialize the mouse class as a USBMouse mouse(REL_MOUSE); // last coordinates as well as a place to store current and old coordinates int set = 0; touch_coordinate_t coords[2]; touch_coordinate_t* last = NULL; touch_coordinate_t* current = &coords[set]; // wait for touch events while (true) { Thread::signal_wait(MY_SIGID_TOUCH); if (touch->read(current, 1) != TouchPanel::TouchError_Ok) { // failed to read the coordinates continue; } if (current->z == 0) { // lifted the finger so we have to think of the next touch // event as the first one (otherwise the relative movement // will be wrong) last = NULL; } if (last != NULL) { // calculate the relative movement since last touch event // and only report it if there has been a change, to avoid // spamming messages. int moveX = current->x - last->x; int moveY = current->y - last->y; if (moveX != 0 && moveY != 0) { mouse.move(current->x - last->x, current->y - last->y); } } last = current; current = &coords[(++set)&1]; } }
The example for USBKeyboard will send a string of characters to the PC every time the USER button on the back of the display module is pressed. Make sure that you have a text editor started an active on the PC before clicking the USER button.
#include "mbed.h" #include "DMBoard.h" #include "USBKeyboard.h" void doStuff() { // initialize the USB class USBKeyboard keyb; while(true) { // wait for button to be pressed down while (!board->buttonPressed()) { Thread::wait(50); } // send the simulated key presses keyb.printf("Hello World!\n"); // wait for button to be released again while (board->buttonPressed()) { Thread::wait(50); } } }
Warning
The USBMouse class can send "mouse clicks" but make absolutely sure that you know how your program works and where those clicks are going before enabling it. If this is not carefully considered you could end up with a program that maybe closes your browser or shuts down your computer.
Warning
The USBKeyboard class acts as a keyboard and all sent characters are directed to the currently selected window on the PC. Make sure that the correct window is selected so that you don't accidentaly send emails or overwrite important documents.
USB Serial¶
The USB Device stack has a USBSerial class to emulate a serial port over USB. You can use this serial port as an extra serial port or as a debug solution.
The RtosLog
class already has support for this, so by enabling the following in dm_board_config.h:
#define DM_BOARD_USE_USBSERIAL_IN_RTOSLOG
the RtosLog will use the USB Device Serial port to send messages instead of the default serial port. This is very useful when debugging using an external debug probe instead of the onboard CMSIS-DAP interface as the normal serial port is disabled then.
An alternative is to use the USBSerial port directly in your program like this:
#include "mbed.h" #include "DMBoard.h" #include "USBSerial.h" void foo() { USBSerial s; int counter = 0; while(true) { Thread::wait(1000); s.printf("Counter %3d\n", counter++); } }
Note
- Information and examples for the USBSerial port
- To use the port you have to install drivers on the PC. Information about that can be found here
- After resetting the board (e.g. after flashing a new program or pressing the RESET button) the USB cable must be disconnected and connected again for the PC to reinitialize the serial port. If this is not done then nothing is received on the PC side.