1515 * Press button pin will move
1616 * - mouse toward bottom right of monitor
1717 * - send 'a' key
18+ *
19+ * Depending on the board, the button pin
20+ * and its active state (when pressed) are different
1821 */
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+
1933
2034// Report ID
2135enum
@@ -31,10 +45,9 @@ uint8_t const desc_hid_report[] =
3145 TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID (RID_MOUSE), )
3246};
3347
48+ // USB HID object
3449Adafruit_USBD_HID usb_hid;
3550
36- const int pin = 7 ;
37-
3851// the setup function runs once when you press reset or power the board
3952void setup ()
4053{
@@ -43,12 +56,10 @@ void setup()
4356
4457 usb_hid.begin ();
4558
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);
4861
4962 Serial.begin (115200 );
50- while ( !Serial ) delay (10 ); // wait for native usb
51-
5263 Serial.println (" Adafruit TinyUSB HID Composite example" );
5364}
5465
@@ -57,28 +68,25 @@ void loop()
5768 // poll gpio once each 10 ms
5869 delay (10 );
5970
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 );
6273
6374 // Remote wakeup
64- if ( tud_suspended () && btn )
75+ if ( USBDevice. suspended () && btn_pressed )
6576 {
6677 // Wake up host if we are in suspend mode
6778 // and REMOTE_WAKEUP feature is enabled by host
6879 USBDevice.remoteWakeup ();
6980 }
7081
7182 /* ------------- Mouse -------------*/
72- if ( usb_hid.ready () )
83+ if ( usb_hid.ready () && btn_pressed )
7384 {
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
7887
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 );
8290 }
8391
8492 /* ------------- Keyboard -------------*/
@@ -87,7 +95,7 @@ void loop()
8795 // use to avoid send multiple consecutive zero report for keyboard
8896 static bool has_key = false ;
8997
90- if ( btn )
98+ if ( btn_pressed )
9199 {
92100 uint8_t keycode[6 ] = { 0 };
93101 keycode[0 ] = HID_KEY_A;
0 commit comments