#ifndef BIWAK_HEARTBEAT_HPP
#define BIWAK_HEARTBEAT_HPP
//----------------------------------------------------------------------------
///
/// \brief   CANIO eval app
///
///          Bringup for CANIO board.
///
/// \date   20210829
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


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


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

#include <biwak/timer.hpp>
#include <biwak/led.hpp>
#include <lepto/log.h>
#include <lepto/lepto.h>            // IS_ENABLED( )


#if defined ( STM32 )
   #define HEARTBEAT_WARNING_DURATION_SECONDS       ( 60 * 5 )
   #define HEARTBEAT_BECKON_DURATION_SECONDS        ( 20 )
#else
   #define HEARTBEAT_WARNING_DURATION_SECONDS       ( 1 )
   #define HEARTBEAT_BECKON_DURATION_SECONDS        ( 1 )
#endif


//---Declaration--------------------------------------------------------------


#if IS_ENABLED( CONFIG_BIWAK_HEARTBEAT )
   #if ! IS_ENABLED( CONFIG_BIWAK_LOG_HEARTBEAT )
      #error   BIWAK_HEARTBEAT is enabled but BIWAK_LOG_HEARTBEAT is not. \
               This is not very usefull.
   #endif
#endif

class CHeartBeat;
extern CHeartBeat *heartBeat;

class CHeartBeat
{
   private:
      #if IS_ENABLED( CONFIG_BIWAK_HEARTBEAT_WARNING_COUNTER )
      int m_warningCounter=0;
      #endif
      lrtimer_t m_timerEvent=0;
      
      // Different blink duration for e.g. warning and beckon
      int m_warnTimeSeconds;
      CLed &m_heartBeat_led;
      ELogBlended m_lastError = ELogCategory::Good | 0;
      ELogCategory m_status=ELogCategory::Good;
      #if IS_ENABLED( CONFIG_BIWAK_HEARTBEAT_EVENT )
         //uint32_t m_event=0;
      #endif

   public:

      CHeartBeat(CLed &heartBeat);
      ~CHeartBeat();
      void eventLoop();
      #if IS_ENABLED( CONFIG_BIWAK_HEARTBEAT_WARNING_COUNTER )
      int getWarningCounter()
      {
         return( m_warningCounter );
      }
      #endif
      void updateLed();
      void logCode( ELogBlended code );
      ELogCategory getStatus()
      {
         return( m_status );
      }
      ELogBlended getLastError()
      {
         return(m_lastError);
      }
};


extern "C"
{
   #if IS_ENABLED( CONFIG_BIWAK_LOG_HEARTBEAT )
      void logHeartBeat( ELogBlended code );
   #endif
}


//---fin----------------------------------------------------------------------
#endif // ? ! BIWAK_HEARTBEAT_HPP
