|
| 1 | +/* |
| 2 | + _______ _ _ _____ ____ |
| 3 | + |__ __| | | | |/ ____| _ \ |
| 4 | + | | ___ ___ _ __ _ _| | | | (___ | |_) | |
| 5 | + | |/ _ \/ _ \ '_ \| | | | | | |\___ \| _ < |
| 6 | + | | __/ __/ | | | |_| | |__| |____) | |_) | |
| 7 | + |_|\___|\___|_| |_|\__, |\____/|_____/|____/ |
| 8 | + __/ | |
| 9 | + |___/ |
| 10 | +
|
| 11 | + TeenyUSB - light weight usb stack for STM32 micro controllers |
| 12 | +
|
| 13 | + Copyright (c) 2019 XToolBox - admin@xtoolbox.org |
| 14 | + www.tusb.org |
| 15 | +
|
| 16 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 17 | + of this software and associated documentation files (the "Software"), to deal |
| 18 | + in the Software without restriction, including without limitation the rights |
| 19 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 20 | + copies of the Software, and to permit persons to whom the Software is |
| 21 | + furnished to do so, subject to the following conditions: |
| 22 | +
|
| 23 | + The above copyright notice and this permission notice shall be included in all |
| 24 | + copies or substantial portions of the Software. |
| 25 | +
|
| 26 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 27 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 28 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 29 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 30 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 31 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 32 | + SOFTWARE. |
| 33 | +*/ |
| 34 | + |
| 35 | +#include "USBHost.h" |
| 36 | + |
| 37 | +USBHost usb; |
| 38 | + |
| 39 | +static int process_key(tusbh_ep_info_t* ep, const uint8_t* key); |
| 40 | + |
| 41 | +static const tusbh_boot_key_class_t cls_boot_key = { |
| 42 | + .backend = &tusbh_boot_keyboard_backend, |
| 43 | + //.on_key = process_key |
| 44 | +}; |
| 45 | + |
| 46 | +static const tusbh_boot_mouse_class_t cls_boot_mouse = { |
| 47 | + .backend = &tusbh_boot_mouse_backend, |
| 48 | + // .on_mouse = process_mouse |
| 49 | +}; |
| 50 | + |
| 51 | +static const tusbh_hid_class_t cls_hid = { |
| 52 | + .backend = &tusbh_hid_backend, |
| 53 | + //.on_recv_data = process_hid_recv, |
| 54 | + //.on_send_done = process_hid_sent, |
| 55 | +}; |
| 56 | + |
| 57 | +static const tusbh_hub_class_t cls_hub = { |
| 58 | + .backend = &tusbh_hub_backend, |
| 59 | +}; |
| 60 | + |
| 61 | +static const tusbh_vendor_class_t cls_vendor = { |
| 62 | + .backend = &tusbh_vendor_backend, |
| 63 | + //.transfer_done = process_vendor_xfer_done |
| 64 | +}; |
| 65 | + |
| 66 | +int msc_ff_mount(tusbh_interface_t* interface, int max_lun, const tusbh_block_info_t* blocks); |
| 67 | +int msc_ff_unmount(tusbh_interface_t* interface); |
| 68 | + |
| 69 | +static const tusbh_msc_class_t cls_msc_bot = { |
| 70 | + .backend = &tusbh_msc_bot_backend, |
| 71 | + // .mount = msc_ff_mount, |
| 72 | + // .unmount = msc_ff_unmount, |
| 73 | +}; |
| 74 | + |
| 75 | +static const tusbh_cdc_acm_class_t cls_cdc_acm = { |
| 76 | + .backend = &tusbh_cdc_acm_backend, |
| 77 | +}; |
| 78 | + |
| 79 | +static const tusbh_cdc_rndis_class_t cls_cdc_rndis = { |
| 80 | + .backend = &tusbh_cdc_rndis_backend, |
| 81 | +}; |
| 82 | + |
| 83 | +static const tusbh_class_reg_t class_table[] = { |
| 84 | + (tusbh_class_reg_t)&cls_boot_key, |
| 85 | + (tusbh_class_reg_t)&cls_boot_mouse, |
| 86 | + (tusbh_class_reg_t)&cls_hub, |
| 87 | + (tusbh_class_reg_t)&cls_msc_bot, |
| 88 | + (tusbh_class_reg_t)&cls_cdc_acm, |
| 89 | + (tusbh_class_reg_t)&cls_cdc_rndis, |
| 90 | + (tusbh_class_reg_t)&cls_hid, |
| 91 | + (tusbh_class_reg_t)&cls_vendor, |
| 92 | + 0, |
| 93 | +}; |
| 94 | + |
| 95 | +void setup() |
| 96 | +{ |
| 97 | + Serial1.begin(115200); |
| 98 | + usb.Init(class_table); |
| 99 | +} |
| 100 | + |
| 101 | +void loop() { |
| 102 | + usb.Task(); |
| 103 | +} |
| 104 | + |
| 105 | +#define MOD_CTRL (0x01 | 0x10) |
| 106 | +#define MOD_SHIFT (0x02 | 0x20) |
| 107 | +#define MOD_ALT (0x04 | 0x40) |
| 108 | +#define MOD_WIN (0x08 | 0x80) |
| 109 | + |
| 110 | +#define LED_NUM_LOCK 1 |
| 111 | +#define LED_CAPS_LOCK 2 |
| 112 | +#define LED_SCROLL_LOCK 4 |
| 113 | + |
| 114 | +#define stdin_recvchar Serial1.write |
| 115 | + |
| 116 | +static uint8_t key_leds; |
| 117 | +static const char knum[] = "1234567890"; |
| 118 | +static const char ksign[] = "!@#$%^&*()"; |
| 119 | +static const char tabA[] = "\t -=[]\\#;'`,./"; |
| 120 | +static const char tabB[] = "\t _+{}|~:\"~<>?"; |
| 121 | +// route the key event to stdin |
| 122 | +static int process_key(tusbh_ep_info_t* ep, const uint8_t* keys) |
| 123 | +{ |
| 124 | + printf("\n"); |
| 125 | + uint8_t modify = keys[0]; |
| 126 | + uint8_t key = keys[2]; |
| 127 | + uint8_t last_leds = key_leds; |
| 128 | + if (key >= KEY_A && key <= KEY_Z) { |
| 129 | + char ch = 'A' + key - KEY_A; |
| 130 | + if ( (!!(modify & MOD_SHIFT)) == (!!(key_leds & LED_CAPS_LOCK)) ) { |
| 131 | + ch += 'a' - 'A'; |
| 132 | + } |
| 133 | + stdin_recvchar(ch); |
| 134 | + } else if (key >= KEY_1 && key <= KEY_0) { |
| 135 | + if (modify & MOD_SHIFT) { |
| 136 | + stdin_recvchar(ksign[key - KEY_1]); |
| 137 | + } else { |
| 138 | + stdin_recvchar(knum[key - KEY_1]); |
| 139 | + } |
| 140 | + } else if (key >= KEY_TAB && key <= KEY_SLASH) { |
| 141 | + if (modify & MOD_SHIFT) { |
| 142 | + stdin_recvchar(tabB[key - KEY_TAB]); |
| 143 | + } else { |
| 144 | + stdin_recvchar(tabA[key - KEY_TAB]); |
| 145 | + } |
| 146 | + } else if (key == KEY_ENTER) { |
| 147 | + stdin_recvchar('\r'); |
| 148 | + } else if (key == KEY_CAPSLOCK) { |
| 149 | + key_leds ^= LED_CAPS_LOCK; |
| 150 | + } else if (key == KEY_NUMLOCK) { |
| 151 | + key_leds ^= LED_NUM_LOCK; |
| 152 | + } else if (key == KEY_SCROLLLOCK) { |
| 153 | + key_leds ^= LED_SCROLL_LOCK; |
| 154 | + } |
| 155 | + |
| 156 | + if (key_leds != last_leds) { |
| 157 | + tusbh_set_keyboard_led(ep, key_leds); |
| 158 | + } |
| 159 | + return 0; |
| 160 | +} |
0 commit comments