Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Arduino_USBHIDHost

<<<<<<< HEAD
[![Check Arduino](https://github.com/bcmi-labs/Arduino_USBHIDHost/actions/workflows/check-arduino.yml/badge.svg)](https://github.com/bcmi-labs/Arduino_USBHIDHost/actions/workflows/check-arduino.yml) [![Compile Examples](https://github.com/bcmi-labs/Arduino_USBHIDHost/actions/workflows/compile-examples.yml/badge.svg)](https://github.com/bcmi-labs/Arduino_USBHIDHost/actions/workflows/compile-examples.yml) [![Spell Check](https://github.com/bcmi-labs/Arduino_USBHIDHost/actions/workflows/spell-check.yml/badge.svg)](https://github.com/bcmi-labs/Arduino_USBHIDHost/actions/workflows/spell-check.yml) [![Sync Labels](https://github.com/bcmi-labs/Arduino_USBHIDHost/actions/workflows/sync-labels.yml/badge.svg)](https://github.com/bcmi-labs/Arduino_USBHIDHost/actions/workflows/sync-labels.yml)


=======
>>>>>>> 3734666 (added barcode and combo example)
This library provides support for USB HID devices such as keyboards and mice on the Portenta C33. It should support any device that emulates keyboards (for example, barcode readers). The library automatically detects the insertion of a compatible device on the USB port of the chosen breakout board. You can use `attachConnectionCallback()` to get notified when a compatible device is connected.

The library has two main classes:
Expand All @@ -14,6 +17,7 @@
- **examples/Mouse** – Demonstrates receiving mouse events via a callback.
- **examples/Keyboard** – Demonstrates receiving keyboard events via both callbacks and the Stream interface.
- **examples/MouseAndKeyboard** – Demonstrates simultaneous support for both mouse and keyboard devices.
- **examples/Barcode** - Uses tthe HID Host implementation to read 13-digit EAN codes from a USB based barcode reader.

Check failure on line 20 in README.md

View workflow job for this annotation

GitHub Actions / spellcheck

tthe ==> the

Check failure on line 20 in README.md

View workflow job for this annotation

GitHub Actions / spellcheck

tthe ==> the

## Usage

Expand Down
49 changes: 49 additions & 0 deletions examples/Barcode/Barcode.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <Arduino.h>
#include "Keyboard.h"

USBHIDKeyboard keyboard;

const int EAN_LENGTH = 13;
char eanBuffer[EAN_LENGTH + 1]; // +1 for null terminator
int eanIndex = 0;
bool collecting = false;

void setup() {
Serial.begin(115200);
keyboard.begin();
Serial.println("Waiting for EAN-13 barcode input...");
}

void loop() {
keyboard.poll();

while (keyboard.available()) {
int key = keyboard.read();

if (key >= '0' && key <= '9') {
if (!collecting) {
collecting = true;
eanIndex = 0;
}

if (eanIndex < EAN_LENGTH) {
eanBuffer[eanIndex++] = (char)key;
}

// When we have 13 digits
if (eanIndex == EAN_LENGTH) {
eanBuffer[EAN_LENGTH] = '\0'; // Null-terminate the string
Serial.print("EAN-13 Code: ");
Serial.println(eanBuffer);
collecting = false;
eanIndex = 0;
}
}

else if (collecting) {
Serial.println("Invalid character detected, resetting...");
collecting = false;
eanIndex = 0;
}
}
}
67 changes: 67 additions & 0 deletions examples/KeyboardAndMouse/KeyboardAndMouse.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <Arduino.h>
#include "Keyboard.h"
#include "Mouse.h"

// Global device instances
USBHIDKeyboard kb;
USBHIDMouse ms;

// Keyboard connection callback
void onKeyboardConnected() {
Serial.println("Keyboard connected (callback).");
}

// Keyboard key event callback
void onKeyboardEvent(uint8_t key) {
Serial.print("Keyboard event (callback): ");
Serial.println((char)key);
}

// Mouse connection callback
void onMouseConnected() {
Serial.println("Mouse connected (callback).");
}

// 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.x);
Serial.print(", y: ");
Serial.print(event.y);
Serial.print(", wheel: ");
Serial.println(event.wheel);
}

void setup() {
Serial.begin(115200);
while (!Serial);

// Setup keyboard
kb.attachConnectionCallback(onKeyboardConnected);
kb.attachKeyboardEventCallback(onKeyboardEvent);
kb.begin();

// Setup mouse
ms.attachConnectionCallback(onMouseConnected);
ms.attachMouseEventCallback(onMouseEvent);
ms.begin();

Serial.println("Keyboard and Mouse listeners started.");
}

void loop() {
// Poll both HID devices
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
}
Loading