Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
usb_host/main.cpp@0:6a73d3dc037e, 2014-07-28 (annotated)
- Committer:
- ShawnHymel
- Date:
- Mon Jul 28 20:29:28 2014 +0000
- Revision:
- 0:6a73d3dc037e
Added main.cpp files from each of the demo programs. Libraries are not included.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ShawnHymel | 0:6a73d3dc037e | 1 | // USB host keyboard and LCD demo |
| ShawnHymel | 0:6a73d3dc037e | 2 | |
| ShawnHymel | 0:6a73d3dc037e | 3 | #include "mbed.h" |
| ShawnHymel | 0:6a73d3dc037e | 4 | #include "USBHostKeyboard.h" |
| ShawnHymel | 0:6a73d3dc037e | 5 | #include "uLCD_4DGL.h" |
| ShawnHymel | 0:6a73d3dc037e | 6 | |
| ShawnHymel | 0:6a73d3dc037e | 7 | // LED to demonstrate multi-threading |
| ShawnHymel | 0:6a73d3dc037e | 8 | DigitalOut led(LED1); |
| ShawnHymel | 0:6a73d3dc037e | 9 | |
| ShawnHymel | 0:6a73d3dc037e | 10 | // Graphic LCD - TX, RX, and RES pins |
| ShawnHymel | 0:6a73d3dc037e | 11 | uLCD_4DGL uLCD(p9,p10,p11); |
| ShawnHymel | 0:6a73d3dc037e | 12 | |
| ShawnHymel | 0:6a73d3dc037e | 13 | // Callback function from thread |
| ShawnHymel | 0:6a73d3dc037e | 14 | void onKey(uint8_t key) { |
| ShawnHymel | 0:6a73d3dc037e | 15 | uLCD.printf("%c", key); |
| ShawnHymel | 0:6a73d3dc037e | 16 | } |
| ShawnHymel | 0:6a73d3dc037e | 17 | |
| ShawnHymel | 0:6a73d3dc037e | 18 | // Function that runs continuously in the thread |
| ShawnHymel | 0:6a73d3dc037e | 19 | void keyboard_task(void const *) { |
| ShawnHymel | 0:6a73d3dc037e | 20 | |
| ShawnHymel | 0:6a73d3dc037e | 21 | USBHostKeyboard keyboard; |
| ShawnHymel | 0:6a73d3dc037e | 22 | |
| ShawnHymel | 0:6a73d3dc037e | 23 | while(1) { |
| ShawnHymel | 0:6a73d3dc037e | 24 | |
| ShawnHymel | 0:6a73d3dc037e | 25 | // Try to connect a USB keyboard |
| ShawnHymel | 0:6a73d3dc037e | 26 | uLCD.printf("Waiting...\n"); |
| ShawnHymel | 0:6a73d3dc037e | 27 | while(!keyboard.connect()) { |
| ShawnHymel | 0:6a73d3dc037e | 28 | Thread::wait(500); |
| ShawnHymel | 0:6a73d3dc037e | 29 | } |
| ShawnHymel | 0:6a73d3dc037e | 30 | uLCD.printf("Connected!\n"); |
| ShawnHymel | 0:6a73d3dc037e | 31 | |
| ShawnHymel | 0:6a73d3dc037e | 32 | // When connected, attach handler called on keyboard event |
| ShawnHymel | 0:6a73d3dc037e | 33 | keyboard.attach(onKey); |
| ShawnHymel | 0:6a73d3dc037e | 34 | |
| ShawnHymel | 0:6a73d3dc037e | 35 | // Wait until the keyboard is disconnected |
| ShawnHymel | 0:6a73d3dc037e | 36 | while(keyboard.connected()) { |
| ShawnHymel | 0:6a73d3dc037e | 37 | Thread::wait(500); |
| ShawnHymel | 0:6a73d3dc037e | 38 | } |
| ShawnHymel | 0:6a73d3dc037e | 39 | uLCD.printf("\nDisconnected!\n"); |
| ShawnHymel | 0:6a73d3dc037e | 40 | } |
| ShawnHymel | 0:6a73d3dc037e | 41 | } |
| ShawnHymel | 0:6a73d3dc037e | 42 | |
| ShawnHymel | 0:6a73d3dc037e | 43 | // Main - the program enters here |
| ShawnHymel | 0:6a73d3dc037e | 44 | int main() { |
| ShawnHymel | 0:6a73d3dc037e | 45 | |
| ShawnHymel | 0:6a73d3dc037e | 46 | // Initialize LCD |
| ShawnHymel | 0:6a73d3dc037e | 47 | uLCD.baudrate(115200); |
| ShawnHymel | 0:6a73d3dc037e | 48 | uLCD.background_color(BLACK); |
| ShawnHymel | 0:6a73d3dc037e | 49 | uLCD.cls(); |
| ShawnHymel | 0:6a73d3dc037e | 50 | uLCD.locate(0,0); |
| ShawnHymel | 0:6a73d3dc037e | 51 | |
| ShawnHymel | 0:6a73d3dc037e | 52 | // Create a thread that runs a function (keyboard_task) |
| ShawnHymel | 0:6a73d3dc037e | 53 | Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4); |
| ShawnHymel | 0:6a73d3dc037e | 54 | |
| ShawnHymel | 0:6a73d3dc037e | 55 | // Flash an LED forever |
| ShawnHymel | 0:6a73d3dc037e | 56 | while(1) { |
| ShawnHymel | 0:6a73d3dc037e | 57 | led=!led; |
| ShawnHymel | 0:6a73d3dc037e | 58 | Thread::wait(500); |
| ShawnHymel | 0:6a73d3dc037e | 59 | } |
| ShawnHymel | 0:6a73d3dc037e | 60 | } |