USBMIDI sampled pipe organ uses real pipe organ samples to produce a realistic sound with 16 note polyphony. A serial output will drive external TPIC6A596 shift registers that could be used to drive pipe organ magnets (Solenoid valves)

Dependencies:   TextLCD USBDevice mbed

Revision:
23:31cb90fa661f
Parent:
22:7d4e07edf503
Child:
26:7b9267a5b3c3
--- a/main.cpp	Sun Nov 15 11:37:09 2015 +0000
+++ b/main.cpp	Sun Nov 15 12:55:31 2015 +0000
@@ -92,13 +92,13 @@
 int x =0;
 int x1=1;
 
-int sw1_count=0;                                                                                        //Switch de-bounce counter
-int sw1_old=0;
-int sw1_phase=0;
-int sw3_count=0;                                                                                        //Switch de-bounce counter
-int sw3_old=0;
-int sw3_phase=0;
-unsigned char stops[5];
+unsigned int swi;
+unsigned int pressed;
+unsigned int sw[8];
+unsigned int sw_count[8];                                                                               //Switch de-bounce counter
+unsigned int sw_phase[8];
+unsigned int sw_status[8]= {0,1,0,0,0,0,0,0};
+unsigned int sw_old[8];
 int oldstops=1;
 
 //Sound generator variables
@@ -233,19 +233,19 @@
         case MIDIMessage::NoteOnType:                                                                   //MIDI note on received
             if (key >23 & key<120) {
                 onoff=1;
-                if (stops[0]==0 & key >35) {
+                if (sw_status[0]==1 & key >35) {
                     keybuf[(key-36)]|= 1 << 0;                                                          //Store @ 16' pitch
                 }
-                if (stops[1]==0) {
+                if (sw_status[1]==1) {
                     keybuf[(key-24)]|= 1 << 1;                                                          //Store @ 8' pitch
                 }
-                if (stops[2]==0& key<108) {
+                if (sw_status[2]==1& key<108) {
                     keybuf[(key)-12]|= 1 << 2;                                                          //Store @ 4' pitch
                 }
-                if (stops[3]==0 & key<101) {
+                if (sw_status[3]==1 & key<101) {
                     keybuf[(key)-5]|= 1 << 3;                                                           //Store @ 2 2/3' pitch
                 }
-                if (stops[4]==0 & key <96) {
+                if (sw_status[4]==1 & key <96) {
                     keybuf[(key)]|= 1 << 4;                                                             //Store @ 2' pitch
                 }
             }
@@ -404,49 +404,41 @@
         wait_us(4);
 
 //Read Stop inputs
-        stops[0]=p1;
-        stops[1]=p2;
-        stops[2]=p3;
-        stops[3]=p4;
-        stops[4]=p5;
+        sw[0]=p1;
+        sw[1]=p2;
+        sw[2]=p3;
+        sw[3]=p4;
+        sw[4]=p5;
+        sw[6]=sw1;
+        sw[7]=sw3;
 
 //Additional code to de-bounce the K64F onboard switches again using multiple loops to de-bounce; has 3 states:
 //0 - Button not pressed
 //1 - Button pressed waiting for de-bounce count of 10 loops
 //2 - Button press stable, this status can be read as the de-bounced status indicating the button press is clean
 
-// De-bounce SW1
-        if (sw1_phase==0 & sw1==0) {                                                                    //Button just been pressed
-            sw1_phase=1;
-            sw1_count=10;
-        }
-        if (sw1_phase==1 & sw1_count==0 & sw1==0) {                                                     //Button still pressed after de-bounce period
-            sw1_phase=2;
-        }
-        if (sw1_phase==1 & sw1_count==0 & sw1==1) {                                                     //Button no longer pressed after de-bounce period
-            sw1_phase=0;
-        }                       
-        if (sw1_phase==2 & sw1==1) {                                                                    //Button  released
-            sw1_phase=0;
+// De-bounce Switches
+        pressed=0;
+        for (swi=0; swi<8; swi++) {                                                                     //Process 8 witches
+            if (sw_phase[swi]==0 & sw[swi]==0) {                                                        //Button just been pressed
+                sw_phase[swi]=1;
+                sw_count[swi]=10;
+            }
+            if (sw_phase[swi]==1 & sw_count[swi]==0 & sw[swi]==0) {                                     //Button still pressed after de-bounce period
+                sw_phase[swi]=2;
+                pressed++;                                                                              //Inc button pressed count
+                sw_status[swi]=!sw_status[swi];                                                         //Toggle switch status
+            }
+            if (sw_phase[swi]==1 & sw_count[swi]==0 & sw[swi]==1) {                                     //Button no longer pressed after de-bounce period
+                sw_phase[swi]=0;
+            }
+            if (sw_phase[swi]==2 & sw[swi]==1) {                                                        //Button  released
+                sw_phase[swi]=0;
+            }
+            sw_count[swi]--;
         }
-        sw1_count--;
-// De-bounce SW3
-        if (sw3_phase==0 & sw3==0) {                                                                    //Button just been pressed
-            sw3_phase=1;
-            sw3_count=10;
-        }
-        if (sw3_phase==1 & sw3_count==0 & sw3==0) {                                                     //Button still pressed after de-bounce period
-            sw3_phase=2;
-        }
-        if (sw3_phase==1 & sw3_count==0 & sw3==1) {                                                     //Button no longer pressed after de-bounce period
-            sw3_phase=0;
-        } 
-        if (sw3_phase==2 & sw3==1) {                                                                    //Button  released
-            sw3_phase=0;
-        }
-        sw3_count--;
-//light blue LED if either button presed
-        if (sw1_phase==2 | sw3_phase==2) {
+//light blue LED if any button presed
+        if (pressed>0) {
             blueled=0;
         } else {
             blueled=1;
@@ -454,24 +446,18 @@
 
 
 //Button 1 will clear all playing notes & switch sampleset
-        if (sw1_phase != sw1_old) {
-            if(sw1_phase==2) {
+        if (sw_status[6] != sw_old[6]) {
                 for (cl=0; cl<108; cl++) {
                     keybuf[cl]=0;                                                                       //Clear Keybuf
                 }
-                sampleset++;
+                sampleset=sw_status[6];
                 oldstops=255;                                                                           //Force a display refresh
-                if (sampleset>1) {
-                    sampleset=0;
-                }
-            }
-            sw1_old=sw1_phase;
+            sw_old[6]=sw_status[6];
         }
 
 
 //Check for display mode button being presed
-        if (sw3_phase != sw3_old) {                                                                     //Mode Switch pressed
-            if(sw3_phase==2) {
+        if (sw_status[7] != sw_old[7]) {                                                                //Mode Switch pressed
                 lcdmode++;
                 if (lcdmode>4) {
                     lcdmode=0;
@@ -510,8 +496,7 @@
                 if (lcdmode==4) {
                     oldstops=255;                                                                       //Force a refresh
                 }
-            }
-        sw3_old=sw3_phase;
+            sw_old[7]=sw_status[7];
         }
 
 
@@ -614,7 +599,7 @@
 //Stop display
         if (lcdmode==4) {
 
-            pos=stops[0] + stops[1]+stops[2]+stops[3]+stops[4];
+            pos=sw_status[0] + sw_status[1]+sw_status[2]+sw_status[3]+sw_status[4];
             if (oldstops != pos) {
                 oldstops=pos;
                 lcd.cls();
@@ -627,23 +612,23 @@
                     }
                 }
                 lcd.locate(0,1);
-                if (stops[0]==0) {
+                if (sw_status[0]==1) {
                     lcd.printf("16 ");
                 }
 
-                if (stops[1]==0) {
+                if (sw_status[1]==1) {
                     lcd.printf("8 ");
                 }
 
-                if (stops[2]==0) {
+                if (sw_status[2]==1) {
                     lcd.printf("4 ");
                 }
 
-                if (stops[3]==0) {
+                if (sw_status[3]==1) {
                     lcd.printf("2-2/3 ");
                 }
 
-                if (stops[4]==0) {
+                if (sw_status[4]==1) {
                     lcd.printf("2");
                 }
             }