Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit 0cfa2eb

Browse files
committed
[USBHost] Refactor example
1 parent 00d80d2 commit 0cfa2eb

File tree

2 files changed

+159
-147
lines changed

2 files changed

+159
-147
lines changed

examples/USB_host/TUSB_helpers.h

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

examples/USB_host/USB_host.ino

Lines changed: 8 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,25 @@
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 - [email protected]
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-
*/
341
#include <Arduino_MachineControl.h>
2+
#include <USBHost.h>
3+
4+
#include "TUSB_helpers.h"
355

366
using namespace machinecontrol;
377

8+
// Redirect log output from MbedOS and low-level libraries to Serial
389
REDIRECT_STDOUT_TO(Serial);
3910

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-
11+
USBHost usb;
9612

9713
void setup()
9814
{
9915
Serial1.begin(115200);
100-
usb_controller.init(class_table);
16+
usb_controller.powerEnable();
17+
usb.Init(USB_CORE_ID_FS, class_table);
10118

10219
}
10320

10421
void loop() {
105-
usb_controller.usb.Task();
22+
usb.Task();
10623
}
10724

108-
#define MOD_CTRL (0x01 | 0x10)
109-
#define MOD_SHIFT (0x02 | 0x20)
110-
#define MOD_ALT (0x04 | 0x40)
111-
#define MOD_WIN (0x08 | 0x80)
11225

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

Comments
 (0)