Skip to content

Commit 37ccfe5

Browse files
authored
Merge pull request #9 from bcmi-labs/AE-577_Test_and_review_latest_version_of_Arduino_USBHIDHost
AE-577: Minor improvements to example sketches and some extra comments.
2 parents 537b1b8 + 65d32e5 commit 37ccfe5

File tree

5 files changed

+97
-26
lines changed

5 files changed

+97
-26
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ void loop() {
5353

5454
The connection callback (via attachConnectionCallback()) notifies you when a compatible keyboard is connected.
5555

56+
Tested with: Oldschool classic Dell L100 and brand new “gaming” modern GamingX Trust keyboards.
57+
58+
5659
### Mouse
5760
Reading Mouse Information
5861
The mouse class provides a callback that returns a structure containing mouse event data. The structure is defined as follows:
@@ -85,16 +88,19 @@ void onMouseEvent(const HIDMouseEvent &mouseEvent) {
8588
usbMouse.attachMouseEventCallback(onMouseEvent);
8689
```
8790

91+
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+
8899
### Temp. Development instructions
89100
Before all the changes get merged into the core, you will have to do some modifications to get this library to compile.
90101

91102
1. Enable TinyUSB HID Host support
92103

93104
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).
94105
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.

examples/Barcode/Barcode.ino

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* This example shows how to use the Arduino Portenta C33 USBHIDHost library to read barcodes like the ones printed on common
3+
* products packaging.
4+
*
5+
* By connecting a handheld barcode reader to the USB-A port on the mid-carrier board of your
6+
* Arduino Portenta C33, you should immediately be able to read standard 13-digit bar codes on
7+
* any ordinary commercial product packaging.
8+
* Please note that barcodes longer than 13 digits will result in "Invalid character detected, resetting..." messages being printed.
9+
*
10+
* Instructions:
11+
* 1. Connect your Arduino Portenta C33 to a mid-carrier board;
12+
* 2. Upload this sketch to the board;
13+
* 3. Open the Serial Monitor and chose the same baud rate as used in the sketch;
14+
* 4. Connect your barcode scanner to the USB-A connector on the mid-carrier board and scan away.
15+
*/
16+
117
#include <Arduino.h>
218
#include <Arduino_USBHIDHost.h>
319

examples/Keyboard/Keyboard.ino

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
/*
2+
* 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+
115
#include <Arduino.h>
216
#include <Arduino_USBHIDHost.h>
317

@@ -7,8 +21,9 @@ void onKeyboardConnected() {
721
Serial.println("Keyboard connected (callback).");
822
}
923

24+
// This function will now be called every time a key is pressed by the user, as a single event
1025
void onKeyboardEvent(uint8_t key) {
11-
Serial.print("Keyboard event (callback): ");
26+
Serial.print("Key pressed (event callback): ");
1227
Serial.println((char) key);
1328
}
1429

@@ -17,15 +32,17 @@ void setup() {
1732
while (!Serial);
1833

1934
kb.attachConnectionCallback(onKeyboardConnected);
20-
kb.attachKeyboardEventCallback(onKeyboardEvent);
35+
kb.attachKeyboardEventCallback(onKeyboardEvent); // Register callback to get the keystrokes via events
2136
kb.begin();
2237
}
2338

2439
void loop() {
25-
kb.poll();
40+
kb.poll(); // This function will continuously check if a key has been pressed, this is generally referred to as "polling"
2641

42+
// If keystrokes were registered, we enter a second loop and print out the entire buffer
2743
while (kb.available() > 0) {
2844
char c = kb.read();
29-
Serial.print(c);
45+
Serial.print("Key pressed (polling detection): ");
46+
Serial.println(c);
3047
}
3148
}

examples/KeyboardAndMouse/KeyboardAndMouse.ino

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff 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+
117
#include <Arduino.h>
218
#include <Arduino_USBHIDHost.h>
19+
#include <tusb_config.h>
320

421
// Global device instances
522
USBHIDKeyboard kb;
623
USBHIDMouse ms;
724

25+
HIDMouseEvent mouseEvent;
26+
bool eventReceived = false;
27+
828
// Keyboard connection callback
929
void onKeyboardConnected() {
1030
Serial.println("Keyboard connected (callback).");
@@ -22,15 +42,12 @@ void onMouseConnected() {
2242
}
2343

2444
// 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.
2549
void onMouseEvent(const HIDMouseEvent &event) {
26-
Serial.print("Mouse event (callback) - Buttons: ");
27-
Serial.print(event.buttons);
28-
Serial.print(", x: ");
29-
Serial.print(event.xMovement);
30-
Serial.print(", y: ");
31-
Serial.print(event.yMovement);
32-
Serial.print(", wheel: ");
33-
Serial.println(event.wheelMovement);
50+
eventReceived = true;
3451
}
3552

3653
void setup() {
@@ -55,12 +72,15 @@ void loop() {
5572
kb.poll();
5673
ms.poll();
5774

58-
// Optional: Read keyboard characters from buffer
59-
while (kb.available() > 0) {
60-
char c = kb.read();
61-
Serial.print("Buffered keystroke: ");
62-
Serial.println(c);
63-
}
64-
65-
// You can also process mouse state if needed
75+
if(eventReceived){
76+
Serial.print("Mouse event (callback) - Buttons: ");
77+
Serial.print(mouseEvent.buttons);
78+
Serial.print(", x: ");
79+
Serial.print(mouseEvent.xMovement);
80+
Serial.print(", y: ");
81+
Serial.print(mouseEvent.yMovement);
82+
Serial.print(", wheel: ");
83+
Serial.println(mouseEvent.wheelMovement);
84+
eventReceived = false;
85+
}
6686
}

examples/Mouse/Mouse.ino

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
/*
2+
* This simple example shows how to read mouse data, like cursor position and key states, by registering a callback and listening for events
3+
*
4+
* Instructions:
5+
* 1. Connect your Arduino Portenta C33 to a mid-carrier board;
6+
* 2. Upload this sketch to the board;
7+
8+
* 3. Open the Serial Monitor and chose the same baud rate (115200 in this case) as used in the sketch;
9+
* 4. Connect your mouse to the USB-A connector and any movement or keypress should be printed as data to the console.
10+
* Please note that the scroll wheel data works inconsistently and might not function correctly with your mouse.
11+
*/
12+
113
#include <Arduino.h>
214
#include <Arduino_USBHIDHost.h>
315

0 commit comments

Comments
 (0)