You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/Keyboard/Keyboard.ino
+9-4Lines changed: 9 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,8 @@
1
1
/*
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.
3
6
*
4
7
* Instructions:
5
8
* 1. Connect your Arduino Portenta C33 to a mid-carrier board;
@@ -18,8 +21,9 @@ void onKeyboardConnected() {
18
21
Serial.println("Keyboard connected (callback).");
19
22
}
20
23
24
+
// This function will now be called every time a key is pressed by the user, as a single event
21
25
voidonKeyboardEvent(uint8_t key) {
22
-
Serial.print("Keyboard event (callback): ");
26
+
Serial.print("Key pressed (event callback): ");
23
27
Serial.println((char) key);
24
28
}
25
29
@@ -33,11 +37,12 @@ void setup() {
33
37
}
34
38
35
39
voidloop() {
36
-
kb.poll();
40
+
kb.poll();// This function will continuosly check if a key has been pressed, this is generally refered to as "polling"
37
41
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
Copy file name to clipboardExpand all lines: examples/KeyboardAndMouse/KeyboardAndMouse.ino
+4-1Lines changed: 4 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -42,9 +42,12 @@ void onMouseConnected() {
42
42
}
43
43
44
44
// 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.
0 commit comments