Skip to content

Commit f41f197

Browse files
committed
Working demo with nice status change notifications
1 parent e25840e commit f41f197

File tree

1 file changed

+49
-23
lines changed

1 file changed

+49
-23
lines changed

libraries/USBHost/examples/KeyboardController/KeyboardController.ino

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Keyboard Controller Example
33
44
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
66
77
created 8 Oct 2012
88
by Cristian Maglie
@@ -15,6 +15,13 @@
1515
// Require keyboard control library
1616
#include <KeyboardController.h>
1717

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+
1825
// Initialize USB Controller
1926
USBHost usb;
2027

@@ -25,65 +32,84 @@ void printKey();
2532

2633
// This function intercepts key press
2734
void keyPressed() {
28-
SERIAL_PORT_MONITOR.print("Pressed: ");
35+
SerialDebug.print("Pressed: ");
2936
printKey();
3037
}
3138

3239
// This function intercepts key release
3340
void keyReleased() {
34-
SERIAL_PORT_MONITOR.print("Released: ");
41+
SerialDebug.print("Released: ");
3542
printKey();
3643
}
3744

3845
void printKey() {
3946
// 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());
4249

4350
// getModifiers() returns a bits field with the modifiers-keys
4451
int mod = keyboard.getModifiers();
45-
SERIAL_PORT_MONITOR.print(" mod:");
46-
SERIAL_PORT_MONITOR.print(mod);
52+
SerialDebug.print(" mod:");
53+
SerialDebug.print(mod);
4754

48-
SERIAL_PORT_MONITOR.print(" => ");
55+
SerialDebug.print(" => ");
4956

5057
if (mod & LeftCtrl)
51-
SERIAL_PORT_MONITOR.print("L-Ctrl ");
58+
SerialDebug.print("L-Ctrl ");
5259
if (mod & LeftShift)
53-
SERIAL_PORT_MONITOR.print("L-Shift ");
60+
SerialDebug.print("L-Shift ");
5461
if (mod & Alt)
55-
SERIAL_PORT_MONITOR.print("Alt ");
62+
SerialDebug.print("Alt ");
5663
if (mod & LeftCmd)
57-
SERIAL_PORT_MONITOR.print("L-Cmd ");
64+
SerialDebug.print("L-Cmd ");
5865
if (mod & RightCtrl)
59-
SERIAL_PORT_MONITOR.print("R-Ctrl ");
66+
SerialDebug.print("R-Ctrl ");
6067
if (mod & RightShift)
61-
SERIAL_PORT_MONITOR.print("R-Shift ");
68+
SerialDebug.print("R-Shift ");
6269
if (mod & AltGr)
63-
SERIAL_PORT_MONITOR.print("AltGr ");
70+
SerialDebug.print("AltGr ");
6471
if (mod & RightCmd)
65-
SERIAL_PORT_MONITOR.print("R-Cmd ");
72+
SerialDebug.print("R-Cmd ");
6673

6774
// getKey() returns the ASCII translation of OEM key
6875
// combined with modifiers.
69-
SERIAL_PORT_MONITOR.write(keyboard.getKey());
70-
SERIAL_PORT_MONITOR.println();
76+
SerialDebug.write(keyboard.getKey());
77+
SerialDebug.println();
7178
}
7279

80+
uint32_t lastUSBstate = 0;
81+
7382
void setup()
7483
{
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");
7886

7987
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");
8291
delay( 20 );
8392
}
8493

8594
void loop()
8695
{
8796
// Process USB tasks
8897
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+
}
89115
}

0 commit comments

Comments
 (0)