diff --git a/README.md b/README.md index 74137b1..7dfe33b 100644 --- a/README.md +++ b/README.md @@ -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: @@ -14,6 +17,7 @@ The library has two main classes: - **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. ## Usage diff --git a/examples/Barcode/Barcode.ino b/examples/Barcode/Barcode.ino new file mode 100644 index 0000000..2488610 --- /dev/null +++ b/examples/Barcode/Barcode.ino @@ -0,0 +1,49 @@ +#include +#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; + } + } +} diff --git a/examples/KeyboardAndMouse/KeyboardAndMouse.ino b/examples/KeyboardAndMouse/KeyboardAndMouse.ino index e69de29..9b9ca69 100755 --- a/examples/KeyboardAndMouse/KeyboardAndMouse.ino +++ b/examples/KeyboardAndMouse/KeyboardAndMouse.ino @@ -0,0 +1,67 @@ +#include +#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 +}