-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpedal_firmware.ino
More file actions
250 lines (196 loc) · 5.98 KB
/
Copy pathpedal_firmware.ino
File metadata and controls
250 lines (196 loc) · 5.98 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "MIDIUSB.h"
// === CONSTANTS ===
const int NUM_BANKS = 4;
const byte MIDI_CHANNEL = 7;
const float FILTER_MAX_RATE = 4.2; // Max amount expr or knob can change in a second
const int POT_LOWER = 132; // deadband of 4 added to both of these
const int POT_UPPER = 1019;
// NOTE Expr pedal seems to follow curve of format y = A*B^(-Dx) + C
// These values were found by plugging in the max and min values of the
// pedal, then solving for all values while tweaking B and D.
// Padded raw max and min measured at [80, 250]
// https://www.desmos.com/calculator/cv4xtfvs05
const float EXPR_CURVE_A = 287.8;
const float EXPR_CURVE_B = 1.2;
const float EXPR_CURVE_C = -37.8;
const float EXPR_CURVE_D = 4.9;
const float EXPR_CURVE_DENOM = log(EXPR_CURVE_B) * EXPR_CURVE_D;
const byte FS_BANK_CCS[NUM_BANKS][2] = {
{20, 21},
{22, 23},
{24, 25},
{67, 64} // These cc's are for piano soft and sustain pedals
};
// Foot controller, mod wheel, and two undefineds
const byte EXPR_BANK_CCS[NUM_BANKS] = {4, 1, 14, 15};
const byte KNOB_BANK_CCS[NUM_BANKS] = {102, 103, 104, 105};
const int PRESS_EVENT = 1;
const int RELEASE_EVENT = -1;
const int NO_EVENT = 0;
// === PINS ===
int toggleLedPins[] = {15, 16};
int statusLedPins[] = {14, 10};
int exprPin = A0;
int fsBankPin = A3;
int knobPin = A2;
int exprBankPin = A1;
int fsPins[] = {3, 2};
int toggleButtonPins[] = {5, 4};
int extraButtonPin = 6;
// === STATE ===
int lastMillis = 0;
float dt = 0;
int currentFsBank = 0;
int currentExprBank = 0;
bool toggleStates[NUM_BANKS][2]; // Whether toggle mode is on or off for each fs in each bank
bool fsMidi[NUM_BANKS][2]; // The last state of each fs control sent over midi
bool physFsState[2]; // The last physical pressed state of each fs
bool physToggleState[2]; // The last physical pressed state of each toggle
float lastExpr = 0;
float lastKnob = 0;
// === HELPERS ===
float normalize(int value, int lower, int upper) {
value -= lower;
return clamp(value / ((float) (upper - lower)));
}
float clamp(float val) {
return min(1, max(0, val));
}
void maybeResetFs(int i) {
if(!toggleStates[currentFsBank][i]) {
writeFs(i, false);
}
}
void writeFs(int i, bool val) {
if(fsMidi[currentFsBank][i] != val) {
writeCc(FS_BANK_CCS[currentFsBank][i], val ? 127 : 0);
fsMidi[currentFsBank][i] = val;
}
}
void writeCc(byte cc, byte val) {
midiEventPacket_t event = {0x0B, 0xB0 | MIDI_CHANNEL, cc, val};
MidiUSB.sendMIDI(event);
}
int getPressEvent(int pin, bool* state) {
int event = NO_EVENT;
bool pressed = (digitalRead(pin) == LOW);
if(pressed && !*state) {
event = PRESS_EVENT;
}
else if(!pressed && *state) {
event = RELEASE_EVENT;
}
*state = pressed;
return event;
}
void updateFs(int i) {
int event = getPressEvent(fsPins[i], &physFsState[i]);
bool isToggle = toggleStates[currentFsBank][i];
if(event == PRESS_EVENT && isToggle) {
writeFs(i, !fsMidi[currentFsBank][i]);
}
else if(event == PRESS_EVENT) {
writeFs(i, true);
}
else if(event == RELEASE_EVENT && !isToggle) {
writeFs(i, false);
}
}
void updateKnob() {
float normalized = normalize(analogRead(knobPin), POT_LOWER, POT_UPPER);
updateControl(normalized, &lastKnob, KNOB_BANK_CCS[currentExprBank]);
}
void updateExpr() {
int raw = analogRead(exprPin);
Serial.println(raw);
float logArg = max(0, (raw - EXPR_CURVE_C) / EXPR_CURVE_A);
float unboundedOut = -log(logArg) / EXPR_CURVE_DENOM;
updateControl(clamp(unboundedOut), &lastExpr, EXPR_BANK_CCS[currentExprBank]);
}
void updateControl(float normalized, float* state, int cc) {
float direction = normalized > *state ? 1 : -1;
float change = abs(normalized - *state);
float rate = change / dt;
if(rate > FILTER_MAX_RATE) {
change = FILTER_MAX_RATE * dt;
}
float filtered = *state + (change * direction);
byte prevMidi = min(127, floor(*state * 128));
byte desiredMidi = min(127, floor(filtered * 128));
if(desiredMidi != prevMidi) {
writeCc(cc, desiredMidi);
}
*state = filtered;
}
void handleBankChanges() {
float fsBankReading = normalize(
analogRead(fsBankPin), POT_LOWER, POT_UPPER);
float exprBankReading = normalize(
analogRead(exprBankPin), POT_LOWER, POT_UPPER);
int desiredFsBank = min(NUM_BANKS - 1, floor(fsBankReading * NUM_BANKS));
int desiredExprBank = min(NUM_BANKS - 1, floor(exprBankReading * NUM_BANKS));
if(desiredFsBank != currentFsBank) {
maybeResetFs(0);
maybeResetFs(1);
}
currentFsBank = desiredFsBank;
currentExprBank = desiredExprBank;
}
void updateToggleState(int i) {
if(getPressEvent(toggleButtonPins[i], &physToggleState[i]) == PRESS_EVENT) {
toggleStates[currentFsBank][i] = !toggleStates[currentFsBank][i];
maybeResetFs(0);
maybeResetFs(1);
}
}
void sendControls() {
updateFs(0);
updateFs(1);
// NOTE - Expression pedal too noisy. Disabled.
// updateExpr();
updateKnob();
MidiUSB.flush();
}
void updateFsDisplay(int i) {
digitalWrite(
toggleLedPins[i],
toggleStates[currentFsBank][i] ? HIGH : LOW);
digitalWrite(
statusLedPins[i],
fsMidi[currentFsBank][i] ? HIGH : LOW);
}
// === MAIN STUFF ===
void setup() {
// Outputs
pinMode(toggleLedPins[0], OUTPUT);
pinMode(toggleLedPins[1], OUTPUT);
pinMode(statusLedPins[0], OUTPUT);
pinMode(statusLedPins[1], OUTPUT);
Serial.begin(115200);
// Analog Inputs
pinMode(exprPin, INPUT);
pinMode(fsBankPin, INPUT);
pinMode(knobPin, INPUT);
pinMode(exprBankPin, INPUT);
// Digital Inputs
pinMode(fsPins[0], INPUT);
pinMode(fsPins[1], INPUT);
pinMode(toggleButtonPins[0], INPUT);
pinMode(toggleButtonPins[1], INPUT);
pinMode(extraButtonPin, INPUT);
// Blink green light to indicate end of startup
digitalWrite(toggleLedPins[1], HIGH);
delay(1000);
digitalWrite(toggleLedPins[1], LOW);
}
void loop() {
int currentMillis = millis();
dt = (currentMillis - lastMillis) / 1000.0;
lastMillis = currentMillis;
handleBankChanges();
updateToggleState(0);
updateToggleState(1);
updateFsDisplay(0);
updateFsDisplay(1);
sendControls();
}