-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.cpp
436 lines (336 loc) · 9.66 KB
/
main.cpp
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
#include "NOKIA_5110.h"
#define MAX_EFFECTS 5
#define MAX_MODIFICATION 50
// Current active effect
int effect_state = 0;
int effect_parameter_count = 4;
int active_parameter_select = 0;
extern float modified;
float in_sample = 0;
#include "mbed.h"
#include "effects.h"
#include <math.h>
//AnalogIn analog_in(PA_0);
AnalogOut analog_out(PA_4);
//Ticker timer;
// Interfacing User buttons to control effects pedal:
DigitalOut ledPin( PA_5 ); //Led used for testing purpose on Nucleo board LED2
DigitalIn buttonUp(D9, PullDown); // Change active parameter
DigitalIn buttonDown(D4, PullDown); // Change active parameter
DigitalIn buttonLeft(D3, PullDown); // Next effect
DigitalIn buttonRight(D2, PullDown); // Previous effect
DigitalIn buttonPlus(D5, PullDown); // Button that will be used to save
DigitalIn buttonMinus(D8, PullDown); // Button to clear effets
NokiaLcd *nokiaLCD;
Serial pc(USBTX, USBRX);
void button_control();
void update_lcd();
void callback();
void send(char c, int numb);
uint32_t samps[2] = {0,0};
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;
static void MX_ADC1_Init(void);
static float adc_sample = 0;
static float modified_sample = 0;
static float effect_sample =0;
char buffer[20];
int main() {
//setup
MX_ADC1_Init();
HAL_ADC_Start(&hadc1);
init_effects();
pc.baud(115200);
pc.attach(&callback);
LcdPins myPins;
myPins.sce = D15;
myPins.rst = D14;
myPins.dc = D10;
myPins.mosi = D11;
myPins.miso = NC;
myPins.sclk = D13;
wait(0.5);
nokiaLCD = new NokiaLcd( myPins );
nokiaLCD->InitLcd();
nokiaLCD->ClearLcdMem();
update_lcd();
while(1)
{
button_control();
adc_sample = HAL_ADC_GetValue(&hadc1) / 4096.0f ;
adc_sample += 0.0126953f;
if(adc_sample< 0.501 && adc_sample > 0.499)
adc_sample = 0.5;
// List of Effects
if(effect_state == 0)
modified_sample = clean(adc_sample);
else if (effect_state == 1)
modified_sample = tremolo(adc_sample);
else if (effect_state == 2)
modified_sample = tremolo2(adc_sample);
else if (effect_state == 3)
modified_sample = ez_distort(adc_sample);
else if (effect_state == 4)
modified_sample = ez_distort2(adc_sample);
else if (effect_state == 5)
modified_sample = flanger(adc_sample);
else if (effect_state == 6)
modified_sample = delay(adc_sample);
modified_sample -= 0.0126953f;
analog_out = modified_sample;
}
}
void update_lcd()
{
nokiaLCD->SetXY(0, 0);
nokiaLCD->ClearLcdMem();
char buf[MAX_MOD_COUNT][10];
int i = 0;
for(;i<MAX_MOD_COUNT; i++)
{
itoa (mod[i],buf[i],10);
}
if(effect_state == 0)
{
nokiaLCD->DrawString("Clean");
nokiaLCD->SetXY(0,1);
nokiaLCD->DrawString("---------");
nokiaLCD->SetXY(0,2);
nokiaLCD->DrawString(" Volume: ");
nokiaLCD->DrawString(buf[0]);
}
if (effect_state == 1)
{
nokiaLCD->DrawString("Tremolo 1");
nokiaLCD->SetXY(0,1);
nokiaLCD->DrawString("---------");
nokiaLCD->SetXY(0,2);
nokiaLCD->DrawString(" Fs: ");
nokiaLCD->DrawString(buf[0]);
}
if (effect_state == 2)
{
nokiaLCD->DrawString("Tremolo 2");
nokiaLCD->SetXY(0,1);
nokiaLCD->DrawString("---------");
nokiaLCD->SetXY(0,2);
nokiaLCD->DrawString(" Fs: ");
nokiaLCD->DrawString(buf[0]);
}
if(effect_state == 3)
{
nokiaLCD->DrawString("Overdrive");
nokiaLCD->SetXY(0,1);
nokiaLCD->DrawString("---------");
nokiaLCD->SetXY(0,2);
nokiaLCD->DrawString(" Threshold: ");
nokiaLCD->DrawString(buf[0]);
}
if (effect_state == 4)
{
nokiaLCD->DrawString("Distortion");
nokiaLCD->SetXY(0,1);
nokiaLCD->DrawString("---------");
nokiaLCD->SetXY(0,2); // Set active line
nokiaLCD->DrawString(" Threshold: ");
nokiaLCD->DrawString(buf[0]); // Show mod value
nokiaLCD->SetXY(0,3);
nokiaLCD->DrawString(" Gain: ");
nokiaLCD->DrawString(buf[1]); // Show mod value
}
if(effect_state == 5)
{
nokiaLCD->DrawString("Flanger");
nokiaLCD->SetXY(0,1);
nokiaLCD->DrawString("---------");
nokiaLCD->SetXY(0,2);
nokiaLCD->DrawString(" Eto: ");
nokiaLCD->DrawString(buf[0]);
}
// active_parameter_select indicates selected mod
nokiaLCD->SetXY(0,active_parameter_select + 2);
nokiaLCD->DrawString(">");
}
void send_effect_state()
{
send('e', effect_state);
}
void send_mods_state()
{
send('m', mod[0]);
wait_us(1);
send('q', mod[1]);
wait_us(1);
send('w', mod[2]);
wait_us(1);
send('e', mod[3]);
wait_us(1);
send('r', mod[4]);
}
void button_control()
{
if (buttonRight == 1) {
wait(0.002);
if (buttonRight == 1) {
while (buttonRight == 1); //wait until button is released
ledPin = 1;
// LATBbits.LATB7 = ~LATBbits.LATB7;
//effect_state += 1;
set_modifications_default();
effect_state = (effect_state+1) % (MAX_EFFECTS);
update_lcd();
send_effect_state();
}
}
if (buttonLeft == 1) {
wait(0.02);
if (buttonLeft == 1) {
while (buttonLeft == 1); //wait until button is released
ledPin = 0;
effect_state -= 1;
set_modifications_default();
if(effect_state < 0)
effect_state = MAX_EFFECTS - 1;
update_lcd();
send_effect_state();
}
}
if (buttonUp == 1) {
wait(0.002);
if (buttonUp == 1) {
while (buttonUp == 1);
active_parameter_select--;
if(active_parameter_select<0)
active_parameter_select = effect_parameter_count-1;
update_lcd();
send_effect_state();
}
}
if (buttonDown == 1) {
wait(0.002);
if (buttonDown == 1) {
while (buttonDown == 1);
active_parameter_select = (active_parameter_select + 1) % effect_parameter_count;
update_lcd();
send_effect_state();
}
}
if (buttonPlus == 1) {
wait(0.002);
if (buttonPlus == 1) {
while (buttonPlus == 1); //wait until button is released
ledPin = 1;
mod[active_parameter_select] += 1;
if(mod[active_parameter_select] > MAX_MODIFICATION)
mod[active_parameter_select] = MAX_MODIFICATION;
update_lcd();
send_mods_state();
}
}
if (buttonMinus == 1) {
wait(0.002);
if (buttonMinus == 1) {
while (buttonMinus == 1); //wait until button is released
ledPin = 0;
mod[active_parameter_select] -= 1;
if(mod[active_parameter_select] < 0)
mod[active_parameter_select] = 0;
update_lcd();
send_mods_state();
}
}
}
void MX_ADC1_Init(void)
{
//DMA
/* DMA controller clock enable */
__DMA1_CLK_ENABLE();
/* DMA interrupt init */
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
GPIO_InitTypeDef GPIO_InitStruct;
__ADC12_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
hdma_adc1.Instance = DMA1_Channel1;
hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_adc1.Init.MemInc = DMA_MINC_DISABLE;
hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_adc1.Init.Mode = DMA_CIRCULAR;
hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
HAL_DMA_Init(&hdma_adc1);
__HAL_LINKDMA(&hadc1,DMA_Handle,hdma_adc1);
/**Common config
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1;
hadc1.Init.Resolution = ADC_RESOLUTION12b;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.EOCSelection = EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.Overrun = OVR_DATA_OVERWRITTEN;
HAL_ADC_Init(&hadc1);
ADC_ChannelConfTypeDef sConfig = {0};
// Configure ADC channel
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
sConfig.Channel = ADC_CHANNEL_1;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
}
void DMA1_Channel1_IRQHandler(void)
{
HAL_DMA_IRQHandler(&hdma_adc1);
}
void send(char c, int numb)
{
pc.printf("%c%d\n", c, numb);
}
void callback()
{
for(int i = 0; i < 5; i++)
buffer[i] = 0;
pc.gets(buffer, 10);
char numbers[2];
numbers[0] = buffer[1];
numbers[1] = buffer[2];
int number = 0;
sscanf(numbers, "%d", &number);
if(buffer[0] == 'e')
{
effect_state = number;
}
if(buffer[0] == 'm')
mod[0] = number;
if(buffer[0] == 'q')
mod[1] = number;
if(buffer[0] == 'w')
mod[2] = number;
if(buffer[0] == 'e')
mod[3] = number;
if(buffer[0] == 'r')
mod[4] = number;
if(buffer[0] == 'p')
{
send_mods_state();
send_effect_state();
}
update_lcd();
}
void SysTick_Handler(void)
{
HAL_IncTick();
HAL_SYSTICK_IRQHandler();
}