-
Notifications
You must be signed in to change notification settings - Fork 12
/
buttons_jacks.c
626 lines (479 loc) · 15.2 KB
/
buttons_jacks.c
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*
* buttons_jacks.c - handles button presses and trigger/gate/clock input jacks
*
* Author: Dan Green ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* See http://creativecommons.org/licenses/MIT/ for more information.
*
* -----------------------------------------------------------------------------
*/
#include "buttons_jacks.h"
#include "globals.h"
#include "adc.h"
#include "params.h"
#include "looping_delay.h"
#include "timekeeper.h"
#include "audio_memory.h"
#include "dig_pins.h"
extern volatile uint32_t ping_tmr;
extern volatile uint32_t ping_time;
extern uint32_t locked_ping_time[NUM_CHAN];
extern uint8_t global_mode[NUM_GLOBAL_MODES];
extern uint8_t mode[NUM_CHAN][NUM_CHAN_MODES];
uint8_t flag_ping_was_changed[NUM_CHAN];
uint8_t flag_inf_change[2]={0,0};
uint8_t flag_rev_change[2]={0,0};
uint8_t flag_pot_changed_revdown[NUM_POT_ADCS]={0,0,0,0,0,0,0,0};
uint8_t flag_pot_changed_infdown[NUM_POT_ADCS]={0,0,0,0,0,0,0,0};
uint8_t ping_button_state=0;
uint8_t ping_jack_state=0;
uint8_t flag_ignore_infdown[2]={0,0};
uint8_t flag_ignore_revdown[2]={0,0};
uint32_t flag_acknowlegde_qcm=0;
void init_inputread_timer(void){
TIM_TimeBaseInitTypeDef tim;
NVIC_InitTypeDef nvic;
RCC_APB1PeriphClockCmd(INFREVBUTTONJACK_PINGBUT_TIM_RCC, ENABLE);
nvic.NVIC_IRQChannel = INFREVBUTTONJACK_PINGBUT_TIM_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 3;
nvic.NVIC_IRQChannelSubPriority = 0;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
TIM_TimeBaseStructInit(&tim);
//3000 --> 28kHz
tim.TIM_Period = 3000;
tim.TIM_Prescaler = 0;
tim.TIM_ClockDivision = 0;
tim.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(INFREVBUTTONJACK_PINGBUT_TIM, &tim);
TIM_ITConfig(INFREVBUTTONJACK_PINGBUT_TIM, TIM_IT_Update, ENABLE);
TIM_Cmd(INFREVBUTTONJACK_PINGBUT_TIM, ENABLE);
//Ping jack timer
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10, ENABLE);
nvic.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 0;
nvic.NVIC_IRQChannelSubPriority = 1;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
TIM_TimeBaseStructInit(&tim);
tim.TIM_Period = 5000; //every 30uS
tim.TIM_Prescaler = 0;
tim.TIM_ClockDivision = 0;
tim.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM10, &tim);
TIM_ITConfig(TIM10, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM10, ENABLE);
}
//enum PingMethods PING_METHOD=IGNORE_PERCENT_DEVIATION;
//Ping jack read
//every 30us (33.3kHz), takes 0.3us if no ping
void TIM1_UP_TIM10_IRQHandler(void)
{
static uint16_t State = 0; // Current debounce status
uint16_t t;
uint32_t t_ping_tmr;
uint32_t diff;
float t_f;
static uint16_t last_ping = 0;
static uint16_t ringbuff[8]={0,0,0,0,0,0,0,0};
static uint16_t ringbuff_pos=0;
static uint32_t ringbuff_filled=0;
if (TIM_GetITStatus(TIM10, TIM_IT_Update) != RESET)
{
// Check Ping jack
// ping detection time is 60us typical, 100us max from incoming clock until this line
if (PINGJACK){
t=0xe000;
} else{
t=0xe001;
}
State=(State<<1) | t;
//if (State==0xfffffff0) //jack low for 27 times (0.810ms), then detected high 4 times (0.120ms)
//if ((State & 0xff)==0xfe) //jack low for 7 times (250us), then detected high 1 time
if (State==0xfffe) //jack low for 12 times (1ms), then detected high 1 time
{
ping_button_state = 0;
ping_jack_state = 0;
t_ping_tmr = ping_tmr;
reset_ping_tmr();
if (global_mode[PING_METHOD] != IGNORE_PERCENT_DEVIATION && global_mode[PING_METHOD] != IGNORE_FLAT_DEVIATION_5 && global_mode[PING_METHOD] != IGNORE_FLAT_DEVIATION_10)
{
CLKOUT_ON;
reset_clkout_trigger_tmr();
LED_PINGBUT_ON;
reset_ping_ledbut_tmr();
//Flag to update the divmult parameters
if (mode[0][PING_LOCKED]==0) flag_ping_was_changed[0]=1;
if (mode[1][PING_LOCKED]==0) flag_ping_was_changed[1]=1;
}
if (global_mode[PING_METHOD] != MOVING_AVERAGE_4 && global_mode[PING_METHOD] != LINEAR_AVERAGE_8 && global_mode[PING_METHOD] != LINEAR_AVERAGE_4)
{
ringbuff_pos=0;
ringbuff_filled=0;
}
switch(global_mode[PING_METHOD])
{
case (IGNORE_PERCENT_DEVIATION):
//Only update if there is a variation >1%
t_f = (float)t_ping_tmr / (float)ping_time;
if (t_f>1.01 || t_f<0.99)
{
CLKOUT_ON;
reset_clkout_trigger_tmr();
LED_PINGBUT_ON;
reset_ping_ledbut_tmr();
//Flag to update the divmult parameters
if (mode[0][PING_LOCKED]==0) flag_ping_was_changed[0]=1;
if (mode[1][PING_LOCKED]==0) flag_ping_was_changed[1]=1;
ping_time = t_ping_tmr;
}
break;
case (IGNORE_FLAT_DEVIATION_5):
case (IGNORE_FLAT_DEVIATION_10):
//Only update if there is a variation > XXXX
if (t_ping_tmr > ping_time) diff = t_ping_tmr - ping_time;
else diff = ping_time - t_ping_tmr;
if (global_mode[PING_METHOD]==IGNORE_FLAT_DEVIATION_5) t=5;
else t =10;
if (diff>t)
{
CLKOUT_ON;
reset_clkout_trigger_tmr();
LED_PINGBUT_ON;
reset_ping_ledbut_tmr();
//Flag to update the divmult parameters
if (mode[0][PING_LOCKED]==0) flag_ping_was_changed[0]=1;
if (mode[1][PING_LOCKED]==0) flag_ping_was_changed[1]=1;
ping_time = t_ping_tmr;
}
break;
case(MOVING_AVERAGE_4):
// Ring buffer... hmm, kinda weird when the clock slows down: all sorts of double-hits
// But, it averages out phasing nicely
ringbuff[ringbuff_pos] = t_ping_tmr;
//Use the clock period the first four times we receive ping after boot
//After that, use an average of the previous 4 clock periods
if (ringbuff_filled)
ping_time=(ringbuff[0] + ringbuff[1] + ringbuff[2] + ringbuff[3])/4;
else
ping_time=ringbuff[ringbuff_pos];
if (ringbuff_pos++>=4) {
ringbuff_pos=0;
ringbuff_filled=1;
}
break;
case(LINEAR_AVERAGE_8):
ringbuff[ringbuff_pos] = t_ping_tmr;
//Update the ping time every 8 clocks
if (++ringbuff_pos>=8) {
ringbuff_pos=0;
ping_time=(ringbuff[0] + ringbuff[1] + ringbuff[2] + ringbuff[3] + ringbuff[4] + ringbuff[5] + ringbuff[6] + ringbuff[7])/8;
}
break;
case(LINEAR_AVERAGE_4):
ringbuff[ringbuff_pos] = t_ping_tmr;
//Update the ping time every 4 clocks
if (++ringbuff_pos>=4) {
ringbuff_pos=0;
ping_time=(ringbuff[0] + ringbuff[1] + ringbuff[2] + ringbuff[3])/4;
}
break;
case(MOVING_AVERAGE_2):
//Simple linear average: the catchup is weird, and the phasing is not much different
ping_time = (t_ping_tmr + last_ping)>>1;
last_ping = t_ping_tmr;
break;
case(EXPO_AVERAGE_4):
//Exponential LPF: slowly gets to the actual tempo, so there is always drift
ping_time=(float)t_ping_tmr*0.25 + (float)ping_time*0.75;
ping_time=ping_time & 0xFFFFFFF8;
break;
case(EXPO_AVERAGE_8):
//Exponential LPF: slowly gets to the actual tempo, so there is always drift
ping_time=(float)t_ping_tmr*0.125 + (float)ping_time*0.875;
ping_time=ping_time & 0xFFFFFFF8;
break;
case(ONE_TO_ONE):
//Track the clock 1:1
ping_time=t_ping_tmr;
break;
}
}
TIM_ClearITPendingBit(TIM10, TIM_IT_Update);
}
}
// Ping and INF/REV button and jack read
// Checks each button and digital input jack to see if it's been low for a certain number of cycles,
// and high for a certain number of cycles. We shift 0's and 1's down a 16-bit variable (State[]) to indicate high/low status.
// takes 2-3us
// runs at 27kHz
void INFREVBUTTONJACK_PINGBUT_IRQHandler(void)
{
static uint16_t State[10] = {0,0,0,0xFFFF,0xFFFF,0,0,0xFFFF,0xFFFF,0}; // Current debounce status
uint16_t t;
static uint32_t ch1_clear_ctr=0,ch2_clear_ctr=0, ch1_contrev_ctr=0, ch2_contrev_ctr=0;
// Clear TIM update interrupt
TIM_ClearITPendingBit(INFREVBUTTONJACK_PINGBUT_TIM, TIM_IT_Update);
// Check Ping Button
if (PINGBUT) {
LED_PINGBUT_ON;
t=0xe000;
} else
t=0xe001;
State[0]=(State[0]<<1) | t;
if (State[0]==0xff00){ //1111 1111 0000 0000 = not pressed for 8 cycles , then pressed for 8 cycles
if (!INF1BUT && !INF2BUT && REV1BUT && REV2BUT)
{
flag_acknowlegde_qcm = (6<<8);
if (global_mode[QUANTIZE_MODE_CHANGES]==0)
global_mode[QUANTIZE_MODE_CHANGES] = 1;
else
global_mode[QUANTIZE_MODE_CHANGES] = 0;
flag_ignore_revdown[0] = 1;
flag_ignore_revdown[1] = 1;
}
else if (REV1BUT && !INF1BUT && !INF2BUT && !REV2BUT)
{
flag_ignore_revdown[0] = 1;
}
else if (REV2BUT && !INF1BUT && !INF2BUT && !REV1BUT)
{
flag_ignore_revdown[1] = 1;
}
else if (INF1BUT && !INF2BUT && !REV1BUT && !REV2BUT)
{
if (mode[0][PING_LOCKED]==0)
{
locked_ping_time[0] = ping_time;
mode[0][PING_LOCKED] = 1;
}
else
{
mode[0][PING_LOCKED] = 0;
set_divmult_time(0);
}
flag_ignore_infdown[0] = 1;
}
else if (INF2BUT && !INF1BUT && !REV1BUT && !REV2BUT)
{
if (mode[1][PING_LOCKED]==0)
{
locked_ping_time[1] = ping_time;
mode[1][PING_LOCKED] = 1;
}
else
{
mode[1][PING_LOCKED] = 0;
set_divmult_time(1);
}
flag_ignore_infdown[1] = 1;
}
else if (INF2BUT && INF1BUT && REV1BUT && REV2BUT)
{
flag_ignore_revdown[0] = 1;
flag_ignore_revdown[1] = 1;
flag_ignore_infdown[0] = 1;
flag_ignore_infdown[1] = 1;
}
else if (!INF2BUT && !INF1BUT && !REV1BUT && !REV2BUT)
{
//Clear the ping jack state
ping_jack_state = 0;
//Log how much time has elapsed since last ping
ping_time=ping_tmr & 0xFFFFFFF8; //multiple of 8
//Reset the timers
CLKOUT_ON;
reset_clkout_trigger_tmr();
LED_PINGBUT_ON;
reset_ping_ledbut_tmr();
ping_tmr = 0;
//Flag to update the divmult parameters
if (mode[0][PING_LOCKED]==0) flag_ping_was_changed[0]=1;
if (mode[1][PING_LOCKED]==0) flag_ping_was_changed[1]=1;
}
}
//Todo: Create a global infbut_pressed[2] and revbut_pressed[2] reflecting the de-bounced state
if (!INF1BUT) t=0xe000; else t=0xe001;
State[1]=(State[1]<<1) | t;
if (State[1]==0xf000)
{
// If we wiggled any pots while the inf button was held down, clear the flag
if (flag_pot_changed_infdown[TIME_POT*2])
flag_pot_changed_infdown[TIME_POT*2]=0;
else if (flag_pot_changed_infdown[REGEN_POT*2])
flag_pot_changed_infdown[REGEN_POT*2]=0;
else if (flag_pot_changed_infdown[LEVEL_POT*2])
flag_pot_changed_infdown[LEVEL_POT*2]=0;
else if (flag_pot_changed_infdown[MIX_POT*2])
flag_pot_changed_infdown[MIX_POT*2]=0;
else if (flag_ignore_infdown[0])
flag_ignore_infdown[0]=0;
else
flag_inf_change[0]=1;
}
if (!INF2BUT) t=0xe000; else t=0xe001;
State[2]=(State[2]<<1) | t;
if (State[2]==0xf000)
{
if (flag_pot_changed_infdown[TIME_POT*2+1])
flag_pot_changed_infdown[TIME_POT*2+1]=0;
else if (flag_pot_changed_infdown[REGEN_POT*2+1])
flag_pot_changed_infdown[REGEN_POT*2+1]=0;
else if (flag_pot_changed_infdown[LEVEL_POT*2+1])
flag_pot_changed_infdown[LEVEL_POT*2+1]=0;
else if (flag_pot_changed_infdown[MIX_POT*2+1])
flag_pot_changed_infdown[MIX_POT*2+1]=0;
else if (flag_ignore_infdown[1])
flag_ignore_infdown[1]=0;
else
flag_inf_change[1]=1;
}
if (INF2JACK) t=0xe000; else t=0xe001;
State[4]=(State[4]<<1) | t;
if (State[4]==0xfff8 || (State[4]==0xe007 && global_mode[INF_GATETRIG] == GATE_MODE))
{
flag_inf_change[1]=1;
}
if (INF1JACK) t=0xe000; else t=0xe001;
State[3]=(State[3]<<1) | t;
//1111 1111 1111 1000 = gate low for 13 cycles (5ms), followed by gate high for 3 cycles (1.1ms)
if (State[3]==0xfff8 || (State[3]==0xe007 && global_mode[INF_GATETRIG] == GATE_MODE))
{
flag_inf_change[0]=1;
}
if (!REV1BUT) t=0xe000; else t=0xe001;
State[5]=(State[5]<<1) | t;
if (State[5]==0xf000)
{
if (flag_pot_changed_revdown[TIME_POT*2])
flag_pot_changed_revdown[TIME_POT*2]=0;
else if (flag_pot_changed_revdown[REGEN_POT*2])
flag_pot_changed_revdown[REGEN_POT*2]=0;
else if (flag_pot_changed_revdown[LEVEL_POT*2])
flag_pot_changed_revdown[LEVEL_POT*2]=0;
else if (flag_pot_changed_revdown[MIX_POT*2])
flag_pot_changed_revdown[MIX_POT*2]=0;
else if (flag_ignore_revdown[0])
flag_ignore_revdown[0]=0;
else
flag_rev_change[0]=1;
}
if (!REV2BUT) t=0xe000; else t=0xe001;
State[6]=(State[6]<<1) | t;
if (State[6]==0xf000)
{
if (flag_pot_changed_revdown[TIME_POT*2+1])
flag_pot_changed_revdown[TIME_POT*2+1]=0;
else if (flag_pot_changed_revdown[REGEN_POT*2+1])
flag_pot_changed_revdown[REGEN_POT*2+1]=0;
else if (flag_pot_changed_revdown[LEVEL_POT*2+1])
flag_pot_changed_revdown[LEVEL_POT*2+1]=0;
else if (flag_pot_changed_revdown[MIX_POT*2+1])
flag_pot_changed_revdown[MIX_POT*2+1]=0;
else if (flag_ignore_revdown[1])
flag_ignore_revdown[1]=0;
else
flag_rev_change[1]=1;
}
if (REV2JACK) t=0xe000; else t=0xe001;
State[8]=(State[8]<<1) | t;
if (State[8]==0xfff8 || (State[8]==0xe007 && global_mode[REV_GATETRIG] == GATE_MODE))
{
flag_rev_change[1]=1;
}
if (REV1JACK) t=0xe000; else t=0xe001;
State[7]=(State[7]<<1) | t;
if (State[7]==0xfff8 || (State[7]==0xe007 && global_mode[REV_GATETRIG] == GATE_MODE))
{
flag_rev_change[0]=1;
}
//check continuously held down buttons:
if (RAM_CLEAR_CH1_BUTTONS)
{
if (ch1_clear_ctr++>54000) {
flag_ignore_infdown[0]=1;
flag_ignore_revdown[0]=1;
LED_REV1_ON;
LED_INF1_ON;
LED_LOOP1_ON;
memory_clear(0);
ch1_clear_ctr=0;
LED_REV1_OFF;
LED_INF1_OFF;
LED_LOOP1_OFF;
}
}
else
ch1_clear_ctr=0;
if (RAM_CLEAR_CH2_BUTTONS)
{
if (ch2_clear_ctr++>54000) {
flag_ignore_infdown[1]=1;
flag_ignore_revdown[1]=1;
LED_REV2_ON;
LED_INF2_ON;
LED_LOOP2_ON;
memory_clear(1);
ch2_clear_ctr=0;
LED_REV2_OFF;
LED_INF2_OFF;
LED_LOOP2_OFF;
}
}
else
ch2_clear_ctr=0;
#ifdef ALLOW_CONT_REVERSE
if (mode[0][INF]==INF_OFF && CONTINUOUS_REV1_BUTTONS)
{
if (ch1_contrev_ctr++>54000) {
flag_ignore_revdown[0]=1;
if (mode[0][CONTINUOUS_REVERSE])
{
mode[0][CONTINUOUS_REVERSE] = 0;
} else
{
mode[0][CONTINUOUS_REVERSE] = 1;
//mode[0][REV] = 0;
}
ch1_contrev_ctr = 0;
}
}
else
ch1_contrev_ctr = 0;
if (mode[1][INF]==INF_OFF && CONTINUOUS_REV2_BUTTONS)
{
if (ch2_contrev_ctr++>54000) {
flag_ignore_revdown[1]=1;
if (mode[1][CONTINUOUS_REVERSE])
{
mode[1][CONTINUOUS_REVERSE] = 0;
} else
{
mode[1][CONTINUOUS_REVERSE] = 1;
//mode[1][REV] = 0;
}
ch2_contrev_ctr = 0;
}
}
else
ch2_contrev_ctr = 0;
#endif
}