diff --git a/examples/host/cdc_msc_hid/src/hid_app.c b/examples/host/cdc_msc_hid/src/hid_app.c index 11437c2b..5e85898d 100644 --- a/examples/host/cdc_msc_hid/src/hid_app.c +++ b/examples/host/cdc_msc_hid/src/hid_app.c @@ -26,6 +26,9 @@ #include "bsp/board.h" #include "tusb.h" +void _notprintf(); +#define printf if (0) _notprintf + //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION //--------------------------------------------------------------------+ @@ -36,7 +39,112 @@ #define MAX_REPORT 4 -static uint8_t const keycode2ascii[128][2] = { HID_KEYCODE_TO_ASCII }; +#define HID_KEYCODE_TO_VECTOR4 \ + {0 , 0 }, /* 0x00 */ \ + {0 , 0 }, /* 0x01 */ \ + {0 , 0 }, /* 0x02 */ \ + {0 , 0 }, /* 0x03 */ \ + {'a' , 'A' , 0x01 , 0x01 }, /* 0x04 */ \ + {'b' , 'B' , 0x02 , 0x02 }, /* 0x05 */ \ + {'c' , 'C' , 0x03 , 0x03 }, /* 0x06 */ \ + {'d' , 'D' , 0x04 , 0x04 }, /* 0x07 */ \ + {'e' , 'E' , 0x05 , 0x05 }, /* 0x08 */ \ + {'f' , 'F' , 0x06 , 0x06 }, /* 0x09 */ \ + {'g' , 'G' , 0x07 , 0x07 }, /* 0x0a */ \ + {'h' , 'H' , 0x08 , 0x08 }, /* 0x0b */ \ + {'i' , 'I' , 0x09 , 0x09 }, /* 0x0c */ \ + {'j' , 'J' , 0x0A , 0x0A }, /* 0x0d */ \ + {'k' , 'K' , 0x0B , 0x0B }, /* 0x0e */ \ + {'l' , 'L' , 0x0C , 0x0C }, /* 0x0f */ \ + {'m' , 'M' , 0x0D , 0x0D }, /* 0x10 */ \ + {'n' , 'N' , 0x0E , 0x0E }, /* 0x11 */ \ + {'o' , 'O' , 0x0F , 0x0F }, /* 0x12 */ \ + {'p' , 'P' , 0x10 , 0x10 }, /* 0x13 */ \ + {'q' , 'Q' , 0x11 , 0x11 }, /* 0x14 */ \ + {'r' , 'R' , 0x12 , 0x12 }, /* 0x15 */ \ + {'s' , 'S' , 0x13 , 0x13 }, /* 0x16 */ \ + {'t' , 'T' , 0x14 , 0x14 }, /* 0x17 */ \ + {'u' , 'U' , 0x15 , 0x15 }, /* 0x18 */ \ + {'v' , 'V' , 0x16 , 0x16 }, /* 0x19 */ \ + {'w' , 'W' , 0x17 , 0x17 }, /* 0x1a */ \ + {'x' , 'X' , 0x18 , 0x18 }, /* 0x1b */ \ + {'y' , 'Y' , 0x19 , 0x19 }, /* 0x1c */ \ + {'z' , 'Z' , 0x1A , 0x1A }, /* 0x1d */ \ + {'1' , '!' , '1' , '!' }, /* 0x1e */ \ + {'2' , '@' , '2' , '@' }, /* 0x1f */ \ + {'3' , '#' , '3' , '#' }, /* 0x20 */ \ + {'4' , '$' , '4' , '$' }, /* 0x21 */ \ + {'5' , '%' , '5' , '%' }, /* 0x22 */ \ + {'6' , '^' , '6' , '^' }, /* 0x23 */ \ + {'7' , '&' , '7' , '&' }, /* 0x24 */ \ + {'8' , '*' , '8' , '*' }, /* 0x25 */ \ + {'9' , '(' , '9' , '(' }, /* 0x26 */ \ + {'0' , ')' , '0' , ')' }, /* 0x27 */ \ + {'\r' , '\r' , '\r' , '\r' }, /* 0x28 */ \ + {'\x1b', '\x1b', '\x1b', '\x1b' }, /* 0x29 */ \ + {'\b' , '\b' , '\b' , '\b' }, /* 0x2a */ \ + {'\t' , 0x1D , '\t' , 0x1D }, /* 0x2b */ \ + {' ' , ' ' , ' ' , ' ' }, /* 0x2c */ \ + {'-' , '_' , '-' , 0x1F }, /* 0x2d */ \ + {'=' , '+' , '=' , '+' }, /* 0x2e */ \ + {'[' , '{' , 0x1B , 0x1B }, /* 0x2f */ \ + {']' , '}' , 0x1D , 0x1D }, /* 0x30 */ \ + {'\\' , '|' , 0x1C , 0x1C }, /* 0x31 */ \ + {'#' , '~' , 0 , 0 }, /* 0x32 */ \ + {';' , ':' , ';' , ':' }, /* 0x33 */ \ + {'\'' , '\"' , '\'' , '\"' }, /* 0x34 */ \ + {'`' , '~' , '`' , '~' }, /* 0x35 */ \ + {',' , '<' , ',' , '<' }, /* 0x36 */ \ + {'.' , '>' , '.' , '>' }, /* 0x37 */ \ + {'/' , '?' , '/' , '?' }, /* 0x38 */ \ + \ + {0 , 0 }, /* 0x39 */ \ + {0xC1 , 0xD1 , 0xE1 , 0xF1 }, /* 0x3a F1 */ \ + {0xC2 , 0xD2 , 0xE2 , 0xF2 }, /* 0x3b F2 */ \ + {0xC3 , 0xD3 , 0xE3 , 0xF3 }, /* 0x3c F3 */ \ + {0xC4 , 0xD4 , 0xE4 , 0xF4 }, /* 0x3d F4 */ \ + {0xC5 , 0xD5 , 0xE5 , 0xF5 }, /* 0x3e F5 */ \ + {0xC6 , 0xD6 , 0xE6 , 0xF6 }, /* 0x3f F6 */ \ + {0xC7 , 0xD7 , 0xE7 , 0xF7 }, /* 0x40 F7 */ \ + {0xC8 , 0xD8 , 0xE8 , 0xF8 }, /* 0x41 F8 */ \ + {0xC9 , 0xD9 , 0xE9 , 0xF9 }, /* 0x42 F9 */ \ + {0xCA , 0xDA , 0xEA , 0xFA }, /* 0x43 F10 */ \ + {0xCB , 0xDB , 0xEB , 0xFB }, /* 0x44 F11 */ \ + {0xCC , 0xDC , 0xEC , 0xFC }, /* 0x45 F12 */ \ + {0 , 0 }, /* 0x46 */ \ + {0 , 0 }, /* 0x47 */ \ + {0 , 0 }, /* 0x48 */ \ + {0 , 0 }, /* 0x49 */ \ + {0 , 0 }, /* 0x4a */ \ + {0 , 0 }, /* 0x4b */ \ + {0x7F , 0x7F , 0x7F , 0x7F }, /* 0x4c Delete Forward */ \ + {0 , 0 }, /* 0x4d */ \ + {0 , 0 }, /* 0x4e */ \ + {0x8D , 0x9D , 0xAD , 0xBD }, /* 0x4f RightArrow */ \ + {0x8C , 0x9C , 0xAC , 0xBC }, /* 0x50 LeftArrow */ \ + {0x8B , 0x9B , 0xAB , 0xBB }, /* 0x51 DownArrow */ \ + {0x8A , 0x9A , 0xAA , 0xBA }, /* 0x52 UpArrow */ \ + {0 , 0 }, /* 0x53 */ \ + \ + {'/' , '/' }, /* 0x54 / 10-key */ \ + {'*' , '*' }, /* 0x55 * */ \ + {0x8E , 0x9E , 0xAE , 0xBE }, /* 0x56 - */ \ + {'+' , '+' }, /* 0x57 + */ \ + {0xA0 , 0xB0 , 0xA0 , 0xB0 }, /* 0x58 Enter */ \ + {0x81 , 0x91 , 0xA1 , 0xA1 }, /* 0x59 1 */ \ + {0x82 , 0x92 , 0xA2 , 0xA2 }, /* 0x5a 2 */ \ + {0x83 , 0x93 , 0xA3 , 0xA3 }, /* 0x5b 3 */ \ + {0x84 , 0x94 , 0xA4 , 0xA4 }, /* 0x5c 4 */ \ + {0x85 , 0x95 , 0xA5 , 0xA5 }, /* 0x5d 5 */ \ + {0x86 , 0x96 , 0xA6 , 0xA6 }, /* 0x5e 6 */ \ + {0x87 , 0x97 , 0xA7 , 0xA7 }, /* 0x5f 7 */ \ + {0x88 , 0x98 , 0xA8 , 0xA8 }, /* 0x60 8 */ \ + {0x89 , 0x99 , 0xA9 , 0xA9 }, /* 0x61 9 */ \ + {0x80 , 0x90 , 0x80 , 0x90 }, /* 0x62 0 */ \ + {0xCF , 0xDF , 0xEF , 0xEF }, /* 0x63 . */ \ + {'=' , '=' }, /* 0x67 */ \ + +static uint8_t const keycode2ascii[128][4] = { HID_KEYCODE_TO_VECTOR4 }; // Each HID instance can has multiple reports static struct @@ -145,10 +253,13 @@ static void process_kbd_report(hid_keyboard_report_t const *report) static hid_keyboard_report_t prev_report = { 0, 0, {0} }; // previous report to check key released //------------- example code ignore control (non-printable) key affects -------------// + uint8_t a_key_is_pressed = 0; for(uint8_t i=0; i<6; i++) { if ( report->keycode[i] ) { + board_led_write(1); + a_key_is_pressed = 1; if ( find_key_in_report(&prev_report, report->keycode[i]) ) { // exist in previous report means the current key is holding @@ -156,15 +267,18 @@ static void process_kbd_report(hid_keyboard_report_t const *report) { // not existed in previous report means the current key is pressed bool const is_shift = report->modifier & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT); - uint8_t ch = keycode2ascii[report->keycode[i]][is_shift ? 1 : 0]; + bool const is_ctrl = report->modifier & (KEYBOARD_MODIFIER_LEFTCTRL | KEYBOARD_MODIFIER_RIGHTCTRL); + uint8_t ch = keycode2ascii[report->keycode[i]][(is_shift ? 1 : 0) + (is_ctrl ? 2 : 0)]; putchar(ch); - if ( ch == '\r' ) putchar('\n'); // added new line for enter key + //if ( ch == '\r' ) putchar('\n'); // added new line for enter key fflush(stdout); // flush right away, else nanolib will wait for newline } } // TODO example skips key released } + if (!a_key_is_pressed) + board_led_write(0); prev_report = *report; } diff --git a/examples/host/cdc_msc_hid/src/main.c b/examples/host/cdc_msc_hid/src/main.c index a14be05e..afdd5b36 100644 --- a/examples/host/cdc_msc_hid/src/main.c +++ b/examples/host/cdc_msc_hid/src/main.c @@ -38,6 +38,9 @@ void led_blinking_task(void); extern void cdc_task(void); extern void hid_app_task(void); +void _notprintf(); +#define printf if (0) _notprintf + /*------------- MAIN -------------*/ int main(void) { @@ -46,12 +49,13 @@ int main(void) printf("TinyUSB Host CDC MSC HID Example\r\n"); tusb_init(); + board_led_write(1); // Signal powered on while (1) { // tinyusb host task tuh_task(); - led_blinking_task(); + //led_blinking_task(); #if CFG_TUH_CDC cdc_task(); diff --git a/hw/bsp/board.h b/hw/bsp/board.h index 782e0939..26d343be 100644 --- a/hw/bsp/board.h +++ b/hw/bsp/board.h @@ -41,7 +41,7 @@ #include "ansi_escape.h" #include "tusb.h" -#define CFG_BOARD_UART_BAUDRATE 115200 +#define CFG_BOARD_UART_BAUDRATE 300 //--------------------------------------------------------------------+ // Board Porting API