forked from tub/Therething
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Therething.pde
984 lines (856 loc) · 24.2 KB
/
Therething.pde
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
#include <URMSerial.h>
#include <NewSoftSerial.h>
#include <avr/pgmspace.h>
#include <LiquidCrystal.h>g
#include <EEPROM.h>
//#define IR_SENSOR true
#define USE_LCD true
#define MENU_TIMEOUT 5000 // Milliseconds until the menu timesout
#ifdef IR_SENSOR
#define SENSOR_MIN 100
#define SENSOR_MAX 500 // Range of valid sensor readings
#else
#define SENSOR_MIN 10 // Lowest valid sensor reading
#define SENSOR_MAX 50 // Range of valid sensor readings
#endif
#define SENSOR_RANGE SENSOR_MAX
#define SENSOR_SCALE_FACTOR (127.0 / (float)SENSOR_RANGE)
#define SCALE_LENGTH 7
#define LED_MAX 255
//////////////////////////////////////////////////////////////////////////////
// Pin configuration
enum encoder_pins {
ENCODER_A = 3,
ENCODER_B = 4,
ENCODER_CLICK = 2
};
enum lcd_pins {
LCD_RS = 18,
LCD_RW = 19,
LCD_ENABLE = 7,
LCD_D4 = 8,
LCD_D5 = 11,
LCD_D6 = 12,
LCD_D7 = 13
};
enum sensor_pins {
SENSOR_L_RX = 15,
SENSOR_L_TX = 14,
SENSOR_R_RX = 17,
SENSOR_R_TX = 16
};
enum mode {
CONTROLLER,
NOTES
};
enum midi{
NOTE_OFF = 0,
NOTE_ON = 1,
CC = 3,
PITCH_BEND = 6,
};
enum leds{
RED_R = 5,
BLUE_R = 6,
RED_L = 10,
BLUE_L= 9
};
enum led_pins {
NOTE_LED = 18,
CONTROLLER_LED = 19
};
char scales[20][7] = {
{ 2,2,1,2,2,2,1 }, // Major
{ 2,1,2,2,1,2,2 }, // Minor
{ 2,2,1,2,2,2,1 }, // Ionian Mode
{ 2,1,2,2,2,1,2 }, // Dorian Mode
{ 1,2,2,2,1,2,2 }, // Phrygian Mode
{ 2,2,2,1,2,2,1 }, // Lydian Mode
{ 2,2,1,2,2,1,2 }, // Mixolydian
{ 2,1,2,2,1,2,2 }, // Aeolian Mode
{ 1,2,2,1,2,2,2 }, // Locrian Mode
{ 2,2,1,2,2,2,1 }, // Major Scale
{ 2,1,2,2,1,3,1 }, // Harmonic Minor
{ 2,1,2,2,2,2,1 }, // Asc. Mel Minor
{ 2,1,2,2,1,2,2 }, // Des. Mel Minor
{ 2,1,2,2,1,2,2 }, // Natural Minor
{ 2,1,3,1,2,2,1 }, // Lydian Dim
{ 2,2,2,2,1,2,1 }, // Lydian Aug
{ 2,2,2,1,2,1,2 }, // Lydian b7
{ 2,1,2,1,2,2,2 }, // Locrian #2
{ 1,2,1,2,2,2,2 }, // Super Locrian
{ 2,2,2,2,2,2,2 } // Whole Tone
};
//////////////////////////////////////////////////////////////////////////////
// Menus
prog_char menu__back[] PROGMEM = "< Back";
prog_char top_menu__note_settings[] PROGMEM = "Note Settings";
prog_char top_menu__controller_settings[] PROGMEM = "Controller Settings";
prog_char top_menu__midi_channel[] PROGMEM = "MIDI Channel";
prog_char controller_menu__inv_left_on[] PROGMEM = "Inv Left [On]";
prog_char controller_menu__inv_left_off[] PROGMEM = "Inv Left [Off]";
prog_char controller_menu__inv_right_on[] PROGMEM = "Inv Right [On]";
prog_char controller_menu__inv_right_off[] PROGMEM = "Inv Right [Off]";
prog_char* top_menu[] = {
top_menu__note_settings,
top_menu__controller_settings,
top_menu__midi_channel,
controller_menu__inv_left_off,
controller_menu__inv_right_off,
NULL
};
prog_char note_menu__scale[] PROGMEM = "Scale";
prog_char note_menu__root[] PROGMEM = "Root";
prog_char note_menu__octave[] PROGMEM = "Octave";
prog_char note_menu__range[] PROGMEM = "Range";
prog_char* note_menu[] = {
note_menu__scale,
note_menu__root,
note_menu__octave,
note_menu__range,
menu__back,
NULL
};
prog_char controller_menu__left_cc_number[] PROGMEM = "Left CC Number";
prog_char controller_menu__right_cc_number[] PROGMEM = "Right CC Number";
prog_char* controller_menu[] = {
controller_menu__left_cc_number,
controller_menu__right_cc_number,
menu__back,
NULL
};
prog_char scale_menu__major[] PROGMEM = "Major";
prog_char scale_menu__minor[] PROGMEM = "Minor";
prog_char scale_menu__ionian_mode[] PROGMEM = "Ionian Mode";
prog_char scale_menu__dorian_mode[] PROGMEM = "Dorian Mode";
prog_char scale_menu__phrygian_mode[] PROGMEM = "Phrygian Mode ";
prog_char scale_menu__lydian_mode[] PROGMEM = "Lydian Mode";
prog_char scale_menu__mixolydian[] PROGMEM = "Mixolydian";
prog_char scale_menu__aeolian_mode[] PROGMEM = "Aeolian Mode";
prog_char scale_menu__locrian_mode[] PROGMEM = "Locrian Mode";
prog_char scale_menu__major_scale[] PROGMEM = "Major Scale";
prog_char scale_menu__harmonic_minor[] PROGMEM = "Harmonic Minor";
prog_char scale_menu__asc_mel_minor[] PROGMEM = "Asc. Mel Minor";
prog_char scale_menu__des_mel_minor[] PROGMEM = "Des. Mel Minor";
prog_char scale_menu__natural_minor[] PROGMEM = "Natural Minor";
prog_char scale_menu__lydian_dim[] PROGMEM = "Lydian Dim";
prog_char scale_menu__lydian_aug[] PROGMEM = "Lydian Aug";
prog_char scale_menu__lydian_b7[] PROGMEM = "Lydian b7";
prog_char scale_menu__locrian_2[] PROGMEM = "Locrian #2";
prog_char scale_menu__super_locrian[] PROGMEM = "Super Locrian";
prog_char scale_menu__whole_tone[] PROGMEM = "Whole Tone";
prog_char* scale_menu[] = {
scale_menu__major,
scale_menu__minor,
scale_menu__ionian_mode,
scale_menu__dorian_mode,
scale_menu__phrygian_mode,
scale_menu__lydian_mode,
scale_menu__mixolydian,
scale_menu__aeolian_mode,
scale_menu__locrian_mode,
scale_menu__major_scale,
scale_menu__harmonic_minor,
scale_menu__asc_mel_minor,
scale_menu__des_mel_minor,
scale_menu__natural_minor,
scale_menu__lydian_dim,
scale_menu__lydian_aug,
scale_menu__lydian_b7,
scale_menu__locrian_2,
scale_menu__super_locrian,
scale_menu__whole_tone,
NULL
};
prog_char root_menu__c[] PROGMEM = "C";
prog_char root_menu__c_[] PROGMEM = "C#";
prog_char root_menu__d[] PROGMEM = "D";
prog_char root_menu__d_[] PROGMEM = "D#";
prog_char root_menu__e[] PROGMEM = "E";
prog_char root_menu__f[] PROGMEM = "F";
prog_char root_menu__f_[] PROGMEM = "F#";
prog_char root_menu__g[] PROGMEM = "G";
prog_char root_menu__g_[] PROGMEM = "G#";
prog_char root_menu__a[] PROGMEM = "A";
prog_char root_menu__a_[] PROGMEM = "A#";
prog_char root_menu__b[] PROGMEM = "B";
prog_char* root_menu[] = {
root_menu__c,
root_menu__c_,
root_menu__d,
root_menu__d_,
root_menu__e,
root_menu__f,
root_menu__f_,
root_menu__g,
root_menu__g_,
root_menu__a,
root_menu__a_,
root_menu__b,
NULL
};
prog_char octave_menu__minus_2[] PROGMEM = "-2";
prog_char octave_menu__minus_1[] PROGMEM = "-1";
prog_char octave_menu__0[] PROGMEM = "0";
prog_char octave_menu__1[] PROGMEM = "1";
prog_char octave_menu__2[] PROGMEM = "2";
prog_char octave_menu__3[] PROGMEM = "3";
prog_char* octave_menu[] = {
octave_menu__minus_2,
octave_menu__minus_1,
octave_menu__0,
octave_menu__1,
octave_menu__2,
NULL
};
prog_char range_menu__1_octave[] PROGMEM = "1 octave";
prog_char range_menu__2_octaves[] PROGMEM = "2 octaves";
prog_char range_menu__3_octaves[] PROGMEM = "3 octaves";
prog_char range_menu__4_octaves[] PROGMEM = "4 octaves";
prog_char* range_menu[] = {
range_menu__1_octave,
range_menu__2_octaves,
range_menu__3_octaves,
range_menu__4_octaves,
NULL
};
enum {
MAIN_MENUS,
LEFT_CC_NO,
RIGHT_CC_NO,
MIDI_CHANNEL
} menu_area = MAIN_MENUS;
enum {
MENU,
MUSIC
} last_loop = MENU;
//////////////////////////////////////////////////////////////////////////////
// Transient state, variables and buffers
prog_char** menu; // The currently active menu
volatile int item = 0; // The currently selected item
volatile int last_item = !item; // The item selected on last display update
char buffer[21]; // String buffer used to copy string constants out of flash
unsigned long last_interrupt = 0; // Last time the rotary encoder was used
mode optionMode;
//Set to true temporarily to flag that UI should show current mode.
boolean modeChanged = false;
unsigned char currentNotes[50]; // The current notes to play, should be mapped to the entire sensing length
float bucketSize; // sensor range / number of notes. Will be set by makeScale()
//////////////////////////////////////////////////////////////////////////////
// Persistant state
// These are restored from eeprom
char scale; // The current scale. Index into scale array
char root; // The root of the scale. Index into C,C#,D,D#,E,F,F#,G,G#,A,A#,B
char octave; // Which octave is at the bottom end of the sensor range
char range; // The number of octaves covered by the sensor
char left_cc_number; // The MIDI CC Number of the left sensor
char right_cc_number; // The MIDI CC NUmber of the right sensor
char midi_channel; // The MIDI channel number
boolean inv_left; // When true the left sensor range is inverted
boolean inv_right; // When true the right sensor range is inverted
//////////////////////////////////////////////////////////////////////////////
// Devices
#ifdef USE_LCD
LiquidCrystal lcd(LCD_RS, LCD_RW, LCD_ENABLE, LCD_D4,LCD_D5,LCD_D6,LCD_D7);
#endif
URMSerial sensor_L;
URMSerial sensor_R;
//////////////////////////////////////////////////////////////////////////////
// Called by the Arduino firmware just after reset.
// Sets up the IO pins, attaches the interrupt service routines, configures
// the devices and sets up the persistant and transient state variables.
void setup() {
// Persistant state
scale = eepromGet(0, 0);
root = eepromGet(1, 0);
octave = eepromGet(2, 2);
range = eepromGet(3, 2);
left_cc_number = eepromGet(4, 2);
right_cc_number = eepromGet(5, 3);
midi_channel = eepromGet(6, 0);
inv_left = eepromGet(7, 0) && 1;
inv_right = eepromGet(8, 0) && 1;
optionMode = eepromGet(9, 1) == 0 ? NOTES : CONTROLLER;
if(inv_left){
top_menu[3] = controller_menu__inv_left_on;
}else{
top_menu[3] = controller_menu__inv_left_off;
}
if(inv_right){
top_menu[4] = controller_menu__inv_right_on;
}else{
top_menu[4] = controller_menu__inv_right_off;
}
// Transient state
menu = top_menu;
item = 0;
last_item = -1;
// Pin setup and ISR for the rotary encoder's click function
pinMode(ENCODER_CLICK, INPUT);
digitalWrite(ENCODER_CLICK, HIGH);
attachInterrupt(0, click, FALLING);
// Pin setup and ISR for the rotary encoder's rotate function
pinMode(ENCODER_A, INPUT);
pinMode(ENCODER_B, INPUT);
digitalWrite(ENCODER_A, HIGH);
digitalWrite(ENCODER_B, HIGH);
attachInterrupt(1, turn, RISING);
analogWrite(RED_R, 255);//OK
// Initialise the LCD
#ifdef USE_LCD
lcd.begin(20,4);
#endif
#ifdef IR_SENSOR
#else
// Initialise the ultra-sonic sensors
sensor_L.begin(SENSOR_L_RX, SENSOR_L_TX, 9600);
sensor_L.setTimeout(20);
sensor_R.begin(SENSOR_R_RX, SENSOR_R_TX, 9600);
sensor_R.setTimeout(20);
#endif
// Set MIDI baud rate
Serial.begin(31250);
// Serial.begin(28800);
makeScale();
pinMode(NOTE_LED, OUTPUT);
pinMode(CONTROLLER_LED, OUTPUT);
digitalWrite(NOTE_LED, optionMode == NOTES ? HIGH : LOW);
digitalWrite(CONTROLLER_LED, optionMode == CONTROLLER ? HIGH : LOW);
}
byte eepromGet(int location, byte fallback){
//ASSUMPTION CITY: if value is 255, it's not been written to yet, mm k?
byte value = EEPROM.read(location);
if(value == 255){
return fallback;
}else{
return value;
}
}
//////////////////////////////////////////////////////////////////////////////
// Main program loop. Called repeatedly by the Arduino firmware
void loop(){
if (last_interrupt > (millis() - MENU_TIMEOUT)) {
doMenu();
last_loop = MENU;
} else {
if(last_loop != MUSIC){
lcd.clear();
}
doMusic();
last_loop = MUSIC;
}
}
//////////////////////////////////////////////////////////////////////////////
// Retreive a measurement from the passed URMSerial object
int getMeasurement(URMSerial s)
{
int value = 0; // This value will be populated
// Request a distance reading from the URM37
switch(s.requestMeasurementOrTimeout(DISTANCE, value)) // Find out the type of request
{
case DISTANCE: // Double check the reading we recieve is of DISTANCE type
value = max(0, min(value-SENSOR_MIN, SENSOR_MAX)); // use the 10-60cm range
break;
default:
value = SENSOR_MAX;
break;
}
return value;
}
int getIrMeasurement(int pin){
int value = analogRead(pin);
value = max(0, min(value-SENSOR_MIN, SENSOR_MAX));
return value;
}
//////////////////////////////////////////////////////////////////////////////
// Calculate and return the number of items in the current menu
int totalItems() {
int total_items = 0;
prog_char** m = menu;
while (*(m++)) total_items++;
return total_items;
}
int getNoteFromScale(int sensorReading){
int bucket = floor(sensorReading / bucketSize);
int note = currentNotes[bucket];
return note;
}
/* Fills in currentNotes with the midi notes to play
and bucketSize with the size of the sections each note will play for along the sensor range */
void makeScale(){
// Add one to the length so we go all the way back to the root note again.
int notesSize = (range * SCALE_LENGTH) + 1;
bucketSize = (float)(SENSOR_RANGE + 1) / (float)notesSize;
int transposition = (octave * 12) + 12;
// Set transposition up so that optionStartOctave of 0 gets us to note 12
currentNotes[0] = root + transposition;
for (int note = 0; note < (notesSize - 1); note++) {
currentNotes[note + 1] = currentNotes[note] + scales[scale][note % SCALE_LENGTH];
}
}
int prevNote = 0;
int prevVel = 0;
void sendNote(int note, int vel){
int noteNumber = getNoteFromScale(note);
lcd.setCursor(0,0);
lcd.print("Note: ");
lcd.print(getNoteName(noteNumber));
lcd.print(getOctaveNumber(noteNumber));
lcd.print(" ");
//Scale velocity over entire sensor range
int scaledVel = (float)vel * SENSOR_SCALE_FACTOR;
lcd.setCursor(0,2);
lcd.print("Vel: ");
lcd.print(scaledVel);
lcd.print(" ");
if(noteNumber != prevNote){
/* next note on, then previous note off -
this is so we can overlap the notes and glide between them if needed.
*/
noteOn(noteNumber, scaledVel);
noteOff();
//save the inputs so we can turn the
//note off when the next ones turned on
prevNote = noteNumber;
prevVel = scaledVel;
}else if(scaledVel == 0 && prevVel != 0){
noteOff();
prevVel = 0;
}
}
/* Note name functions */
char* noteNames[] = {
"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
char* getNoteName(int noteNumber){
return noteNames[noteNumber % 12];
}
int getOctaveNumber(int noteNumber){
return (noteNumber / 12) - 1;
}
/* MIDI Functions */
//Sends a note on event over serial
void noteOn(unsigned char noteNo, unsigned char vel){
sendMidi(NOTE_ON, noteNo, vel);
}
//Sends a note on event over serial
void noteOff(){
sendMidi(NOTE_OFF, prevNote, prevVel);
}
int prevCont1 = 256;
int prevCont2 = 256;
void sendControllers(int c1, int c2){
//Scale CCs over entire sensor range
int controller1 = (float)c1 * SENSOR_SCALE_FACTOR;
int controller2 = (float)c2 * SENSOR_SCALE_FACTOR;
if(controller1 != prevCont1){
prevCont1 = controller1;
sendCC(left_cc_number, controller1);
}
lcd.setCursor(0,0);
lcd.print("CC# ");
lcd.print((int)left_cc_number);
lcd.setCursor(7,0);
lcd.print(": ");
lcd.print(controller1);
lcd.print(" ");
if(controller2 != prevCont2){
prevCont2 = controller2;
sendCC(right_cc_number, controller2);
}
lcd.setCursor(0,2);
lcd.print("CC# ");
lcd.print((int)right_cc_number);
lcd.setCursor(7,2);
lcd.print(": ");
lcd.print(controller2);
lcd.print(" ");
}
// This function sends a Midi CC.
void sendCC(byte c_num, byte c_val){
sendMidi(CC,c_num,c_val);
}
void sendMidi(int type, byte partOne, byte partTwo){
Serial.print(genctrl(type), BYTE);
Serial.print(partOne, BYTE);
Serial.print(partTwo, BYTE);
}
/*! Internal method, don't care about this one.. \n It generates a status byte over a channel and a type, by bitshifting. */
byte genctrl(byte type) {
byte result = 128;
result += ((type & 0x07)<<4) & 0x70;
result += ((midi_channel) & 0x0F);
//return 0x9f;
return result;
}
int lastLeft = 0;
int lastRight = 0;
void doMusic() {
if(modeChanged == true){
modeChanged = false;
lcd.clear();
if(optionMode == CONTROLLER){
lcd.setCursor(5, 0);
lcd.print("Controller");
}else{
lcd.setCursor(8, 0);
lcd.print("Notes");
}
delay(500);
lcd.clear();
}
//Read Sensors
#ifdef IR_SENSOR
int left = getIrMeasurement(0);//ain 0 = pin 14
int right = getIrMeasurement(1);//ain 1 = pin 15
#else
int left = getMeasurement(sensor_L);
int right = getMeasurement(sensor_R);
#endif
if (left == -1) left = lastLeft;
if (right == -1) right = lastRight;
lastLeft = left;
lastRight = right;
analogWrite(RED_L, (SENSOR_MAX - left) * (LED_MAX / SENSOR_MAX));
analogWrite(RED_R, (SENSOR_MAX - right) * (LED_MAX / SENSOR_MAX));
if(inv_left){
left = SENSOR_MAX - left;
}
if(inv_right){
right = SENSOR_MAX - right;
}
lcd.setCursor(0, 1);
drawBar(left);
lcd.setCursor(0, 3);
drawBar(right);
//Switch depending on mode
switch(optionMode){
case CONTROLLER:
sendControllers(left, right);
break;
case NOTES:
sendNote(left, right);
break;
}
}
void drawBar(int value){
int length = ((float)value) * (20.0 / SENSOR_RANGE);
for(int i = 0; i < 20; i++){
if(i < length){
lcd.write(0xff);
}else{
lcd.write(' ');
}
}
}
void doMenu() {
if (menu_area == MAIN_MENUS) {
if (last_item != item) {
last_item = item;
lcd.clear();
int total_items = totalItems();
int top_item = item - 1;
if (top_item > (total_items - 4)) {
top_item = total_items - 4;
}
if (top_item < 0) {
top_item = 0;
}
for (int line = 0; line < 4; line++) {
if (line + top_item < total_items) {
lcd.setCursor(0, line);
if (item == (line + top_item)) {
lcd.write(0x7e);
} else {
lcd.print(" ");
}
strcpy_P(buffer, menu[line+top_item]);
lcd.print(buffer);
}
}
}
} else {
// We're in one of the number choosing menus
// Work out the current value
unsigned char cur_value;
switch (menu_area) {
case LEFT_CC_NO:
cur_value = left_cc_number;
break;
case RIGHT_CC_NO:
cur_value = right_cc_number;
break;
case MIDI_CHANNEL:
cur_value = midi_channel + 1;
break;
}
// If we need to do anything...
if (last_item != cur_value) {
// Work out the max, min and display text
int max_value, min_value;
switch (menu_area) {
case LEFT_CC_NO:
min_value = 0;
max_value = 127;
cur_value = left_cc_number;
strcpy_P(buffer, controller_menu__left_cc_number);
break;
case RIGHT_CC_NO:
min_value = 0;
max_value = 127;
cur_value = right_cc_number;
strcpy_P(buffer, controller_menu__right_cc_number);
break;
case MIDI_CHANNEL:
min_value = 1;
max_value = 16;
cur_value = midi_channel + 1;
strcpy_P(buffer, top_menu__midi_channel);
break;
}
if (last_item == -1) {
lcd.clear();
lcd.setCursor((20 - strlen(buffer))/2, 0);
lcd.print(buffer);
}
itoa(cur_value, buffer, 10);
lcd.setCursor(6, 2);
if (cur_value > min_value) {
lcd.write(0x7f);
} else {
lcd.write(0x20);
}
for(int i = 0; i < (4 - strlen(buffer)); i++) lcd.print(" ");
lcd.print(buffer);
lcd.print(" ");
if (cur_value < max_value) {
lcd.write(0x7e);
} else {
lcd.write(0x20);
}
last_item = cur_value;
}
}
}
//////////////////////////////////////////////////////////////////////////////
// Interrupt servies routine for the click function of the rotary encoder.
void click() {
delayMicroseconds(2000); // Debounce
if (digitalRead(ENCODER_CLICK)) return;
last_interrupt = millis();
switch (menu_area) {
case MAIN_MENUS:
if (menu == top_menu) {
switch(item) {
case 0: // Note Settings
menu = note_menu;
item = 0;
break;
case 1: // Controller Settings
menu = controller_menu;
item = 0;
break;
case 2: // MIDI Channel
menu_area = MIDI_CHANNEL;
break;
case 3: // Invert Left Range
inv_left = !inv_left;
EEPROM.write(7, inv_left);
top_menu[3] =
inv_left ?
controller_menu__inv_left_on :
controller_menu__inv_left_off;
break;
case 4: // Invert Right Range
inv_right = !inv_right;
EEPROM.write(8, inv_right);
top_menu[4] =
inv_right ?
controller_menu__inv_right_on :
controller_menu__inv_right_off;
break;
}
} else if (menu == note_menu) {
switch(item) {
case 0: // Scale
menu = scale_menu;
item = scale;
break;
case 1: // Root
menu = root_menu;
item = root;
break;
case 2: // Octave
menu = octave_menu;
item = octave + 2;
break;
case 3: // Range
menu = range_menu;
item = range - 1;
break;
case 4: // Back
menu = top_menu;
item = 0;
break;
}
} else if (menu == controller_menu) {
switch(item) {
case 0: // Left CC Number
menu_area = LEFT_CC_NO;
break;
case 1: // Right CC Number
menu_area = RIGHT_CC_NO;
break;
case 2: // Back
menu = top_menu;
item = 1;
break;
}
} else if (menu == scale_menu) {
menu = note_menu;
scale = item;
EEPROM.write(0,scale);
item = 0;
} else if (menu == root_menu) {
menu = note_menu;
root = item;
EEPROM.write(1,root);
item = 1;
} else if (menu == octave_menu) {
menu = note_menu;
octave = item - 2;
EEPROM.write(2,octave);
item = 2;
} else if (menu == range_menu) {
menu = note_menu;
range = item + 1;
EEPROM.write(3,range);
item = 3;
}
makeScale();
break;
case LEFT_CC_NO:
menu_area = MAIN_MENUS;
menu = controller_menu;
item = 0;
break;
case RIGHT_CC_NO:
menu_area = MAIN_MENUS;
menu = controller_menu;
item = 2;
break;
case MIDI_CHANNEL:
menu_area = MAIN_MENUS;
menu = top_menu;
item = 2;
break;
}
last_item = -1;
}
//////////////////////////////////////////////////////////////////////////////
// Interrupt service routine for the turn function of the rotary encoder
void turn() {
// Ignore calls too close together to try and debounce
if((millis() - last_interrupt) < 5){
last_interrupt = millis();
return;
}
blinkBlue();
boolean up = digitalRead(ENCODER_B);
//If we're not in a menu, just change mode
if (last_loop == MUSIC) {
if (up == true && optionMode == CONTROLLER) {
optionMode = NOTES;
EEPROM.write(9, 0);
modeChanged = true;
}
if (up == false && optionMode == NOTES) {
optionMode = CONTROLLER;
EEPROM.write(9, 1);
modeChanged = true;
}
return;
}
last_interrupt = millis();
switch (menu_area) {
case MAIN_MENUS:
if (up) {
int total_items = totalItems();
item++;
if (item >= total_items){
item = total_items - 1;
blinkRed();
}
} else {
item--;
if (item < 0){
item = 0;
blinkRed();
}
}
break;
case LEFT_CC_NO:
if (up) {
left_cc_number++;
// If we're going UP and we end up negative, we've overflown to -128. Set to 127
if (left_cc_number < 0){
left_cc_number = 127;
blinkRed();
}
} else {
left_cc_number--;
if (left_cc_number < 0){
left_cc_number = 0;
blinkRed();
}
}
EEPROM.write(4, left_cc_number);
break;
case RIGHT_CC_NO:
if (up) {
right_cc_number++;
// If we're going UP and we end up negative, we've overflown to -128. Set to 127
if (right_cc_number < 0){
right_cc_number = 127;
blinkRed();
}
} else {
right_cc_number--;
if (right_cc_number < 0){
right_cc_number = 0;
blinkRed();
}
}
EEPROM.write(5, right_cc_number);
break;
case MIDI_CHANNEL:
if (up) {
midi_channel++;
if (midi_channel > 15){
midi_channel = 15;
blinkRed();
}
} else {
midi_channel--;
if (midi_channel < 0){
midi_channel = 0;
blinkRed();
}
}
EEPROM.write(6, midi_channel);
break;
}
}
void blinkRed(){
analogWrite(RED_L, 255);
analogWrite(RED_R, 255);
delayMicroseconds(100000);//0.1 sec
analogWrite(RED_L, 0);
analogWrite(RED_R, 0);
}
void blinkBlue(){
analogWrite(BLUE_L, 100);
analogWrite(BLUE_R, 100);
delayMicroseconds(100000);//0.1 sec
analogWrite(BLUE_L, 0);
analogWrite(BLUE_R, 0);
}