Function generator

Dependencies:   Serial_HL mbed

Revision:
3:c8c3b755ef54
Parent:
2:100244cef3d6
diff -r 100244cef3d6 -r c8c3b755ef54 FuncGen.cpp
--- a/FuncGen.cpp	Thu Mar 17 13:45:13 2016 +0000
+++ b/FuncGen.cpp	Tue May 14 12:16:55 2019 +0000
@@ -1,5 +1,6 @@
 
 #include "FuncGen.h"
+#include "mbed.h"
 
 // Konstruktor
 SignedRampGen::SignedRampGen()
@@ -68,27 +69,57 @@
     val=0;
     _inc=_phase=0;
     SetPointsPerPeriod(10);
+    _thres = 0.5;
 }
 
 void RectGen::SetPointsPerPeriod(int aPoints)
 {
-    _inc = 1.0/aPoints;
+    _inc = 2.0/aPoints;
 }
 
 void RectGen::SetFrequ(float aFrequ)
 {
     SetPointsPerPeriod(1.0/aFrequ);
 }
+void RectGen::SetPulsWidth(float aPercent)
+{
+    _thres = aPercent;
+}
 
 void RectGen::CalcOneStep()
 {
     _phase += _inc;
     if( _phase>=1.0 )
         _phase = _phase - 1.0;
-    if( _phase>0.5 )
+    if( _phase>_thres )
         val = 1.0;
     else
         val = -1.0;
 }
 
+SinusGen::SinusGen()
+{
+    val = 0;
+    _inc = 1;
+    entry = 0;
+    for(int i = 0; i < 1000; i++)
+    {
+        sinWave[i] = sin(((3.1415*2)/1000)*i); 
+    }
+}       
 
+void SinusGen::SetFrequ(float aFrequ)
+{
+    int freq = 22000 * aFrequ;
+    _inc = 1000 * freq/22000;
+}
+
+void SinusGen::CalcOneStep()
+{
+    val = sinWave[(int)entry];
+    entry = entry + _inc;
+    if(entry > 1000)
+        entry = entry - 1000;
+}
+
+