Skip to content

Commit 0174639

Browse files
committed
clean up hid_composite example
1 parent 16b018b commit 0174639

File tree

7 files changed

+39
-35
lines changed

7 files changed

+39
-35
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ before_install:
2626
- source install.sh
2727

2828
install:
29-
- arduino --install-library "Adafruit SPIFlash","MIDI Library"
29+
- arduino --install-library "Adafruit SPIFlash","MIDI Library","Adafruit seesaw Library"
3030
- git clone --quiet https://github.com/adafruit/SdFat.git $HOME/Arduino/libraries/SdFat
3131
- pip3 install --user adafruit-nrfutil
3232

examples/Composite/mouse_external_flash/mouse_external_flash.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void loop()
8787
uint32_t const btn = 1 - digitalRead(pin);
8888

8989
// Remote wakeup
90-
if ( tud_suspended() && btn )
90+
if ( USBDevice.suspended() && btn )
9191
{
9292
// Wake up host if we are in suspend mode
9393
// and REMOTE_WAKEUP feature is enabled by host

examples/Composite/mouse_ramdisk/mouse_ramdisk.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void loop()
7171
uint32_t const btn = 1 - digitalRead(pin);
7272

7373
// Remote wakeup
74-
if ( tud_suspended() && btn )
74+
if ( USBDevice.suspended() && btn )
7575
{
7676
// Wake up host if we are in suspend mode
7777
// and REMOTE_WAKEUP feature is enabled by host

examples/HID/hid_composite/hid_composite.ino

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,21 @@
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
2135
enum
@@ -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
3449
Adafruit_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
3952
void 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;

examples/HID/hid_composite_joy_featherwing/hid_composite_joy_featherwing.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void loop()
8181
if ( (abs(dx) > 3) || (abs(dy) > 30) )
8282
{
8383
// Remote wakeup if PC is suspended
84-
if ( tud_suspended() )
84+
if ( USBDevice.suspended() )
8585
{
8686
// Wake up host if we are in suspend mode
8787
// and REMOTE_WAKEUP feature is enabled by host

examples/HID/hid_keyboard/hid_keyboard.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void loop()
5959
delay(2);
6060

6161
// // Remote wakeup
62-
// if ( tud_suspended() && btn )
62+
// if ( USBDevice.suspended() && btn )
6363
// {
6464
// // Wake up host if we are in suspend mode
6565
// // and REMOTE_WAKEUP feature is enabled by host

examples/HID/hid_mouse/hid_mouse.ino

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,23 @@ void loop()
6161
// poll gpio once each 10 ms
6262
delay(10);
6363

64-
// button is active low
65-
uint32_t const btn = digitalRead(pin);
64+
// Whether button is pressed
65+
bool btn_pressed = (digitalRead(pin) == activeState);
66+
67+
// nothing to do if button is not pressed
68+
if (!btn_pressed) return;
6669

6770
// Remote wakeup
68-
if ( tud_suspended() && (btn == activeState) )
71+
if ( USBDevice.suspended() )
6972
{
7073
// Wake up host if we are in suspend mode
7174
// and REMOTE_WAKEUP feature is enabled by host
7275
USBDevice.remoteWakeup();
7376
}
7477

75-
/*------------- Mouse -------------*/
7678
if ( usb_hid.ready() )
7779
{
78-
if ( btn == activeState )
79-
{
80-
int8_t const delta = 5;
81-
usb_hid.mouseMove(0, delta, delta); // no ID: right + down
82-
83-
// delay a bit before attempt to send keyboard report
84-
delay(10);
85-
}
80+
int8_t const delta = 5;
81+
usb_hid.mouseMove(0, delta, delta); // no ID: right + down
8682
}
8783
}

0 commit comments

Comments
 (0)