Skip to content

Commit c8cd4cf

Browse files
authored
Merge pull request #9 from adafruit/develop
Add more hid examples with joy feather wing.
2 parents c525b52 + 62a9520 commit c8cd4cf

File tree

16 files changed

+241
-63
lines changed

16 files changed

+241
-63
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: 31 additions & 20 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,51 +56,49 @@ 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");
64+
65+
// wait until device mounted
66+
while( !USBDevice.mounted() ) delay(1);
5367
}
5468

5569
void loop()
5670
{
5771
// poll gpio once each 10 ms
5872
delay(10);
5973

60-
// button is active low
61-
uint32_t const btn = 1 - digitalRead(pin);
74+
// Whether button is pressed
75+
bool btn_pressed = (digitalRead(pin) == activeState);
6276

6377
// Remote wakeup
64-
if ( tud_suspended() && btn )
78+
if ( USBDevice.suspended() && btn_pressed )
6579
{
6680
// Wake up host if we are in suspend mode
6781
// and REMOTE_WAKEUP feature is enabled by host
68-
tud_remote_wakeup();
82+
USBDevice.remoteWakeup();
6983
}
7084

7185
/*------------- Mouse -------------*/
72-
if ( usb_hid.ready() )
86+
if ( usb_hid.ready() && btn_pressed )
7387
{
74-
if ( btn )
75-
{
76-
int8_t const delta = 5;
77-
usb_hid.mouseMove(RID_MOUSE, delta, delta); // right + down
88+
int8_t const delta = 5;
89+
usb_hid.mouseMove(RID_MOUSE, delta, delta); // right + down
7890

79-
// delay a bit before attempt to send keyboard report
80-
delay(10);
81-
}
91+
// delay a bit before attempt to send keyboard report
92+
delay(10);
8293
}
8394

8495
/*------------- Keyboard -------------*/
8596
if ( usb_hid.ready() )
8697
{
87-
// use to avoid send multiple consecutive zero report for keyboard
98+
// use to prevent sending multiple consecutive zero report
8899
static bool has_key = false;
89100

90-
if ( btn )
101+
if ( btn_pressed )
91102
{
92103
uint8_t keycode[6] = { 0 };
93104
keycode[0] = HID_KEY_A;
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*********************************************************************
2+
Adafruit invests time and resources providing this open source code,
3+
please support Adafruit and open-source hardware by purchasing
4+
products from Adafruit!
5+
6+
MIT license, check LICENSE for more information
7+
Copyright (c) 2019 Ha Thach for Adafruit Industries
8+
All text above, and the splash screen below must be included in
9+
any redistribution
10+
*********************************************************************/
11+
12+
/* This sketch demonstrates USB HID Mouse and Keyboard with Joy Feather Wing.
13+
* - The analog stick move mouse cursor
14+
* - Button A, B, X, Y will send character a, b, x, y
15+
* - Any actions will wake up PC host if it is in suspended (standby) mode.
16+
*
17+
* Joy Feather Wing: https://www.adafruit.com/product/3632
18+
*
19+
* Following library is required
20+
* - Adafruit_seesaw
21+
*/
22+
23+
#include "Adafruit_TinyUSB.h"
24+
#include "Adafruit_seesaw.h"
25+
26+
#define BUTTON_A 6
27+
#define BUTTON_B 7
28+
#define BUTTON_Y 9
29+
#define BUTTON_X 10
30+
uint32_t button_mask = (1 << BUTTON_A) | (1 << BUTTON_B) |
31+
(1 << BUTTON_Y) | (1 << BUTTON_X);
32+
33+
Adafruit_seesaw ss;
34+
35+
// Report ID
36+
enum
37+
{
38+
RID_KEYBOARD = 1,
39+
RID_MOUSE
40+
};
41+
42+
// HID report descriptor using TinyUSB's template
43+
uint8_t const desc_hid_report[] =
44+
{
45+
TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(RID_KEYBOARD), ),
46+
TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(RID_MOUSE), )
47+
};
48+
49+
// USB HID object
50+
Adafruit_USBD_HID usb_hid;
51+
52+
int last_x, last_y;
53+
54+
// the setup function runs once when you press reset or power the board
55+
void setup()
56+
{
57+
usb_hid.setPollInterval(2);
58+
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
59+
60+
usb_hid.begin();
61+
62+
Serial.begin(115200);
63+
Serial.println("Adafruit TinyUSB HID Mouse with Joy FeatherWing example");
64+
65+
if(!ss.begin(0x49)){
66+
Serial.println("ERROR! seesaw not found");
67+
while(1);
68+
} else {
69+
Serial.println("seesaw started");
70+
Serial.print("version: ");
71+
Serial.println(ss.getVersion(), HEX);
72+
}
73+
ss.pinModeBulk(button_mask, INPUT_PULLUP);
74+
ss.setGPIOInterrupts(button_mask, 1);
75+
76+
last_y = ss.analogRead(2);
77+
last_x = ss.analogRead(3);
78+
79+
// wait until device mounted
80+
while( !USBDevice.mounted() ) delay(1);
81+
}
82+
83+
void loop()
84+
{
85+
// poll gpio once each 10 ms
86+
delay(10);
87+
88+
// If either analog stick or any buttons is pressed
89+
bool has_action = false;
90+
91+
/*------------- Mouse -------------*/
92+
int y = ss.analogRead(2);
93+
int x = ss.analogRead(3);
94+
95+
// reduce the delta by half to slow down the cursor move
96+
int dx = (x - last_x) / 2;
97+
int dy = (y - last_y) / 2;
98+
99+
if ( (abs(dx) > 3) || (abs(dy) > 3) )
100+
{
101+
has_action = true;
102+
103+
if ( usb_hid.ready() )
104+
{
105+
usb_hid.mouseMove(RID_MOUSE, dx, dy); // no ID: right + down
106+
107+
last_x = x;
108+
last_y = y;
109+
110+
// delay a bit before attempt to send keyboard report
111+
delay(10);
112+
}
113+
}
114+
115+
116+
/*------------- Keyboard -------------*/
117+
// button is active low, invert read value for convenience
118+
uint32_t buttons = ~ss.digitalReadBulk(button_mask);
119+
120+
if ( usb_hid.ready() )
121+
{
122+
// use to prevent sending multiple consecutive zero report
123+
static bool has_key = false;
124+
125+
if ( buttons & button_mask )
126+
{
127+
has_action = true;
128+
has_key = true;
129+
130+
uint8_t keycode[6] = { 0 };
131+
132+
if ( buttons & (1 << BUTTON_A) ) keycode[0] = HID_KEY_A;
133+
if ( buttons & (1 << BUTTON_B) ) keycode[0] = HID_KEY_B;
134+
if ( buttons & (1 << BUTTON_X) ) keycode[0] = HID_KEY_X;
135+
if ( buttons & (1 << BUTTON_Y) ) keycode[0] = HID_KEY_Y;
136+
137+
usb_hid.keyboardReport(RID_KEYBOARD, 0, keycode);
138+
}else
139+
{
140+
// send empty key report if previously has key pressed
141+
if (has_key) usb_hid.keyboardRelease(RID_KEYBOARD);
142+
has_key = false;
143+
}
144+
}
145+
146+
/*------------- Remote Wakeup -------------*/
147+
// Remote wakeup if PC is suspended and we has user interaction with joy feather wing
148+
if ( has_action && USBDevice.suspended() )
149+
{
150+
// Wake up only works if REMOTE_WAKEUP feature is enable by host
151+
// Usually this is the case with Mouse/Keyboard device
152+
USBDevice.remoteWakeup();
153+
}
154+
}

examples/HID/hid_generic_inout/hid_generic_inout.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ void setup()
4949
usb_hid.begin();
5050

5151
Serial.begin(115200);
52-
while ( !Serial ) delay(10); // wait for native usb
52+
53+
// wait until device mounted
54+
while( !USBDevice.mounted() ) delay(1);
5355

5456
Serial.println("Adafruit TinyUSB HID Generic In Out example");
5557
}

examples/HID/hid_keyboard/hid_keyboard.ino

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ void setup()
5050
{
5151
pinMode(pins[i], INPUT_PULLUP);
5252
}
53+
54+
// wait until device mounted
55+
while( !USBDevice.mounted() ) delay(1);
5356
}
5457

5558

@@ -59,11 +62,11 @@ void loop()
5962
delay(2);
6063

6164
// // Remote wakeup
62-
// if ( tud_suspended() && btn )
65+
// if ( USBDevice.suspended() && btn )
6366
// {
6467
// // Wake up host if we are in suspend mode
6568
// // and REMOTE_WAKEUP feature is enabled by host
66-
// tud_remote_wakeup();
69+
// USBDevice.remoteWakeup();
6770
// }
6871

6972
if ( !usb_hid.ready() ) return;

0 commit comments

Comments
 (0)