//----------------------------------------------------------------------------
///
///
///
//----------------------------------------------------------------------------


#include <fosh/command.h>
#include <stdio.h>
#include <time.h>       // time.h, localtime, strftime
#include <string.h>     // strncpy
#include <sys/time.h>   // settimeofday
#include <stdlib.h>      // atoi

#ifdef STM32
   //#include <arena/platform.h>
   #include <biwak/rtc.hpp>
   #include <biwak/timer.hpp>
#endif

extern CRtc* libcRtc;

class CCommandDrift: public CCommand
{
   public:
      CCommandDrift(const char *_name)
         :CCommand( _name, "   drift: calculate difference between high resolution timers to RTC\n" )
      {}
      virtual int exec(int argc, const char *argv[]) const;
      virtual void printHelp() const;

      int setDrift(const char* dataStr) const;  // format like MMDDYY
      int getDrift() const;
};


int CCommandDrift::exec(int argc, const char *argv[]) const /* virtual */
{
   time_t steadyTime, startTime, endTime, end2Time;
   hrtimer_t startHRTimer, endHRTimer, end2HRTimer, diffHRTimer, diff2HRTimer;
   hrtimer_t startLRTimer, endLRTimer, diffLRTimer;
   int diff;

   /*
   steadyTime=time(0);
   while( (startTime=time(0)) == steadyTime )
   {;};
   startHRTimer=getHrTimerValue();
   while( (endTime=time(0)) - startTime < 1 )
   {;};
   endHRTimer=getHrTimerValue();
   */
   startHRTimer=getHrTimerValue();
   steadyTime=libcBiwakRtc->getSec();
   while( (startTime=libcBiwakRtc->getSec()) == steadyTime )
   {;};
   startHRTimer=getHrTimerValue();
   while( (endTime=libcBiwakRtc->getSec()) == startTime )
   {;};
   endHRTimer=getHrTimerValue();
   while( (end2Time=libcBiwakRtc->getSec()) == endTime )
   {;};
   end2HRTimer=getHrTimerValue();


   diffHRTimer=endHRTimer-startHRTimer;
   diff2HRTimer=end2HRTimer-endHRTimer;
   diff=USECS_PER_SEC- hrtValueToUsec(diffHRTimer);

   printf("HR:\n");
   printf("System Clock: %ld\n", SystemCoreClock);
   printf("start / stop / steady: %lld / %lld / %lld\n", startTime, endTime, steadyTime);
   printf("HR-Diff per sec: %ld\n", diffHRTimer);
   printf("HR-Diff per sec 2: %ld\n", diff2HRTimer);
   printf("Delta: %d usec per sec\n" , (int)hrtValueToUsec(diffHRTimer) );
   printf("Diff: %d usec per sec\n" , diff );
   printf("Thats %d / %d %% of 1 second\n" , (diff * 100), (int)USECS_PER_SEC );
   printf("Calib: %ld\n", SysTick->CALIB & SysTick_CALIB_TENMS_Msk);
   // Float does not work yet

   //printf("Thats %g %% of 1 second\n" , ((float)diff/(float)USECS_PER_SEC) * 100.0 );


   steadyTime=time(0);
   while( (startTime=time(0)) == steadyTime )
   {;};
   startLRTimer=getLrTimerValue();
   while( (endTime=time(0)) == startTime )
   {;};
   endLRTimer=getLrTimerValue();

   diffLRTimer=endLRTimer-startLRTimer;
   diff=MSECS_PER_SEC- lrtValueToMsec(diffLRTimer);

   printf("Delta: %d msec\n" , (int)lrtValueToMsec(diffLRTimer) );
   printf("Diff: %d msec\n" , diff );
   printf("Thats %d / %d %% of 1 second\n" , (diff * 100), (int)MSECS_PER_SEC );

   while(0)
   {
      int systickval=SysTick->VAL;
      int systick=getSystick();
      printf("%X / %X\n" , systick, systickval );
   }

   return( 0 );
}

const CCommand *createInstance_drift()
{
   return( new CCommandDrift("drift") );
}


STTERM_COMMAND(drift);


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