15
15
// Require mouse control library
16
16
#include < MouseController.h>
17
17
18
+ // on a zero with debug port, use debug port
19
+ // #define SerialDebug Serial
20
+
21
+ // on a feather or non-debug Zero, use Serial1 (since USB is taken!)
22
+ #define SerialDebug Serial1
23
+
24
+ uint32_t lastUSBstate = 0 ;
25
+
18
26
// Initialize USB Controller
19
27
USBHost usb;
20
28
@@ -28,70 +36,87 @@ boolean rightButton = false;
28
36
29
37
// This function intercepts mouse movements
30
38
void mouseMoved () {
31
- SERIAL_PORT_MONITOR .print (" Move: " );
32
- SERIAL_PORT_MONITOR .print (mouse.getXChange ());
33
- SERIAL_PORT_MONITOR .print (" , " );
34
- SERIAL_PORT_MONITOR .println (mouse.getYChange ());
39
+ SerialDebug .print (" Move: " );
40
+ SerialDebug .print (mouse.getXChange ());
41
+ SerialDebug .print (" , " );
42
+ SerialDebug .println (mouse.getYChange ());
35
43
}
36
44
37
45
// This function intercepts mouse movements while a button is pressed
38
46
void mouseDragged () {
39
- SERIAL_PORT_MONITOR .print (" DRAG : " );
40
- SERIAL_PORT_MONITOR .print (mouse.getXChange ());
41
- SERIAL_PORT_MONITOR .print (" , " );
42
- SERIAL_PORT_MONITOR .println (mouse.getYChange ());
47
+ SerialDebug .print (" Drag : " );
48
+ SerialDebug .print (mouse.getXChange ());
49
+ SerialDebug .print (" , " );
50
+ SerialDebug .println (mouse.getYChange ());
43
51
}
44
52
45
53
// This function intercepts mouse button press
46
54
void mousePressed () {
47
- SERIAL_PORT_MONITOR .print (" Pressed: " );
55
+ SerialDebug .print (" Pressed: " );
48
56
if (mouse.getButton (LEFT_BUTTON)) {
49
- SERIAL_PORT_MONITOR .print (" L" );
57
+ SerialDebug .print (" L" );
50
58
leftButton = true ;
51
59
}
52
60
if (mouse.getButton (MIDDLE_BUTTON)) {
53
- SERIAL_PORT_MONITOR .print (" M" );
61
+ SerialDebug .print (" M" );
54
62
middleButton = true ;
55
63
}
56
64
if (mouse.getButton (RIGHT_BUTTON)) {
57
- SERIAL_PORT_MONITOR .print (" R" );
65
+ SerialDebug .print (" R" );
58
66
rightButton = true ;
59
67
}
60
- SERIAL_PORT_MONITOR .println ();
68
+ SerialDebug .println ();
61
69
}
62
70
63
71
// This function intercepts mouse button release
64
72
void mouseReleased () {
65
- SERIAL_PORT_MONITOR .print (" Released: " );
73
+ SerialDebug .print (" Released: " );
66
74
if (!mouse.getButton (LEFT_BUTTON) && leftButton == true ) {
67
- SERIAL_PORT_MONITOR .print (" L" );
75
+ SerialDebug .print (" L" );
68
76
leftButton = false ;
69
77
}
70
78
if (!mouse.getButton (MIDDLE_BUTTON) && middleButton == true ) {
71
- SERIAL_PORT_MONITOR .print (" M" );
79
+ SerialDebug .print (" M" );
72
80
middleButton = false ;
73
81
}
74
82
if (!mouse.getButton (RIGHT_BUTTON) && rightButton == true ) {
75
- SERIAL_PORT_MONITOR .print (" R" );
83
+ SerialDebug .print (" R" );
76
84
rightButton = false ;
77
85
}
78
- SERIAL_PORT_MONITOR .println ();
86
+ SerialDebug .println ();
79
87
}
80
88
81
89
void setup ()
82
90
{
83
- SERIAL_PORT_MONITOR.begin ( 115200 );
84
- while (!SERIAL_PORT_MONITOR); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
85
- SERIAL_PORT_MONITOR.println (" Mouse Controller Program started" );
91
+ SerialDebug.begin ( 115200 );
92
+ SerialDebug.println (" USB Host Mouse Controller Program started" );
86
93
87
94
if (usb.Init () == -1 )
88
- SERIAL_PORT_MONITOR .println (" OSC did not start." );
95
+ SerialDebug .println (" USB Host did not start." );
89
96
97
+ SerialDebug.println (" USB Host started" );
90
98
delay ( 20 );
91
99
}
92
100
93
101
void loop ()
94
102
{
95
103
// Process USB tasks
96
104
usb.Task ();
105
+
106
+ uint32_t currentUSBstate = usb.getUsbTaskState ();
107
+ if (lastUSBstate != currentUSBstate) {
108
+ SerialDebug.print (" USB state changed: 0x" );
109
+ SerialDebug.print (lastUSBstate, HEX);
110
+ SerialDebug.print (" -> 0x" );
111
+ SerialDebug.println (currentUSBstate, HEX);
112
+ switch (currentUSBstate) {
113
+ case USB_ATTACHED_SUBSTATE_SETTLE: SerialDebug.println (" Device Attached" ); break ;
114
+ case USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE: SerialDebug.println (" Detached, waiting for Device" ); break ;
115
+ case USB_ATTACHED_SUBSTATE_RESET_DEVICE: SerialDebug.println (" Resetting Device" ); break ;
116
+ case USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE: SerialDebug.println (" Reset complete" ); break ;
117
+ case USB_STATE_CONFIGURING: SerialDebug.println (" USB Configuring" ); break ;
118
+ case USB_STATE_RUNNING: SerialDebug.println (" USB Running" ); break ;
119
+ }
120
+ lastUSBstate = currentUSBstate;
121
+ }
97
122
}
0 commit comments