new

Dependencies:   mbed

Revision:
1:461a4f04db9b
Parent:
0:89cc1300ce7e
Child:
2:94a34bcf8f09
--- a/main.cpp	Tue Feb 17 21:31:58 2015 +0000
+++ b/main.cpp	Thu Feb 19 18:33:11 2015 +0000
@@ -5,13 +5,16 @@
 //Initialize serial comms
 Serial pc(USBTX, USBRX);
 
-PwmOut PWMh(p26);
-PwmOut PWMl(p21);
+PwmOut PWMh(p22);
+//because both use same ticker make a new manual pmw
+DigitalOut PWMl(p21);
+Ticker PWMcont;
+//PwmOut PWMl(p21);
 
-DigitalOut row1(p25);
-DigitalOut row2(p22);
-DigitalOut row3(p23);
-DigitalOut row4(p24);
+DigitalOut row1(p26);
+DigitalOut row2(p23);
+DigitalOut row3(p24);
+DigitalOut row4(p25);
 
 DigitalIn col1(p20);
 DigitalIn col2(p19);
@@ -23,6 +26,9 @@
 map<int, char> keypadMap;
 map<int, char *> dtmfMap; 
 
+//global
+int on = 0;
+
 //Mores Code -> Character Map
 void init(){
         keypadMap[0x11]='1';
@@ -60,6 +66,13 @@
         dtmfMap[0x88]="1633,941"; //D
 }
 
+void flip() {
+    if(on == 1)
+        PWMl = !PWMl;
+    else
+        PWMl = 0;
+}
+
 int main() {
     init();
     //Initialize rows to 0
@@ -67,66 +80,96 @@
     row2 = 0;
     row3 = 0;
     row4 = 0;
+    PWMl = 0;
     t1.start();
     int cur_input = 0x00;
     int prev_input = 0x00;
+    
+    //main loop
     while(1) {
-        //make all rows 0 to add stability
-        row1 = 0;
-        row2 = 0;
-        row3 = 0;
-        row4 = 0;
-       //Keep each led lit for 4ms
-       switch(t1.read_ms()%4){
-                        case 0:
-                        row1 = 1;
-                        cur_input |= 0x10;   
-                        break;
-                        case 1:
-                        row2 = 1;
-                        cur_input |= 0x20;
-                        break;
-                        case 2:
-                        row3 = 1;
-                        cur_input |= 0x40;
-                        break;
-                        case 3:
-                        row4 = 1; 
-                        cur_input |= 0x80;
-                        break; 
-                        }
-     if(col1 == 1)
-     cur_input |= 0x01;
-     else if(col2 == 1)
-     cur_input |= 0x02;
-     else if(col3 == 1)
-     cur_input |= 0x04;
-     else if(col4 == 1)
-     cur_input |= 0x08;
-     else cur_input = 0;
+        
+        
+        //If there was no previous input than switch rows
+        if(prev_input == 0){
+            //make all rows 0 to add stability
+            row1 = 0;
+            row2 = 0;
+            row3 = 0;
+            row4 = 0;
+           //Keep each pin on for 4ms
+           //"turn on" appropriat bit in map when on.
+           switch(t1.read_ms()%4){
+                            case 0:
+                            row1 = 1;
+                            cur_input |= 0x10;   
+                            break;
+                            case 1:
+                            row2 = 1;
+                            cur_input |= 0x20;
+                            break;
+                            case 2:
+                            row3 = 1;
+                            cur_input |= 0x40;
+                            break;
+                            case 3:
+                            row4 = 1; 
+                            cur_input |= 0x80;
+                            break; 
+                            }
+        }
+        
+   
      
-     
-     if(cur_input) {
-        if(cur_input != prev_input) {
-            char c = keypadMap[cur_input]; //assign charcter based on input
-            if(c != 0){
-                pc.printf("%c\r",c);  //print input and char
+            
+            //Check each colum to see if it is high
+            //if it is "turn on" that respective bit
+             if(col1 == 1)
+             cur_input |= 0x01;
+             else if(col2 == 1)
+             cur_input |= 0x02;
+             else if(col3 == 1)
+             cur_input |= 0x04;
+             else if(col4 == 1)
+             cur_input |= 0x08;
+             else cur_input = 0;    //Detect button release or no input set input to 0
+             
+             //If curr input is non zero continue
+             if(cur_input) {
+                if(cur_input != prev_input) {
+                    char c = keypadMap[cur_input]; //assign charcter based on input
+                    if(c != 0){
+                        pc.printf("%c\r",c);  //print input and char
+                    }
+                    float freqh,freql;
+                    char *freq = dtmfMap[cur_input];
+                    sscanf(freq,"%f,%f",&freqh,&freql);
+                    pc.printf("High:%f, Low:%f\r",freqh,freql);
+                    
+              
+                    
+                    //PWMl.period(1/freql);               // set PWM period to user specified for low tone
+                    //PWMl=0.5;                         // set duty cycle to 50%
+                    on = 1;
+                    PWMcont.attach(&flip, (1/freql)); // the address of the function to be attached (flip) and the interval (1/freql)
+                    
+                    PWMh.period(1/freqh);               // set PWM period to user specified for high tone
+                    PWMh=0.5;                         // set duty cycle to 50%
+                    
+                }
+                 
             }
-            float freqh,freql;
-            char *freq = dtmfMap[cur_input];
-            sscanf(freq,"%f,%f",&freqh,&freql);
-            pc.printf("High:%f, Low:%f\r",freqh,freql);
-            PWMh.period(1/freqh);               // set PWM period to user specified   
-            PWMh=0.5;                         // set duty cycle to 50% 
-            PWMl.period(1/freql);               // set PWM period to user specified   
-            PWMl=0.5;                         // set duty cycle to 50%
-            prev_input = cur_input;
-        }
-         
+            //Turn off pulse width modulation if curr input is 0
+            else {
+                PWMh = 0.0;
+                //PWMl = 0.0;
+                on = 0;
+             } 
+       
+     
+     //Maintain the past input
+     prev_input = cur_input;
+     
+
     }
-    else {
-        PWMh = 0.0;
-        PWMl = 0.0;
-     } 
-    }
+  
 }
\ No newline at end of file