libraries / Mitov / Mitov_BasicGenerator.hon commit Added link to project report (97a3ba0)
   1////////////////////////////////////////////////////////////////////////////////
   2//                                                                            //
   3//     This software is supplied under the terms of a license agreement or    //
   4//     nondisclosure agreement with Mitov Software and may not be copied      //
   5//     or disclosed except in accordance with the terms of that agreement.    //
   6//         Copyright(c) 2002-2016 Mitov Software. All Rights Reserved.        //
   7//                                                                            //
   8////////////////////////////////////////////////////////////////////////////////
   9
  10#ifndef _MITOV_BASIC_GENERATOR_h
  11#define _MITOV_BASIC_GENERATOR_h
  12
  13#include <Mitov.h>
  14
  15namespace Mitov
  16{
  17        template<typename T> class BasicGenerator : public Mitov::CommonEnableSource, public Mitov::ClockingSupport
  18        {
  19                typedef Mitov::CommonSource inherited;
  20
  21        public: // Needs to be public due to compiler bug :-(
  22                T       FValue = T( 0 );
  23
  24        protected:
  25                inline void SendOutput() // Needed to be due to compiler bug :-(
  26                {
  27                         OutputPin.Notify( &FValue );
  28                }
  29
  30        };
  31//---------------------------------------------------------------------------
  32        template<typename T> class BasicFrequencyGenerator : public Mitov::BasicGenerator<T>
  33        {
  34                typedef Mitov::BasicGenerator<T> inherited;
  35
  36        public:
  37                float   Frequency = 1.0f;
  38                T       Amplitude;
  39                T       Offset;
  40
  41                // 0 - 1
  42                float   Phase = 0.0f;
  43
  44        public:
  45                void SetFrequency( float AValue )
  46                {
  47                        if( Frequency == AValue )
  48                                return;
  49
  50                        Frequency = AValue;
  51                        inherited::CalculateFields();
  52                }
  53
  54                void SetPhase( float AValue )
  55                {
  56                        if( Phase == AValue )
  57                                return;
  58
  59                        Phase = AValue;
  60                        FPhase = Phase;
  61                }
  62
  63        protected:
  64                float   FPhase = 0.0f;
  65                unsigned long FLastTime = 0;
  66                
  67        protected:
  68                virtual void SystemStart()
  69                {
  70                        FPhase = Phase;
  71
  72                        inherited::SystemStart();
  73                }
  74
  75                virtual void SystemLoopBegin( unsigned long currentMicros )
  76                {
  77                        if( ! inherited::ClockInputPin.IsConnected() )
  78                                Generate( currentMicros );
  79
  80                        inherited::SystemLoopBegin( currentMicros );
  81                }
  82
  83                void Generate( unsigned long currentMicros )
  84                {
  85                        if( inherited::Enabled )
  86                        {
  87                                float APeriod = 1000000 / Frequency;
  88
  89                                float ATime = ( currentMicros - FLastTime );
  90                                ATime /= APeriod;
  91                                FPhase += ATime;
  92                                FPhase = V_FMOD( FPhase, 1 );
  93
  94                                CalculateValue();
  95                        }
  96
  97                        FLastTime = currentMicros;
  98                        inherited::SendOutput();
  99                }
 100
 101        protected:
 102                virtual void CalculateValue() = 0;
 103
 104                virtual void DoClockReceive( void *_Data )
 105                {
 106                        Generate( micros() );
 107                }
 108
 109        public:
 110                BasicFrequencyGenerator( T AAmplitude, T AOffset ) :
 111                        Amplitude( AAmplitude ),
 112                        Offset( AOffset )
 113                {
 114                }
 115
 116        };
 117//---------------------------------------------------------------------------
 118        template<typename T> class AsymmetricGenerator : public Mitov::BasicFrequencyGenerator<T>
 119        {
 120                typedef Mitov::BasicFrequencyGenerator<T> inherited;
 121
 122        public:
 123                // -1 - 1
 124                float   Asymmetry = 0.0f;
 125
 126        public:
 127                using inherited::inherited;
 128
 129        };
 130}
 131
 132#endif