Skip to content
15 changes: 15 additions & 0 deletions examples/Barcode/Barcode.ino
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* This example shows how to use the Arduino Portenta C33 USBHIDHost library to read barcodes like the ones printed on common
* products packaging.
*
* By connecting a handheld barcode reader to the USB-A port on the mid-carrier board of your Arduino Portenta C33,
* you should immediately be able to read standard 13-digit bar codes on any ordinary commercial product packaging.
* Please note that barcodes longer than 13 digits will result in the "Invalid character detected, resetting..." being printed.
*
* Instructions:
* 1. Connect your Arduino Portenta C33 to a mid-carrier board;
* 2. Upload this sketch to the Portenta;
* 3. Open the Serial Monitor and chose the same baud rate as used in the sketch;
* 4. Connect your barcode scanner to the USB-A connector on the mid-carrier board and scan away.
*/

#include <Arduino.h>
#include <Arduino_USBHIDHost.h>

Expand Down
15 changes: 13 additions & 2 deletions examples/Keyboard/Keyboard.ino
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/*
* 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
*
* Instructions:
* 1. Connect your Arduino Portenta C33 to a mid-carrier board;
* 2. Upload this sketch to the Portenta;
* 3. Open the Serial Monitor and chose the same baud rate as used in the sketch;
* 4. Connect your keyboard to the USB-A connector and any keypress should be printed to the console window.
*/

#include <Arduino.h>
#include <Arduino_USBHIDHost.h>

Expand All @@ -17,15 +27,16 @@ void setup() {
while (!Serial);

kb.attachConnectionCallback(onKeyboardConnected);
kb.attachKeyboardEventCallback(onKeyboardEvent);
kb.attachKeyboardEventCallback(onKeyboardEvent); // Register callback to get the keystrokes via events
kb.begin();
}

void loop() {
kb.poll();

// Second way of reading back the keystrokes is via polling for available characters
while (kb.available() > 0) {
char c = kb.read();
Serial.print(c);
Serial.println(c);
}
}
49 changes: 33 additions & 16 deletions examples/KeyboardAndMouse/KeyboardAndMouse.ino
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
/*
* 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
*
* 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)
* 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).
Comment on lines +4 to +5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be necessary. If it is, we need to fix that.

* 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
* 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.
*
* Instructions:
* 1. Connect your Arduino Portenta C33 to a mid-carrier board;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or use a USB Hub

* 2. Upload this sketch to the Portenta;
* 3. Open the Serial Monitor and chose the same baud rate as used in the sketch;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 3. Open the Serial Monitor and chose the same baud rate as used in the sketch;
* 3. Open the Serial Monitor and chose 115200 as baud rate;

* 4. Connect a USB hub to the USB-A connector on the mid-carrier board;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...or a USB-Hub with PD directly

* 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.
*/

#include <Arduino.h>
#include <Arduino_USBHIDHost.h>
#include <tusb_config.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ew. Is this really necessary? If so, this header file should be included in the library's umbrella header


// Global device instances
USBHIDKeyboard kb;
USBHIDMouse ms;

HIDMouseEvent mouseEvent;
bool eventReceived = false;

// Keyboard connection callback
void onKeyboardConnected() {
Serial.println("Keyboard connected (callback).");
Expand All @@ -23,14 +43,8 @@ void onMouseConnected() {

// Mouse movement/button event callback
void onMouseEvent(const HIDMouseEvent &event) {
Serial.print("Mouse event (callback) - Buttons: ");
Serial.print(event.buttons);
Serial.print(", x: ");
Serial.print(event.xMovement);
Serial.print(", y: ");
Serial.print(event.yMovement);
Serial.print(", wheel: ");
Serial.println(event.wheelMovement);
eventReceived = true;
mouseEvent = event;
}

void setup() {
Expand All @@ -55,12 +69,15 @@ void loop() {
kb.poll();
ms.poll();

// Optional: Read keyboard characters from buffer
while (kb.available() > 0) {
char c = kb.read();
Serial.print("Buffered keystroke: ");
Serial.println(c);
}

// You can also process mouse state if needed
if(eventReceived){
Serial.print("Mouse event (callback) - Buttons: ");
Serial.print(mouseEvent.buttons);
Serial.print(", x: ");
Serial.print(mouseEvent.xMovement);
Serial.print(", y: ");
Serial.print(mouseEvent.yMovement);
Serial.print(", wheel: ");
Serial.println(mouseEvent.wheelMovement);
eventReceived = false;
}
}
11 changes: 11 additions & 0 deletions examples/Mouse/Mouse.ino
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/*
* This simple example shows how to read mouse data, like cursor position and key states, by registering a callback and listening for events
*
* Instructions:
* 1. Connect your Arduino Portenta C33 to a mid-carrier board;
* 2. Upload this sketch to the Portenta;
* 3. Open the Serial Monitor and chose the same baud rate as used in the sketch;
* 4. Connect your mouse to the USB-A connector and any movement or keypress should be printed as data to the console.
* Please not that scroll wheel data works inconsistently and might not function correctly with your mouse.
*/

#include <Arduino.h>
#include <Arduino_USBHIDHost.h>

Expand Down
Loading