15
15
* Press button pin will move
16
16
* - mouse toward bottom right of monitor
17
17
* - send 'a' key
18
+ *
19
+ * Depending on the board, the button pin
20
+ * and its active state (when pressed) are different
18
21
*/
22
+ #if defined ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS
23
+ const int pin = 4 ; // Left Button
24
+ bool activeState = true ;
25
+ #elif defined ARDUINO_NRF52840_FEATHER
26
+ const int pin = 7 ; // UserSw
27
+ bool activeState = false ;
28
+ #else
29
+ const int pin = 12 ;
30
+ bool activeState = false ;
31
+ #endif
32
+
19
33
20
34
// Report ID
21
35
enum
@@ -31,10 +45,9 @@ uint8_t const desc_hid_report[] =
31
45
TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID (RID_MOUSE), )
32
46
};
33
47
48
+ // USB HID object
34
49
Adafruit_USBD_HID usb_hid;
35
50
36
- const int pin = 7 ;
37
-
38
51
// the setup function runs once when you press reset or power the board
39
52
void setup ()
40
53
{
@@ -43,12 +56,10 @@ void setup()
43
56
44
57
usb_hid.begin ();
45
58
46
- // Set up button
47
- pinMode (pin, INPUT_PULLUP);
59
+ // Set up button, pullup opposite to active state
60
+ pinMode (pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
48
61
49
62
Serial.begin (115200 );
50
- while ( !Serial ) delay (10 ); // wait for native usb
51
-
52
63
Serial.println (" Adafruit TinyUSB HID Composite example" );
53
64
}
54
65
@@ -57,28 +68,25 @@ void loop()
57
68
// poll gpio once each 10 ms
58
69
delay (10 );
59
70
60
- // button is active low
61
- uint32_t const btn = 1 - digitalRead (pin);
71
+ // Whether button is pressed
72
+ bool btn_pressed = ( digitalRead (pin) == activeState );
62
73
63
74
// Remote wakeup
64
- if ( tud_suspended () && btn )
75
+ if ( USBDevice. suspended () && btn_pressed )
65
76
{
66
77
// Wake up host if we are in suspend mode
67
78
// and REMOTE_WAKEUP feature is enabled by host
68
79
USBDevice.remoteWakeup ();
69
80
}
70
81
71
82
/* ------------- Mouse -------------*/
72
- if ( usb_hid.ready () )
83
+ if ( usb_hid.ready () && btn_pressed )
73
84
{
74
- if ( btn )
75
- {
76
- int8_t const delta = 5 ;
77
- usb_hid.mouseMove (RID_MOUSE, delta, delta); // right + down
85
+ int8_t const delta = 5 ;
86
+ usb_hid.mouseMove (RID_MOUSE, delta, delta); // right + down
78
87
79
- // delay a bit before attempt to send keyboard report
80
- delay (10 );
81
- }
88
+ // delay a bit before attempt to send keyboard report
89
+ delay (10 );
82
90
}
83
91
84
92
/* ------------- Keyboard -------------*/
@@ -87,7 +95,7 @@ void loop()
87
95
// use to avoid send multiple consecutive zero report for keyboard
88
96
static bool has_key = false ;
89
97
90
- if ( btn )
98
+ if ( btn_pressed )
91
99
{
92
100
uint8_t keycode[6 ] = { 0 };
93
101
keycode[0 ] = HID_KEY_A;
0 commit comments