-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFcManager.cpp
More file actions
311 lines (252 loc) · 9.63 KB
/
FcManager.cpp
File metadata and controls
311 lines (252 loc) · 9.63 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "config.h"
#include "FcManager.h"
#include "ExpPedals.h"
#include "FastMux.h"
#include "TimerUtils.h"
#include "SwitchHoldManager.h"
static AxeSystem Axe;
//TODO: investigate for use of more tempo functionality
// static Tempo tempo;
// Pedal / controller definitions
static unsigned int pedalValue[numberOfPedals];
static unsigned int pedalValueOld[numberOfPedals];
static byte controllerValueOld[numberOfPedals];
static byte controllerValue[numberOfPedals];
//Struct to hold information about each scenePlay
struct SceneInfo {
SceneNumber number = -1;
const char *name;
};
//A list of all of the scenes for current preset
static SceneInfo scenes[NUM_SCENES];
static int PresetNumb;
static byte SceNumb;
FcManager::FcManager() : FootController(MIDI_AFX3)
{
}
/// Controller initialization
void FcManager::init() {
Axe.begin(MIDI_PORT); // TODO in future: implement more midi devices not necesarily assuming a direct serial connection
Axe.registerPresetChangeCallback(onPresetChange);
Axe.registerPresetChangingCallback(onPresetChanging);
Axe.registerSystemChangeCallback(onSystemChange);
Axe.registerSceneNameCallback(onSceneName);
Axe.registerTunerStatusCallback(onTunerStatus);
Axe.registerTunerDataCallback(onTunerData);
Axe.registerTapTempoCallback(onTapTempo);
Axe.enableRefresh(AXE_REFRESH_RATE);
//Axe.requestPresetDetails();
Axe.refresh(true);
}
/// Controller update handling
void FcManager::update() {
_leds.update(); // update the timer
Axe.update();
handleEvents();
// handleLayoutChange();
}
/// foot switch pressed for a longer time
/// TODO: use the 2 first buttons rows and long timing to switch layouts
/// as an example the tuner + the loop functiona could be remapped on the second row
void FcManager::handleLayoutChange(){
// long press function to Switch 11
// if (_buttons[12].pressedFor(2000)) {
// PL_("Switch long pressed");
// }
}
void FcManager::notifyPresetChanged(AxePreset preset) {
if (!Axe.isTunerEngaged() )
{
int num = preset.getPresetNumber();
Axe.sendPresetChange(num);
}
}
void FcManager::onSceneName(const SceneNumber number, const char* name, const byte length) {
if (strcmp(scenes[number-1].name, name)==0) return;
Serial.print(F("onSceneName(): "));
Serial.print(number);
Serial.print(F(": "));
Serial.println(name);
//Record current scene in the list scenes[number-1].number = number;
scenes[number-1].name = name;
//Request the first scene that we don't have yet.
//Only request one at a time to avoid filling up RX buffer
for (byte i=0; i<NUM_SCENES; i++) {
if (scenes[i].number == -1) {
Axe.requestSceneName(i+1);
break;
}
}
}
// Fetch All Scene Names
void FcManager::onPresetChanging(const PresetNumber number) {
//Reset the scene list for the new preset
for (byte i=0; i<NUM_SCENES; i++) {
scenes[i].number = -1;
}
if(number==PresetNumb) return;
#ifdef DEBUG
if (number>=0) {
Serial.print(F("onPresetChanging(): "));
Serial.println(number);
}
#endif
}
void FcManager::onPresetChange(AxePreset preset) {
PresetNumb = preset.getPresetNumber();
preset.copyPresetName(FcDisplay::presetNameString(), MaxPresetNameLen + 1);
Serial.print(F("onPresetChange(): "));
Serial.print(PresetNumb);
Serial.print(F(": "));
Serial.println(preset.getPresetName());
SceNumb = preset.getSceneNumber();
preset.copySceneName(FcDisplay::sceneNameString(), MaxSceneNameLen + 1);
_display.presetNameToLCD(PresetNumb, SceNumb);
_leds.turnOnSceneLed(SceNumb);
}
void FcManager::onTunerData(const char *note, const byte string, const byte fineTune) {
_display.displayFineTune(fineTune);
}
void FcManager::onTunerStatus(bool engaged) {
if (!Axe.isTunerEngaged())
{
_display.clear();
_display.presetNameToLCD(PresetNumb, SceNumb);
}
}
void FcManager::handleEvents() {
static bool looperPlaying = false;
static int lastScene = -1;
static int lastLoopPreset = -1;
int scene; // +1 if second scene row
bool isTunerEngaged;
long nowTimeMs = millis();
for (byte switchIndex = 0; switchIndex < NUM_BUTTONS; switchIndex++) {
#ifdef HAS_MUX
_mux.select(switchIndex);
#endif
auto& currentButton = _buttons[switchIndex];
currentButton.update();
// handle hold durations
const auto fellState = currentButton.fell();
const auto roseState = currentButton.rose();
HoldMgr.update(switchIndex, fellState, roseState, nowTimeMs);
const auto changed = HoldMgr.hasChanged(switchIndex);
const auto presetOffset = HoldMgr.getPresetAutoRepeatAmplitude(switchIndex);
constexpr int NumPresets = 1024;
// First handle autorepeat keys:
if (switchIndex == SWITCH_PRESET_DEC || switchIndex == SWITCH_PRESET_INC) {
auto newPreset = switchIndex == SWITCH_PRESET_DEC ?
(PresetNumb + NumPresets - presetOffset) % NumPresets :
(PresetNumb + presetOffset) % NumPresets;
if (fellState || changed) {
if (switchIndex == SWITCH_PRESET_DEC ) {
if (presetOffset == 1) Axe.sendPresetDecrement(); else Axe.sendPresetChange(newPreset);
} else {
if (presetOffset == 1) Axe.sendPresetIncrement(); else Axe.sendPresetChange(newPreset);
}
PresetNumb = newPreset;
_leds.flashLed(switchIndex, PEDAL_ACTIVE_FLASH );
if(lastLoopPreset >= 0) {
_leds.setLooperLeds( PresetNumb == lastLoopPreset && looperPlaying ? 2 : 0);
}
HoldMgr.clearChanged(switchIndex);
}
}
else if (fellState) {
isTunerEngaged = Axe.isTunerEngaged();
if (switchIndex == SWITCH_TUNER || isTunerEngaged ) {
Axe.toggleTuner();
isTunerEngaged = Axe.isTunerEngaged();
}
switch ( switchIndex ) {
// Switches 1-4, 6-9 (Scene 1-8)
case SWITCH_S1: case SWITCH_S2: case SWITCH_S3: case SWITCH_S4:
case SWITCH_S5: case SWITCH_S6: case SWITCH_S7: case SWITCH_S8:
scene = sceneFromSwitchValue(switchIndex);
doSceneChange(scene );
_leds.turnOnSceneLed (scene );
lastScene = scene;
_leds.setLooperLeds(0);
break;
case SWITCH_LOOPER_RECORD:
Axe.getLooper().record();
_leds.setLooperLeds(1);
_leds.turnOffSceneLeds();
lastLoopPreset = PresetNumb;
_display.print(F("RECORD "));
break;
case SWITCH_LOOPER_PLAY:
Axe.getLooper().play();
if (!looperPlaying) {
_leds.setLooperLeds(2);
_leds.turnOffSceneLeds();
_display.print(F("PLAY "));
} else {
_display.print(F("STOP "));
_leds.setLooperLeds(0);
_leds.turnOnSceneLed (lastScene );
lastLoopPreset = -1;
}
looperPlaying = !looperPlaying;
break;
case SWITCH_LOOPER_UNDO:
Axe.getLooper().undo();
_leds.setLooperLeds(3);
_leds.turnOffSceneLeds();
_display.print(F("UNDO/ERASE "));
break;
case SWITCH_TAP_TEMPO: // tap tempo
Axe.sendTap();
break;
}
_leds.setTunerLed(isTunerEngaged);
Serial.println(F("-----------"));
Serial.print(F("Switch ")); Serial.print(switchIndex + 1); Serial.println(F(" pressed."));
} // if currentButton.fell())
} // for
// TODO:
handleExpressionPedals();
}
void FcManager::handleExpressionPedals() {
static unsigned long lastTimeActive=0UL;
static bool mustClear = false;
unsigned long currentTime = millis();
//This line smooths the measured values. The measured values of an unused potentiometer can jumb back and forth between values.
// Without the smoothing the controller would constantly send CC data, which I don't want. There are other ways to smooth, but this one works best for me.
// The values 0.4 and 0.6 need to add up to 1.0. 0.4 and 0.6 gave the best results. Change to taste!
for (int i = 0; i < numberOfPedals; i++) {
pedalValue[i] = analogRead(pedal[i]) * 0.4 + pedalValueOld[i] * 0.6;
//Here the measured values are scaled down to 0 to 127
controllerValue[i] = map(pedalValue[i], pedalValueCalibrationMinimumLevel[i],
pedalValueCalibrationMaximumLevel[i], controllerValueMinimumLevel[i], controllerValueMaximumLevel[i]);
//Only send CC data in case the potentiometer / expression pedal is being used (turned) and in case it's not deactivated
if (controllerValue[i]!=0 && controllerValue[i] != controllerValueOld[i]) {
Axe.sendControlChange(pedalCC[i], controllerValue[i], MidiChannel);
_display.displayControllerValue("Exp", i+1, controllerValue[i]);
lastTimeActive = currentTime;
mustClear = true;
} else if (mustClear && abs(currentTime-lastTimeActive)>AUTO_CLEAR_CONTROL_MS) {
_display.clearControllerValue("Exp", 0);
mustClear = false;
}
controllerValueOld[i] = controllerValue[i];
pedalValueOld[i] = pedalValue[i];
}
}
void FcManager::doSceneChange(byte scene) {
_leds.turnOnSceneLed(scene);
Axe.sendSceneChange(scene);
Axe.update();
}
// =======================================================================================================
// TODO: future
void FcManager::onSystemChange() {
// Display the current tempo at the LCD
// lcd.setCursor(0, 3); lcd.print(F("Tempo: ")); lcd.print(Axe.getTempo()); lcd.print(F(" "));
}
//this will only work if realtime sysex is enabled
void FcManager::onTapTempo() {
// Flashes a LED on tempo
// _leds.flashLed( 3, TAP_TEMPO_LED_DURATION ); // pending assign to correspondent tempo led
}