Function generator

Dependencies:   Serial_HL mbed

Revision:
2:100244cef3d6
Child:
3:c8c3b755ef54
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FuncGen.cpp	Thu Mar 17 13:45:13 2016 +0000
@@ -0,0 +1,94 @@
+
+#include "FuncGen.h"
+
+// Konstruktor
+SignedRampGen::SignedRampGen()
+{
+    val=0;
+    SetPointsPerPeriod(10);
+}
+
+void SignedRampGen::SetPointsPerPeriod(int aPoints)
+{
+    _inc = 2.0/aPoints;
+}
+
+void SignedRampGen::SetFrequ(float aFrequ)
+{
+    SetPointsPerPeriod(1.0/aFrequ);
+}
+
+void SignedRampGen::CalcOneStep()
+{
+    val = val + _inc;
+    if( val>=1.0 )
+        val = -1 + (val - 1.0);
+}
+
+
+
+TriangleGen::TriangleGen()
+{
+    val=0;
+    _state=1;
+    SetPointsPerPeriod(10);
+}
+
+void TriangleGen::SetPointsPerPeriod(int aPoints)
+{
+    _inc = 4.0/aPoints;
+}
+
+void TriangleGen::SetFrequ(float aFrequ)
+{
+    SetPointsPerPeriod(1.0/aFrequ);
+}
+
+void TriangleGen::CalcOneStep()
+{
+    if( _state==1 ) {
+        val = val + _inc;
+        if( val>1.0 ) {
+            val = val - _inc;
+            _state = 2;
+        }
+    } else if( _state==2 ) {
+        val = val - _inc;
+        if( val<-1.0 ) {
+            val = val + _inc;
+            _state = 1;
+        }
+    }
+}
+
+
+
+RectGen::RectGen()
+{
+    val=0;
+    _inc=_phase=0;
+    SetPointsPerPeriod(10);
+}
+
+void RectGen::SetPointsPerPeriod(int aPoints)
+{
+    _inc = 1.0/aPoints;
+}
+
+void RectGen::SetFrequ(float aFrequ)
+{
+    SetPointsPerPeriod(1.0/aFrequ);
+}
+
+void RectGen::CalcOneStep()
+{
+    _phase += _inc;
+    if( _phase>=1.0 )
+        _phase = _phase - 1.0;
+    if( _phase>0.5 )
+        val = 1.0;
+    else
+        val = -1.0;
+}
+
+