Fertiges FuncGen

Dependencies:   Serial_HL mbed

Fork of ProcVisDemo by michael hollegha

Files at this revision

API Documentation at this revision

Comitter:
Polteko123
Date:
Thu Apr 06 13:17:39 2017 +0000
Parent:
1:e88b745f2ca2
Commit message:
Fertig_FunkGen

Changed in this revision

BtnEventM0.h Show annotated file Show diff for this revision Revisions of this file
FunkGen.cpp Show annotated file Show diff for this revision Revisions of this file
FunkGen.h Show annotated file Show diff for this revision Revisions of this file
ProcVisDemo.cpp Show annotated file Show diff for this revision Revisions of this file
Serial_HL.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r e88b745f2ca2 -r 6de5bcffd991 BtnEventM0.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BtnEventM0.h	Thu Apr 06 13:17:39 2017 +0000
@@ -0,0 +1,174 @@
+
+// V4.0
+
+class BtnEventM0 {
+    public:
+        int16_t pressed;
+        
+        BtnEventM0(PinName pin) : _isr(pin)
+            { pressed=0; }
+
+        // Ist eine steigende Flanke aufgetreten ?
+        int CheckFlag();
+    
+    // 1..Button is pressed  else 0
+        int CheckButton()
+            { return _isr.read(); }
+        
+        void Init();
+            // { _isr.rise(this,&BtnEventM0::RisingISR); }
+
+        void RisingISR();
+        
+    protected:
+        InterruptIn _isr;
+};
+
+void BtnEventM0::Init()
+            { _isr.rise(this,&BtnEventM0::RisingISR); }
+
+void BtnEventM0::RisingISR()
+{
+    if( _isr.read() )
+        pressed = 1;
+}
+
+int BtnEventM0::CheckFlag()
+{
+  if( pressed )
+      { pressed=0; return 1; }
+  return 0;
+}
+
+
+
+
+
+/*
+class BtnEventM02 : public BtnEventM0
+{
+public:
+    BtnEventM02(PinName pin) : BtnEventM0(pin) {
+        _tm.stop();
+        _tm.reset();
+        _state=1;
+    }
+
+    void Init() {
+        _isr.rise(this, &BtnEventM02::RisingISR);
+    }
+
+    void RisingISR() {
+        if( !_isr.read() )
+            return;
+        pressed = 1;
+        _tm.start();
+        _state = 2;
+    }
+
+    void CheckButton() {
+        if( _state==1 )
+            return;
+        if( _state==2 ) {
+            if( !_isr.read() ) {
+                _state = 1;
+                return;
+            }
+            if( _tm.read_ms()>500 ) {
+                _tm.reset();
+                _state = 3;
+                pressed = 1;
+            }
+        } else if( _state==3 ) {
+            if( !_isr.read() ) {
+                _state = 1;
+                return;
+            }
+            if( _tm.read_ms()>100 ) {
+                _tm.reset();
+                _state = 3;
+                pressed = 1;
+            }
+        }
+    }
+private:
+    int16_t _state;
+    Timer _tm;
+};
+*/
+
+class AnalogInHL : public AnalogIn
+{
+public:
+    AnalogInHL(PinName pin) : AnalogIn(pin) { }
+    int Read() {
+        return read_u16()>>6;
+    }
+};
+
+class BtnEventM0S {
+    public:
+    BtnEventM0S(PinName pin) : _btn(pin) { }
+
+    void Init() {}
+
+        // Ist eine steigende Flanke aufgetreten ?
+        int CheckFlag()
+    {
+      if( _btn )
+        { wait_ms(100); return 1; }
+      else
+        return 0;
+    }
+
+  protected:
+        DigitalIn _btn;
+};
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff -r e88b745f2ca2 -r 6de5bcffd991 FunkGen.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FunkGen.cpp	Thu Apr 06 13:17:39 2017 +0000
@@ -0,0 +1,111 @@
+# include "FunkGen.h"
+
+SignedRampGen::SignedRampGen()
+{
+    // sinnvolle Frequenz setzen
+    SetPointsPerPeriod(50.0);
+}
+
+void SignedRampGen::SetPointsPerPeriod(float 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);
+}
+
+
+// TiangleGen
+
+TriangleGen::TriangleGen()
+{
+    // sinnvolle Frequenz setzen
+    SetPointsPerPeriod(50.0);
+    _state = 1;
+}
+
+void TriangleGen::SetPointsPerPeriod(float aPoints)
+{
+    _inc = 4.0/aPoints;
+}
+
+void TriangleGen::SetFrequ(float aFrequ)
+{
+    SetPointsPerPeriod(1.0/aFrequ);
+}
+
+void TriangleGen::CalcOneStep()
+{
+    _phase = _phase + _inc;
+    if(_phase > 1.0)
+    {
+        _phase = -1 + (_phase - 1.0);
+        if(_state == 1)
+            _state = 2;
+            
+        else
+            _state = 1;
+    }
+        
+    if(_state == 1)
+        val = _phase;
+        
+    if(_state == 2)
+        val = -_phase;
+    
+    
+}
+
+// RectangleGen
+
+RectangleGen::RectangleGen()
+{
+    // sinnvolle Frequenz setzen
+    SetPointsPerPeriod(50.0);
+    _state = 1;
+}
+
+void RectangleGen::SetPointsPerPeriod(float aPoints)
+{
+    _max = aPoints;
+}
+
+void RectangleGen::SetFrequ(float aFrequ)
+{
+    SetPointsPerPeriod(1.0/aFrequ);
+}
+
+void RectangleGen::CalcOneStep()
+{
+    if(_count >= _max/2)
+    {
+        _count = 0;
+        if(_state == 1)
+            _state = 2;
+            
+        else
+            _state = 1;
+    }
+    
+    if(_state == 1)
+    {
+        _count++;
+        val = 1;
+    }
+    
+    else 
+    {
+        _count++;
+        val = -1;
+    } 
+}
+
diff -r e88b745f2ca2 -r 6de5bcffd991 FunkGen.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FunkGen.h	Thu Apr 06 13:17:39 2017 +0000
@@ -0,0 +1,68 @@
+#ifndef FUNKGEN_h
+#define FUNKGEN_h
+
+class SignedRampGen
+{
+    public:
+        float val;  // momentaner Ausgangswert
+    private:
+        float _inc;
+    public:
+        SignedRampGen();
+        
+        
+        void SetPointsPerPeriod(float aPoint);
+        
+        // bezogen auf   Fsample 0..0.5
+        void SetFrequ(float aFrequ);
+        
+        //Einen Abtastwert berechnen
+        // wird bei z.B. Fsample=100Hz 100x pro Sec aufgerufen
+        void CalcOneStep();  
+};
+
+class TriangleGen
+{
+    public:
+        float val;  // momentaner Ausgangswert
+    private:
+        float _inc;
+        int _state;
+        float _phase;   // Ausgangswert des zugrundeliegenden Generators 
+    public:
+        TriangleGen();
+        
+        
+        void SetPointsPerPeriod(float aPoint);
+        
+        // bezogen auf   Fsample 0..0.5
+        void SetFrequ(float aFrequ);
+        
+        //Einen Abtastwert berechnen
+        // wird bei z.B. Fsample=100Hz 100x pro Sec aufgerufen
+        void CalcOneStep();  
+};
+
+class RectangleGen
+{
+    public:
+        float val;  // momentaner Ausgangswert
+    private:
+        int _state;
+        int _count;
+        int _max;
+    public:
+        RectangleGen();
+        
+        
+        void SetPointsPerPeriod(float aPoint);
+        
+        // bezogen auf   Fsample 0..0.5
+        void SetFrequ(float aFrequ);
+        
+        //Einen Abtastwert berechnen
+        // wird bei z.B. Fsample=100Hz 100x pro Sec aufgerufen
+        void CalcOneStep();  
+};
+
+#endif
\ No newline at end of file
diff -r e88b745f2ca2 -r 6de5bcffd991 ProcVisDemo.cpp
--- a/ProcVisDemo.cpp	Fri Oct 09 07:58:26 2015 +0000
+++ b/ProcVisDemo.cpp	Thu Apr 06 13:17:39 2017 +0000
@@ -1,66 +1,92 @@
 #include "mbed.h"
 #include "Serial_HL.h"
+#include "BtnEventM0.h"
+#include "FunkGen.h"
 
 SerialBLK pc(USBTX, USBRX);
 SvProtocol ua0(&pc);
 
-// V2.0
-// BusOut leds(LED1,LED2,LED3,LED4); Bertl14
-// M0-Board
-BusOut leds(P1_13,P1_12,P1_7,P1_6,P1_4,P1_3,P1_1,P1_0,LED4,LED3,LED2,LED1);
+void CommandHandler();
+
+SignedRampGen fg1;
+float ampl=1.0, v1=0.0;
 
+// 2ter Generator + Variablen
+TriangleGen fg2;
+float ampl2=1.0, v2=0.0;
 
-void CommandHandler();
+// RectangleGen
+RectangleGen fg3;
+float ampl3=1.0, v3=0.0;
+
+void ExecSignalChain();
 
 int main(void)
 {
     pc.format(8,SerialBLK::None,1);
     pc.baud(115200);
-    leds = 9;
 
-    ua0.SvMessage("SvTest_Serial_HL"); // Meldung zum PC senden
+    ua0.SvMessage("FuncGenMain"); // Meldung zum PC senden
 
-    int16_t sv1=0, sv2=100;
+
     Timer stw;
     stw.start();
     while(1) {
         CommandHandler();
-        if( ua0.acqON && (stw.read_ms()>100) ) { // 10Hz
+
+        if( ua0.acqON && (stw.read_ms()>10) ) { // 100Hz
             // dieser Teil wird mit 10Hz aufgerufen
             stw.reset();
-            sv1++;
-            sv2++;
+            ExecSignalChain();
             if( ua0.acqON ) {
-                // nur wenn vom PC aus das Senden eingeschaltet wurde
-                // wird auch etwas gesendet
-                ua0.WriteSvI16(1, sv1);
-                ua0.WriteSvI16(2, sv2);
+
+                ua0.WriteSV(1, v1);
+                ua0.WriteSV(2, v2);
+                ua0.WriteSV(3, v3);
             }
         }
     }
     return 1;
 }
 
+void ExecSignalChain()
+{
+    fg1.CalcOneStep();
+    v1 = fg1.val*ampl; 
+    
+    // 2ten Generator rechnen  
+    fg2.CalcOneStep();
+    v2 = fg2.val*ampl2; 
+    
+    fg3.CalcOneStep();
+    v3 = fg3.val*ampl3;
+}
+
 void CommandHandler()
 {
     uint8_t cmd;
-    int16_t idata1, idata2;
 
-    // Fragen ob überhaupt etwas im RX-Reg steht
     if( !pc.IsDataAvail() )
         return;
-
-    // wenn etwas im RX-Reg steht
-    // Kommando lesen
     cmd = ua0.GetCommand();
 
-    if( cmd==2 ) {
-        // cmd2 hat 2 int16 Parameter
-        idata1 = ua0.ReadI16();
-        idata2 = ua0.ReadI16();
-        // für die Analyse den Wert einfach nur zum PC zurücksenden
-        ua0.SvPrintf("Command2 %d %d", idata1, idata2);
+    if(cmd==2)  // Set Frequ
+    {
+        fg1.SetFrequ(ua0.ReadF());
+        fg2.SetFrequ(ua0.ReadF());
+        fg3.SetFrequ(ua0.ReadF());
+        ua0.SvMessage("SetFrequ");
     }
+
+    if(cmd==3)  // Set Ampl
+    {
+        ampl = ua0.ReadF();
+        ampl2 = ua0.ReadF();
+        ampl3 = ua0.ReadF();
+        ua0.SvMessage("SetAmpl");
+    }
+    
+    // Amplitude und Frequenz auch für 2ten Generator setzen
 }
 
 
diff -r e88b745f2ca2 -r 6de5bcffd991 Serial_HL.lib
--- a/Serial_HL.lib	Fri Oct 09 07:58:26 2015 +0000
+++ b/Serial_HL.lib	Thu Apr 06 13:17:39 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/hollegha2/code/Serial_HL/#b958bdf108cf
+http://mbed.org/users/hollegha2/code/Serial_HL/#1a03b6d5226f
diff -r e88b745f2ca2 -r 6de5bcffd991 mbed.bld
--- a/mbed.bld	Fri Oct 09 07:58:26 2015 +0000
+++ b/mbed.bld	Thu Apr 06 13:17:39 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/6473597d706e
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/e1686b8d5b90
\ No newline at end of file