USBHostKeyboard

Table of Contents

  1. Hello World
  2. API
  3. Related

mbed OS 2

This is the handbook for mbed OS 2.

The USBHostKeyboard interface is used to communicate with a USB keyboard.

Library in Beta!

This library is in Beta. If you have any problems using the USBHost library, please send a bug report at support@mbed.org

The USB Host connector should be attached to

  • p31 (D+), p32 (D-) and GND for the LPC1768
  • add two 15k resistors tied to GND on D+ and D-

Hello World

Import program

00001 #include "mbed.h"
00002 #include "USBHostKeyboard.h"
00003 
00004 DigitalOut led(LED1);
00005 
00006 void onKey(uint8_t key) {
00007     printf("Key: %c\r\n", key);
00008 }
00009 
00010 void keyboard_task(void const *) {
00011     
00012     USBHostKeyboard keyboard;
00013     
00014     while(1) {
00015         // try to connect a USB keyboard
00016         while(!keyboard.connect())
00017             Thread::wait(500);
00018     
00019         // when connected, attach handler called on keyboard event
00020         keyboard.attach(onKey);
00021         
00022         // wait until the keyboard is disconnected
00023         while(keyboard.connected())
00024             Thread::wait(500);
00025     }
00026 }
00027 
00028 int main() {
00029     Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4);
00030     while(1) {
00031         led=!led;
00032         Thread::wait(500);
00033     }
00034 }

API

Import library

Public Member Functions

  USBHostKeyboard ()
  Constructor.
bool  connect ()
  Try to connect a keyboard device.
bool  connected ()
  Check if a keyboard is connected.
void  attach (void(*ptr)(uint8_t key))
  Attach a callback called when a keyboard event is received.
void  attach (void(*ptr)(uint8_t keyCode, uint8_t modifier))
  Attach a callback called when a keyboard event is received.

Troobleshooting

If your mbed board is automatically resetted when you plug a USB device, you should consider to use an external power supply