This library generate four classic waves, have an escape button to stop to generate wave. And is possible to use function for calculate period,... separately

Revision:
1:cb84b066ab29
Parent:
0:c1150498cbe9
--- a/WaveGen.cpp	Fri Jul 24 13:38:10 2015 +0000
+++ b/WaveGen.cpp	Fri May 06 06:40:10 2016 +0000
@@ -3,9 +3,8 @@
 
 using namespace mbed;
 
-WaveGen::WaveGen(PinName ext,PinName WaveForm):_ext(ext),_WaveForm(WaveForm)
+WaveGen::WaveGen(PinName WaveForm):_WaveForm(WaveForm)
 {
-    _ext.mode(PullUp); 
     _WaveForm.write(0);   
 }
 
@@ -40,107 +39,110 @@
     return temp;   
 }
 
-bool WaveGen::SineWave(float Frequency,float Amplitude)
+void WaveGen::SineWave(float Frequency,float Amplitude)
 {
     float temp = 0;
-    int T = TCalc(20,Frequency);
+    int T1 = TCalc(6,Frequency);//porzione 30°
+    int T2 = TCalc(12,Frequency);//porzione 120°
     float V = VCalc(Amplitude) * 2;
-    while(1)
+    for(int a = 0; a <= 30; a += 5)
+    {
+        temp = 0.5 + V * sin(RadCalc((float)a));
+        _WaveForm = temp;
+        wait_us(T1);       
+    }
+    for(int b = 30; b <= 150; b += 10)
+    {
+        temp = 0.5 + V * sin(RadCalc((float)b));
+        _WaveForm = temp;
+        wait_us(T2);    
+    } 
+    for(int c = 150; c <= 210; c += 5)
     {
-        for(int i = 0; i < 360; i += 18)
-        {
-            temp = 0.5 + V * sin(RadCalc((float)i));
-            _WaveForm = temp;
-            wait_us(T);
-            if(!_ext.read())
-                return true;
-            else 
-                return false;    
-        }   
-    }    
-}
-
-bool WaveGen::SquareWave(float Frequency,float Amplitude,float DutyCycle)
-{
-    int Ton = TCalc(0,Frequency);
-    Ton *= DutyCycle;
-    int Toff = Ton - TCalc(0,Frequency);  
-    float V = VCalc(Amplitude); 
-    while(1)
+        temp = 0.5 + V * sin(RadCalc((float)c));
+        _WaveForm = temp;
+        wait_us(T1);    
+    } 
+    for(int d = 210; d <= 330; d += 10)
     {
-        _WaveForm = V;
-        wait_us(Ton);
-        _WaveForm = 0;
-        wait_us(Toff); 
-        if(!_ext.read())
-            return true;
-    }    
+        temp = 0.5 + V * sin(RadCalc((float)d));
+        _WaveForm = temp;
+        wait_us(T2);    
+    } 
+    for(int e = 330; e <= 360; e += 5)
+    {
+        temp = 0.5 + V * sin(RadCalc((float)e));
+        _WaveForm = temp;
+        wait_us(T1);    
+    } 
 }
 
-bool WaveGen::TriangularWave(float Frequency,float Amplitude)
+void WaveGen::SquareWave(float Frequency,float Amplitude,float DutyCycle)
+{
+    float V = VCalc(Amplitude);
+    float temp = 0;
+    int Ton,Toff = 0;
+    int Temp = TCalc(0,Frequency);
+    if(DutyCycle != 50)
+    {
+        temp = DutyCycle / 100;
+        temp *= (float)Temp;
+        Ton = (int)temp;
+        Toff = Temp - Ton;
+    }
+    else 
+    {
+        Ton = TCalc(0,Frequency);
+        Toff = Ton;    
+    } 
+    _WaveForm = V;
+    wait_us(Ton);
+    _WaveForm = 0;
+    wait_us(Toff);
+}
+
+void WaveGen::TriangularWave(float Frequency,float Amplitude)
 {
     float temp = 0;
-    int T = TCalc(80,Frequency);
+    int T = TCalc(50,Frequency);
     float V = VCalc(Amplitude);
-    while(1)
+    int min,max,pass;
+    //Calcola gli intervalli di tensione
+    temp = V * 1000;
+    min = 500 - (int)temp;
+    max = 500 + (int)temp;
+    pass = (max - min) / 50;
+    for(int a = min; a <= max; a += pass)
     {
-        for(int a = 50; a < V; a += 5)
-        {
-            temp = (float)a / 100;
-            _WaveForm = temp; 
-            wait_us(T);
-            if(!_ext.read())
-                return true;    
-        }
-        for(int b = V; b > 50; b -= 5)
-        {
-            temp = (float)b / 100;
-            _WaveForm = temp;
-            wait_us(T);
-            if(!_ext.read())
-                return true;
-        }
-        for(int c = 50; c < V - 50; c -= 5)
-        {
-            temp = (float)c / 100;
-            _WaveForm = temp;
-            wait_us(T);
-            if(!_ext.read())
-                return true;
-        }
-        for(int d = V - 50; d < 50; d += 5)
-        {
-            temp = (float)d / 100;
-            _WaveForm = temp;
-            wait_us(T);
-            if(!_ext.read())
-                return true;
-        } 
+        temp = (float)a;
+        temp /= 1000;
+        _WaveForm = temp;
+        wait_us(T);   
+    }
+    for(int b = max; b >= min; b -= pass)
+    {
+        temp = (float)b;
+        temp /= 1000;
+        _WaveForm = temp;
+        wait_us(T);   
     }
 }
 
-bool WaveGen::SawToothWave(float Frequency,float Amplitude)
+void WaveGen::SawToothWave(float Frequency,float Amplitude)
 {
     float temp = 0;
-    int T = TCalc(40,Frequency);
+    int T = TCalc(50,Frequency);
     float V = VCalc(Amplitude);
-    while(1)
+    int min,max,pass;
+    temp = V * 100;
+    min = 50 - (int)temp;
+    max = 50 + (int)temp;
+    pass = (max - min) / 50;
+    for(int a = min; a <= max; a += pass)
     {
-        for(int a = 50; a < V; a += 5)
-        {
-            temp = (float)a / 100;
-            _WaveForm = temp;
-            wait_us(T);
-            if(!_ext.read())
-                return true;
-        }
-        for(int b = 50; b > V - 50; b -= 5)
-        {
-            temp = (float)b / 100;
-            _WaveForm = temp;
-            wait_us(T);
-            if(!_ext.read())
-                return true;
-        }    
-    }
+        temp = (float)a;
+        temp /= 100;
+        _WaveForm = temp;
+        wait_us(T);
+    }  
 }