//----------------------------------------------------------------------------
///
/// \file   main.cpp
///
/// \brief  main file for pinmux
///
/// \date   20220619
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


//---Documentation------------------------------------------------------------


//---Includes -----------------------------------------------------------------


//---General--------------------------

#if defined ( CATCH_V3 )
   #include <catch2/catch_test_macros.hpp>
#elif defined ( CATCH_V2 )
   #include <catch2/catch.hpp>
#elif defined ( CATCH_V1 )
   #include <catch/catch.hpp>
#else
   #error "Either 'catch' or 'catch2' has to be installed"
#endif

#include <biwak/touch.h>
#include <biwak/barrel_organ.h>

//--- Own ----------------------------

#define TEST_ALL
#define STOP_ON_FAIL

//---Implementation------------------------------------------------------------


TEST_CASE( "Barrel", "[default]" ) {

    CBarrelOrgan barrel;
    CBarrelOrgan::SSlope slopes[]=
    {
        // Fade out
        {1000, 80, 1000},
        { CBarrelOrgan::finished, 0, 0 }
    };

    CBarrelOrgan::SSlope slopes2[]=
    {
        // Fade out
        {10000, 0, 2000},
        { CBarrelOrgan::finished, 0, 0 }
    };

    CBarrelOrgan::SSlope slopes3[]=
    {
        // Fade out
        {1000, 500, 2000},
        { CBarrelOrgan::finished, 0, 0 }
    };

   SECTION( "autoForward" )
   {
      uint16_t illumination1;
      uint16_t illumination2;
      uint16_t illumination3;

      illumination1=barrel.startSlopes(slopes);
      illumination2=barrel.startSlopes(slopes, 540);

      // Just the plain start value
      REQUIRE ( illumination1==1000 );
      // in the current illumination there is already time involved; not recognizable
      REQUIRE ( illumination2 == 540 );

      // 540 is the middel of 1000<->80; need 500ms to reach it
      REQUIRE ( barrel.calculateTimeShift( slopes[0], 540 ) == -500 );
      // 770 is 1/4 of 1000<->80; need 250ms to reach it
      REQUIRE ( barrel.calculateTimeShift( slopes[0], 770 ) == -250 );
      // 310 is 3/4 of 1000<->80; need 750ms to reach it
      REQUIRE ( barrel.calculateTimeShift( slopes[0], 310 ) == -750 );

      illumination2=barrel.startSlopes(slopes2, 5000);
      illumination3=barrel.startSlopes(slopes2, 12000);
      REQUIRE ( illumination3 != illumination2 );
      REQUIRE ( illumination2 == 5000 );
      REQUIRE ( illumination3 <= 12000 );
      REQUIRE ( illumination3 >= 11900 );

      int val=msecToHrtValue(barrel.calculateTimeShift( slopes[0], 540 ));
      REQUIRE ( val == -500000 );

      // The current value is below the target value
      illumination2=barrel.startSlopes(slopes3, 100);
      REQUIRE ( illumination2 == 100 );
      //usleep(100);
      //illumination2=barrel.eventLoop();
      //REQUIRE ( illumination2 < 100 );
   }
}


//---fin-----------------------------------------------------------------------
