forked from hotchk155/arpie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arpie.ino
4298 lines (3925 loc) · 121 KB
/
arpie.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
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
////////////////////////////////////////////////////////////////////////////////
//
// //
//
// ////// // //// ////// // ////
// // //// // // // // //
// //////// // // // // ////////
// // // // // // // //
// ////// // ////// // ////
// MIDI ARPEGGIATOR //
// hotchk155/2013-19 //
//
// Revision History
// 1.00 16Apr13 Baseline
// 1.01 21Apr13 Improvements for MIDI thru control
// 1.02 26Apr13 Synch source/input lockout options
// 1.03 12May13 Fix issue with synch thru/change lockout blink rate
// 1.04 09Nov13 Support MIDI stop/continue on external synch
// 1.05 16May14 Force to scale options
// 1.06 19May14 Poly Gate/MIDI transpose/Skip on rest
// 1.07 16Nov14 Revert to primary menu function, glide mode
// 4.0 Feb2015 Release A4
// 5.0 Jun2017 Release A5
// 5.1 18Jul17 Hack header fix
// 5.2 11Mar18 Fix timing glitch on aux sync
// 6.0 23Sep19 CVTab support plus enhancements/bug fixes
// 6.1 05Feb19 MemoTab support
//
#define VERSION_HI 6
#define VERSION_LO 1
//
// INCLUDE FILES
//
#include <avr/interrupt.h>
#include <avr/io.h>
#include <EEPROM.h>
#include <Wire.h>
// Midi CC numbers to associate with hack header inputs
#define HH_CC_PC0 16
#define HH_CC_PC4 17
#define HH_CC_PC5 18
// The controller number used for Velocity CC mode
#define VELOCITY_CC 41 // Korg Volca FM
#define LOCAL_ON_CC 122
// Hack header pulse clock config
#define SYNCH_CLOCK_PULSE_WIDTH 10
#define SYNCH_CLOCK_MIN_PERIOD 20
#define SYNCH_CLOCK_INVERT 0
// I2C ADDRESSES FOR DAUGHTER BOARDS
#define DAC_ADDR 0b1100000
#define EEPROM_ADDR 0b1010000
//
// PREFERENCES WORD
//
enum {
PREF_HACKHEADER= (unsigned int)0b1111111100000000,
PREF_HHSW_PB3= (unsigned int)0b0100000000000000,
PREF_HHPOT_PC0= (unsigned int)0b0011000000000000,
PREF_HHPOT_PC0_TEMPO= (unsigned int)0b0001000000000000,
PREF_HHPOT_PC0_GATE= (unsigned int)0b0010000000000000,
PREF_HHPOT_PC0_CC= (unsigned int)0b0011000000000000,
PREF_HHPOT_PC4= (unsigned int)0b0000110000000000,
PREF_HHPOT_PC4_VEL= (unsigned int)0b0000010000000000,
PREF_HHPOT_PC4_PB= (unsigned int)0b0000100000000000,
PREF_HHPOT_PC4_CC= (unsigned int)0b0000110000000000,
PREF_HHPOT_PC5= (unsigned int)0b0000001100000000,
PREF_HHPOT_PC5_MOD= (unsigned int)0b0000000100000000,
PREF_HHPOT_PC5_TRANS= (unsigned int)0b0000001000000000,
PREF_HHPOT_PC5_CC= (unsigned int)0b0000001100000000,
PREF_HH_8THCLOCK= (unsigned int)0b1000000000000000, // ext clock is 8th notes
PREF_HH_CVTAB_ACC= (unsigned int)0b0100000000000000, // cvtab clock out is accent
PREF_HH_CVTAB_HZVOLT= (unsigned int)0b0010000000000000, // cvtab uses hz/volt scaling
PREF_HH_CVCAL= (unsigned int)0b0000000100000000, // cvtab is in calibration mode
PREF_AUTOREVERT= (unsigned int)0b0000000000010000,
PREF_LONGPRESS= (unsigned int)0b0000000000001100, //Mask
PREF_LONGPRESS0= (unsigned int)0b0000000000000000, //Shortest
PREF_LONGPRESS1= (unsigned int)0b0000000000000100, //:
PREF_LONGPRESS2= (unsigned int)0b0000000000001000, //:
PREF_LONGPRESS3= (unsigned int)0b0000000000001100, //Longest
PREF_LEDPROFILE = (unsigned int)0b0000000000000011, // Mask
PREF_LEDPROFILE0 = (unsigned int)0b0000000000000000, // STD GREEN
PREF_LEDPROFILE1 = (unsigned int)0b0000000000000001, // STD BLUE
PREF_LEDPROFILE2 = (unsigned int)0b0000000000000010, // SUPER BRIGHT BLUE
PREF_LEDPROFILE3 = (unsigned int)0b0000000000000011, // SUPER BRIGHT WHITE
PREF_MASK = (unsigned int)0b1111111100011111 // Which bits of the prefs register are mapped to actual prefs
};
// HACK HEADER MODES
enum {
HH_MODE_NONE,
HH_MODE_CTRLTAB,
HH_MODE_SYNCTAB,
HH_MODE_CVTAB,
HH_MODE_MEMOTAB
};
enum {
HH_GATE_CLOSE,
HH_GATE_OPEN,
HH_GATE_RETRIG
};
#define P_HH_CVTAB_GATE 14
#define HH_CVCAL_CC_SCALE 70
#define HH_CVCAL_CC_OFS 71
#define HH_CVCAL_CC_SET 72
#define HH_CVCAL_CC_SAVE 73
// The preferences word
unsigned int gPreferences;
// Hack header mode
byte hhMode;
char hhCurrentPatch;
// CV TAB calibration data
char hhCVCalScale;
char hhCVCalOfs;
// CV TAB pitch glide info
long hhDACCurrent;
long hhDACTarget;
long hhDACIncrement;
byte hhGlideActive;
// Hack header function prototypes
void hhSetCV(long note, byte glide);
void hhSetGate(byte state);
void hhCVCalSave();
// Forward declare the UI refresh flag
#define EDIT_FLAG_FORCE_REFRESH 0x01
#define EDIT_FLAG_IS_HELD 0x02 // means that a menu button is held down
#define EDIT_FLAG_IS_NEW 0x04
#define EDIT_FLAG_1 0x10 // these flags are used for some special button logic
#define EDIT_FLAG_2 0x20
extern byte editFlags;
#define SYNCH_TO_MIDI 0x0001 // This flag indicates we are slaving to MIDI clock
#define SYNCH_SEND_CLOCK 0x0002 // This flag indicates that we should send MIDI clock to output
#define SYNCH_BEAT 0x0004 // This flag indicates that the beat clock should flash
#define SYNCH_SEND_START 0x0008 // This flag indicates that a START message needs to be sent to slaves
#define SYNCH_SEND_STOP 0x0010 // This flag indicates that a STOP message needs to be sent to slaves
#define SYNCH_SEND_CONTINUE 0x0020 // This flag indicates that a CONTINUE message needs to be sent to slaves
#define SYNCH_RESET_NEXT_STEP_TIME 0x0040 // This flag indicates that the next step will be timed from now
#define SYNCH_RESTART_ON_BEAT 0x0080 // This flag indicates that the sequence will restart at the next beat
#define SYNCH_AUX_RUNNING 0x0100 // This flag indicates that the AUX SYNCH input in a running state
#define SYNCH_ZERO_TICK_COUNT 0x0200 // This flag indicates cause the tick count to restart (and get beat LED in synch)
#define SYNCH_HOLD_AT_ZERO 0x0400 // This flag resets and suspends arpeggiator while still passing MIDI clock
#define SYNCH_PLAY_ADVANCE 0x0800 // Whether to advance play counter to next step
extern volatile unsigned int synchFlags;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// DEFINE THE PATCH CONTENT
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#define ARP_PATCH_VERSION 0xA0 // identifies patch version in EEPROM
// sizes of arrays
#define ARP_MAX_CHORD 12 // max notes in a chord
#define ARP_MAX_PATTERN 16 // max steps in a rhythmic pattern
#define ARP_MAX_TRAN_SEQ 16 // max steps in the transpose sequence
// bit flags for the pattern
#define ARP_PATN_PLAY 0x01
#define ARP_PATN_GLIDE 0x02
#define ARP_PATN_TIE 0x04
#define ARP_PATN_ACCENT 0x08
#define ARP_PATN_OCTAVE 0x10
#define ARP_PATN_OCTDN 0x20
#define ARP_PATN_4TH 0x40
#define ARP_PATN_PLAYTHRU 0x80
// Values for arpType
enum
{
ARP_TYPE_UP = 0,
ARP_TYPE_DOWN,
ARP_TYPE_UP_DOWN,
ARP_TYPE_RANDOM,
ARP_TYPE_RANDOM2,
ARP_TYPE_MANUAL,
ARP_TYPE_POLYGATE
};
// Values for arpInsertMode
enum
{
ARP_INSERT_OFF = 0,
ARP_INSERT_HI,
ARP_INSERT_LOW,
ARP_INSERT_3_1,
ARP_INSERT_4_2
};
// Values for force to scale masks
enum
{
// 0123456789012345
ARP_SCALE_CHROMATIC = (unsigned)0b0000111111111111, //no scale
ARP_SCALE_IONIAN = (unsigned)0b0000101011010101, //diatonic modes
ARP_SCALE_DORIAN = (unsigned)0b0000101101010110, //:
ARP_SCALE_PHRYGIAN = (unsigned)0b0000110101011010, //:
ARP_SCALE_LYDIAN = (unsigned)0b0000101010110101, //:
ARP_SCALE_MIXOLYDIAN= (unsigned)0b0000101011010110, //:
ARP_SCALE_AEOLIAN = (unsigned)0b0000101101011010, //:
ARP_SCALE_LOCRIAN = (unsigned)0b0000110101101010, //:
};
// Force to scale mode for out of scale notes
enum
{
ARP_SCALE_ADJUST_SKIP = (unsigned)0x0000, // Out of scale notes are skipped
ARP_SCALE_ADJUST_MUTE = (unsigned)0x1000, // Out of scale notes are muted
ARP_SCALE_ADJUST_FLAT = (unsigned)0x2000, // Out of scale notes are flattened to bring them to scale
ARP_SCALE_ADJUST_SHARP = (unsigned)0x3000, // Out of scale notes are sharpened to bring them to scale
ARP_SCALE_ADJUST_TOGGLE = (unsigned)0x4000, // Out of scale notes are sharpened/flattened alternately
ARP_SCALE_ADJUST_MASK = (unsigned)0x7000,
ARP_SCALE_TOGGLE_FLAG = (unsigned)0x8000
};
// structure to store status of the arp as a "patch"
typedef struct {
byte ver;
int synchPlayRate; // ratio of ticks per arp note
byte arpType; // arpeggio type
char arpOctaveShift; // octave transpose
byte arpOctaveSpan; // number of octaves to span with the arpeggio
byte arpInsertMode; // additional note insertion mode
byte arpVelocityMode; // (0 = original, 1 = override)
byte arpVelocity; // velocity
byte arpAccentVelocity; // accent velocity
byte arpGateLength; // gate length (0 = tie notes, 1-127 = fraction of beat)
char arpTranspose; // up/down transpose
char arpForceToScaleRoot; // Defines the root note of the scale (0 = C)
unsigned int arpForceToScaleMask; // Force to scale interval mask (defines the scale)
unsigned int arpChord[ARP_MAX_CHORD]; // Chord notes
int arpChordLength; // number of notes in the chord
byte arpPattern[ARP_MAX_PATTERN];
byte arpPatternLength; // user-defined pattern length (1-16)
char arpTransposeSequence[ARP_MAX_TRAN_SEQ];
byte arpTransposeSequenceLen;
unsigned int arpManualChord; // Manual chord selected by the user
} ARP_PATCH;
// the active patch
ARP_PATCH _P;
////////////////////////////////////////////////////////////////////////////////
//
//
//
// LOW LEVEL USER INTERFACE FUNCTIONS
// - Drive the LEDs and read the switches
//
//
//
////////////////////////////////////////////////////////////////////////////////
// pin definitions
#define P_UI_HOLDSW 12
#define P_UI_IN_LED 8
#define P_UI_SYNCH_LED 16
#define P_UI_OUT_LED 17
#define P_UI_HOLD_LED 9
#define P_UI_CLK 5 //PD5
#define P_UI_DATA 7 //PD7
#define P_UI_STROBE 15 //PC1
#define P_UI_READ1 10 //PB2
#define P_UI_READ0 6 //PD6
#define DBIT_UI_CLK 0b00100000 // D5
#define DBIT_UI_DATA 0b10000000 // D7
#define CBIT_UI_STROBE 0b00000010 // C1
#define DBIT_UI_READ0 0b01000000 // D6
#define BBIT_UI_READ1 0b00000100 // B2
#define NO_VALUE (-1)
#define DEBOUNCE_COUNT 50
// Time in ms that counts as a long button press
enum
{
UI_HOLD_TIME_0 = 250,
UI_HOLD_TIME_1 = 500,
UI_HOLD_TIME_2 = 1000,
UI_HOLD_TIME_3 = 1500
};
unsigned int uiLongHoldTime;
// Brightness levels of LEDs
enum
{
UI_LEDPROFILE0_HI = 100,
UI_LEDPROFILE0_MED = 3,
UI_LEDPROFILE0_LO = 1,
UI_LEDPROFILE1_HI = 255,
UI_LEDPROFILE1_MED = 25,
UI_LEDPROFILE1_LO = 1,
UI_LEDPROFILE2_HI = 255,
UI_LEDPROFILE2_MED = 25,
UI_LEDPROFILE2_LO = 4,
UI_LEDPROFILE3_HI = 255,
UI_LEDPROFILE3_MED = 35,
UI_LEDPROFILE3_LO = 10
};
byte uiLedBright;
byte uiLedMedium;
byte uiLedDim;
#define UI_IN_LED_TIME 20
#define UI_OUT_LED_TIME 20
#define UI_SYNCH_LED_TIME 5
#define UI_HOLD_PRESSED 0x01
#define UI_HOLD_HELD 0x02
#define UI_HOLD_CHORD 0x04
#define UI_HOLD_LOCKED 0x08
#define UI_HOLD_AS_SHIFT 0x10
#define SYNCH_SOURCE_NONE 0
#define SYNCH_SOURCE_INTERNAL 1
#define SYNCH_SOURCE_INPUT 2
#define SYNCH_SOURCE_AUX 3
#define SYNCH_SOURCE_MANUAL 4
volatile byte uiLeds[16];
volatile char uiDataKey;
volatile char uiLastDataKey;
volatile char uiMenuKey;
volatile char uiLastMenuKey;
volatile byte uiDebounce;
volatile byte uiScanPosition;
volatile byte uiLedOffPeriod;
volatile byte uiFlashHold;
unsigned long uiUnflashInLED;
unsigned long uiUnflashOutLED;
unsigned long uiUnflashSynchLED;
byte uiHoldType;
unsigned long uiHoldPressedTime;
#define UI_DEBOUNCE_MS 100
// Enumerate the menu keys
// Columns A, B, C
// Rows 1,2,3,4
enum {
UI_KEY_A1 = 0,
UI_KEY_A2 = 1,
UI_KEY_A3 = 2,
UI_KEY_A4 = 3,
UI_KEY_B1 = 4,
UI_KEY_B2 = 5,
UI_KEY_B3 = 6,
UI_KEY_B4 = 7,
UI_KEY_C1 = 8,
UI_KEY_C2 = 9,
UI_KEY_C3 = 10,
UI_KEY_C4 = 11
};
////////////////////////////////////////////////////////////////////////////////
//
// INTERRUPT SERVICE ROUTINE FOR UPDATING THE DISPLAY AND READING KEYBOARD
//
////////////////////////////////////////////////////////////////////////////////
ISR(TIMER2_OVF_vect)
{
// turn off LED strobe
PORTC &= ~CBIT_UI_STROBE;
// Do we need to wait until turning the next LED on? (this is used implement
// crude PWM brightness control)
if(uiLedOffPeriod)
{
// ok, we need another interrupt before we illuminate the next LED
TCNT2 = 255 - uiLedOffPeriod;
uiLedOffPeriod = 0;
}
else
{
// used to flash hold light
++uiFlashHold;
// need to start scanning from the start of the led row again?
if(uiScanPosition >= 15)
{
// Clock a single bit into the shift registers
PORTD &= ~DBIT_UI_CLK;
PORTD |= DBIT_UI_DATA;
PORTD |= DBIT_UI_CLK;
}
// Shift the bit along (NB we need to shift it once
// before it will appear at shift reg output 0)
PORTD &= ~DBIT_UI_DATA;
PORTD &= ~DBIT_UI_CLK;
PORTD |= DBIT_UI_CLK;
// look up the button index
byte buttonIndex = 15 - uiScanPosition;
// does this LED need to be lit?
byte ledOnPeriod = uiLeds[buttonIndex];
if(ledOnPeriod)
{
// ok enable led strobe line
PORTC |= CBIT_UI_STROBE;
}
// set up the off period to make up a 255 clock tick cycle
uiLedOffPeriod = 255 - ledOnPeriod;
// check we're not debouncing the data entry keys
if(!uiDebounce)
{
// is a data entry key pressed?
if(!!(PINB & BBIT_UI_READ1))
{
// is it a new keypress?
if(uiLastDataKey != buttonIndex)
{
// store it
uiDataKey = buttonIndex;
uiLastDataKey = uiDataKey;
uiDebounce = DEBOUNCE_COUNT;
}
}
// has the previously pressed key been released?
else if(buttonIndex == uiLastDataKey)
{
// no key is pressed
uiLastDataKey = NO_VALUE;
}
}
else
{
// debouncing
--uiDebounce;
}
// check for input at the menu keys
// here we don't bother with a debounce
if(!!(PIND & DBIT_UI_READ0))
{
// is a new key pressed
if(buttonIndex != uiLastMenuKey)
{
// report it
uiMenuKey = buttonIndex;
uiLastMenuKey = uiMenuKey;
}
}
else if(buttonIndex == uiLastMenuKey)
{
// previous key no longer pressed
uiLastMenuKey = NO_VALUE;
}
if(!uiScanPosition)
uiScanPosition = 15;
else
--uiScanPosition;
TCNT2 = 255 - ledOnPeriod;
}
}
////////////////////////////////////////////////////////////////////////////////
// UI INIT
void uiInit()
{
pinMode(P_UI_CLK, OUTPUT);
pinMode(P_UI_DATA, OUTPUT);
pinMode(P_UI_READ1, INPUT);
pinMode(P_UI_READ0, INPUT);
pinMode(P_UI_STROBE, OUTPUT);
pinMode(P_UI_HOLDSW, INPUT_PULLUP); // If you get an error here, get latest Arduino IDE
pinMode(P_UI_IN_LED, OUTPUT);
pinMode(P_UI_OUT_LED, OUTPUT);
pinMode(P_UI_SYNCH_LED, OUTPUT);
pinMode(P_UI_HOLD_LED, OUTPUT);
// enable pullups
// digitalWrite(P_UI_HOLDSW, HIGH);
for(int i=0;i<16;++i)
uiLeds[i] = 0;
uiDataKey = NO_VALUE;
uiLastDataKey = NO_VALUE;
uiMenuKey = NO_VALUE;
uiDebounce = 0;
uiScanPosition = 15;
uiLedOffPeriod = 0;
uiUnflashInLED = 0;
uiUnflashOutLED = 0;
uiUnflashSynchLED = 0;
uiHoldPressedTime = 0;
uiHoldType = 0;
uiFlashHold = 0;
uiLongHoldTime = UI_HOLD_TIME_3;
uiLedBright = UI_LEDPROFILE2_HI;
uiLedMedium = UI_LEDPROFILE2_MED;
uiLedDim = UI_LEDPROFILE2_LO;
// start the interrupt to service the UI
TCCR2A = 0;
TCCR2B = 1<<CS21 | 1<<CS20;
TIMSK2 = 1<<TOIE2;
TCNT2 = 0;
}
////////////////////////////////////////////////////////////////////////////////
// SHOW FIRMWARE VERSION (BCD)
byte uiShowVersion()
{
if(digitalRead(P_UI_HOLDSW) == LOW) {
uiLeds[0] = !!((VERSION_HI/10)&0x8) ? uiLedBright:uiLedDim;
uiLeds[1] = !!((VERSION_HI/10)&0x4) ? uiLedBright:uiLedDim;
uiLeds[2] = !!((VERSION_HI/10)&0x2) ? uiLedBright:uiLedDim;
uiLeds[3] = !!((VERSION_HI/10)&0x1) ? uiLedBright:uiLedDim;
uiLeds[4] = !!((VERSION_HI%10)&0x8) ? uiLedBright:uiLedDim;
uiLeds[5] = !!((VERSION_HI%10)&0x4) ? uiLedBright:uiLedDim;
uiLeds[6] = !!((VERSION_HI%10)&0x2) ? uiLedBright:uiLedDim;
uiLeds[7] = !!((VERSION_HI%10)&0x1) ? uiLedBright:uiLedDim;
uiLeds[8] = !!((VERSION_LO/10)&0x8) ? uiLedBright:uiLedDim;
uiLeds[9] = !!((VERSION_LO/10)&0x4) ? uiLedBright:uiLedDim;
uiLeds[10] = !!((VERSION_LO/10)&0x2) ? uiLedBright:uiLedDim;
uiLeds[11] = !!((VERSION_LO/10)&0x1) ? uiLedBright:uiLedDim;
uiLeds[12] = !!((VERSION_LO%10)&0x8) ? uiLedBright:uiLedDim;
uiLeds[13] = !!((VERSION_LO%10)&0x4) ? uiLedBright:uiLedDim;
uiLeds[14] = !!((VERSION_LO%10)&0x2) ? uiLedBright:uiLedDim;
uiLeds[15] = !!((VERSION_LO%10)&0x1) ? uiLedBright:uiLedDim;
while(digitalRead(P_UI_HOLDSW) == LOW);
return 1;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// FLASH IN LED
void uiFlashInLED(unsigned long milliseconds)
{
digitalWrite(P_UI_IN_LED, HIGH);
uiUnflashInLED = milliseconds + UI_IN_LED_TIME;
}
////////////////////////////////////////////////////////////////////////////////
// FLASH OUT LED
void uiFlashOutLED(unsigned long milliseconds)
{
digitalWrite(P_UI_OUT_LED, HIGH);
uiUnflashOutLED = milliseconds + UI_OUT_LED_TIME;
}
////////////////////////////////////////////////////////////////////////////////
// FLASH SYNCH LED
void uiFlashSynchLED(unsigned long milliseconds)
{
digitalWrite(P_UI_SYNCH_LED, HIGH);
uiUnflashSynchLED = milliseconds + UI_SYNCH_LED_TIME;
}
////////////////////////////////////////////////////////////////////////////////
// CLEAR ALL LEDS
void uiClearLeds()
{
for(int i=0;i<16;++i)
uiLeds[i] = 0;
}
////////////////////////////////////////////////////////////////////////////////
// SET A RANGE OF LEDS TO SAME STATUS
void uiSetLeds(int start, int len, byte newStatus)
{
while(start < 16 && len-- > 0)
uiLeds[start++] = newStatus;
}
////////////////////////////////////////////////////////////////////////////////
// SET ALL LEDS FROM 16-BIT UINT
void uiSetLeds(unsigned int enabled, unsigned int status)
{
unsigned int m=0x8000;
for(int i=0; i<16;++i)
{
if(status & m)
uiLeds[i] = uiLedBright;
else if(enabled & m)
uiLeds[i] = uiLedDim;
else
uiLeds[i] = 0;
m>>=1;
}
}
////////////////////////////////////////////////////////////////////////////////
// RUN THE UI STATE MACHINE
void uiRun(unsigned long milliseconds)
{
if(uiUnflashInLED && uiUnflashInLED < milliseconds)
{
digitalWrite(P_UI_IN_LED, LOW);
uiUnflashInLED = 0;
}
if(uiUnflashOutLED && uiUnflashOutLED < milliseconds)
{
digitalWrite(P_UI_OUT_LED, LOW);
uiUnflashOutLED = 0;
}
if(uiUnflashSynchLED && uiUnflashSynchLED < milliseconds)
{
digitalWrite(P_UI_SYNCH_LED, LOW);
uiUnflashSynchLED = 0;
}
// Hold button logic
if(milliseconds < uiHoldPressedTime + UI_DEBOUNCE_MS)
{
// debouncing... ignore everything
}
else
if(!digitalRead(P_UI_HOLDSW))
{
// button pressed... new press?
if(!(uiHoldType & UI_HOLD_PRESSED))
{
// record and debounce it
uiHoldType |= UI_HOLD_PRESSED;
uiHoldPressedTime = milliseconds;
}
else if(!(uiHoldType & UI_HOLD_HELD) && !(uiHoldType & UI_HOLD_AS_SHIFT) && (milliseconds > uiHoldPressedTime + uiLongHoldTime))
{
// record a long hold and set LOCK flag
uiHoldType |= UI_HOLD_HELD;
uiHoldType |= UI_HOLD_LOCKED;
uiFlashHold = 0;
}
}
else
if(!!(uiHoldType & UI_HOLD_PRESSED))
{
if(!(uiHoldType & UI_HOLD_HELD) && !(uiHoldType & UI_HOLD_AS_SHIFT))
{
// release after short hold clears lock flag
// and toggles hold
if(!!(uiHoldType & UI_HOLD_LOCKED))
uiHoldType &= ~UI_HOLD_LOCKED;
else
uiHoldType ^= UI_HOLD_CHORD;
}
uiHoldType &= ~UI_HOLD_PRESSED;
uiHoldType &= ~UI_HOLD_HELD;
uiHoldType &= ~UI_HOLD_AS_SHIFT;
uiHoldPressedTime = milliseconds;
}
if(uiHoldType & UI_HOLD_LOCKED)
digitalWrite(P_UI_HOLD_LED, !!(uiFlashHold & 0x80));
else
digitalWrite(P_UI_HOLD_LED, !!(uiHoldType & UI_HOLD_CHORD));
}
void uiSetHold() {
uiHoldType |= UI_HOLD_CHORD;
uiHoldType &= ~UI_HOLD_PRESSED;
uiHoldType &= ~UI_HOLD_HELD;
uiHoldType &= ~UI_HOLD_AS_SHIFT;
}
void uiClearHold() {
uiHoldType &= ~UI_HOLD_CHORD;
uiHoldType &= ~UI_HOLD_PRESSED;
uiHoldType &= ~UI_HOLD_HELD;
uiHoldType &= ~UI_HOLD_AS_SHIFT;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//
//
// EEPROM PROXY FUNCTIONS
//
//
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
enum {
EEPROM_MAGIC_COOKIE = 99,
EEPROM_INPUT_CHAN = 100,
EEPROM_OUTPUT_CHAN,
EEPROM_SYNCH_SOURCE,
EEPROM_SYNCH_SEND,
EEPROM_MIDI_OPTS,
EEPROM_PREFS0,
EEPROM_PREFS1,
EEPROM_ARPOPTIONS0,
EEPROM_ARPOPTIONS1,
EEPROM_MIDI_OPTS2,
EEPROM_HH_MODE
};
#define EEPROM_MAGIC_COOKIE_VALUE 0x24
////////////////////////////////////////////////////////////////////////////////
// SET A VALUE IN EEPROM
void eepromSet(byte which, byte value)
{
EEPROM.write(which, value);
}
////////////////////////////////////////////////////////////////////////////////
// GET A VALUE FROM EEPROM
byte eepromGet(byte which, byte minValue = 0x00, byte maxValue = 0xFF, byte defaultValue = 0x00)
{
byte value = EEPROM.read(which);
if(value == defaultValue)
return value;
if(value < minValue || value > maxValue)
{
value = defaultValue;
eepromSet(which, value);
}
return value;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//
//
// LOW LEVEL MIDI HANDLING
//
//
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// max ms we will wait for a mandatory midi parameter to arrive
#define MIDI_PARAM_TIMEOUT 50
// state variables
byte midiInRunningStatus;
byte midiOutRunningStatus;
byte midiNumParams;
byte midiParams[2];
char midiParamIndex;
byte midiSendChannel;
byte midiReceiveChannel;
byte midiOptions;
byte midiOptions2;
// midi options
#define MIDI_OPTS_SEND_CHMSG 0x01
#define MIDI_OPTS_PASS_INPUT_NOTES 0x02
#define MIDI_OPTS_PASS_INPUT_CHMSG 0x04
#define MIDI_OPTS_SYNCH_INPUT 0x08
#define MIDI_OPTS_SYNCH_AUX 0x10
#define MIDI_OPTS_FILTER_CHMODE 0x20
#define MIDI_OPTS_VOLCAFM_VEL 0x40
#define MIDI_OPTS_LOCAL_OFF 0x80
#define MIDI_OPTS2_USE_NOTE_OFF 0x01
#define MIDI_OPTS_MAX_VALUE 0xFF
#define MIDI_OPTS_DEFAULT_VALUE (MIDI_OPTS_SEND_CHMSG|MIDI_OPTS_SYNCH_INPUT|MIDI_OPTS_SYNCH_AUX)
#define MIDI_OPTS2_MAX_VALUE 0x01
#define MIDI_OPTS2_DEFAULT_VALUE 0
// macros
#define MIDI_IS_NOTE_ON(msg) ((msg & 0xf0) == 0x90)
#define MIDI_IS_NOTE_OFF(msg) ((msg & 0xf0) == 0x80)
#define MIDI_MK_NOTE_ON (0x90 | midiSendChannel)
#define MIDI_MK_NOTE_OFF (0x80 | midiSendChannel)
#define MIDI_MK_CTRL_CHANGE (0xB0 | midiSendChannel)
#define MIDI_MK_PITCHBEND (0xE0 | midiSendChannel)
// realtime synch messages
#define MIDI_SYNCH_TICK 0xf8
#define MIDI_SYNCH_START 0xfa
#define MIDI_SYNCH_CONTINUE 0xfb
#define MIDI_SYNCH_STOP 0xfc
#define MIDI_OMNI 0xff
extern void midiLocalOff(byte local_off);
////////////////////////////////////////////////////////////////////////////////
// MIDI INIT
void midiInit()
{
// init the serial port
Serial.begin(31250);
// Serial.begin(9600);
Serial.flush();
midiInRunningStatus = 0;
midiOutRunningStatus = 0;
midiNumParams = 0;
midiParamIndex = 0;
midiSendChannel = eepromGet(EEPROM_OUTPUT_CHAN, 0, 15, 0);
midiReceiveChannel = eepromGet(EEPROM_INPUT_CHAN, 0, 15, MIDI_OMNI);
midiOptions = eepromGet(EEPROM_MIDI_OPTS, 0, MIDI_OPTS_MAX_VALUE, MIDI_OPTS_DEFAULT_VALUE);
midiOptions2 = eepromGet(EEPROM_MIDI_OPTS2, 0, MIDI_OPTS2_MAX_VALUE, MIDI_OPTS2_DEFAULT_VALUE);
}
////////////////////////////////////////////////////////////////////////////////
// MIDI WRITE
void midiWrite(byte statusByte, byte param1, byte param2, byte numParams, unsigned long milliseconds)
{
if((statusByte & 0xf0) == 0xf0)
{
// realtime byte pass straight through
Serial.write(statusByte);
}
else
{
// send channel message
if(midiOutRunningStatus != statusByte)
{
Serial.write(statusByte);
midiOutRunningStatus = statusByte;
}
if(numParams > 0)
Serial.write(param1);
if(numParams > 1)
Serial.write(param2);
}
// indicate activity
uiFlashOutLED(milliseconds);
}
////////////////////////////////////////////////////////////////////////////////
// MIDI READ
byte midiRead(unsigned long milliseconds, byte passThru, byte isMidiLockout)
{
// loop while we have incoming MIDI serial data
while(Serial.available())
{
// fetch the next byte
byte ch = Serial.read();
// REALTIME MESSAGE
if((ch & 0xf0) == 0xf0)
{
switch(ch)
{
case MIDI_SYNCH_TICK:
if(!!(synchFlags & SYNCH_TO_MIDI) && !!(midiOptions & MIDI_OPTS_SYNCH_INPUT))
synchTick(SYNCH_SOURCE_INPUT);
break;
case MIDI_SYNCH_START:
if(!!(synchFlags & SYNCH_TO_MIDI) && !!(midiOptions & MIDI_OPTS_SYNCH_INPUT))
synchStart(SYNCH_SOURCE_INPUT);
break;
case MIDI_SYNCH_CONTINUE:
if(!!(synchFlags & SYNCH_TO_MIDI) && !!(midiOptions & MIDI_OPTS_SYNCH_INPUT))
synchContinue(SYNCH_SOURCE_INPUT);
break;
case MIDI_SYNCH_STOP:
if(!!(synchFlags & SYNCH_TO_MIDI) && !!(midiOptions & MIDI_OPTS_SYNCH_INPUT))
synchStop(SYNCH_SOURCE_INPUT);
break;
}
}
// CHANNEL STATUS MESSAGE
else if(!!(ch & 0x80))
{
midiParamIndex = 0;
midiInRunningStatus = ch;
switch(ch & 0xF0)
{
case 0xC0: // Patch change 1 instrument #
case 0xD0: // Channel Pressure 1 pressure
midiNumParams = 1;
break;
case 0xA0: // Aftertouch 2 key touch
case 0x80: // Note-off 2 key velocity
case 0x90: // Note-on 2 key veolcity
case 0xB0: // Continuous controller 2 controller # controller value
case 0xE0: // Pitch bend 2 lsb (7 bits) msb (7 bits)
default:
midiNumParams = 2;
break;
}
}
else if(midiInRunningStatus)
{
// gathering parameters
midiParams[midiParamIndex++] = ch;
if(midiParamIndex >= midiNumParams)
{
midiParamIndex = 0;
// flash the LED
uiFlashInLED(milliseconds);
// is it a channel message for our channel?
if(MIDI_OMNI == midiReceiveChannel ||
(midiInRunningStatus & 0x0F) == midiReceiveChannel)
{
switch(midiInRunningStatus & 0xF0)
{
case 0x80: // note off
case 0x90: // note on
if(!!(midiOptions & MIDI_OPTS_PASS_INPUT_NOTES) || isMidiLockout) {
midiWrite(midiInRunningStatus, midiParams[0], midiParams[1], midiNumParams, milliseconds);
}
return midiInRunningStatus; // return to the arp engine
case 0xB0: // CC
if(!!(midiOptions & MIDI_OPTS_FILTER_CHMODE)) {
if(midiParams[0] >= 120) {
// break from switch - ignore message
break;
}
}
if(hhMode == HH_MODE_CVTAB) {
if(gPreferences & PREF_HH_CVCAL) {
switch(midiParams[0]) {
case HH_CVCAL_CC_SCALE:
hhCVCalScale = (midiParams[1]&0x7F)-64;
break;
case HH_CVCAL_CC_OFS:
hhCVCalOfs = (midiParams[1]&0x7F)-64;
break;
case HH_CVCAL_CC_SET:
hhSetCV(midiParams[1],0);
break;
case HH_CVCAL_CC_SAVE:
gPreferences &= ~PREF_HH_CVCAL;
hhCVCalSave();
prefsSave();
break;
}
}
}
// otherwise fall through
default:
if(!!(midiOptions & MIDI_OPTS_PASS_INPUT_CHMSG) || !!(uiHoldType & UI_HOLD_LOCKED))
midiWrite(midiInRunningStatus, midiParams[0], midiParams[1], midiNumParams, milliseconds);
// send to the new channel
if(!!(midiOptions & MIDI_OPTS_SEND_CHMSG))
midiWrite((midiInRunningStatus & 0xF0)|midiSendChannel, midiParams[0], midiParams[1], midiNumParams, milliseconds);
}
}
else
{
// send thru to output
midiWrite(midiInRunningStatus, midiParams[0], midiParams[1], midiNumParams, milliseconds);
}
}
}
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// MIDI SEND REALTIME
void midiSendRealTime(byte msg)
{
Serial.write(msg);
}