Skip to content

Commit 4065ac8

Browse files
committed
AE-577: Added basic comments to example sketches to explain their intended purpose; Implemented in "KeyboardAndMouse" sketch the same solution to print mouse data to console, as used in the "Mouse" example.
1 parent 537b1b8 commit 4065ac8

File tree

4 files changed

+40
-18
lines changed

4 files changed

+40
-18
lines changed

examples/Barcode/Barcode.ino

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*
2+
* By connecting a handheld barcode reader to the USB-A port on the mid-carrier board of your Arduino Portenta C33,
3+
* you should immediately be able to read standard 13-digit bar codes on any ordinary commercial product packaging.
4+
* Please note that barcodes longer than 13 digits will result in the "Invalid character detected, resetting..." being printed.
5+
*/
6+
17
#include <Arduino.h>
28
#include <Arduino_USBHIDHost.h>
39

examples/Keyboard/Keyboard.ino

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* 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 */
2+
13
#include <Arduino.h>
24
#include <Arduino_USBHIDHost.h>
35

@@ -17,15 +19,16 @@ void setup() {
1719
while (!Serial);
1820

1921
kb.attachConnectionCallback(onKeyboardConnected);
20-
kb.attachKeyboardEventCallback(onKeyboardEvent);
22+
kb.attachKeyboardEventCallback(onKeyboardEvent); // Register callback to get the keystrokes via events
2123
kb.begin();
2224
}
2325

2426
void loop() {
2527
kb.poll();
2628

29+
// Second way of reading back the keystrokes is via polling for available characters
2730
while (kb.available() > 0) {
2831
char c = kb.read();
29-
Serial.print(c);
32+
Serial.println(c);
3033
}
3134
}

examples/KeyboardAndMouse/KeyboardAndMouse.ino

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1+
/* 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 */
2+
13
#include <Arduino.h>
24
#include <Arduino_USBHIDHost.h>
35

6+
/*
7+
* 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)
8+
* 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).
9+
* 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
10+
* 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.
11+
*/
12+
13+
#include <tusb_config.h>
14+
415
// Global device instances
516
USBHIDKeyboard kb;
617
USBHIDMouse ms;
718

19+
HIDMouseEvent mouseEvent;
20+
bool eventReceived = false;
21+
822
// Keyboard connection callback
923
void onKeyboardConnected() {
1024
Serial.println("Keyboard connected (callback).");
@@ -23,14 +37,8 @@ void onMouseConnected() {
2337

2438
// Mouse movement/button event callback
2539
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);
40+
eventReceived = true;
41+
mouseEvent = event;
3442
}
3543

3644
void setup() {
@@ -55,12 +63,15 @@ void loop() {
5563
kb.poll();
5664
ms.poll();
5765

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
66+
if(eventReceived){
67+
Serial.print("Mouse event (callback) - Buttons: ");
68+
Serial.print(mouseEvent.buttons);
69+
Serial.print(", x: ");
70+
Serial.print(mouseEvent.xMovement);
71+
Serial.print(", y: ");
72+
Serial.print(mouseEvent.yMovement);
73+
Serial.print(", wheel: ");
74+
Serial.println(mouseEvent.wheelMovement);
75+
eventReceived = false;
76+
}
6677
}

examples/Mouse/Mouse.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* This simple example shows how to read mouse data, like cursor position and key states, by registering a callback and listening for events */
2+
13
#include <Arduino.h>
24
#include <Arduino_USBHIDHost.h>
35

0 commit comments

Comments
 (0)