Skip to content

Commit 5dadfc8

Browse files
initial
1 parent 5cd9ed7 commit 5dadfc8

File tree

13 files changed

+722
-0
lines changed

13 files changed

+722
-0
lines changed

Arduino_USBHIDHost.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include "src/Mouse.h"
2+
#include "src/Keyboard.h"

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+

examples/Keyboard/Keyboard.ino

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <Arduino.h>
2+
#include "Keyboard.h"
3+
4+
USBHIDKeyboard kb;
5+
6+
void onKeyboardConnected() {
7+
Serial.println("Keyboard connected (callback).");
8+
}
9+
10+
void onKeyboardEvent(uint8_t key) {
11+
Serial.print("Keyboard event (callback): ");
12+
Serial.println((char) key);
13+
}
14+
15+
void setup() {
16+
Serial.begin(115200);
17+
while (!Serial);
18+
19+
kb.attachConnectionCallback(onKeyboardConnected);
20+
kb.attachKeyboardEventCallback(onKeyboardEvent);
21+
kb.begin();
22+
}
23+
24+
void loop() {
25+
kb.poll();
26+
27+
while (kb.available() > 0) {
28+
char c = kb.read();
29+
Serial.print(c);
30+
}
31+
}

examples/KeyboardAndMouse/KeyboardAndMouse.ino

Whitespace-only changes.

examples/Mouse/Mouse.ino

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <Arduino.h>
2+
#include "Mouse.h"
3+
4+
// Create a global mouse instance.
5+
USBHIDMouse ms;
6+
7+
// User-defined connection callback.
8+
void onMouseConnected() {
9+
Serial.println("Mouse connected (callback).");
10+
}
11+
12+
// User-defined mouse event callback.
13+
void onMouseEvent(const HIDMouseEvent &event) {
14+
Serial.print("Mouse event (callback) - Buttons: ");
15+
Serial.print(event.buttons);
16+
Serial.print(", x: ");
17+
Serial.print(event.x);
18+
Serial.print(", y: ");
19+
Serial.print(event.y);
20+
Serial.print(", wheel: ");
21+
Serial.println(event.wheel);
22+
}
23+
24+
void setup() {
25+
Serial.begin(115200);
26+
while (!Serial);
27+
28+
ms.attachConnectionCallback(onMouseConnected);
29+
ms.attachMouseEventCallback(onMouseEvent);
30+
ms.begin();
31+
}
32+
33+
void loop() {
34+
ms.poll();
35+
// Additional mouse processing if needed.
36+
}

library.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=Arduino_USBHIDHost
2+
version=1.0.0
3+
author=Arduino
4+
maintainer=Arduino <info@arduino.cc>
5+
sentence=A library that provides access to using USB HID Devices like mice and keyboards on the Portenta C33 board.
6+
paragraph=The Arduino Portenta H7 Low Power library provides functionality to enable, get information about, and debug low-power modes on the Portenta H7 board.
7+
category=Device Control
8+
url=https://github.com/arduino-libraries/Arduino_USBHIDHost
9+
architectures=renesas_portanta
10+
includes=Arduino_USBHIDHost.h

src/HIDBase.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "HIDBase.h"
2+
3+
USBHIDDevice::USBHIDDevice()
4+
: isDeviceConnected(false),
5+
connectionCallback(nullptr)
6+
{
7+
}
8+
9+
USBHIDDevice::~USBHIDDevice() { }
10+
11+
void USBHIDDevice::attachConnectionCallback(HIDConnectionCallback connectionCallback) {
12+
this->connectionCallback = connectionCallback;
13+
}
14+
15+
void USBHIDDevice::handleConnection(HIDDeviceType deviceType) {
16+
// Only support keyboard and mouse devices.
17+
if (deviceType != HIDDeviceType::KEYBOARD && deviceType != HIDDeviceType::MOUSE) {
18+
Serial.println("Error: Connected device is not supported!");
19+
while (true) { } // Halt execution.
20+
}
21+
isDeviceConnected = true;
22+
Serial.println("HID device connected successfully.");
23+
if (connectionCallback) {
24+
connectionCallback();
25+
}
26+
}

src/HIDBase.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#ifndef HIDBASE_H
2+
#define HIDBASE_H
3+
4+
#include <Arduino.h>
5+
6+
/**
7+
* @brief Enumeration for HID device types.
8+
*/
9+
enum class HIDDeviceType : uint8_t {
10+
KEYBOARD, ///< Keyboard device
11+
MOUSE, ///< Mouse device
12+
UNKNOWN ///< Unknown device
13+
};
14+
15+
/**
16+
* @brief Callback type for a successful HID device connection.
17+
*
18+
* This callback is raised when an HID device is successfully connected.
19+
*/
20+
typedef void (*HIDConnectionCallback)(void);
21+
22+
/**
23+
* @brief Base class for USB HID devices.
24+
*
25+
* Provides common connection callback functionality for all HID devices.
26+
*/
27+
class USBHIDDevice {
28+
public:
29+
/**
30+
* @brief Construct a new USBHIDDevice object.
31+
*/
32+
USBHIDDevice();
33+
34+
/**
35+
* @brief Destroy the USBHIDDevice object.
36+
*/
37+
virtual ~USBHIDDevice();
38+
39+
/**
40+
* @brief Attach a connection callback.
41+
*
42+
* @param connectionCallback A callback function to be invoked when the device connects.
43+
*/
44+
void attachConnectionCallback(HIDConnectionCallback connectionCallback);
45+
46+
/**
47+
* @brief Internal handler for connection events.
48+
*
49+
* Called by the USB host callbacks to indicate a device connection.
50+
* If the connected device type is not supported, an error is printed and execution halts.
51+
*
52+
* @param deviceType The detected HID device type.
53+
*/
54+
void handleConnection(HIDDeviceType deviceType);
55+
56+
/**
57+
* @brief Check whether the device is connected.
58+
*
59+
* @return true if connected, false otherwise.
60+
*/
61+
bool isConnected() const { return isDeviceConnected; }
62+
63+
protected:
64+
bool isDeviceConnected; ///< Flag indicating if the device is connected.
65+
HIDConnectionCallback connectionCallback; ///< User-provided connection callback.
66+
};
67+
68+
#endif // HIDBASE_H

0 commit comments

Comments
 (0)