DJT

Dependencies:   mbed USBHost

Committer:
yutation
Date:
Tue Sep 21 22:56:51 2021 +0000
Revision:
0:1668dc5479ac
USB_keyboard_DJT;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yutation 0:1668dc5479ac 1 #include "mbed.h"
yutation 0:1668dc5479ac 2 #include "USBHostKeyboard.h"
yutation 0:1668dc5479ac 3
yutation 0:1668dc5479ac 4 DigitalOut led(LED1);
yutation 0:1668dc5479ac 5
yutation 0:1668dc5479ac 6 void onKey(uint8_t key) {
yutation 0:1668dc5479ac 7 printf("Key: %c\r\n", key);
yutation 0:1668dc5479ac 8 }
yutation 0:1668dc5479ac 9
yutation 0:1668dc5479ac 10 void keyboard_task(void const *) {
yutation 0:1668dc5479ac 11
yutation 0:1668dc5479ac 12 USBHostKeyboard keyboard;
yutation 0:1668dc5479ac 13
yutation 0:1668dc5479ac 14 while(1) {
yutation 0:1668dc5479ac 15 // try to connect a USB keyboard
yutation 0:1668dc5479ac 16 while(!keyboard.connect())
yutation 0:1668dc5479ac 17 Thread::wait(500);
yutation 0:1668dc5479ac 18
yutation 0:1668dc5479ac 19 // when connected, attach handler called on keyboard event
yutation 0:1668dc5479ac 20 keyboard.attach(onKey);
yutation 0:1668dc5479ac 21
yutation 0:1668dc5479ac 22 // wait until the keyboard is disconnected
yutation 0:1668dc5479ac 23 while(keyboard.connected())
yutation 0:1668dc5479ac 24 Thread::wait(500);
yutation 0:1668dc5479ac 25 }
yutation 0:1668dc5479ac 26 }
yutation 0:1668dc5479ac 27
yutation 0:1668dc5479ac 28 int main() {
yutation 0:1668dc5479ac 29 Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4);
yutation 0:1668dc5479ac 30 while(1) {
yutation 0:1668dc5479ac 31 led=!led;
yutation 0:1668dc5479ac 32 Thread::wait(500);
yutation 0:1668dc5479ac 33 }
yutation 0:1668dc5479ac 34 }