2
2
Keyboard Controller Example
3
3
4
4
Shows the output of a USB Keyboard connected to
5
- the Native USB port on an Arduino Due Board.
5
+ the Native USB port on an Arduino Zero
6
6
7
7
created 8 Oct 2012
8
8
by Cristian Maglie
15
15
// Require keyboard control library
16
16
#include < KeyboardController.h>
17
17
18
+
19
+ // on a zero with debug port, use debug port
20
+ // #define SerialDebug Serial
21
+
22
+ // on a feather or non-debug Zero, use Serial1 (since USB is taken!)
23
+ #define SerialDebug Serial1
24
+
18
25
// Initialize USB Controller
19
26
USBHost usb;
20
27
@@ -25,65 +32,84 @@ void printKey();
25
32
26
33
// This function intercepts key press
27
34
void keyPressed () {
28
- SERIAL_PORT_MONITOR .print (" Pressed: " );
35
+ SerialDebug .print (" Pressed: " );
29
36
printKey ();
30
37
}
31
38
32
39
// This function intercepts key release
33
40
void keyReleased () {
34
- SERIAL_PORT_MONITOR .print (" Released: " );
41
+ SerialDebug .print (" Released: " );
35
42
printKey ();
36
43
}
37
44
38
45
void printKey () {
39
46
// getOemKey() returns the OEM-code associated with the key
40
- SERIAL_PORT_MONITOR .print (" key:" );
41
- SERIAL_PORT_MONITOR .print (keyboard.getOemKey ());
47
+ SerialDebug .print (" key:" );
48
+ SerialDebug .print (keyboard.getOemKey ());
42
49
43
50
// getModifiers() returns a bits field with the modifiers-keys
44
51
int mod = keyboard.getModifiers ();
45
- SERIAL_PORT_MONITOR .print (" mod:" );
46
- SERIAL_PORT_MONITOR .print (mod);
52
+ SerialDebug .print (" mod:" );
53
+ SerialDebug .print (mod);
47
54
48
- SERIAL_PORT_MONITOR .print (" => " );
55
+ SerialDebug .print (" => " );
49
56
50
57
if (mod & LeftCtrl)
51
- SERIAL_PORT_MONITOR .print (" L-Ctrl " );
58
+ SerialDebug .print (" L-Ctrl " );
52
59
if (mod & LeftShift)
53
- SERIAL_PORT_MONITOR .print (" L-Shift " );
60
+ SerialDebug .print (" L-Shift " );
54
61
if (mod & Alt)
55
- SERIAL_PORT_MONITOR .print (" Alt " );
62
+ SerialDebug .print (" Alt " );
56
63
if (mod & LeftCmd)
57
- SERIAL_PORT_MONITOR .print (" L-Cmd " );
64
+ SerialDebug .print (" L-Cmd " );
58
65
if (mod & RightCtrl)
59
- SERIAL_PORT_MONITOR .print (" R-Ctrl " );
66
+ SerialDebug .print (" R-Ctrl " );
60
67
if (mod & RightShift)
61
- SERIAL_PORT_MONITOR .print (" R-Shift " );
68
+ SerialDebug .print (" R-Shift " );
62
69
if (mod & AltGr)
63
- SERIAL_PORT_MONITOR .print (" AltGr " );
70
+ SerialDebug .print (" AltGr " );
64
71
if (mod & RightCmd)
65
- SERIAL_PORT_MONITOR .print (" R-Cmd " );
72
+ SerialDebug .print (" R-Cmd " );
66
73
67
74
// getKey() returns the ASCII translation of OEM key
68
75
// combined with modifiers.
69
- SERIAL_PORT_MONITOR .write (keyboard.getKey ());
70
- SERIAL_PORT_MONITOR .println ();
76
+ SerialDebug .write (keyboard.getKey ());
77
+ SerialDebug .println ();
71
78
}
72
79
80
+ uint32_t lastUSBstate = 0 ;
81
+
73
82
void setup ()
74
83
{
75
- SERIAL_PORT_MONITOR.begin ( 115200 );
76
- while (!SERIAL_PORT_MONITOR); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
77
- SERIAL_PORT_MONITOR.println (" Keyboard Controller Program started" );
84
+ SerialDebug.begin ( 115200 );
85
+ SerialDebug.println (" USB Host Keyboard Controller Program started" );
78
86
79
87
if (usb.Init () == -1 )
80
- SERIAL_PORT_MONITOR.println (" OSC did not start." );
81
-
88
+ SerialDebug.println (" USB Host did not start." );
89
+
90
+ SerialDebug.println (" USB Host started" );
82
91
delay ( 20 );
83
92
}
84
93
85
94
void loop ()
86
95
{
87
96
// Process USB tasks
88
97
usb.Task ();
98
+
99
+ uint32_t currentUSBstate = usb.getUsbTaskState ();
100
+ if (lastUSBstate != currentUSBstate) {
101
+ SerialDebug.print (" USB state changed: 0x" );
102
+ SerialDebug.print (lastUSBstate, HEX);
103
+ SerialDebug.print (" -> 0x" );
104
+ SerialDebug.println (currentUSBstate, HEX);
105
+ switch (currentUSBstate) {
106
+ case USB_ATTACHED_SUBSTATE_SETTLE: SerialDebug.println (" Device Attached" ); break ;
107
+ case USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE: SerialDebug.println (" Detached, waiting for Device" ); break ;
108
+ case USB_ATTACHED_SUBSTATE_RESET_DEVICE: SerialDebug.println (" Resetting Device" ); break ;
109
+ case USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE: SerialDebug.println (" Reset complete" ); break ;
110
+ case USB_STATE_CONFIGURING: SerialDebug.println (" USB Configuring" ); break ;
111
+ case USB_STATE_RUNNING: SerialDebug.println (" USB Running" ); break ;
112
+ }
113
+ lastUSBstate = currentUSBstate;
114
+ }
89
115
}
0 commit comments