/*=========================================================================

  Program:   Image Guided Surgery Software Toolkit
  Module:    $RCSfile: HelloWorld.cxx,v $
  Language:  C++
  Date:      $Date: 2009-08-31 20:35:51 $
  Version:   $Revision: 1.25 $

  Copyright (c) ISC  Insight Software Consortium.  All rights reserved.
  See IGSTKCopyright.txt or http://www.igstk.org/copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#if defined(_MSC_VER)
//Warning about: identifier was truncated to '255' characters 
//in the debug information (MVC6.0 Debug)
#pragma warning( disable : 4284 )
#endif

#include "igstkPulseGenerator.h"

class PulseObserver : public ::itk::Command 
{
public:
  typedef  PulseObserver              Self;
  typedef  ::itk::Command             Superclass;
  typedef  ::itk::SmartPointer<Self>  Pointer;
  
  itkNewMacro( Self );
  
  typedef ::igstk::PulseGenerator  PulseGeneratorType;
  typedef std::map< std::string, std::vector < double > >
      TrackerToolTransformContainerType;
  

protected:
 
  PulseObserver() 
    {
    m_PulseCounter = 0;    
    }
  
public:

  void Execute(const itk::Object *caller, const itk::EventObject & event)
    {
    std::cerr << "Execute( const * ) should not be called" << std::endl;
    }
      
  void Execute(itk::Object *caller, const itk::EventObject & event)
    { 
    PulseGeneratorType * generator = 
        dynamic_cast< PulseGeneratorType * >( caller );
      
    if( ::igstk::PulseEvent().CheckEvent( &event ) )
      {
      m_PulseCounter++;
      std::cout << m_PulseCounter << "\n";
      }
    }

private:
  unsigned long       m_PulseCounter;  
};


int main(int , char** )
{ 
  igstk::RealTimeClock::Initialize();

  typedef igstk::PulseGenerator  PulseGeneratorType;    
  PulseGeneratorType::Pointer pulseGenerator = PulseGeneratorType::New();

  typedef PulseObserver  ObserverType;  
  ObserverType::Pointer observer = ObserverType::New();

  pulseGenerator->AddObserver( igstk::PulseEvent(), observer );

  pulseGenerator->RequestSetFrequency( 100 );  // 100 Hz
  pulseGenerator->RequestStart();  


  unsigned int refreshRate = 30;

try

  {

    while( 1 )

    {

      igstk::PulseGenerator::Sleep( unsigned int( 1000 / refreshRate ) );

      igstk::PulseGenerator::CheckTimeouts();

    }

  }

  catch( std::exception &e )

  {

    std::cerr<<e.what()<<"\n";

    return EXIT_FAILURE;

  }
}
