libraries / Mitov / Mitov_RampToValue.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_RAMP_TO_VALUE_h
  11#define _MITOV_RAMP_TO_VALUE_h
  12
  13#include <Mitov.h>
  14
  15namespace Mitov
  16{
  17        class RampToValue : public Mitov::CommonEnableFilter, public Mitov::ClockingSupport
  18        {
  19                typedef Mitov::CommonEnableFilter inherited;
  20
  21        public:
  22                float   InitialValue = 0.0f;
  23                float   Slope = 1.0f;
  24
  25        protected:
  26                unsigned long   FLastTime = 0;
  27                float                   FCurrentValue = 0.0f;
  28                float                   FTargetValue = 0.0f;
  29
  30        protected:
  31                virtual void DoReceive( void *_Data )
  32                {
  33                        bool AValue = *(float *)_Data;
  34                        if( FTargetValue == AValue )
  35                                return;
  36
  37                        FTargetValue = AValue;
  38//                      FStartTime = micros();
  39                }
  40
  41                virtual void DoClockReceive( void *_Data ) override
  42                {
  43                        Generate( micros(), true );
  44                }
  45
  46                void Generate( unsigned long currentMicros, bool FromClock )
  47                {
  48                        if( FCurrentValue != FTargetValue )
  49                        {
  50                                if( inherited::Enabled )
  51                                {
  52                                        float ARamp = abs( ( currentMicros - FLastTime ) * Slope / 1000000 );
  53                                        if( FCurrentValue < FTargetValue )
  54                                        {
  55                                                FCurrentValue += ARamp;
  56                                                if( FCurrentValue > FTargetValue )
  57                                                        FCurrentValue = FTargetValue;
  58
  59                                        }
  60                                        else
  61                                        {
  62                                                FCurrentValue -= ARamp;
  63                                                if( FCurrentValue < FTargetValue )
  64                                                        FCurrentValue = FTargetValue;
  65
  66                                        }
  67
  68                                        inherited::OutputPin.Notify( &FCurrentValue );
  69                                }
  70                        }
  71
  72                        else if( FromClock )
  73                                inherited::OutputPin.Notify( &FCurrentValue );
  74
  75                        FLastTime = currentMicros;
  76//                      inherited::SendOutput();
  77                }
  78
  79        protected:
  80                virtual void SystemStart() override
  81                {
  82                        FCurrentValue = InitialValue;
  83                        FTargetValue = InitialValue;
  84
  85                        inherited::OutputPin.Notify( &FCurrentValue );
  86                        inherited::SystemStart();
  87                }
  88
  89                virtual void SystemLoopBegin( unsigned long currentMicros ) override
  90                {
  91                        if( ! ClockInputPin.IsConnected() )
  92                                Generate( currentMicros, false );
  93
  94                        inherited::SystemLoopBegin( currentMicros );
  95                }
  96
  97        };
  98}
  99
 100#endif