14
14
/* This sketch demonstrates USB HID mouse
15
15
* Press button pin will move
16
16
* - mouse toward bottom right of monitor
17
+ *
18
+ * Depending on the board, the button pin
19
+ * and its active state (when pressed) are different
17
20
*/
21
+ #if defined ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS
22
+ const int pin = 4 ; // Left Button
23
+ bool activeState = true ;
24
+ #elif defined ARDUINO_NRF52840_FEATHER
25
+ const int pin = 7 ; // UserSw
26
+ bool activeState = false ;
27
+ #else
28
+ const int pin = 12 ;
29
+ bool activeState = false ;
30
+ #endif
31
+
18
32
19
33
// HID report descriptor using TinyUSB's template
20
34
// Single Report (no ID) descriptor
@@ -25,24 +39,23 @@ uint8_t const desc_hid_report[] =
25
39
26
40
Adafruit_USBD_HID usb_hid;
27
41
28
- const int pin = 7 ;
29
-
30
42
// the setup function runs once when you press reset or power the board
31
43
void setup ()
32
44
{
33
- // Set up button
34
- pinMode (pin, INPUT_PULLUP);
45
+ // Set up button, pullup opposite to active state
46
+ pinMode (pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
35
47
36
48
usb_hid.setPollInterval (2 );
37
49
usb_hid.setReportDescriptor (desc_hid_report, sizeof (desc_hid_report));
38
50
39
51
usb_hid.begin ();
40
52
41
53
Serial.begin (115200 );
42
- while ( !Serial ) delay (10 ); // wait for native usb
43
54
44
55
Serial.println (" Adafruit TinyUSB HID Mouse example" );
45
- Serial.print (" Wire pin " ); Serial.print (pin); Serial.println (" to GND to move cursor to bottom right corner." )
56
+ Serial.print (" Wire pin " );
57
+ Serial.print (pin);
58
+ Serial.println (" to GND to move cursor to bottom right corner." );
46
59
}
47
60
48
61
void loop ()
@@ -51,10 +64,10 @@ void loop()
51
64
delay (10 );
52
65
53
66
// button is active low
54
- uint32_t const btn = 1 - digitalRead (pin);
67
+ uint32_t const btn = digitalRead (pin);
55
68
56
69
// Remote wakeup
57
- if ( tud_suspended () && btn )
70
+ if ( tud_suspended () && ( btn == activeState) )
58
71
{
59
72
// Wake up host if we are in suspend mode
60
73
// and REMOTE_WAKEUP feature is enabled by host
@@ -64,7 +77,7 @@ void loop()
64
77
/* ------------- Mouse -------------*/
65
78
if ( usb_hid.ready () )
66
79
{
67
- if ( btn )
80
+ if ( btn == activeState )
68
81
{
69
82
int8_t const delta = 5 ;
70
83
usb_hid.mouseMove (0 , delta, delta); // no ID: right + down
0 commit comments