//////////////////////////////////////////////////////////////////////////////// // // // This software is supplied under the terms of a license agreement or // // nondisclosure agreement with Mitov Software and may not be copied // // or disclosed except in accordance with the terms of that agreement. // // Copyright(c) 2002-2016 Mitov Software. All Rights Reserved. // // // //////////////////////////////////////////////////////////////////////////////// #ifndef _MITOV_MATH_h #define _MITOV_MATH_h #include namespace Mitov { #define Min Min #define Max Max //--------------------------------------------------------------------------- #define MITOV_PI 3.14159265359 //--------------------------------------------------------------------------- template class CommonValueMathFilter : public CommonTypedFilter { typedef CommonTypedFilter inherited; public: T Value; public: CommonValueMathFilter( T AValue = 0 ) : Value( AValue ) {} }; //--------------------------------------------------------------------------- template class AddValue : public CommonValueMathFilter { typedef CommonValueMathFilter inherited; protected: virtual T FilterValue( T AValue ) { return AValue + inherited::Value; } }; //--------------------------------------------------------------------------- template class SubtractValue : public CommonValueMathFilter { typedef CommonValueMathFilter inherited; protected: virtual T FilterValue( T AValue ) { return AValue - inherited::Value; } }; //--------------------------------------------------------------------------- template class SubtractFromValue : public CommonValueMathFilter { typedef CommonValueMathFilter inherited; protected: virtual T FilterValue( T AValue ) { return inherited::Value - AValue; } }; //--------------------------------------------------------------------------- template class MultiplyByValue : public CommonValueMathFilter { typedef CommonValueMathFilter inherited; protected: virtual T FilterValue( T AValue ) { return AValue * inherited::Value; } public: MultiplyByValue() : inherited( 1 ) { } }; //--------------------------------------------------------------------------- template class DivideByValue : public CommonValueMathFilter { typedef CommonValueMathFilter inherited; protected: virtual T FilterValue( T AValue ) { return AValue / inherited::Value; } public: DivideByValue() : inherited( 1 ) { } }; //--------------------------------------------------------------------------- template class DivideValue : public CommonValueMathFilter { typedef CommonValueMathFilter inherited; protected: virtual T FilterValue( T AValue ) { return inherited::Value / AValue; } }; //--------------------------------------------------------------------------- template class Inverse : public CommonTypedFilter { typedef CommonTypedFilter inherited; protected: virtual T FilterValue( T AValue ) { return -AValue; } }; //--------------------------------------------------------------------------- template class Abs : public CommonTypedFilter { typedef CommonTypedFilter inherited; protected: virtual T FilterValue( T AValue ) { return abs( AValue ); } }; //--------------------------------------------------------------------------- class MinLimit : public CommonValueMathFilter { protected: virtual float FilterValue( float AValue ) { return ( AValue < Value ) ? Value : AValue; } }; //--------------------------------------------------------------------------- class MaxLimit : public CommonValueMathFilter { typedef CommonValueMathFilter inherited; protected: virtual float FilterValue( float AValue ) { return ( AValue > Value ) ? Value : AValue; } public: MaxLimit() : inherited( 1.0 ) { } }; //--------------------------------------------------------------------------- class Limit : public CommonTypedFilter { public: float Min = 0.0; float Max = 1.0; protected: virtual float FilterValue( float AValue ) { return ( AValue > Max ) ? Max : ( AValue < Min ) ? Min : AValue; } }; //--------------------------------------------------------------------------- enum CompareValueType { ctEqual, ctNotEqual, ctBigger, ctSmaller, ctBiggerOrEqual, ctSmallerOrEqual }; //--------------------------------------------------------------------------- template class CompareValue : public CommonEnableFilter { typedef CommonEnableFilter inherited; public: T Value; protected: T FCurrentValue; public: CompareValueType CompareType : 3; bool OnlyChanged : 1; protected: bool FOutputValue : 1; bool FStarted : 1; bool FProcessedOnce : 1; public: void SetValue( T AValue ) { if( OnlyChanged ) if( Value == AValue ) return; Value = AValue; ProcessOutput(); } protected: void ProcessOutput() { if( ! inherited::Enabled ) return; // Serial.print( FCurrentValue ); Serial.print( " ? " ); Serial.println( Value ); bool ABoolValue; switch( CompareType ) { case ctEqual: ABoolValue = ( FCurrentValue == Value ); break; case ctNotEqual: ABoolValue = ( FCurrentValue != Value ); break; case ctBigger: ABoolValue = ( FCurrentValue > Value ); break; case ctSmaller: ABoolValue = ( FCurrentValue < Value ); break; case ctBiggerOrEqual: ABoolValue = ( FCurrentValue >= Value ); break; case ctSmallerOrEqual: ABoolValue = ( FCurrentValue <= Value ); break; } if( ! OnlyChanged ) if( FOutputValue ) { FOutputValue = false; OutputPin.SendValue( false ); } if( ( !FStarted ) || ( FOutputValue != ABoolValue ) ) { OutputPin.Notify( &ABoolValue ); FOutputValue = ABoolValue; FStarted = true; } } protected: virtual void DoReceive( void *_Data ) override { T AValue = *(T*)_Data; if( OnlyChanged ) if( FProcessedOnce ) if( AValue == FCurrentValue ) return; FCurrentValue = AValue; FProcessedOnce = true; ProcessOutput(); } public: CompareValue( T AInitialValue ) : Value( AInitialValue ), FCurrentValue( AInitialValue ), CompareType( ctEqual ), OnlyChanged( true ), FProcessedOnce( false ), FStarted( false ), FOutputValue( false ) { } }; //--------------------------------------------------------------------------- class CompareStringValue : public CompareValue { typedef CompareValue inherited; protected: virtual void DoReceive( void *_Data ) override { String AValue = String( (char*)_Data ); if( OnlyChanged ) if( FProcessedOnce ) if( AValue == FCurrentValue ) return; FCurrentValue = AValue; FProcessedOnce = true; ProcessOutput(); } public: CompareStringValue() : inherited( String() ) { } }; //--------------------------------------------------------------------------- class AveragePeriod : public CommonFilter { public: unsigned long Period = 1000; protected: float FSum = 0.0f; unsigned long FCount = 0; unsigned long FLastTime = 0; protected: virtual void DoReceive( void *_Data ) override { FSum += *(float *)_Data; ++FCount; } virtual void SystemLoopBegin( unsigned long currentMicros ) override { if( currentMicros - FLastTime < Period ) return; if( ! FCount ) return; float AValue = FSum / FCount; FSum = 0.0; FCount = 0; FLastTime = currentMicros; OutputPin.Notify( &AValue ); } }; //--------------------------------------------------------------------------- template class CommonMathMultiInput : public CommonClockedMultiInput { typedef CommonClockedMultiInput inherited; public: bool Enabled = true; }; //--------------------------------------------------------------------------- template class Add : public CommonMathMultiInput { typedef CommonMathMultiInput inherited; protected: virtual T CalculateOutput() override { T AValue = 0; for( int i = 0; i < C_NUM_INPUTS; ++i ) AValue += inherited::InputPins[ i ].Value; return AValue; } }; //--------------------------------------------------------------------------- template class Multiply : public CommonMathMultiInput { typedef CommonMathMultiInput inherited; protected: virtual T CalculateOutput() override { T AValue = 1; for( int i = 0; i < C_NUM_INPUTS; ++i ) AValue *= inherited::InputPins[ i ].Value; return AValue; } }; //--------------------------------------------------------------------------- class ValueRange { public: float Min = 0; float Max = 1; }; //--------------------------------------------------------------------------- class MapRange : public CommonTypedFilter { typedef CommonTypedFilter inherited; public: ValueRange InputRange; ValueRange OutputRange; protected: virtual float FilterValue( float AValue ) override { return (( AValue - InputRange.Min ) * (OutputRange.Max - OutputRange.Min) / (InputRange.Max - InputRange.Min)) + OutputRange.Min; } }; //--------------------------------------------------------------------------- template class RaiseToPower : public CommonValueMathFilter { typedef CommonValueMathFilter inherited; protected: virtual T FilterValue( T AValue ) override { return pow( AValue, inherited::Value ); } }; //--------------------------------------------------------------------------- class Sine : public CommonTypedFilter { typedef CommonTypedFilter inherited; protected: virtual float FilterValue( float AValue ) override { return sin( AValue ); } }; //--------------------------------------------------------------------------- class Cosine : public CommonTypedFilter { typedef CommonTypedFilter inherited; protected: virtual float FilterValue( float AValue ) override { return cos( AValue ); } }; //--------------------------------------------------------------------------- class RadToDegrees : public CommonTypedFilter { typedef CommonTypedFilter inherited; protected: virtual float FilterValue( float AValue ) override { return AValue * 180 / MITOV_PI; } }; //--------------------------------------------------------------------------- class DegreesToRad : public CommonTypedFilter { typedef CommonTypedFilter inherited; protected: virtual float FilterValue( float AValue ) override { return AValue * MITOV_PI / 180; } }; //--------------------------------------------------------------------------- class AndUnsignedValue : public CommonValueMathFilter { typedef CommonValueMathFilter inherited; protected: virtual uint32_t FilterValue( uint32_t AValue ) override { return AValue & Value; } }; //--------------------------------------------------------------------------- class OrUnsignedValue : public CommonValueMathFilter { typedef CommonValueMathFilter inherited; protected: virtual uint32_t FilterValue( uint32_t AValue ) override { return AValue | Value; } }; //--------------------------------------------------------------------------- class XorUnsignedValue : public CommonValueMathFilter { typedef CommonValueMathFilter inherited; protected: virtual uint32_t FilterValue( uint32_t AValue ) override { return AValue ^ Value; } }; //--------------------------------------------------------------------------- class NotUnsignedValue : public CommonTypedFilter { typedef CommonTypedFilter inherited; protected: virtual uint32_t FilterValue( uint32_t AValue ) override { return ~AValue; } }; //--------------------------------------------------------------------------- #undef Min #undef Max } #endif