-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.ino
More file actions
203 lines (172 loc) · 4.25 KB
/
main.ino
File metadata and controls
203 lines (172 loc) · 4.25 KB
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
#include "Keyboard.h"
// PIN numbers (CONSTANTS)
const int TLPIN = 12; // top row: left, center right
const int TCPIN = 11;
const int TRPIN = 10;
const int MLPIN = 9; // middle row: left, center, right
const int MCPIN = 8;
const int MRPIN = 7;
const int BLPIN = 6; // bottom row: left, right
const int BRPIN = 5;
const int BNPIN = 4;// rotary encoder: button
const int RPIN = 2; // rotary encoder: right
const int LPIN = 3; // rotary encoder: left
const int FPIN = A0; // foot switch pin (digital)
const int APIN = A1; // potentiometer pin (analog)
const int JPIN = A3; // jumper pin (deactivates all functions if pulled down)
// pins to monitor
const int MPINS[] = {TLPIN, TCPIN, TRPIN, MLPIN, MCPIN, MRPIN, BLPIN, BRPIN, BNPIN, RPIN, FPIN};
int last_state[11]; // last pin states
int decay[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // current decay times per pin
// other digital pins
const int OPINS[] = {LPIN, JPIN};
// other variables and constants
const int DLY = 0; // extra delay per run, in ms (refresh rate)
const int DCY = 5; // decay per key, in runs (min. DCY*DLY milliseconds)
int analog_val = 0;
double analog_ratio = 0;
// *********************************
// MAIN FUNCTION, WITH CONFIGURATION
// *********************************
void keyEvent(int n, bool pressed) {
// handles key event at n-th monitored pin
// 'pressed' == false if released
// *** TOP LEFT KEY ***
if (n == 0) {
if (pressed) {
Keyboard.press('q');
}
else {
Keyboard.release('q');
}
}
// *** TOP CENTER KEY ***
if (n == 1) {
if (pressed) {
Keyboard.press('w');
}
else {
Keyboard.release('w');
}
}
// *** TOP RIGHT KEY ***
if (n == 2) {
if (pressed) {
Keyboard.press('e');
}
else {
Keyboard.release('e');
}
}
// *** MIDDLE LEFT KEY ***
if (n == 3) {
if (pressed) {
Keyboard.press('a');
}
else {
Keyboard.release('a');
}
}
// *** MIDDLE CENTER KEY ***
if (n == 4) {
if (pressed) {
Keyboard.press('s');
}
else {
Keyboard.release('s');
}
}
// *** MIDDLE RIGHT KEY ***
if (n == 5) {
if (pressed) {
Keyboard.press('d');
}
else {
Keyboard.release('d');
}
}
// *** BOTTOM LEFT KEY ***
if (n == 6) {
if (pressed) {
Keyboard.press(KEY_LEFT_ALT);
}
else {
Keyboard.release(KEY_LEFT_ALT);
}
}
// *** BOTTOM RIGHT KEY ***
if (n == 7) {
if (pressed) {
Keyboard.press(KEY_LEFT_CTRL);
}
else {
Keyboard.release(KEY_LEFT_CTRL);
}
}
// *** ROTARY ENCODER BUTTON ***
if (n == 8) {
if (pressed) {
Keyboard.press(KEY_RETURN);
}
else {
Keyboard.release(KEY_RETURN);
}
}
// *** MONITOR ROTARY ENCODER ***
if (n == 9) {
if (!pressed) { // right pin is high...
if (digitalRead(LPIN)) { // ... and left one not yet: clockwise
Keyboard.write(']'); // plus on German keyboard
}
else { // ... and left one already high: counter-clockwise
Keyboard.write('/'); // minus on German keyboard
}
}
}
// *** FOOT SWITCH ***
if (n == 10) {
if (!pressed) { // foot switch is inverted!
Keyboard.press((char) 0x20); // space key
}
else {
Keyboard.release((char) 0x20);
}
}
// important: save state
last_state[n] = !pressed; // pressed==false is HIGH
}
// ********************
// END OF MAIN FUNCTION
void setup() {
// initialize monitored pins
for (int i = 0; i < 11; i++) {
pinMode(MPINS[i], INPUT_PULLUP);
last_state[i] = digitalRead(MPINS[i]);
}
// initialize other pins and analog in
for (int i = 0; i < 2; i++) {
pinMode(OPINS[i], INPUT_PULLUP);
}
analog_val = analogRead(APIN);
analog_ratio = analog_val / 1024;
Serial.begin(9600); // for debugging only
Keyboard.begin();
}
void loop() {
// monitor pins
for (int i = 0; i < 11; i++) {
if (decay[i] > 0) {
decay[i] -= 1;
}
else if (digitalRead(MPINS[i]) != last_state[i]) {
keyEvent(i, last_state[i]); // second argument: pressed (true) or released?
decay[i] = DCY;
}
}
// read analog value
analog_val = analogRead(APIN);
analog_ratio = analog_val / 1024;
//Serial.println(analog_ratio);
// extra delay per run
delay(DLY);
}