/*
{{copyright}} 
*/

/*
{{version}} 
*/

/*
{{license}} 
*/

#pragma once

#include <functional>

namespace sdi_uToolBox::generic
{
//--------------------------------------------------------------
class IGPIO
{
  public:
  enum class PinType
  {
    Invalid,
    Input,
    ReversedInput,
    Output,             // HIGH level to enable
    ReversedOutput,     // LOW level to enable
  };

  public:
  IGPIO()                                  = default;     // Constructor
  virtual ~IGPIO()                         = default;     // Destructor
  IGPIO(const IGPIO &other)                = delete;      // Copy constructor
  IGPIO(IGPIO &&other) noexcept            = delete;      // Move constructor
  IGPIO &operator=(const IGPIO &other)     = delete;      // Copy assignment
  IGPIO &operator=(IGPIO &&other) noexcept = delete;      // Move assignment

  virtual void set(bool enable = true) const = 0;     // Sets/Resets IO state
  void         reset() const;                         // Resets IO state
  virtual bool get() const = 0;                       // Returns IO state
  virtual void toggle() const;                        // Flips IO state

  using Callback = std::function<void()>;
  void setITCallback(const Callback &callback);     // Defines the callback function for a possible interruption

  protected:
  Callback m_itCallback = nullptr;
};
//--------------------------------------------------------------

//--------------------------------------------------------------
/* Resets IO state */
inline void IGPIO::reset() const
{
  set(false);
}
//--------------------------------------------------------------
/* Flips IO state */
inline void IGPIO::toggle() const
{
  const auto newValue = !get();
  set(newValue);
}
//--------------------------------------------------------------
/* Defines the callback function for a possible interruption */
inline void IGPIO::setITCallback(const Callback &callback)
{
  m_itCallback = callback;
}
//--------------------------------------------------------------
}     // namespace sdi_uToolBox::generic
