Skip to content

Commit a99579d

Browse files
committed
AE-577: Addressed code review findings.
1 parent 3c21cd3 commit a99579d

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

examples/Keyboard/Keyboard.ino

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/*
2-
* This simple example shows how to read keyboard data by both listening for events and polling for available character data in the internal buffers of the USB HID Host
2+
* This simple example shows how to read keyboard data by both listening for events and polling for available character data in the internal buffers of the USB HID Host.
3+
* There are two general ways user input can be detected, either by constantly checking for the state of a buffer (or pin, for example), which we refer to as the "polling" method, or
4+
* by having some event fire as soon as the user input (key press in this case) is detected, which, in turn, calls a function we registered previously with the lower layers.
5+
* The below example demonstrates both these methods of capturing user input.
36
*
47
* Instructions:
58
* 1. Connect your Arduino Portenta C33 to a mid-carrier board;
@@ -18,8 +21,9 @@ void onKeyboardConnected() {
1821
Serial.println("Keyboard connected (callback).");
1922
}
2023

24+
// This function will now be called every time a key is pressed by the user, as a single event
2125
void onKeyboardEvent(uint8_t key) {
22-
Serial.print("Keyboard event (callback): ");
26+
Serial.print("Key pressed (event callback): ");
2327
Serial.println((char) key);
2428
}
2529

@@ -33,11 +37,12 @@ void setup() {
3337
}
3438

3539
void loop() {
36-
kb.poll();
40+
kb.poll(); // This function will continuosly check if a key has been pressed, this is generally refered to as "polling"
3741

38-
// Second way of reading back the keystrokes is via polling for available characters
42+
// If keystrokes were registered, we enter a second loop and print out the entire buffer
3943
while (kb.available() > 0) {
4044
char c = kb.read();
45+
Serial.print("Key pressed (polling detection): ");
4146
Serial.println(c);
4247
}
4348
}

examples/KeyboardAndMouse/KeyboardAndMouse.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ void onMouseConnected() {
4242
}
4343

4444
// Mouse movement/button event callback
45+
// Note here the use of the "eventReceived" global variable. Since this function is called in an interrupt context,
46+
// we cannot do the processing and printin of data using Serial.print inside the interrupt, because it takes too much time.
47+
// 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".
48+
// 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.
4549
void onMouseEvent(const HIDMouseEvent &event) {
4650
eventReceived = true;
47-
mouseEvent = event;
4851
}
4952

5053
void setup() {

examples/Mouse/Mouse.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* 1. Connect your Arduino Portenta C33 to a mid-carrier board;
66
* 2. Upload this sketch to the board;
77
8-
* 3. Open the Serial Monitor and chose the same baud rate as used in the sketch;
8+
* 3. Open the Serial Monitor and chose the same baud rate (115200 in this case) as used in the sketch;
99
* 4. Connect your mouse to the USB-A connector and any movement or keypress should be printed as data to the console.
10-
* Please not that scroll wheel data works inconsistently and might not function correctly with your mouse.
10+
* Please note that the scroll wheel data works inconsistently and might not function correctly with your mouse.
1111
*/
1212

1313
#include <Arduino.h>

0 commit comments

Comments
 (0)