Function generator

Dependencies:   Serial_HL mbed

FuncGen.cpp

Committer:
hollegha2
Date:
2016-03-17
Revision:
2:100244cef3d6
Child:
3:c8c3b755ef54

File content as of revision 2:100244cef3d6:


#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;
}