-
Notifications
You must be signed in to change notification settings - Fork 5
/
twigs.cc
executable file
·801 lines (701 loc) · 22.8 KB
/
twigs.cc
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
//
// Twigs
// Alternate firmware for MI Branches
// Copyright 2016 Ari Russo
//
// Licensed GPL3.0
//
// Based on code from the original MI Branches firmware
// Copyright 2012 Emilie Gillet and licensed GPL 3.0
//
#include <avr/eeprom.h>
#include "avrlib/adc.h"
#include "avrlib/boot.h"
#include "avrlib/gpio.h"
#include "avrlib/watchdog_timer.h"
using namespace avrlib;
// Hardware
Gpio<PortD, 4> in_1;
Gpio<PortD, 3> out_1_a;
Gpio<PortD, 0> out_1_b;
Gpio<PortD, 1> led_1_a;
Gpio<PortD, 2> led_1_k;
Gpio<PortD, 7> in_2;
Gpio<PortD, 6> out_2_a;
Gpio<PortD, 5> out_2_b;
Gpio<PortB, 1> led_2_a;
Gpio<PortB, 0> led_2_k;
Gpio<PortC, 2> button_2;
Gpio<PortC, 3> button_1;
// Global
#define SYSTEM_NUM_CHANNELS 2
// Gate inputs
// Top input must be the reset function since the two inputs are hardware normaled
#define GATE_INPUT_RESET_INDEX 0
#define GATE_INPUT_TRIG_INDEX 1
// Buttons
#define BUTTON_LONG_PRESS_DURATION 9375 // 1200 * 8000 / 1024
// LEDs
#define LED_THRU_GATE_DURATION 0x100
#define LED_FACTORED_GATE_DURATION 0x080
#define PULSE_TRACKER_BUFFER_SIZE 2
// ADC
#define ADC_DELTA_THRESHOLD 4 // ignore ADC updates less than this absolute value
#define ADC_MAX_VALUE 250
// amount of cycles between adc scans. higher number = better performace
#define ADC_POLL_RATIO 5 // 1:5
// Common function values
#define FUNCTION_TIMING_ERROR_CORRECTION_AMOUNT 12
// Swing
#define SWING_FACTOR_MIN 50
// Swing maximum amount can be adjusted up to 99
#define SWING_FACTOR_MAX 70
// Factorer
//
// The number 15 represents the set:
// -8, -7, -6, -5, -4, -3, -2, 1, 2, 3, 4, 5, 6, 7, 8
//
// negative numbers are multiplier factors
// positive numbers are divider factors
// and 1 is bypass
#define FACTORER_NUM_FACTORS 15
// The index of 1 in the above set
// This is the control setting where factorer is neither dividing nor
// multiplying
#define FACTORER_BYPASS_INDEX 7
#define FACTORER_BYPASS_VALUE 1
// Timer counter max value
#define TCNT1_MAX 0xffff
// Trigger length 0.128ms * 20 = 2.56ms
// If you don't need to extend trigger length, set this value to 0
#define TRIGGER_EXTEND_COUNT 20
// Adc
AdcInputScanner adc;
uint8_t adc_counter;
int16_t adc_value[SYSTEM_NUM_CHANNELS];
// Gate input
bool gate_input_state[SYSTEM_NUM_CHANNELS];
// Buttons
bool button_state[SYSTEM_NUM_CHANNELS];
bool button_is_inhibited[SYSTEM_NUM_CHANNELS];
uint16_t button_last_press_at[SYSTEM_NUM_CHANNELS];
// LEDs
uint8_t led_state[SYSTEM_NUM_CHANNELS];
uint16_t led_gate_duration[SYSTEM_NUM_CHANNELS];
// Channel state
uint16_t channel_last_action_at[SYSTEM_NUM_CHANNELS];
uint8_t exec_state[SYSTEM_NUM_CHANNELS];
uint8_t trigger_extend_count[SYSTEM_NUM_CHANNELS];
// Available functions
enum ChannelFunction {
CHANNEL_FUNCTION_FACTORER,
CHANNEL_FUNCTION_SWING,
CHANNEL_FUNCTION_LAST
};
// Default functions
ChannelFunction channel_function_[SYSTEM_NUM_CHANNELS] = {
CHANNEL_FUNCTION_SWING,
CHANNEL_FUNCTION_FACTORER
};
// Common function vars
uint16_t pulse_tracker_buffer[PULSE_TRACKER_BUFFER_SIZE];
uint16_t pulse_tracker_recorded_count;
int16_t factor[SYSTEM_NUM_CHANNELS];
// Multiply
bool multiply_is_debouncing[SYSTEM_NUM_CHANNELS];
// Divide
int8_t divide_counter[SYSTEM_NUM_CHANNELS];
// Swing
int16_t swing[SYSTEM_NUM_CHANNELS];
int8_t swing_counter[SYSTEM_NUM_CHANNELS];
void ClockInit();
// Initialize the gate inputs (used for trig/reset)
void GateInputsInit() {
in_1.set_mode(DIGITAL_INPUT);
in_1.High();
gate_input_state[0] = false;
in_2.set_mode(DIGITAL_INPUT);
in_2.High();
gate_input_state[1] = false;
}
// Initialize the push buttons
void ButtonsInit() {
button_1.set_mode(DIGITAL_INPUT);
button_2.set_mode(DIGITAL_INPUT);
button_1.High();
button_2.High();
button_state[0] = button_state[1] = false;
}
// Initialize the outputs
void GateOutputsInit() {
out_1_a.set_mode(DIGITAL_OUTPUT);
out_1_b.set_mode(DIGITAL_OUTPUT);
out_2_a.set_mode(DIGITAL_OUTPUT);
out_2_b.set_mode(DIGITAL_OUTPUT);
}
// Initialize the LEDs
void LedsInit() {
led_1_a.set_mode(DIGITAL_OUTPUT);
led_1_k.set_mode(DIGITAL_OUTPUT);
led_2_a.set_mode(DIGITAL_OUTPUT);
led_2_k.set_mode(DIGITAL_OUTPUT);
led_1_a.Low();
led_2_a.Low();
led_1_k.Low();
led_2_k.Low();
led_state[0] = led_state[1] = 0;
}
// The value for the pot/CV input for the given channel
inline int16_t AdcReadValue(uint8_t channel) {
uint8_t pin = (channel == 0) ? 1 : 0;
return adc.Read8(pin);
}
// Cache the adc value for the given channel
inline void AdcSetValue(uint8_t channel, int16_t value) {
// store control value
adc_value[channel] = ADC_MAX_VALUE - value;
// appears to be variance between channels, so limit the value
if (adc_value[channel] < 0) {
adc_value[channel] = 0;
} else if (adc_value[channel] > ADC_MAX_VALUE) {
adc_value[channel] = ADC_MAX_VALUE;
}
}
// Initialize the pots and CV inputs
void AdcInit() {
adc.Init();
adc.set_num_inputs(SYSTEM_NUM_CHANNELS);
Adc::set_reference(ADC_DEFAULT);
Adc::set_alignment(ADC_LEFT_ALIGNED);
adc_counter = 1;
// set initial value
for (uint8_t i = 0; i < SYSTEM_NUM_CHANNELS; ++i) {
AdcSetValue(i, AdcReadValue(i));
}
}
// Load the stored system settings from the eeprom
// Currently, this consists of which functions are active on each channel
void SystemLoadState() {
uint8_t configuration_byte = ~eeprom_read_byte((uint8_t*) 0);
// byte values 1 2 4 8
for (uint8_t i = 0; i < SYSTEM_NUM_CHANNELS; ++i) {
uint8_t b = (i+1) * (i+1);
if (configuration_byte & b) {
channel_function_[i] = CHANNEL_FUNCTION_FACTORER;
} else if (configuration_byte & (b * 2)) {
channel_function_[i] = CHANNEL_FUNCTION_SWING;
}
}
}
// Initialize the system
void SystemInit() {
Gpio<PortB, 4>::set_mode(DIGITAL_OUTPUT);
Gpio<PortB, 4>::Low();
// Hardware interface
GateInputsInit();
ClockInit();
ButtonsInit();
GateOutputsInit();
LedsInit();
AdcInit();
SystemLoadState();
TCCR1A = 0;
TCCR1B = 5;
}
// Read the value of the given gate input
inline bool GateInputRead(uint8_t channel) {
return channel == 0 ? !in_1.value() : !in_2.value();
}
// Read the value of the given button
bool ButtonRead(uint8_t channel) {
return channel == 0 ? !button_1.value() : !button_2.value();
}
// Set the given output to high
inline void GateOutputOn(uint8_t channel) {
switch (channel) {
case 0: out_1_a.High();
out_1_b.High();
break;
case 1: out_2_a.High();
out_2_b.High();
break;
}
}
// Set the given output to low
inline void GateOutputOff(uint8_t channel) {
switch (channel) {
case 0: out_1_a.Low();
out_1_b.Low();
break;
case 1: out_2_a.Low();
out_2_b.Low();
break;
}
}
// Clear both of the values in the Pulse Tracker
inline void PulseTrackerClear() {
pulse_tracker_buffer[PULSE_TRACKER_BUFFER_SIZE - 2] = 0;
pulse_tracker_buffer[PULSE_TRACKER_BUFFER_SIZE - 1] = 0;
pulse_tracker_recorded_count = 0;
}
// The amount of time since the last tracked event
inline uint16_t PulseTrackerGetElapsed() {
return (TCNT1 >= pulse_tracker_buffer[PULSE_TRACKER_BUFFER_SIZE - 1])
? TCNT1 - pulse_tracker_buffer[PULSE_TRACKER_BUFFER_SIZE - 1]
: TCNT1 + (TCNT1_MAX - pulse_tracker_buffer[PULSE_TRACKER_BUFFER_SIZE - 1]);
}
// The period of time between the last two recorded events
inline uint16_t PulseTrackerGetPeriod() {
return (pulse_tracker_buffer[PULSE_TRACKER_BUFFER_SIZE - 1] >= pulse_tracker_buffer[PULSE_TRACKER_BUFFER_SIZE - 2])
? pulse_tracker_buffer[PULSE_TRACKER_BUFFER_SIZE - 1] - pulse_tracker_buffer[PULSE_TRACKER_BUFFER_SIZE - 2]
: pulse_tracker_buffer[PULSE_TRACKER_BUFFER_SIZE - 1] + (TCNT1_MAX - pulse_tracker_buffer[PULSE_TRACKER_BUFFER_SIZE - 2]);
}
// Is the pulse tracker populated with enough events to perform multiply?
inline bool PulseTrackerHasPeriod(uint8_t channel) {
return pulse_tracker_recorded_count >= PULSE_TRACKER_BUFFER_SIZE;
}
// Record the current time as the latest pulse tracker event and shift the last one back
void PulseTrackerRecord() {
// shift
pulse_tracker_buffer[PULSE_TRACKER_BUFFER_SIZE - 2] = pulse_tracker_buffer[PULSE_TRACKER_BUFFER_SIZE - 1];
pulse_tracker_buffer[PULSE_TRACKER_BUFFER_SIZE - 1] = TCNT1;
if (pulse_tracker_recorded_count < PULSE_TRACKER_BUFFER_SIZE) {
pulse_tracker_recorded_count += 1;
}
}
// Is the factor control setting such that we're in multiplier mode?
inline bool MultiplyIsEnabled(uint8_t channel) {
return factor[channel] < FACTORER_BYPASS_VALUE;
}
// The time interval between multiplied events
// eg if clock is comes in at 100 and 200, and the clock multiply factor is 2,
// the result will be 50
inline uint16_t MultiplyInterval(uint8_t channel) {
return PulseTrackerGetPeriod() / -factor[channel];
}
// Should the multiplier function exec on this cycle?
inline bool MultiplyShouldStrike(uint8_t channel, uint16_t elapsed) {
uint16_t interval = MultiplyInterval(channel);
if (elapsed % interval <= FUNCTION_TIMING_ERROR_CORRECTION_AMOUNT) {
if (!multiply_is_debouncing[channel]) {
return true;
}
// debounce window/return false
} else {
// debounce is finished
multiply_is_debouncing[channel] = false;
}
return false;
}
// Is the factor setting such that we're in divider mode?
inline bool DivideIsEnabled(uint8_t channel) {
return factor[channel] > FACTORER_BYPASS_VALUE;
}
// Should the divider function exec on this cycle?
inline bool DivideShouldStrike(uint8_t channel) {
return divide_counter[channel] <= 0;
}
// What is the current factor setting?
inline int16_t FactorGet(uint8_t channel) {
int16_t factor_index = (adc_value[channel] / (ADC_MAX_VALUE / (FACTORER_NUM_FACTORS - 1))) - FACTORER_BYPASS_INDEX;
// offset result so that there's no -1 or 0 factors, but values are still evenly spaced
if (factor_index == 0) {
return FACTORER_BYPASS_VALUE;
} else if (factor_index < 0) {
return --factor_index; // abs
} else {
return ++factor_index;
}
}
// Turn off the LED for the given channel
inline void LedOff(uint8_t channel) {
switch(channel) {
case 0: led_1_a.Low();
led_1_k.Low();
break;
case 1: led_2_a.Low();
led_2_k.Low();
break;
}
}
// Make the LED for the given channel green
inline void LedGreen(uint8_t channel) {
switch(channel) {
case 0: led_1_a.Low();
led_1_k.High();
break;
case 1: led_2_a.Low();
led_2_k.High();
break;
}
}
// Make the LED for the given channel red
inline void LedRed(uint8_t channel) {
switch(channel) {
case 0: led_1_a.High();
led_1_k.Low();
break;
case 1: led_2_a.High();
led_2_k.Low();
break;
}
}
// Scan both pots and CV inputs for changes
inline void AdcScan() {
if (adc_counter == (ADC_POLL_RATIO-1)) {
adc.Scan();
adc_counter = 0;
} else {
++adc_counter;
}
}
// Does the pot/CV input for the given channel have a new value since last checked?
bool AdcHasNewValue(uint8_t channel) {
if (adc_counter == 0) {
int16_t value = AdcReadValue(channel);
// compare to stored control value
int16_t delta = value - adc_value[channel];
// abs
if (delta < 0) {
delta = -delta;
}
if (delta > ADC_DELTA_THRESHOLD) {
AdcSetValue(channel, value);
return true;
}
}
return false;
}
// Initialize the pulse tracker and other time based variables
inline void ClockInit() {
PulseTrackerClear();
for (uint8_t i = 0; i < SYSTEM_NUM_CHANNELS; ++i) {
channel_last_action_at[i] = 0;
trigger_extend_count[i] = 0;
button_last_press_at[i] = 0;
button_is_inhibited[i] = false;
}
}
// For the given channel, use the LEDs to signify that trig thru is occurring
// EG in multiplier mode, an output that occurs at the same time as a trig input
inline void LedExecThru(uint8_t channel) {
led_gate_duration[channel] = LED_THRU_GATE_DURATION;
led_state[channel] = 1;
}
// For the given channel, use the LEDs to signify that a factored output is happening
// EG in multiplier mode, an output that occurs between trig inputs
inline void LedExecStrike(uint8_t channel) {
led_gate_duration[channel] = LED_FACTORED_GATE_DURATION;
led_state[channel] = 2;
}
// For the given channel, get the current swing amount value specified by the pot/CV input
inline int16_t SwingGet(uint8_t channel) {
return (adc_value[channel] / (ADC_MAX_VALUE / (SWING_FACTOR_MAX - SWING_FACTOR_MIN))) + SWING_FACTOR_MIN;
}
// Update the LEDs for the given channel based on the current system state
inline void LedUpdate(uint8_t channel) {
//
if (led_gate_duration[channel]) {
--led_gate_duration[channel];
if (!led_gate_duration[channel]) {
led_state[channel] = 0;
}
}
// Update Leds
switch (led_state[channel]) {
case 0: LedOff(channel);
break;
case 1: LedGreen(channel);
break;
case 2: LedRed(channel);
break;
}
}
// Is the gate input for the given channel seeing a new pulse?
inline bool GateInputIsRisingEdge(uint8_t channel) {
bool last_state = gate_input_state[channel];
// store current input state
gate_input_state[channel] = GateInputRead(channel);
//
return gate_input_state[channel] && !last_state;
}
// For the given channel, update state for a multiply strike
inline void MultiplyExecStrike(uint8_t channel) {
channel_last_action_at[channel] = TCNT1;
exec_state[channel] = 2;
multiply_is_debouncing[channel] = true;
}
// For the given channel and current system state, execute a single
// cycle of the multiplier function
inline void MultiplyExec(uint8_t channel) {
if (MultiplyIsEnabled(channel) &&
PulseTrackerHasPeriod(channel) &&
MultiplyShouldStrike(channel, PulseTrackerGetElapsed())) {
MultiplyExecStrike(channel);
}
}
// For the given channel, reset the divider function
inline void DivideReset(uint8_t channel) {
divide_counter[channel] = 0;
}
// For the given channel and current system state, should the divider function reset?
inline bool DivideShouldReset(uint8_t channel) {
return divide_counter[channel] >= (factor[channel] - 1);
}
// For the given channel, update state for a divide strike
inline void DivideExecStrike(uint8_t channel) {
channel_last_action_at[channel] = TCNT1;
exec_state[channel] = 2; // divide converts thru to exec on every division
}
// Update the given channel's state to reflect a multiplier thru for this cycle
inline void MultiplyExecThru(uint8_t channel) {
exec_state[channel] = 1;
channel_last_action_at[channel] = TCNT1;
}
// For the given channel, process a new pulse using the factorer function
void FactorerHandleInputGateRisingEdge(uint8_t channel) {
//
if (DivideIsEnabled(channel)) {
if (DivideShouldStrike(channel)) {
DivideExecStrike(channel);
}
// deal with counter
if (DivideShouldReset(channel)) {
DivideReset(channel);
} else {
++divide_counter[channel];
}
} else {
MultiplyExecThru(channel); // mult always acknowledges thru
}
}
// For the given channel, pulses stored in the pulse tracker, and the factor setting
// of the swing function, what is the time interval that the swung output will be delayed
// passed the corresponding input gate?
//
// IE in the following illustration of a full swing routine, the interval between "input pulse 2"
// and "swing strike"
//
// [input pulse1/swing thru].......[input pulse2]....[swing strike]..........
//
inline uint16_t SwingInterval(uint8_t channel) {
uint16_t period = PulseTrackerGetPeriod();
return ((10 * (period * 2)) / (1000 / swing[channel])) - period;
}
// For the given amount of time since the last swing strike/thru, should the swing function
// on the given channel exec during this cycle?
inline bool SwingShouldStrike(uint8_t channel, uint16_t elapsed) {
if (swing_counter[channel] >= 2 && swing[channel] > SWING_FACTOR_MIN) {
uint16_t interval = SwingInterval(channel);
return (elapsed >= interval &&
elapsed <= interval + FUNCTION_TIMING_ERROR_CORRECTION_AMOUNT);
} else {
// thru
return false;
}
}
// Reset the swing function for the given channel
inline void SwingReset(uint8_t channel) {
swing_counter[channel] = 0;
}
// Update the given channel's state to reflect a swing thru execution for this cycle
inline void SwingExecThru(uint8_t channel) {
exec_state[channel] = 1;
channel_last_action_at[channel] = TCNT1;
}
// Update the given channel's state to reflect a swing strike execution for this cycle
inline void SwingExecStrike(uint8_t channel) {
exec_state[channel] = 2;
channel_last_action_at[channel] = TCNT1;
}
// For the given channel, process a new pulse using the swing function
void SwingHandleInputGateRisingEdge(uint8_t channel) {
switch (swing_counter[channel]) {
case 0: // thru beat
SwingExecThru(channel);
swing_counter[channel] = 1;
break;
case 1: // skipped thru beat
// unless lowest setting, no swing - should do thru
if (swing[channel] <= SWING_FACTOR_MIN) {
SwingExecStrike(channel);
SwingReset(channel);
} else {
// rest
exec_state[channel] = 0;
swing_counter[channel] = 2;
}
break;
default: SwingReset(channel); // something is wrong if we're here so reset
break;
}
}
// For the given channel and current system state, execute a single
// cycle of the swing function
inline void SwingExec(uint8_t channel) {
if (SwingShouldStrike(channel, PulseTrackerGetElapsed())) {
SwingExecStrike(channel);
SwingReset(channel); // reset
}
}
// For the given channel, handle a new value at the pot/CV input
inline void FunctionHandleNewAdcValue(uint8_t channel) {
switch(channel_function_[channel]) {
case CHANNEL_FUNCTION_FACTORER: factor[channel] = FactorGet(channel);
break;
case CHANNEL_FUNCTION_SWING: swing[channel] = SwingGet(channel);
break;
}
}
// For the given channel's function, execute a single cycle
inline void FunctionExec(uint8_t channel) {
switch(channel_function_[channel]) {
case CHANNEL_FUNCTION_FACTORER: MultiplyExec(channel);
break;
case CHANNEL_FUNCTION_SWING: SwingExec(channel);
break;
}
// Do stuff
if (exec_state[channel] > 0) {
GateOutputOn(channel);
trigger_extend_count[channel] = TRIGGER_EXTEND_COUNT;
(exec_state[channel] < 2) ? LedExecThru(channel) : LedExecStrike(channel);
} else {
if (trigger_extend_count[channel] <= 0) {
GateOutputOff(channel);
} else {
trigger_extend_count[channel] -= 1;
}
}
exec_state[channel] = 0; // clean up
}
// Reset the given channel's function
inline void FunctionReset(uint8_t channel) {
switch(channel_function_[channel]) {
case CHANNEL_FUNCTION_FACTORER: DivideReset(channel);
break;
case CHANNEL_FUNCTION_SWING: SwingReset(channel);
break;
}
}
// For the given channel's function, handle a new input gate
inline void FunctionHandleInputGateRisingEdge(uint8_t channel) {
switch(channel_function_[channel]) {
case CHANNEL_FUNCTION_FACTORER: FactorerHandleInputGateRisingEdge(channel);
break;
case CHANNEL_FUNCTION_SWING: SwingHandleInputGateRisingEdge(channel);
break;
}
}
// Based on the given channel's state, execute a single system cycle
inline void ChannelExec(uint8_t channel) {
// do stuff
FunctionExec(channel);
LedUpdate(channel);
}
// Update the given channel's state according to the system input state
inline void ChannelStateUpdate(uint8_t channel, bool is_trig, bool is_reset) {
// Update for pot/cv in
if (AdcHasNewValue(channel)) {
FunctionHandleNewAdcValue(channel);
}
// Update for clock/trig/gate input
if (is_trig) {
FunctionHandleInputGateRisingEdge(channel);
}
// Update for reset
if (is_reset) {
FunctionReset(channel);
}
}
// Save the system state to the eeprom
// Currently stores which functions are selected by the user
void SystemStateSave() {
uint8_t configuration_byte = 0;
// byte values 1 2 4 8
for (uint8_t i = 0; i < SYSTEM_NUM_CHANNELS; ++i) {
uint8_t b = (i+1) * (i+1);
switch(channel_function_[i]) {
case CHANNEL_FUNCTION_FACTORER: configuration_byte |= b;
break;
case CHANNEL_FUNCTION_SWING: configuration_byte |= (b * 2);
break;
}
}
eeprom_write_byte((uint8_t*) 0, ~configuration_byte);
}
// Toggle the function for the given channel
void ChannelFunctionToggle(uint8_t channel) {
switch(channel_function_[channel]) {
case CHANNEL_FUNCTION_FACTORER: channel_function_[channel] = CHANNEL_FUNCTION_SWING;
break;
case CHANNEL_FUNCTION_SWING: channel_function_[channel] = CHANNEL_FUNCTION_FACTORER;
break;
}
FunctionReset(channel);
}
// For the given channel, record a button press start
inline void ButtonHandleNewlyPressed(uint8_t channel) {
button_last_press_at[channel] = TCNT1;
button_is_inhibited[channel] = false;
}
// For the given channel, is the button in a new state than it was last cycle?
inline bool ButtonIsNewState(uint8_t channel) {
bool input_state = ButtonRead(channel);
if (!button_state[channel] && input_state) {
ButtonHandleNewlyPressed(channel);
}
return input_state;
}
// Scan the button state and execute any actions accordingly
void ButtonsScanAndExec() {
for (uint8_t i = 0; i < SYSTEM_NUM_CHANNELS; ++i) {
bool new_input_state = ButtonIsNewState(i);
if (button_state[i] && !button_is_inhibited[i]) {
uint16_t button_press_time = (TCNT1 >= button_last_press_at[i])
? TCNT1 - button_last_press_at[i]
: TCNT1 + (TCNT1_MAX - button_last_press_at[i]);
if (button_press_time >= BUTTON_LONG_PRESS_DURATION) {
button_is_inhibited[i] = true;
// long press
// toggle functions & save
ChannelFunctionToggle(i);
SystemStateSave();
} else if (new_input_state) {
// short press
// do reset
FunctionReset(i);
}
}
button_state[i] = new_input_state;
}
}
// Single system loop
inline void Loop() {
// Scan pot/cv in
AdcScan();
// Scan buttons
ButtonsScanAndExec();
// Scan clock/trig/gate input
bool is_trig = false;
if (GateInputIsRisingEdge(GATE_INPUT_TRIG_INDEX)) {
// Pulse tracker is always recording. this should help smooth transitions
// between functions even though divide doesn't use it
PulseTrackerRecord();
is_trig = true;
}
// scan reset input
bool is_reset = GateInputIsRisingEdge(GATE_INPUT_RESET_INDEX);
// do stuff
for (uint8_t i = 0; i < SYSTEM_NUM_CHANNELS; ++i) {
ChannelStateUpdate(i, is_trig, is_reset);
ChannelExec(i);
}
}
int main(void) {
ResetWatchdog();
SystemInit();
while (1) {
Loop();
}
}