-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb_keyboard_button_box.ino
235 lines (202 loc) · 7.31 KB
/
usb_keyboard_button_box.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
MIT License
Copyright (c) 2021 [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/* USB Keyboard Button Box with 11 buttons and 3 rotary encoders.
*
* Tested on Playstation 4 with F1 2020 racing game. Note a second
* keyboard can be plugged in at the same time. For example, a number
* keypad can be used for buttons and this project for rotary encoders.
*/
#include "Adafruit_seesaw.h" // Install using IDE library manager
#include <seesaw_neopixel.h>
#include <Bounce2.h> // Install using IDE library manager
#include <Keyboard.h>
/* Make this true only for debugging because it slows down sketch. */
#define DEBUG_ON false
#if DEBUG_ON
#define DEBUG_begin(...) Serial.begin(__VA_ARGS__)
#define DEBUG_print(...) Serial.print(__VA_ARGS__)
#define DEBUG_println(...) Serial.println(__VA_ARGS__)
#else
#define DEBUG_begin(...)
#define DEBUG_print(...)
#define DEBUG_println(...)
#endif
/* Use GPIO pins as button inputs */
#define NUM_BUTTONS 11
Bounce * buttons = new Bounce[NUM_BUTTONS];
typedef struct {
const uint8_t pin_number;
const uint8_t usb_keycode;
bool key_down;
} usb_key_pins_t;
usb_key_pins_t USB_Keys[NUM_BUTTONS] = {
{A0, 'a', false},
{A1, 'b', false},
{A2, 'c', false},
{A3, 'd', false},
{SDA, 'e', false},
{SCL, 'f', false},
{PIN_SERIAL1_TX, 'g', false},
{PIN_SERIAL1_RX, 'h', false},
{SCK, 'i', false},
{MISO, 'j', false},
{MOSI, 'k', false},
};
/* Setup for I2C rotary encoder boards */
#define SS_SWITCH 24 // this is the pin on the encoder connected to switch
#define SS_NEOPIX 6 // this is the pin on the encoder connected to neopixel
#define SEESAW_BASE_ADDR 0x36 // I2C address, starts with 0x36
#define NUM_ENCODERS 3
// create encoders!
Adafruit_seesaw encoders[NUM_ENCODERS];
// create encoder pixels
seesaw_NeoPixel encoder_pixels[NUM_ENCODERS] = {
seesaw_NeoPixel(1, SS_NEOPIX, NEO_GRB + NEO_KHZ800),
seesaw_NeoPixel(1, SS_NEOPIX, NEO_GRB + NEO_KHZ800),
seesaw_NeoPixel(1, SS_NEOPIX, NEO_GRB + NEO_KHZ800)};
int32_t encoder_positions[NUM_ENCODERS] = {0, 0, 0};
bool found_encoders[NUM_ENCODERS] = {false, false, false};
bool encoder_buttons[NUM_ENCODERS] = {false, false, false};
const uint8_t USB_KEYS_DOWN[NUM_ENCODERS] = {KEY_F1, KEY_F3, KEY_F5};
const uint8_t USB_KEYS_UP[NUM_ENCODERS] = {KEY_F2, KEY_F4, KEY_F6};
const uint8_t USB_KEYS_BUTTON[NUM_ENCODERS] = {KEY_F7, KEY_F8, KEY_F9};
void setup()
{
#if DEBUG_ON
DEBUG_begin(115200);
// wait for serial port to open
while (!Serial) delay(10);
#endif
DEBUG_println("USB Keyboard Button Box");
for (int i = 0; i < NUM_BUTTONS; i++) {
buttons[i].attach( USB_Keys[i].pin_number , INPUT_PULLUP );
buttons[i].interval(5);
}
DEBUG_println("Looking for rotary encoder seesaws!");
for (uint8_t enc=0; enc < NUM_ENCODERS; enc++) {
// See if we can find encoders on this address
if (! encoders[enc].begin(SEESAW_BASE_ADDR + enc) ||
! encoder_pixels[enc].begin(SEESAW_BASE_ADDR + enc)) {
DEBUG_print("Couldn't find encoder #");
DEBUG_println(enc);
}
else {
DEBUG_print("Found encoder + pixel #");
DEBUG_println(enc);
uint32_t version = ((encoders[enc].getVersion() >> 16) & 0xFFFF);
if (version != 4991) {
DEBUG_print("Wrong firmware loaded? ");
DEBUG_println(version);
while(1) delay(10);
}
DEBUG_println("Found Product 4991");
// use a pin for the built in encoder switch
encoders[enc].pinMode(SS_SWITCH, INPUT_PULLUP);
// get starting position
encoder_positions[enc] = encoders[enc].getEncoderPosition();
DEBUG_println("Turning on interrupts");
delay(10);
encoders[enc].setGPIOInterrupts((uint32_t)1 << SS_SWITCH, 1);
encoders[enc].enableEncoderInterrupt();
// set not so bright!
encoder_pixels[enc].setBrightness(30);
encoder_pixels[enc].show();
found_encoders[enc] = true;
}
}
DEBUG_println("Encoders started");
Keyboard.begin();
}
void loop()
{
for (int pin = 0; pin < NUM_BUTTONS; pin++) {
buttons[pin].update();
if (buttons[pin].fell()) {
if (!USB_Keys[pin].key_down) {
DEBUG_print("pin_number ");
DEBUG_print(USB_Keys[pin].pin_number);
DEBUG_print(" press usb_keycode ");
DEBUG_println(USB_Keys[pin].usb_keycode, HEX);
Keyboard.press(USB_Keys[pin].usb_keycode);
USB_Keys[pin].key_down = true;
}
}
else if (buttons[pin].rose()) {
if (USB_Keys[pin].key_down) {
DEBUG_print("pin_number ");
DEBUG_print(USB_Keys[pin].pin_number);
DEBUG_print(" release usb_keycode ");
DEBUG_println(USB_Keys[pin].usb_keycode, HEX);
Keyboard.release(USB_Keys[pin].usb_keycode);
USB_Keys[pin].key_down = false;
}
}
}
for (int enc = 0; enc < NUM_ENCODERS; enc++) {
if (found_encoders[enc] == false) continue;
// Handle encoder button presses and releases
int new_button = encoders[enc].digitalRead(SS_SWITCH);
if (encoder_buttons[enc] != new_button) {
if (new_button) {
Keyboard.release(USB_KEYS_BUTTON[enc]);
encoder_buttons[enc] = false;
DEBUG_print("Encoder button #");
DEBUG_print(enc);
DEBUG_println(" released");
}
else {
Keyboard.press(USB_KEYS_BUTTON[enc]);
encoder_buttons[enc] = true;
DEBUG_print("Encoder button #");
DEBUG_print(enc);
DEBUG_println(" pressed");
}
encoder_buttons[enc] = new_button;
}
// Handle encoder rotation
int32_t new_position = encoders[enc].getEncoderPosition();
if (encoder_positions[enc] != new_position) {
DEBUG_print("Encoder #");
DEBUG_print(enc);
DEBUG_print(" -> ");
DEBUG_println(new_position);
// Clock wise rotation
if (new_position < encoder_positions[enc]) {
Keyboard.write(USB_KEYS_UP[enc]);
encoder_pixels[enc].setPixelColor(0, 0xFF0000);
encoder_pixels[enc].show();
}
// Counter clock wise rotation
else if (new_position > encoder_positions[enc]) {
Keyboard.write(USB_KEYS_DOWN[enc]);
encoder_pixels[enc].setPixelColor(0, 0x00FF00);
encoder_pixels[enc].show();
}
encoder_positions[enc] = new_position;
}
else {
encoder_pixels[enc].setPixelColor(0, 0);
encoder_pixels[enc].show();
}
}
#if DEBUG_ON
delay(10);
#endif
}