-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1520 lines (1276 loc) · 35.6 KB
/
main.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
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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* The software for the x0xb0x is available for use in accordance with the
* following open source license (MIT License). For more information about
* OS licensing, please visit -> http://www.opensource.org/
*
* For more information about the x0xb0x project, please visit
* -> http://www.ladyada.net/make/x0xb0x
*
* *****
* Copyright (c) 2005 Limor Fried
*
* 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.
* *****
*
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include "main.h"
#include "led.h"
#include "switch.h"
#include "delay.h"
#include "pattern.h"
#include "compcontrol.h"
#include "keyboard.h"
#include "midi.h"
#include "eeprom.h"
#include "synth.h"
#include "dinsync.h"
#include "randomizer.h"
/*
*
* Check if on of the both supported CPU types is defined
* #define some helpers, so the code is less cluttered with #ifdef __WHICH_CPU_BLAH___
*/
#if defined( __AVR_ATmega162__ )
#define ENA_LED 0xBB
#define CURRENT_CPU_TIMSK3 ETIMSK
#define CURRENT_CPU_EE_WRITE_EN EEWE
#define CURRENT_CPU_EE_WRITE_PREPARE EEMWE
#elif defined( __AVR_ATmega2561__ )
#define ENA_LED 0xF7
#define CURRENT_CPU_TIMSK3 TIMSK3
#define CURRENT_CPU_EE_WRITE_EN EEPE
#define CURRENT_CPU_EE_WRITE_PREPARE EEMPE
#else
#error "Selected CPU not supported"
#endif
#define UART_BAUDRATE 19200UL
#define MIDI_BAUDRATE 31250UL // the MIDI spec baudrate
#define TEMPOISRUNNING (CURRENT_CPU_TIMSK3&(1<<OCIE3A))
volatile int8_t tk_value; // tempo knob moved
#ifdef HASDOUBLECLICKF
volatile uint8_t doublePressTimeOut; // counted down by 1ms interrupt
#endif
volatile uint8_t debounce_timer; // counted up by 1ms interrupt,
volatile uint8_t run_tempo; // to start do_tempo if (Tempo) Timer is elapsed or Din-Sync or Midi-Sync received.
volatile uint16_t tapTempoTimer; // for measuring time between two taps.
uint8_t settings[NUM_SETTINGS_TOTAL] ={ // "USER C" + other permanent user settings configuration bytes from/to internal EEPROM
SETTINGS0_LIVE_SETCHAIN | SETTINGS0_KEEP_PATT | SETTINGS0_AUTO_INC, // default settings 0
SETTINGS1_STARTMODE | SETTINGS1_HELPLED, // default settings 1
0,0,0,SETTINGS_VERSION_MAGIC, // settings 2,3,4, Version-Magic
0,0,0,0,0,0,0,0,0,0 // unused bytes for future expansion
};
uint8_t curPatternRun = 0;
uint8_t playing; // are we playing?
uint8_t tempo; // current internal tempo
uint16_t delay_clock9ms; // the value timer 3 must be changed to get a proper delay for start to clock
uint8_t sync = INTERNAL_SYNC;
uint8_t note_counter; // counts 1/32 notes (as the 303 gate length was 1/32 note)
uint8_t curr_note;
uint8_t prev_note;
uint8_t swing_it = 0;
// The randomizer control values which are editable with the knob mode
uint8_t accentRand;
uint8_t slideRand;
uint8_t octRand;
uint8_t restRand;
uint8_t octDownRand;
uint8_t variFirstStep ;
uint8_t variBarModulo;
uint8_t variLastStep=15;
// Tempo knob function parameter info:
uint8_t * const tk_paras[NUM_KNOB_PARAMS] = { NULL, &octRand, &octDownRand, &slideRand, &accentRand,&restRand, &swingPercent, &gateLen, &variBarModulo,&variFirstStep, &variLastStep };
const uint8_t tk_maxval[NUM_KNOB_PARAMS] = { 0, 61, 61, 61, 61, 61, 100, 38, 15, 15, 15 };
const uint8_t tk_fnkey[NUM_KNOB_PARAMS] = { 0, KEY_UP, KEY_DOWN, KEY_SLIDE, KEY_ACCENT, KEY_REST, KEY_PREV, KEY_NEXT, KEY_CS, KEY_DS, KEY_FS };
#ifdef SYNC_OUT
// when doing midi sync to dinsync conversion, this is the timeout
// to dropping the clock after a MIDICLOCK message
static volatile uint8_t dinsync_clock_timeout = 0;
static uint16_t pendingDinPulse[12];
static uint8_t note_counterFullTempo;
#endif
volatile uint8_t tempoMeasure;
static uint8_t tmpo = 120;
static uint8_t midi_tempo = 120;
static uint8_t prevSlide = 0;
static uint8_t isSlide = 0;
static uint8_t swingNoteOff = 0;
static uint8_t swingNoteOn = 0;
static uint16_t swingNoteOff_timeout = 0;
static uint16_t swingNoteOn_timeout = 0;
static volatile uint8_t dispatch_swing_note;
static uint8_t wasAcc = 0;
// store pitch shifted note values here, to be able to sent matching noteOff after pitchshift changed
static uint8_t prevNoteSent = 0;
static uint8_t noteSent = 0;
/* 8th note stuff */
static uint8_t skipit = TRUE;
static uint8_t runhalf = FALSE;
static uint8_t onemore = FALSE;
static int8_t scaleCorrTab[2][12] =
{
{ 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 }, // major
{ 0, 1, 0, 0, -1, 0, 1, 0, 0, -1, 0, -1 }, // minor
};
static const uint16_t scaleModifications[2][2][2] = // main type / dim2nd / harm min
{
{ { 0x05ad,
0x09ad },
{ 0x05ab,
0x09ab } },
{ { 0x0ab5,
0x0ab5 },
{ 0x0ab3,
0x0ab3 } }
};
//void dispatch_note_off(int8_t pitch_shift);
void dispatch_note_off(void);
void dispatch_note_on(int8_t pitch_shift);
void load_next_chain(uint8_t reset);
void setup_scales(void);
void dumb_functions(uint8_t func);
/*
*
* Set up pins and UARTs on start up
*
*/
void ioinit()
{
#if defined( __AVR_ATmega162__ )
PORTA = 0x3C; // pullups on rotary1,2,4,8
DDRA = 0xC0; // led latch (o), rotary com (o), rot1, rot2, rot4, ro8, tempoa, tempob
PORTB = 0x04; // Uart Tx high
DDRB = 0xB9; // spi_clk, spi_in, spi_out, NC, USB-UART TX, RX, LED_ENABLE (was NC), switch latch (o)
DDRC = 0xFF; // accent, slide, note[0-5]
PORTD = 0x02; // Midi TX high
DDRD = 0xFF; // dinsync1, 2, 3, 4 (outputs), NC, NC, MIDI TX & RX
DDRE = 0xFF; // note latch, gate, NC
#elif defined( __AVR_ATmega2561__ )
PORTA = 0x3C; // pullups on rotary1,2,4,8
DDRA = 0xC0; // led latch (o), rotary com (o), rot1, rot2, rot4, ro8, tempoa, tempob
PORTB = 0x0; // Bootloader messed that up => back to zero
DDRB = 0xB7; // NC,LED_ENABLE (was NC) ,gate,note latch,miso,mosi,sck,switch latch
DDRC = 0xFF; // accent, slide, note[0-5]
DDRD = 0xF8; // dinsync1,2,3,4(o),usb tx(o),usb rx,i2c(not used at the moment)
PORTD = 0x0b; // pullups on for unsed pins, usb tx set high
PORTE = 0xff; // pullups on for unused pins, midi tx set high
DDRE = 0x02; // nc,nc,eeprom,nc,nc,nc,nc,nc,midi tx(o), midi rx
PORTF = 0xff; // pullups on for unused pins
DDRF = 0x00; // all pins unused
PORTG = 0xff; // pullups on for unused pins
DDRG = 0x00; // all pins unused
#endif
// SPI set up:
#if CFG_SPI_CLOCK_FREQ_MHZ==1
SPCR = (1 << SPE) | (1 << MSTR) | (1<<SPR0); // master spi, clk=fosc/16 = 1mhz
#elif CFG_SPI_CLOCK_FREQ_MHZ==2
SPSR = (1<<SPI2X);
SPCR = (1 << SPE) | (1 << MSTR) | (1<<SPR0); // master spi, clk=fosc/8 = 2mhz
#elif CFG_SPI_CLOCK_FREQ_MHZ==4
SPCR = (1 << SPE) | (1 << MSTR); // master spi, clk=fosc/4 = 4mhz
#else
SPSR = (1<<SPI2X);
SPCR = (1 << SPE) | (1 << MSTR); // master spi, clk=fosc/2 = 8mhz
#endif
/* setup the USB-UART */
uint16_t baud = (F_CPU / (16 * UART_BAUDRATE)) - 1;
UCSR1B |= (1 << RXEN1) | (1 << TXEN1); // read and write & intr
UBRR1L = (uint8_t) baud; // set baudrate
UBRR1H = (uint8_t) (baud >> 8);
UCSR1B |= (1 << RXCIE1); // now turn on RX interrupts
/* setup the MIDI UART */
baud = (F_CPU / (16 * MIDI_BAUDRATE)) - 1;
UCSR0B |= (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0); // read and write, interrupt on recv.
UBRR0L = (uint8_t) baud; // set baudrate
UBRR0H = (uint8_t) (baud >> 8);
}
////////////////////////////////// main()
int main(void)
{
uint8_t i;
ioinit(); // set up IO ports and the UART
init_tempo(); // start the tempo timer
init_timer0(); // start the 1ms 'rtc' timer0
#ifdef DIN_SYNC_IN
dinsync_set_out(); // output DINSYNC
#endif
init_midi();
clock_leds(); // set shift register outputs to zero - this enables the interrupts also!
// sei(); // enable interrupts aready done by clock_leds()
DDRB = ENA_LED; // enable LEDs. (=> for Hardware mod to *NOT* have the anoying random start up state of the HC594 shown on the LEDs. )
#if !ALL_FIXED_SETTING
uint8_t version = internal_eeprom_read8(SETTINGS_VERSION+USERSETTINGS_EEADDR);
if (version != SETTINGS_VERSION_MAGIC)
{
for(i=0;i<NUM_SETTINGS_TOTAL;++i)
internal_eeprom_write8(USERSETTINGS_EEADDR + i , settings[i]);
}
for(i=0;i<NUM_SETTINGS_TOTAL;++i)
settings[i] = internal_eeprom_read8(USERSETTINGS_EEADDR+i); //read settings from EEPROM
#endif
i=4;
while(read_switches() || i--) ; // four "real" reads required to get all switches in current positions.
// the main loop!
while(1)
{
tempoKnobMode=0; // end parameter edit
edit_mode=EDIT_NONE;
playing=0; // mode change stops all playing
curr_pitch_shift=0; // start up with no shift, so it hast not be cleared on every mode function startup and return.
next_pitch_shift=0;
setup_scales();
dinsync_stop(); // just in case it was on during last mode ..
clear_all_leds(); // start up every mode with no LEDs, so not every mode has to clear them on start up!
turn_off_tempo(); // start up every function with tempo of, so it has just to be switched on if needed
midi_clr_input(); // not all modes read midi in, so clean up the buffer to remove anything old accumulated in another mode
note_off(0); // shut up after mode change.
sync = INTERNAL_SYNC; // just the most often as default ... saves a few bytes.
switch(function)
{
case EDIT_PATTERN_FUNC:
sync = MIDI_SYNC;
do_pattern_edit();
break;
case PLAY_PATTERN_FUNC:
do_patterntrack_play();
break;
case PLAY_PATTERN_DINSYNC_FUNC:
sync = DIN_SYNC;
do_patterntrack_play();
break;
case PLAY_PATTERN_MIDISYNC_FUNC:
sync = MIDI_SYNC;
do_patterntrack_play();
break;
case MIDI_CONTROL_FUNC:
do_midi_mode();
break;
case KEYBOARD_MODE_FUNC:
do_keyboard_mode();
break;
case A_FUNC: // = edit pattern with midi sync on
sync = MIDI_SYNC;
do_pattern_edit();
break;
case B_FUNC:
sync = DIN_SYNC;
do_pattern_edit();
break;
case RANDOM_MODE_FUNC:
#ifdef HASRANDOM
set_syncmode();
#endif
case COMPUTER_CONTROL_FUNC:
case EDIT_TRACK_FUNC:
default:
dumb_functions(function);
break;
}
}
}
void clearPendingDinPulses(void)
{
#ifdef SYNC_OUT
for(uint8_t i = 0; i < 12; i++)
pendingDinPulse[i] = 0;
note_counterFullTempo = 0;
#endif
skipit = 1;
}
/*void clearScheduledNotes()
{
swingNoteOn = 0;
swingNoteOn_timeout = 0;
swingNoteOff = 0;
swingNoteOff_timeout = 0;
}*/
uint8_t findEOP(void)
{
uint8_t index=0;
while((index < patt_length) && (pattern_buff[index] != END_OF_PATTERN))
(index)++;
return index;
}
uint8_t variationAllowed(void)
{
if( variationOn
&& pattern_play_index >= variFirstStep
&& pattern_play_index <= variLastStep
&& curPatternRun % (variBarModulo + 1) == variBarModulo )
{
return 1;
}
else
{
return 0;
}
}
/* */
// the 'tempo' interrupt! (on timer 3)
// gets called 2*4*DINSYNC_PPQ times per beat (192 calls per beat @ sync24)
// fastest is 255PM -> 1.225ms
ISR(TIMER3_COMPA_vect)
{
run_tempo++;
}
void clock_ticks()
{
if(run_tempo)
{
cli();
run_tempo--;
sei();
do_tempo();
}
if(dispatch_swing_note==1)
{
dispatch_note_on(swingNoteOn);
}
if(dispatch_swing_note==2)
{
//dispatch_note_off(swingNoteOff);
dispatch_note_off();
}
dispatch_swing_note=0;
}
void do_tempo(void)
{
#ifdef HASRANDOM
static uint8_t next_random_note;
#endif
uint8_t curr_function = function;
uint8_t division_factor = patt_length / 4;
cli();
int8_t GL = gateLen - 18;
#ifdef SYNC_OUT
if(!note_counter && !dinsync_counter)
{
note_counterFullTempo = 0;
swing_counter = 0;
}
if(swing_counter > 5)
swing_counter = 0;
if(!swing_counter)
{
tmpo = tempo;
if(sync == MIDI_SYNC || sync == DIN_SYNC)
tmpo = midi_tempo;
if(note_counterFullTempo > 7)
note_counterFullTempo = 0;
if(!note_counterFullTempo)
{
clearPendingDinPulses();
}
if(!(note_counterFullTempo % 2))
{
uint8_t swung = (note_counterFullTempo % 4) > 0;
uint16_t *pend = pendingDinPulse;
if(swung)
pend += 6;
uint16_t straightPulseDist = 10000 / tmpo;
for(uint16_t i = 0; i < 6; i++)
{
int16_t factor;
if(swung) //swung, go out
factor = 50 - (i * 8);
else
//unswung go in
factor = i * 8;
*pend++ = ((i * straightPulseDist+2)/4) + (factor * swingPercent) / tmpo + 1;
}
}
note_counterFullTempo++;
}
swing_counter++;
#else
tmpo = tempo;
if(sync == MIDI_SYNC || sync == DIN_SYNC)
tmpo = midi_tempo;
#endif
// if the sync is internal or whatever, we have to generate dinsync/midisync msgs
if(dinsync_counter >= DINSYNC_PPQ / division_factor) // 6 or 8 (triplet->8)
{
dinsync_counter = 0;
if(!(note_counter & 0x1))
swing_it = !swing_it;
}
uint16_t swingShift = (50 * swingPercent) / tmpo; //ms
if(swingPercent && !swingShift)
swingShift=1; // avoid 0 if swingPercent != 0, as that does not work proper!
int16_t fac = 7500;
if(patt_length == 15)
fac = 10000;
if(runhalf)
fac *= 2;
int16_t gateLenNorm = fac / tmpo; //ms of a 1/32 note
uint16_t maxNoteoffTime = 0;
//TODO check all this for triplets and/or eight-mode
maxNoteoffTime = gateLenNorm * 2 - 5;
// if (!swing_it)
// maxNoteoffTime += swingShift; // cant handle noteOff for longer notes!!
if(!swing_it)
swingShift = 0;
// 24 pulses per quarter, increment
// make sure that all notes actually start on the zero count
// so that tempo and SYNC are aligned.
if(dinsync_counter != 0)
{
dinsync_counter++;
sei();
return;
}
dinsync_counter++;
sei();
if(!(note_counter & 0x1))
{
//change the grid only for done "rounds" of clocks!
if(pattern_buff[PATT_SIZE - 1] == 0x01)
patt_length = 15;
else
patt_length = 16;
}
skipit = !skipit; /* 8th note stuff */
if((!runhalf && !onemore) || !skipit)
{
int8_t schedule_note=-128;
// reset note counter
if(note_counter >= 8)
note_counter = 0;
if(note_counter & 0x1)
{ // sixteenth notes
#ifdef HASRANDOM
schedule_note=0;
#endif
if(playing)
{
schedule_note=curr_pitch_shift;
if(loop && pattern_play_index == loop_start - 1)
{
curr_pitch_shift = next_pitch_shift;
/* 8th note stuff */
if(runhalf != eighths)
{
runhalf = eighths;
skipit = runhalf;
onemore = !skipit;
}
if(!chains_equiv(next_chain, curr_chain))
{
pattern_play_index = patt_length;
loop_start = 1;
loop_end = patt_length;
loop = FALSE;
}
}
// last note of this pattern?
if((pattern_play_index >= patt_length) || (pattern_buff[pattern_play_index] == END_OF_PATTERN))
{
/* 8th note stuff */
if(runhalf != eighths)
{
runhalf = eighths;
skipit = runhalf;
onemore = !skipit;
}
pattern_play_index = 0; // start next pattern in chain
curPatternRun++;
if(curr_function==PLAY_PATTERN_FUNC ||curr_function==PLAY_PATTERN_DINSYNC_FUNC ||curr_function==PLAY_PATTERN_MIDISYNC_FUNC )
{ // one of the play modes, these switch patterns. Edit modes should not do that.
load_next_pattern(); // load next pattern in chain routine
}
}
} //playing
if(schedule_note!=-128)
{
if(GL == 0 && swing_it && swingPercent > 0)
{
swingNoteOff_timeout = swingShift; //(50*swingPercent) / tempo; //ms
swingNoteOff = curr_pitch_shift;
}
else if(GL == 0)
{
//dispatch_note_off(curr_pitch_shift);
dispatch_note_off();
}
}
}
else
{
/* break out */
prev_note = curr_note;
#ifdef HASRANDOM
if(curr_function==RANDOM_MODE_FUNC)
{
curr_note = next_random_note;
next_random_note = random();
set_note_led(curr_note);
schedule_note=0;
}
#endif
if(playing)
{
if(!tempoKnobMode)
set_current_index_led();
// load up the next note
curr_note = pattern_buff[pattern_play_index];
if(curr_note != 0xFF)
{
if( (edit_mode&EDIT_RUNNING) && !store_mode ) // running edit, DONE not pressed: display notes
set_note_led(curr_note);
schedule_note=curr_pitch_shift; //+ get_pitchshift_from_patt(curr_patt) );
}
pattern_play_index = get_next_patt_idx();
}
if(schedule_note !=-128)
{
if(swing_it && swingPercent > 0)
{
swingNoteOn_timeout = swingShift; //(50*swingPercent) / tempo; //ms
swingNoteOn = schedule_note;
}
else
{
dispatch_note_on(schedule_note);
}
if(GL != 0)
{
swingNoteOff_timeout = gateLenNorm + GL * gateLenNorm / 20 + swingShift;
if(swingNoteOff_timeout > maxNoteoffTime)
swingNoteOff_timeout = maxNoteoffTime;
swingNoteOff = schedule_note;
}
}
}
// blink the tempo led & any other LEDs!
if(!tempoKnobMode)
{
if(note_counter < 2)
{
set_led(LED_TEMPO);
}
else // if(note_counter < 8)
{
clear_led(LED_TEMPO);
}
}
blink_led_on = !(note_counter&2);
dimblink_led_on = !(note_counter%8);
note_counter++;
}
else if(onemore)
onemore = FALSE;
}
// 1ms "RTC" timer
#ifdef __AVR_ATmega162__
ISR(TIMER0_COMP_vect)
#endif
#ifdef __AVR_ATmega2561__
ISR(TIMER0_COMPA_vect)
#endif
{
// Count all the "stop watches" needed here and there...
if(tempoMeasure!=0xff)
tempoMeasure++;
if(debounce_timer != 0xFF)
debounce_timer++;
if(uart_timeout != 0xFFFF)
uart_timeout++;
if(tapTempoTimer!=0xffff)
tapTempoTimer++;
#ifdef HASDOUBLECLICKF
if(doublePressTimeOut > 0)
doublePressTimeOut--;
#endif
dimblink_led_ctr++;
// Timers for the swinging notes:
if(swingNoteOff_timeout != 0)
{
swingNoteOff_timeout--;
if(swingNoteOff_timeout == 0)
dispatch_swing_note=2; // just generate a "do it now" "event" for the main loop.
}
if(swingNoteOn_timeout != 0)
{
swingNoteOn_timeout--;
if(swingNoteOn_timeout == 0)
dispatch_swing_note=1;
}
#ifdef __AVR_ATmega2561__
// Tempo knob polling, the MEGA162 does that with its port change interrupt, so not here
static uint8_t last_tk=3;
static int8_t cnt;
uint8_t curr_tk;
curr_tk = TEMPO_PIN & 0x3; // pins A0 and A1
// clicks are on "3"
// CW (+) : 3 -> 1 -> 0 -> 2 -> 3
// CCW (-): 3 -> 2 -> 0 -> 1 -> 3
if(last_tk!=curr_tk)
{ // tempo knob change!
if( (last_tk==3 && curr_tk==1 )
|| (last_tk==1 && curr_tk==0 )
|| (last_tk==0 && curr_tk==2 )
|| (last_tk==2 && curr_tk==3 )
)
{ // valid CW
cnt++;
if(cnt==4)
{
cnt=0;
tk_value++;
}
}
else if(
( last_tk==3 && curr_tk==2 )
|| ( last_tk==2 && curr_tk==0 )
|| ( last_tk==0 && curr_tk==1 )
|| ( last_tk==1 && curr_tk==3 )
)
{ // valid CCW
cnt--;
if(cnt==-4)
{
cnt=0;
tk_value--;
}
}
else // quad err
cnt=0;
last_tk = curr_tk;
}
#endif
// any (!) creation of din/MIDI clock now here!!!
// direct creation in main/1ms timer for DINin-MIDIout is cut!
// direct creation in midi.c for MIDIin-DINout is cut!
// any setting of the pending pulses happens in do_tempo for any 12ths do_tempo (1/16 note)
#ifdef SYNC_OUT
uint16_t *p = pendingDinPulse;
if((sync != DIN_SYNC) && (dinsync_clock_timeout != 0))
{
dinsync_clock_timeout--;
if(dinsync_clock_timeout == 0)
cbi(DINSYNC_DATA, DINSYNC_CLK); // lower the clock
}
for(uint8_t i = 0; i < 12; i++)
{
if(*p > 1)
*p = *p - 1;
if(*p == 1)
{
if(sync != DIN_SYNC)
{
sbi(DINSYNC_DATA, DINSYNC_CLK); // rising edge on note start
dinsync_clock_timeout = 6; // schedule pulse to end
}
midi_putchar(MIDI_CLOCK);
*p = 0;
}
p++;
}
#endif
#ifdef DIN_SYNC_IN
if(sync == DIN_SYNC)
{
static uint8_t last_dinsync_c;
uint8_t curr_dinsync_c;
curr_dinsync_c = DINSYNC_PINS & (1<<DINSYNC_CLK);
if(last_dinsync_c==0 && curr_dinsync_c)
{
if(DINSYNC_PINS & (1<< DINSYNC_START))
{
bar_pos++;
if(bar_pos>=96 )
{
bar_pos=0;
if(sync_startstop>0)
{
sync_startstop=0;
midi_realtime_cmd=MIDI_START;
}
if(sync_startstop<0)
{
sync_startstop=0;
midi_realtime_cmd=MIDI_STOP;
}
}
}
run_tempo +=2 ; // notify a clock was recv'd
// "Clock out" is not done on "clock in" but by the swing timing!
measureTempo();
}
last_dinsync_c = curr_dinsync_c;
}
#endif
// Blink by timer if there is no clock source
// internal stopped or external time out
if( ( !TEMPOISRUNNING && sync==INTERNAL_SYNC ) || (sync!=INTERNAL_SYNC && tempoMeasure==255) )
{
if(dimblink_led_ctr == 0)
{
// turn off
blink_leds_off();
}
else if(dimblink_led_ctr == 128)
{
// turn on
blink_leds_on();
}
}
}
#ifdef __AVR_ATmega162__
// Tempo Knob (port pin changed) interupt
ISR(PCINT0_vect)
{
static uint8_t last_tk;
uint8_t curr_tk;
// tempo knob change!
curr_tk = TEMPO_PIN & 0x3; // pins A0 and A1
// if (IS_SET1( SETTINGS1_ENC_Q1 ) )
{
if((last_tk == 3) && (curr_tk == 2) )
tk_value--;
if((last_tk == 2) && (curr_tk == 3) )
tk_value++;
}
if (IS_SET2( SETTINGS2_ENC_Q2 ) )
{
if((last_tk == 1) && (curr_tk == 3) )
tk_value--;
if((last_tk == 3) && (curr_tk == 1) )
tk_value++;
}
/*
if (IS_SET1( SETTINGS1_ENC_Q3 ) )
{
if((last_tk == 1) && (curr_tk == 0) )
tk_value++;
if((last_tk == 0) && (curr_tk == 1) )
tk_value--;
}
if (IS_SET1( SETTINGS1_ENC_Q4 ) )
{
if((last_tk == 0) && (curr_tk == 2) )
tk_value++;
if((last_tk == 2) && (curr_tk == 4) )
tk_value--;
}
*/
last_tk = curr_tk;
}
#endif
// the loop for the simple modes - just "all in one" to save some bytes
void dumb_functions(uint8_t func)
{
#if !ALL_FIXED_SETTING
int8_t which_setting=0;
#endif
#ifdef DACBITS_TEST
uint8_t notePort=NOTE_PORT;
// uint8_t gatePort=0;
#endif
// set_syncmode();
while (func==function)
{
read_switches();
#ifdef DACBITS_TEST
if(function == EDIT_TRACK_FUNC)
{
static const uint8_t notePortKeys[]={ KEY_C, KEY_D,KEY_E,KEY_F, KEY_G, KEY_A, KEY_SLIDE, KEY_ACCENT };
uint8_t i;
/*
a) Port C0..5 = 6Bit Dac for note CV (pitch) (Keys C=0, D=1; E=2; F=3, G=3, A=4)
b) Port C6 = Slide, Key=Slide
c) Port C7 = Accent, Key Accent (inverted, so LED OFF=High=NO ACCENT / LED-ON=Low=ACCENT )
d) Port E1 = GATE, Key RUN/STOP LED_On=High=GateON
e) Port E2 = NoteLatch, Key NEXT, Latches on Rising edge
*/
// if(just_pressed(KEY_RS))
{
// gatePort=!gatePort;
// if(gatePort)
if(is_pressed(KEY_RS))
{
sbi(GATE_PORT, GATE_PIN);
set_led(LED_RS);
}
else
{
cbi(GATE_PORT, GATE_PIN);
clear_led(LED_RS);
}
}
if(is_pressed(KEY_NEXT))
{
sbi(NOTELATCH_PORT, NOTELATCH_PIN);
set_led(LED_NEXT);
}else
{
cbi(NOTELATCH_PORT, NOTELATCH_PIN);
clear_led(LED_NEXT);
}
for(i=0;i<8;++i)
{
if(just_pressed(notePortKeys[i]))
{
notePort^=(1<<i);
}
if(notePort &(1<<i))
set_key_led(notePortKeys[i]);
else
clear_key_led(notePortKeys[i]);
}
NOTE_PORT = notePort ^ ACCENTPIN;
}
#endif
#ifdef KEYLED_TEST
if(function == COMPUTER_CONTROL_FUNC)
{
int8_t i;
for(i=0;i<24;++i)
{
if(switches[i>>3]&(1<<(i&7)))