/
teensy_rotary_redux_mono.ino
482 lines (411 loc) · 12.6 KB
/
teensy_rotary_redux_mono.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// Include project header files
#include "audio_tool_export.h"
#include "helper_functions.h"
#include "pin_definitions.h"
#include "potentiometer.h"
#include <IntervalTimer.h>
// Include arduino libraries
#include <string.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SigmaDSP.h>
#include <ezLED.h>
#include "encoder.h"
#include "switch.h"
#include <EEPROM.h>
extern float tempmonGetTemp(void);
// Include generated parameter file
#include "SigmaDSP_parameters.h"
// Display definitions
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// SigmaDSP library definitions
SigmaDSP dsp(Wire, DSP_I2C_ADDRESS, 44100.00f /*, DSP_RESET*/);
//DSPEEPROM ee(Wire, EEPROM_I2C_ADDRESS, 256, LED);
IntervalTimer timer;
IntervalTimer timer_temp;
// Global variables
int bypass_state = 1;
int speed_state = 0; // Chorale vs Tremolo
char input = 0;
long lastPeriod = -1;
const float volume_minVal = 0.02;
const float volume_maxVal = 2.0;
Potentiometer volume(pot6, volume_minVal, volume_maxVal, true);
const float mix_minVal = 0.0;
const float mix_maxVal = 1.0;
Potentiometer mix(pot2, mix_minVal, mix_maxVal, false);
const float gain_minVal = 0.5;
const float gain_maxVal = 50.0;
Potentiometer gain(pot4, gain_minVal, gain_maxVal, true);
const float speed_minVal = 0.1;
const float speed_maxVal = 1.0;
Potentiometer speed(pot1, speed_minVal, speed_maxVal, false);
const float horn_minVal = 0.0;
const float horn_maxVal = 10.0;
Potentiometer horn(pot3, horn_minVal, horn_maxVal, false);
/* POT LAYOUT
SPEED MIX HORN
GAIN ENC VOLUME
BYPASS TREM/CHORALE
*/
ezLED led(LED, CTRL_ANODE);
Switch encSw = Switch(SW1);
Switch button1 = Switch(FS1);
Switch button2 = Switch(FS2);
bool mixNeedsUpdate = false;
Encoder enc = Encoder(ENCA, ENCB);
struct menuItem {
char name[20];
volatile float value;
float minValue;
float maxValue;
float stepSize;
};
#define NUM_MENU_ITEMS 7
#define reverbMix 0
#define reverbSize 1
#define reverbDamping 2
#define reverbCompansion 3
#define reverbFilter 4
#define inputPad 5
#define outputGain 6
struct menuItem menuItems[] = {
{ "Reverb Mix", 0.3, 0.0, 1.0, 0.1 },
{ "Reverb Size", 0.5, 0.0, 1.0, 0.1 },
{ "Reverb Damping", 1.0, 0.0, 1.0, 0.1 },
{ "Reverb Compansion", 1.0, 0.1, 1.0, 0.1 },
{ "Post-reverb LPF", 3000, 1000, 10000, 200 },
{ "Input Pad", 0, 0, 1, 1 },
{ "Output Boost", 0, 0, 1, 1 }
};
menuItem rd;
int currentMenuItem = 0;
volatile float currentValue = menuItems[currentMenuItem].value;
volatile bool isChangingValue = false;
volatile bool needsDisplayUpdate = false;
volatile bool needsDSPUpdate = false;
// Arduino Setup function
void setup() {
AudioMemory(8); // Allocate memory for the audio processing
setMasterClock();
setPinModes();
analogWriteFrequency(LED, 375000);
digitalWrite(DSP_SELFBOOT, LOW);
digitalWrite(DSP_RESET, LOW);
delay(100);
digitalWrite(DSP_RESET, HIGH);
delay(100);
Serial.begin(9600);
Serial.println(F("Hello world!"));
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 16); // Start at top-left corner
printTextCenter("Locrius", 16);
printTextCenter("DSP Pedal", 32);
display.display();
Wire.begin();
dsp.begin();
delay(2000);
Serial.println(F("Pinging i2c bus...\n0 -> device is present\n2 -> device is not present"));
Serial.print(F("DSP response: "));
Serial.println(dsp.ping());
// Use this step if no EEPROM is present
Serial.print(F("\nLoading DSP program... "));
loadProgram(dsp);
Serial.println(F("Done!\n"));
Serial.println("Firmware version 1.0");
// Init DSP Values
getMenuItemsFromEEPROM();
mixer1.gain(1, menuItems[reverbMix].value); // reverb channel
mixer1.gain(0, 1 - menuItems[reverbMix].value); // dry channel
reverb1.roomsize(menuItems[reverbSize].value);
reverb1.damping(menuItems[reverbDamping].value);
biquad1.setLowpass(0, menuItems[reverbFilter].value, 0.7);
amp2.gain(menuItems[reverbCompansion].value);
amp3.gain(1.f / menuItems[reverbCompansion].value);
digitalWrite(OUTPUT_GAIN, !menuItems[outputGain].value);
digitalWrite(INPUT_PAD, menuItems[inputPad].value);
rotary.setParamValue("Volume", volume.getCurrentValue());
rotary.setParamValue("Gain", gain.getCurrentValue());
rotary.setParamValue("Speed", speed.getCurrentValue());
led.blink(calculateLedOnTime(), calculateLedOnTime(), 0);
rotary.setParamValue("Horn", horn.getCurrentValue());
updateMixParam();
// Un-bypass the DSP
digitalWrite(BYPASS, bypass_state);
// Clear display
display.clearDisplay();
updateDisplay();
// Start control loop update timer
timer.begin(timerCallback, 2500); // 2500 microseconds = 2.5 milliseconds
//timer_temp.begin(timertempCallback, 10000000); // every 10 seconds
}
// Arduino loop function
void loop() {
led.loop();
// Switch.TaskFunction Returns:
// -1 if nothing happened
// 0 if switch pressed
// 1 if switch is held
// 2 if switch is released before being held
switch (encSw.TaskFunction()) {
case 0:
encPress();
break;
case 1:
break;
case 2:
break;
default:
break;
}
switch (button1.TaskFunction()) {
case 0:
break;
case 1:
getMenuItemsFromEEPROM();
break;
case 2:
bypass_state = !bypass_state;
digitalWrite(BYPASS, bypass_state);
needsDisplayUpdate = true;
break;
default:
break;
}
switch (button2.TaskFunction()) {
case 0:
break;
case 1:
writeMenuItemsToEEPROM();
break;
case 2:
speed_state = !speed_state;
led.blink(calculateLedOnTime(), calculateLedOnTime(), 0);
rotary.setParamValue("Chorale/Tremolo", speed_state);
needsDisplayUpdate = true;
break;
default:
break;
}
if (mixNeedsUpdate) {
updateMixParam();
}
if (isChangingValue) {
switch (enc.TaskFunction()) {
case 0: // CW
currentValue = currentValue + menuItems[currentMenuItem].stepSize;
currentValue = max(menuItems[currentMenuItem].minValue, min(menuItems[currentMenuItem].maxValue, currentValue));
needsDisplayUpdate = true;
break;
case 1: // CCW
currentValue = currentValue - menuItems[currentMenuItem].stepSize;
currentValue = max(menuItems[currentMenuItem].minValue, min(menuItems[currentMenuItem].maxValue, currentValue));
needsDisplayUpdate = true;
break;
default: // No change
break;
}
} else { // Changing menu item
int newMenuItem = currentMenuItem;
switch (enc.TaskFunction()) {
case 0: // CW
newMenuItem++;
if (newMenuItem < 0) {
newMenuItem = NUM_MENU_ITEMS - 1;
}
if (newMenuItem >= NUM_MENU_ITEMS) {
newMenuItem = 0;
}
currentMenuItem = newMenuItem;
currentValue = menuItems[currentMenuItem].value;
needsDisplayUpdate = true;
break;
case 1: // CCW
newMenuItem--;
if (newMenuItem < 0) {
newMenuItem = NUM_MENU_ITEMS - 1;
}
if (newMenuItem >= NUM_MENU_ITEMS) {
newMenuItem = 0;
}
currentMenuItem = newMenuItem;
currentValue = menuItems[currentMenuItem].value;
needsDisplayUpdate = true;
break;
default: // No change
break;
}
}
// Check if the display needs to be updated and perform the update
if (needsDisplayUpdate) {
updateDisplay();
needsDisplayUpdate = false;
}
if (needsDSPUpdate) {
mixer1.gain(1, menuItems[reverbMix].value); // reverb channel
mixer1.gain(0, 1 - menuItems[reverbMix].value); // dry channel
reverb1.roomsize(menuItems[reverbSize].value);
reverb1.damping(menuItems[reverbDamping].value);
biquad1.setLowpass(0, menuItems[reverbFilter].value, 0.7);
amp2.gain(menuItems[reverbCompansion].value);
amp3.gain(1.f / menuItems[reverbCompansion].value);
digitalWrite(OUTPUT_GAIN, !menuItems[outputGain].value);
digitalWrite(INPUT_PAD, menuItems[inputPad].value);
needsDSPUpdate = false;
}
}
void updateMixParam() {
dsp.volume(MOD_2XMIXER1_ALG0_STAGE0_VOLONE_ADDR, 20 * log(cos(HALF_PI * mix.getCurrentValue())));
dsp.volume(MOD_2XMIXER1_ALG0_STAGE0_VOLTWO_ADDR, 20 * log(cos(HALF_PI * (1 - mix.getCurrentValue()))));
mixNeedsUpdate = false;
}
void timerCallback() {
// Code to execute every 2.5ms
if (volume.hasValueChanged()) {
rotary.setParamValue("Volume", volume.getCurrentValue());
}
if (gain.hasValueChanged()) {
rotary.setParamValue("Gain", gain.getCurrentValue());
}
if (speed.hasValueChanged()) {
rotary.setParamValue("Speed", speed.getCurrentValue());
led.blink(calculateLedOnTime(), calculateLedOnTime(), 0);
}
if (horn.hasValueChanged()) {
rotary.setParamValue("Horn", horn.getCurrentValue());
}
if (mix.hasValueChanged()) {
mixNeedsUpdate = true;
}
}
void timertempCallback() {
Serial.print(tempmonGetTemp());
Serial.println(F("°C"));
Serial.print(F("MAX CPU: "));
Serial.print(AudioProcessorUsageMax());
Serial.println(F("%"));
Serial.print(F("CPU: "));
Serial.print(AudioProcessorUsage());
Serial.println(F("%\n"));
}
int calculateLedOnTime() {
int val = 500 / speed.getCurrentValue();
if (speed_state) {
val = val / 8;
}
return val;
}
void encPress() {
static unsigned long lastDebounceTime = 0;
static unsigned long debounceDelay = 50;
unsigned long currentMillis = millis();
if (currentMillis - lastDebounceTime < debounceDelay) {
return;
}
if (!isChangingValue) {
isChangingValue = true;
} else {
// for some reason I need both the first line and the switch statement
menuItems[currentMenuItem].value = currentValue;
isChangingValue = false;
needsDSPUpdate = true;
}
needsDisplayUpdate = true;
lastDebounceTime = currentMillis;
}
void updateDisplay() {
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.println(menuItems[currentMenuItem].name);
display.setTextSize(2);
display.setCursor(0, 12);
display.print(currentValue);
if (isChangingValue) {
display.drawLine(display.width() - 1, 12, display.width() - 1, 25, SSD1306_WHITE);
} else {
display.drawLine(display.width() - 1, 0, display.width() - 1, 7, SSD1306_WHITE);
}
display.setCursor(0, 48);
if (bypass_state)
display.println(" ");
else
display.println("BYP");
display.setCursor(80, 48);
if (speed_state)
display.println("TREM");
else
display.println("CHOR");
display.display();
}
void printTextCenter(const char* buf, int y) {
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(buf, 0, 0, &x1, &y1, &w, &h); //calc width of new string
display.setCursor((display.width() - w) / 2, y);
display.print(buf);
}
void writeMenuItemsToEEPROM() {
for (int i = 0; i < NUM_MENU_ITEMS; i++) {
EEPROM.put(i * sizeof(menuItem), menuItems[i]);
}
Serial.println("SAVED");
printTextCenter("SAVED", 30);
display.display();
}
void getMenuItemsFromEEPROM() {
for (int i = 0; i < NUM_MENU_ITEMS; i++) {
EEPROM.get(i * sizeof(menuItem), rd);
printMenuItem(rd);
bool skip = false;
if (strncmp(rd.name, menuItems[i].name, sizeof(rd.name)) != 0) {
Serial.println("Name not equal");
skip = true;
}
if (rd.value < menuItems[i].minValue || rd.value > menuItems[i].maxValue) {
Serial.println("Value out of bounds");
skip = true;
}
if (rd.stepSize > (rd.maxValue - rd.minValue)) {
Serial.println("StepSize too big");
skip = true;
}
if (skip) {
Serial.print("Skip: ");
Serial.println(rd.name);
continue;
}
memcpy(&menuItems[i], &rd, sizeof(menuItem));
}
Serial.println("LOADED");
printTextCenter("LOADED", 30);
display.display();
currentValue = menuItems[currentMenuItem].value;
needsDSPUpdate = true;
}
void printMenuItem(menuItem item) {
Serial.print("Name: ");
Serial.println(item.name);
Serial.print("Value: ");
Serial.println(item.value);
Serial.print("MinValue: ");
Serial.println(item.minValue);
Serial.print("MaxValue: ");
Serial.println(item.maxValue);
Serial.print("StepSize: ");
Serial.println(item.stepSize);
Serial.print("\n");
}