99 any redistribution
1010*********************************************************************/
1111
12- /* This sketch demonstrates USB HID mouse using Joystick Feather Wing
13- * https://www.adafruit.com/product/3632
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
1418 *
1519 * Following library is required
1620 * - Adafruit_seesaw
1923#include " Adafruit_TinyUSB.h"
2024#include " Adafruit_seesaw.h"
2125
22- #define BUTTON_RIGHT 6
23- #define BUTTON_DOWN 7
24- #define BUTTON_LEFT 9
25- #define BUTTON_UP 10
26- #define BUTTON_SEL 14
27- uint32_t button_mask = (1 << BUTTON_RIGHT) | (1 << BUTTON_DOWN) |
28- (1 << BUTTON_LEFT) | (1 << BUTTON_UP) | (1 << BUTTON_SEL);
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);
2932
3033Adafruit_seesaw ss;
3134
35+ // Report ID
36+ enum
37+ {
38+ RID_KEYBOARD = 1 ,
39+ RID_MOUSE
40+ };
41+
3242// HID report descriptor using TinyUSB's template
33- // Single Report (no ID) descriptor
3443uint8_t const desc_hid_report[] =
3544{
36- TUD_HID_REPORT_DESC_MOUSE ()
45+ TUD_HID_REPORT_DESC_KEYBOARD ( HID_REPORT_ID (RID_KEYBOARD), ),
46+ TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID (RID_MOUSE), )
3747};
3848
3949// USB HID object
@@ -72,26 +82,24 @@ void loop()
7282 // poll gpio once each 10 ms
7383 delay (10 );
7484
85+ // If either analog stick or any buttons is pressed
86+ bool has_action = false ;
87+
88+ /* ------------- Mouse -------------*/
7589 int y = ss.analogRead (2 );
7690 int x = ss.analogRead (3 );
7791
78- int dx = x - last_x;
79- int dy = y - last_y;
92+ // reduce the delta by half to slow down the cursor move
93+ int dx = (x - last_x) / 2 ;
94+ int dy = (y - last_y) / 2 ;
8095
81- if ( (abs (dx) > 3 ) || (abs (dy) > 30 ) )
96+ if ( (abs (dx) > 3 ) || (abs (dy) > 3 ) )
8297 {
83- // Remote wakeup if PC is suspended
84- if ( USBDevice.suspended () )
85- {
86- // Wake up host if we are in suspend mode
87- // and REMOTE_WAKEUP feature is enabled by host
88- USBDevice.remoteWakeup ();
89- }
98+ has_action = true ;
9099
91- /* ------------- Mouse -------------*/
92100 if ( usb_hid.ready () )
93101 {
94- usb_hid.mouseMove (0 , dx, dy); // no ID: right + down
102+ usb_hid.mouseMove (RID_MOUSE , dx, dy); // no ID: right + down
95103
96104 last_x = x;
97105 last_y = y;
@@ -100,4 +108,44 @@ void loop()
100108 delay (10 );
101109 }
102110 }
111+
112+
113+ /* ------------- Keyboard -------------*/
114+ // button is active low, invert read value for convenience
115+ uint32_t buttons = ~ss.digitalReadBulk (button_mask);
116+
117+ if ( usb_hid.ready () )
118+ {
119+ // use to prevent sending multiple consecutive zero report
120+ static bool has_key = false ;
121+
122+ if ( buttons & button_mask )
123+ {
124+ has_action = true ;
125+ has_key = true ;
126+
127+ uint8_t keycode[6 ] = { 0 };
128+
129+ if ( buttons & (1 << BUTTON_A) ) keycode[0 ] = HID_KEY_A;
130+ if ( buttons & (1 << BUTTON_B) ) keycode[0 ] = HID_KEY_B;
131+ if ( buttons & (1 << BUTTON_X) ) keycode[0 ] = HID_KEY_X;
132+ if ( buttons & (1 << BUTTON_Y) ) keycode[0 ] = HID_KEY_Y;
133+
134+ usb_hid.keyboardReport (RID_KEYBOARD, 0 , keycode);
135+ }else
136+ {
137+ // send empty key report if previously has key pressed
138+ if (has_key) usb_hid.keyboardRelease (RID_KEYBOARD);
139+ has_key = false ;
140+ }
141+ }
142+
143+ /* ------------- Remote Wakeup -------------*/
144+ // Remote wakeup if PC is suspended and we has user interaction with joy feather wing
145+ if ( has_action && USBDevice.suspended () )
146+ {
147+ // Wake up only works if REMOTE_WAKEUP feature is enable by host
148+ // Usually this is the case with Mouse/Keyboard device
149+ USBDevice.remoteWakeup ();
150+ }
103151}
0 commit comments