Skip to content

File evamExponentialSmoothDecor.h

File List > src > evamExponentialSmoothDecor.h

Go to the documentation of this file

#pragma once

#include <evafExponentialAverage.h>
#include "evaStdReaders.h"

namespace evam
{
    struct ExponentialSmoothConfig
    {
        unsigned short alpha;

        ExponentialSmoothConfig(unsigned short alpha) : alpha(alpha) {}
    };

    template <class TMotor, unsigned short tAlpha = 200>
    class ExponentialSmoothDecor
        : public TMotor
    {
    private:
        using Filter = evaf::ExponentialAverage<eva::ValueReader, tAlpha>;

        Filter mFilter;

    public:
        ExponentialSmoothDecor() : TMotor() {}

        template <typename... Args>
        ExponentialSmoothDecor(ExponentialSmoothConfig config, Args... args)
            : TMotor(args...),
              mFilter(config.alpha) {}

        void Go(signed short value)
        {
            mFilter.setValue(constrain(value, -1000, 1000));
            TMotor::Go(mFilter.getValue());
        }

        void SetAlpha(unsigned short alpha)
        {
            mFilter.setAlpha(alpha);
        }

        unsigned short GetAlpha() const
        {
            return mFilter.getAlpha();
        }
    };
}