#ifndef I2CMASTER_H
#define I2CMASTER_H

class Print;

class I2CMaster {
  public:
    explicit I2CMaster () noexcept;
    bool Send    (const unsigned char addr, const unsigned char * data, const unsigned len);
    bool Receive (const unsigned char addr,       unsigned char * data, const unsigned len);
    
    void FindActive (Print & print);
  protected:
    bool stop_condition ();
    bool send_address   (const unsigned char addr);
    
    template<typename F> bool wait_for (F f, const unsigned timeout = 10000) {
      volatile unsigned counter = 0u;
      while (f()) {
        counter += 1u;
        if (counter > timeout) return false;
      }
      return true;
    }
    
};

#endif // I2CMASTER_H