Skip to content

Commit 97f7466

Browse files
added example sketches for combo and barcode
1 parent 48c4c3e commit 97f7466

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# Arduino_USBHIDHost
22

3+
<<<<<<< HEAD
34
[![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)
45

56

7+
=======
8+
>>>>>>> 3734666 (added barcode and combo example)
69
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.
710

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

1822
## Usage
1923

examples/Barcode/Barcode.ino

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <Arduino.h>
2+
#include "Keyboard.h"
3+
4+
USBHIDKeyboard keyboard;
5+
6+
const int EAN_LENGTH = 13;
7+
char eanBuffer[EAN_LENGTH + 1]; // +1 for null terminator
8+
int eanIndex = 0;
9+
bool collecting = false;
10+
11+
void setup() {
12+
Serial.begin(115200);
13+
keyboard.begin();
14+
Serial.println("Waiting for EAN-13 barcode input...");
15+
}
16+
17+
void loop() {
18+
keyboard.poll();
19+
20+
while (keyboard.available()) {
21+
int key = keyboard.read();
22+
23+
if (key >= '0' && key <= '9') {
24+
if (!collecting) {
25+
collecting = true;
26+
eanIndex = 0;
27+
}
28+
29+
if (eanIndex < EAN_LENGTH) {
30+
eanBuffer[eanIndex++] = (char)key;
31+
}
32+
33+
// When we have 13 digits
34+
if (eanIndex == EAN_LENGTH) {
35+
eanBuffer[EAN_LENGTH] = '\0'; // Null-terminate the string
36+
Serial.print("EAN-13 Code: ");
37+
Serial.println(eanBuffer);
38+
collecting = false;
39+
eanIndex = 0;
40+
}
41+
}
42+
43+
else if (collecting) {
44+
Serial.println("Invalid character detected, resetting...");
45+
collecting = false;
46+
eanIndex = 0;
47+
}
48+
}
49+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <Arduino.h>
2+
#include "Keyboard.h"
3+
#include "Mouse.h"
4+
5+
// Global device instances
6+
USBHIDKeyboard kb;
7+
USBHIDMouse ms;
8+
9+
// Keyboard connection callback
10+
void onKeyboardConnected() {
11+
Serial.println("Keyboard connected (callback).");
12+
}
13+
14+
// Keyboard key event callback
15+
void onKeyboardEvent(uint8_t key) {
16+
Serial.print("Keyboard event (callback): ");
17+
Serial.println((char)key);
18+
}
19+
20+
// Mouse connection callback
21+
void onMouseConnected() {
22+
Serial.println("Mouse connected (callback).");
23+
}
24+
25+
// Mouse movement/button event callback
26+
void onMouseEvent(const HIDMouseEvent &event) {
27+
Serial.print("Mouse event (callback) - Buttons: ");
28+
Serial.print(event.buttons);
29+
Serial.print(", x: ");
30+
Serial.print(event.x);
31+
Serial.print(", y: ");
32+
Serial.print(event.y);
33+
Serial.print(", wheel: ");
34+
Serial.println(event.wheel);
35+
}
36+
37+
void setup() {
38+
Serial.begin(115200);
39+
while (!Serial);
40+
41+
// Setup keyboard
42+
kb.attachConnectionCallback(onKeyboardConnected);
43+
kb.attachKeyboardEventCallback(onKeyboardEvent);
44+
kb.begin();
45+
46+
// Setup mouse
47+
ms.attachConnectionCallback(onMouseConnected);
48+
ms.attachMouseEventCallback(onMouseEvent);
49+
ms.begin();
50+
51+
Serial.println("Keyboard and Mouse listeners started.");
52+
}
53+
54+
void loop() {
55+
// Poll both HID devices
56+
kb.poll();
57+
ms.poll();
58+
59+
// Optional: Read keyboard characters from buffer
60+
while (kb.available() > 0) {
61+
char c = kb.read();
62+
Serial.print("Buffered keystroke: ");
63+
Serial.println(c);
64+
}
65+
66+
// You can also process mouse state if needed
67+
}

0 commit comments

Comments
 (0)