9
9
any redistribution
10
10
*********************************************************************/
11
11
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
14
18
*
15
19
* Following library is required
16
20
* - Adafruit_seesaw
19
23
#include " Adafruit_TinyUSB.h"
20
24
#include " Adafruit_seesaw.h"
21
25
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);
29
32
30
33
Adafruit_seesaw ss;
31
34
35
+ // Report ID
36
+ enum
37
+ {
38
+ RID_KEYBOARD = 1 ,
39
+ RID_MOUSE
40
+ };
41
+
32
42
// HID report descriptor using TinyUSB's template
33
- // Single Report (no ID) descriptor
34
43
uint8_t const desc_hid_report[] =
35
44
{
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), )
37
47
};
38
48
39
49
// USB HID object
@@ -72,26 +82,24 @@ void loop()
72
82
// poll gpio once each 10 ms
73
83
delay (10 );
74
84
85
+ // If either analog stick or any buttons is pressed
86
+ bool has_action = false ;
87
+
88
+ /* ------------- Mouse -------------*/
75
89
int y = ss.analogRead (2 );
76
90
int x = ss.analogRead (3 );
77
91
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 ;
80
95
81
- if ( (abs (dx) > 3 ) || (abs (dy) > 30 ) )
96
+ if ( (abs (dx) > 3 ) || (abs (dy) > 3 ) )
82
97
{
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 ;
90
99
91
- /* ------------- Mouse -------------*/
92
100
if ( usb_hid.ready () )
93
101
{
94
- usb_hid.mouseMove (0 , dx, dy); // no ID: right + down
102
+ usb_hid.mouseMove (RID_MOUSE , dx, dy); // no ID: right + down
95
103
96
104
last_x = x;
97
105
last_y = y;
@@ -100,4 +108,44 @@ void loop()
100
108
delay (10 );
101
109
}
102
110
}
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
+ }
103
151
}
0 commit comments