Skip to content

Commit 6bfc929

Browse files
committed
update hid mouse to work cplayground express
button active state = 1
1 parent 2ac0b69 commit 6bfc929

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

examples/HID/hid_mouse/hid_mouse.ino

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,21 @@
1414
/* This sketch demonstrates USB HID mouse
1515
* Press button pin will move
1616
* - mouse toward bottom right of monitor
17+
*
18+
* Depending on the board, the button pin
19+
* and its active state (when pressed) are different
1720
*/
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+
1832

1933
// HID report descriptor using TinyUSB's template
2034
// Single Report (no ID) descriptor
@@ -25,24 +39,23 @@ uint8_t const desc_hid_report[] =
2539

2640
Adafruit_USBD_HID usb_hid;
2741

28-
const int pin = 7;
29-
3042
// the setup function runs once when you press reset or power the board
3143
void setup()
3244
{
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);
3547

3648
usb_hid.setPollInterval(2);
3749
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
3850

3951
usb_hid.begin();
4052

4153
Serial.begin(115200);
42-
while ( !Serial ) delay(10); // wait for native usb
4354

4455
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.");
4659
}
4760

4861
void loop()
@@ -51,10 +64,10 @@ void loop()
5164
delay(10);
5265

5366
// button is active low
54-
uint32_t const btn = 1 - digitalRead(pin);
67+
uint32_t const btn = digitalRead(pin);
5568

5669
// Remote wakeup
57-
if ( tud_suspended() && btn )
70+
if ( tud_suspended() && (btn == activeState) )
5871
{
5972
// Wake up host if we are in suspend mode
6073
// and REMOTE_WAKEUP feature is enabled by host
@@ -64,7 +77,7 @@ void loop()
6477
/*------------- Mouse -------------*/
6578
if ( usb_hid.ready() )
6679
{
67-
if ( btn )
80+
if ( btn == activeState )
6881
{
6982
int8_t const delta = 5;
7083
usb_hid.mouseMove(0, delta, delta); // no ID: right + down

0 commit comments

Comments
 (0)