|
| 1 | +# Arduino_USBHIDHost |
| 2 | + |
| 3 | + |
| 4 | +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. |
| 5 | + |
| 6 | +The library has two main classes: |
| 7 | +- **Keyboard**: Provides both event callbacks and a standard Arduino [`Stream`](https://www.arduino.cc/reference/en/language/functions/communication/stream/) implementation (e.g., using `read()` and `available()`) for reading keyboard data. |
| 8 | +- **Mouse**: Provides event callbacks for receiving mouse information. |
| 9 | + |
| 10 | +## Examples |
| 11 | + |
| 12 | +- **examples/Mouse** – Demonstrates receiving mouse events via a callback. |
| 13 | +- **examples/Keyboard** – Demonstrates receiving keyboard events via both callbacks and the Stream interface. |
| 14 | +- **examples/MouseAndKeyboard** – Demonstrates simultaneous support for both mouse and keyboard devices. |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +To use this library, simply include the appropriate header(s) in your sketch, attach any callbacks you desire, and then call the device’s `begin()` method in `setup()` and `poll()` in `loop()`. Below are some code snippets illustrating the usage. |
| 19 | + |
| 20 | +### Keyboard |
| 21 | + |
| 22 | +You can use the keyboard class in two ways: |
| 23 | + |
| 24 | +1. **Event Callback:** |
| 25 | + Attach a keyboard event callback that will be invoked for every key event (with the ASCII code). |
| 26 | + ```cpp |
| 27 | + void onKeyboardEvent(uint8_t key) { |
| 28 | + Serial.print("Keyboard event (callback): "); |
| 29 | + Serial.println((char) key); |
| 30 | + } |
| 31 | + |
| 32 | + void setup(){ |
| 33 | + usbKetboard.begin(); |
| 34 | + usbKeyboard.attachKeyboardEventCallback(onKeyboardEvent); |
| 35 | + } |
| 36 | + |
| 37 | + ``` |
| 38 | +
|
| 39 | +2. **Stream Interface** |
| 40 | +The USBHIDKeyboard class also implements the Arduino Stream interface so you can use standard functions like read() and available() |
| 41 | +```cpp |
| 42 | +void loop() { |
| 43 | + usbKeyboard.poll(); |
| 44 | + while (usbKeyboard.available() > 0) { |
| 45 | + char keyChar = usbKeyboard.read(); |
| 46 | + Serial.print(keyChar); |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +The connection callback (via attachConnectionCallback()) notifies you when a compatible keyboard is connected. |
| 52 | + |
| 53 | +### Mouse |
| 54 | +Reading Mouse Information |
| 55 | +The mouse class provides a callback that returns a structure containing mouse event data. The structure is defined as follows: |
| 56 | + |
| 57 | +```cpp |
| 58 | +struct HIDMouseEvent { |
| 59 | + uint8_t buttons; ///< Bitmask for mouse buttons (e.g., left, right, middle) |
| 60 | + int16_t xMovement; ///< Relative movement in the x-direction |
| 61 | + int16_t yMovement; ///< Relative movement in the y-direction |
| 62 | + int16_t wheelMovement; ///< Movement of the mouse wheel |
| 63 | +}; |
| 64 | +``` |
| 65 | +
|
| 66 | +Attach your mouse event callback as shown below: |
| 67 | +
|
| 68 | +```cpp |
| 69 | +void onMouseEvent(const HIDMouseEvent &mouseEvent) { |
| 70 | + Serial.print("Mouse event (callback) - Buttons: "); |
| 71 | + Serial.print(mouseEvent.buttons); |
| 72 | + Serial.print(", x: "); |
| 73 | + Serial.print(mouseEvent.xMovement); |
| 74 | + Serial.print(", y: "); |
| 75 | + Serial.print(mouseEvent.yMovement); |
| 76 | + Serial.print(", wheel: "); |
| 77 | + Serial.println(mouseEvent.wheelMovement); |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +```cpp |
| 82 | +usbMouse.attachMouseEventCallback(onMouseEvent); |
| 83 | +``` |
| 84 | + |
0 commit comments