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+ #include < MachineControl.h>
35+
36+ using namespace machinecontrol ;
37+
38+ REDIRECT_STDOUT_TO (Serial);
39+
40+ static int process_key (tusbh_ep_info_t * ep, const uint8_t * key);
41+
42+ static const tusbh_boot_key_class_t cls_boot_key = {
43+ .backend = &tusbh_boot_keyboard_backend,
44+ // .on_key = process_key
45+ };
46+
47+ static const tusbh_boot_mouse_class_t cls_boot_mouse = {
48+ .backend = &tusbh_boot_mouse_backend,
49+ // .on_mouse = process_mouse
50+ };
51+
52+ static const tusbh_hid_class_t cls_hid = {
53+ .backend = &tusbh_hid_backend,
54+ // .on_recv_data = process_hid_recv,
55+ // .on_send_done = process_hid_sent,
56+ };
57+
58+ static const tusbh_hub_class_t cls_hub = {
59+ .backend = &tusbh_hub_backend,
60+ };
61+
62+ static const tusbh_vendor_class_t cls_vendor = {
63+ .backend = &tusbh_vendor_backend,
64+ // .transfer_done = process_vendor_xfer_done
65+ };
66+
67+ int msc_ff_mount (tusbh_interface_t * interface, int max_lun, const tusbh_block_info_t * blocks);
68+ int msc_ff_unmount (tusbh_interface_t * interface);
69+
70+ static const tusbh_msc_class_t cls_msc_bot = {
71+ .backend = &tusbh_msc_bot_backend,
72+ // .mount = msc_ff_mount,
73+ // .unmount = msc_ff_unmount,
74+ };
75+
76+ static const tusbh_cdc_acm_class_t cls_cdc_acm = {
77+ .backend = &tusbh_cdc_acm_backend,
78+ };
79+
80+ static const tusbh_cdc_rndis_class_t cls_cdc_rndis = {
81+ .backend = &tusbh_cdc_rndis_backend,
82+ };
83+
84+ static const tusbh_class_reg_t class_table[] = {
85+ (tusbh_class_reg_t )&cls_boot_key,
86+ (tusbh_class_reg_t )&cls_boot_mouse,
87+ (tusbh_class_reg_t )&cls_hub,
88+ (tusbh_class_reg_t )&cls_msc_bot,
89+ (tusbh_class_reg_t )&cls_cdc_acm,
90+ (tusbh_class_reg_t )&cls_cdc_rndis,
91+ (tusbh_class_reg_t )&cls_hid,
92+ (tusbh_class_reg_t )&cls_vendor,
93+ 0 ,
94+ };
95+
96+
97+ void setup ()
98+ {
99+ Serial1.begin (115200 );
100+ usb_controller.init (class_table);
101+
102+ }
103+
104+ void loop () {
105+ usb_controller.usb .Task ();
106+ }
107+
108+ #define MOD_CTRL (0x01 | 0x10 )
109+ #define MOD_SHIFT (0x02 | 0x20 )
110+ #define MOD_ALT (0x04 | 0x40 )
111+ #define MOD_WIN (0x08 | 0x80 )
112+
113+ #define LED_NUM_LOCK 1
114+ #define LED_CAPS_LOCK 2
115+ #define LED_SCROLL_LOCK 4
116+
117+ #define stdin_recvchar Serial1.write
118+
119+ static uint8_t key_leds;
120+ static const char knum[] = " 1234567890" ;
121+ static const char ksign[] = " !@#$%^&*()" ;
122+ static const char tabA[] = " \t -=[]\\ #;'`,./" ;
123+ static const char tabB[] = " \t _+{}|~:\" ~<>?" ;
124+ // route the key event to stdin
125+ static int process_key (tusbh_ep_info_t * ep, const uint8_t * keys)
126+ {
127+ printf (" \n " );
128+ uint8_t modify = keys[0 ];
129+ uint8_t key = keys[2 ];
130+ uint8_t last_leds = key_leds;
131+ if (key >= KEY_A && key <= KEY_Z) {
132+ char ch = ' A' + key - KEY_A;
133+ if ( (!!(modify & MOD_SHIFT)) == (!!(key_leds & LED_CAPS_LOCK)) ) {
134+ ch += ' a' - ' A' ;
135+ }
136+ stdin_recvchar (ch);
137+ Serial.print (ch);
138+ } else if (key >= KEY_1 && key <= KEY_0) {
139+ if (modify & MOD_SHIFT) {
140+ stdin_recvchar (ksign[key - KEY_1]);
141+ } else {
142+ stdin_recvchar (knum[key - KEY_1]);
143+ }
144+ } else if (key >= KEY_TAB && key <= KEY_SLASH) {
145+ if (modify & MOD_SHIFT) {
146+ stdin_recvchar (tabB[key - KEY_TAB]);
147+ } else {
148+ stdin_recvchar (tabA[key - KEY_TAB]);
149+ }
150+ } else if (key == KEY_ENTER) {
151+ stdin_recvchar (' \r ' );
152+ } else if (key == KEY_CAPSLOCK) {
153+ key_leds ^= LED_CAPS_LOCK;
154+ } else if (key == KEY_NUMLOCK) {
155+ key_leds ^= LED_NUM_LOCK;
156+ } else if (key == KEY_SCROLLLOCK) {
157+ key_leds ^= LED_SCROLL_LOCK;
158+ }
159+
160+ if (key_leds != last_leds) {
161+ tusbh_set_keyboard_led (ep, key_leds);
162+ }
163+ return 0 ;
164+ }
0 commit comments