You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Tested with: basic wired mouse Genius X-Scroll GM-110020, new model GamingX Trust “gaming” mouse and Dell WM 126 wireless mouse.
92
+
93
+
94
+
### Keyboard and Mouse
95
+
In order to use two (or more) HID devices connected via a USB hub to your Portenta C33 board, please open "tusb_config.h" and make sure that "CFG_TUH_HUB" is set to value 1, and that "CFG_TUH_HID" is set to the number of HID devices you intend to connect to your Arduino (2 in this example).
96
+
Please also keep in mind that some keyboards and mice which include advanced illumination features might draw more power than the Arduino is able to provide on its
97
+
USB-A port and might therefore lead to a reset or failure to be enumerated by the board. Ideally, use basic USB keyboards and mice, these should work best.
98
+
88
99
### Temp. Development instructions
89
100
Before all the changes get merged into the core, you will have to do some modifications to get this library to compile.
90
101
91
102
1. Enable TinyUSB HID Host support
92
103
93
104
In the core by modify [variants/PORTENTA_C33/tusb_config.h](https://github.com/arduino/ArduinoCore-renesas/blob/main/variants/PORTENTA_C33/tusb_config.h).
94
105
On line 106, add `#define CFG_TUH_HID 1`.
95
-
Check [this PR](https://github.com/arduino/ArduinoCore-renesas/compare/main...cristidragomir97:ArduinoCore-renesas:hid_host_c33) for more information.
96
-
97
-
2. Enable weak callback for `tuh_hid_report_received_cb`
98
-
When enabling CFG_TUH_HID in tusb_config.h, the stack will expect a tuh_hid_report_received_cb callback to be defined in every sketch, preventing any sketch that doesn't have anything to do with the HID Host stack from compiling. The hid_host.h file defines weak callbacks in order to prevent this issue, but the TU_ATTR_WEAK is prefixed to most callbacks except fortuh_hid_report_received_cb. These changes add this attribute, allowing any sketch to compile.
99
-
100
-
Check [this PR](https://github.com/arduino/tinyusb/pull/3/commits/e3e9dd066cd64d98de6bd19d2920fec3019b71c4) for more information.
106
+
Check [this PR](https://github.com/arduino/ArduinoCore-renesas/compare/main...cristidragomir97:ArduinoCore-renesas:hid_host_c33) for more information.
* This simple example shows how to read keyboard data by both listening for events and polling for available character data in the internal buffers of the USB HID Host.
3
+
* There are two general ways user input can be detected, either by constantly checking for the state of a buffer (or pin, for example), which we refer to as the "polling" method, or
4
+
* by having some event fire as soon as the user input (key press in this case) is detected, which, in turn, calls a function we registered previously with the lower layers.
5
+
* The below example demonstrates both these methods of capturing user input.
6
+
*
7
+
* Instructions:
8
+
* 1. Connect your Arduino Portenta C33 to a mid-carrier board;
9
+
* 2. Upload this sketch to the Portenta;
10
+
* 3. Open the Serial Monitor and chose 115200 as baud rate;
11
+
12
+
* 4. Connect your keyboard to the USB-A connector and any keypress should be printed to the console window.
13
+
*/
14
+
1
15
#include<Arduino.h>
2
16
#include<Arduino_USBHIDHost.h>
3
17
@@ -7,8 +21,9 @@ void onKeyboardConnected() {
7
21
Serial.println("Keyboard connected (callback).");
8
22
}
9
23
24
+
// This function will now be called every time a key is pressed by the user, as a single event
10
25
voidonKeyboardEvent(uint8_t key) {
11
-
Serial.print("Keyboard event (callback): ");
26
+
Serial.print("Key pressed (event callback): ");
12
27
Serial.println((char) key);
13
28
}
14
29
@@ -17,15 +32,17 @@ void setup() {
17
32
while (!Serial);
18
33
19
34
kb.attachConnectionCallback(onKeyboardConnected);
20
-
kb.attachKeyboardEventCallback(onKeyboardEvent);
35
+
kb.attachKeyboardEventCallback(onKeyboardEvent);// Register callback to get the keystrokes via events
21
36
kb.begin();
22
37
}
23
38
24
39
voidloop() {
25
-
kb.poll();
40
+
kb.poll();// This function will continuously check if a key has been pressed, this is generally referred to as "polling"
26
41
42
+
// If keystrokes were registered, we enter a second loop and print out the entire buffer
Copy file name to clipboardExpand all lines: examples/KeyboardAndMouse/KeyboardAndMouse.ino
+36-16Lines changed: 36 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,30 @@
1
+
/*
2
+
* This simple example demonstrates how to read mouse and keyboard data, by connecting these devices at the same time to the Portenta, via a USB hub
3
+
*
4
+
* In order to use two (or more) HID devices connected via a USB hub to your Portenta C33 board, please open "tusb_config.h" below (right click -> Go To Definition)
5
+
* and make sure that "CFG_TUH_HUB" is set to value 1, and that "CFG_TUH_HID" is set to the number of HID devices you intend to connect to your Arduino (2 in this example).
6
+
* Please also keep in mind that some keyboards and mice which include advanced illumination features might draw more power than the Arduino is able to provide on its
7
+
* USB-A port and might therefore lead to a reset or failure to be enumerated by the board. Ideally, use basic USB keyboards and mice, these should work best.
8
+
*
9
+
* Instructions:
10
+
* 1. Connect your Arduino Portenta C33 to a mid-carrier board;
11
+
* 2. Upload this sketch to the Portenta;
12
+
* 3. Open the Serial Monitor and chose the same baud rate as used in the sketch;
13
+
* 4. Connect a USB hub to the USB-A connector on the mid-carrier board;
14
+
* 5. Now connect your keyboard and mouse to the USB hub and check the printed output on the console when pressing a key on the keyboard or moving the mouse.
15
+
*/
16
+
1
17
#include<Arduino.h>
2
18
#include<Arduino_USBHIDHost.h>
19
+
#include<tusb_config.h>
3
20
4
21
// Global device instances
5
22
USBHIDKeyboard kb;
6
23
USBHIDMouse ms;
7
24
25
+
HIDMouseEvent mouseEvent;
26
+
bool eventReceived = false;
27
+
8
28
// Keyboard connection callback
9
29
voidonKeyboardConnected() {
10
30
Serial.println("Keyboard connected (callback).");
@@ -22,15 +42,12 @@ void onMouseConnected() {
22
42
}
23
43
24
44
// Mouse movement/button event callback
45
+
// Note here the use of the "eventReceived" global variable. Since this function is called in an interrupt context,
46
+
// we cannot do the processing and printing out of data using Serial.print, inside the interrupt, because it takes too much time.
47
+
// Therefore, we set a global flag to "true", which is then checked in the "loop", outside of the ISR (interrupt service routine), then set back to "false".
48
+
// An even better and more robut, but also more complex, way of handling this is by using a buffer queue for the mouse data and a state machine to process it.
0 commit comments