-
Notifications
You must be signed in to change notification settings - Fork 0
AE-577: Minor improvements to example sketches and some extra comments. #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4065ac8
8caaaef
9fdf67c
6f8bb66
abda6d2
de539e7
3c21cd3
a99579d
65d32e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,10 +1,30 @@ | ||||||
| /* | ||||||
| * This simple example demonstrates how to read mouse and keyboard data, by connecting these devices at the same time to the Portenta, via a USB hub | ||||||
| * | ||||||
| * In order to use two (or more) HID devices connected via a USB hub to your Portenta C33 board, please open "tusb_config.h" below (right click -> Go To Definition) | ||||||
| * and make sure that "CFG_TUH_HUB" is set to value 1, and that "CFG_TUH_HID" is set to the number of HID devices you intend to connect to your Arduino (2 in this example). | ||||||
|
Comment on lines
+4
to
+5
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not be necessary. If it is, we need to fix that. |
||||||
| * Please also keep in mind that some keyboards and mice which include advanced illumination features might draw more power than the Arduino is able to provide on its | ||||||
| * USB-A port and might therefore lead to a reset or failure to be enumerated by the board. Ideally, use basic USB keyboards and mice, these should work best. | ||||||
| * | ||||||
| * Instructions: | ||||||
| * 1. Connect your Arduino Portenta C33 to a mid-carrier board; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or use a USB Hub |
||||||
| * 2. Upload this sketch to the Portenta; | ||||||
| * 3. Open the Serial Monitor and chose the same baud rate as used in the sketch; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| * 4. Connect a USB hub to the USB-A connector on the mid-carrier board; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...or a USB-Hub with PD directly |
||||||
| * 5. Now connect your keyboard and mouse to the USB hub and check the printed output on the console when pressing a key on the keyboard or moving the mouse. | ||||||
| */ | ||||||
|
|
||||||
| #include <Arduino.h> | ||||||
| #include <Arduino_USBHIDHost.h> | ||||||
| #include <tusb_config.h> | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ew. Is this really necessary? If so, this header file should be included in the library's umbrella header |
||||||
|
|
||||||
| // Global device instances | ||||||
| USBHIDKeyboard kb; | ||||||
| USBHIDMouse ms; | ||||||
|
|
||||||
| HIDMouseEvent mouseEvent; | ||||||
| bool eventReceived = false; | ||||||
|
|
||||||
| // Keyboard connection callback | ||||||
| void onKeyboardConnected() { | ||||||
| Serial.println("Keyboard connected (callback)."); | ||||||
|
|
@@ -22,15 +42,12 @@ void onMouseConnected() { | |||||
| } | ||||||
|
|
||||||
| // Mouse movement/button event callback | ||||||
| // Note here the use of the "eventReceived" global variable. Since this function is called in an interrupt context, | ||||||
| // we cannot do the processing and printing out of data using Serial.print, inside the interrupt, because it takes too much time. | ||||||
| // Therefore, we set a global flag to "true", which is then checked in the "loop", outside of the ISR (interrupt service routine), then set back to "false". | ||||||
| // An even better and more robut, but also more complex, way of handling this is by using a buffer queue for the mouse data and a state machine to process it. | ||||||
| void onMouseEvent(const HIDMouseEvent &event) { | ||||||
| Serial.print("Mouse event (callback) - Buttons: "); | ||||||
| Serial.print(event.buttons); | ||||||
| Serial.print(", x: "); | ||||||
| Serial.print(event.xMovement); | ||||||
| Serial.print(", y: "); | ||||||
| Serial.print(event.yMovement); | ||||||
| Serial.print(", wheel: "); | ||||||
| Serial.println(event.wheelMovement); | ||||||
| eventReceived = true; | ||||||
| } | ||||||
|
|
||||||
| void setup() { | ||||||
|
|
@@ -55,12 +72,15 @@ void loop() { | |||||
| 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 | ||||||
| if(eventReceived){ | ||||||
| Serial.print("Mouse event (callback) - Buttons: "); | ||||||
| Serial.print(mouseEvent.buttons); | ||||||
| Serial.print(", x: "); | ||||||
| Serial.print(mouseEvent.xMovement); | ||||||
| Serial.print(", y: "); | ||||||
| Serial.print(mouseEvent.yMovement); | ||||||
| Serial.print(", wheel: "); | ||||||
| Serial.println(mouseEvent.wheelMovement); | ||||||
| eventReceived = false; | ||||||
| } | ||||||
| } | ||||||
Uh oh!
There was an error while loading. Please reload this page.