Skip to content

File I2cHealth.h

File List > Firmware > Libraries > PanelGroup > Helpers > I2cHealth > I2cHealth.h

Go to the documentation of this file


#pragma once
#ifdef ARDUINO_ARCH_STM32

#include <Arduino.h>

namespace OpenSkyhawk {

class I2cHealth {
public:
    bool i2cHealthy() const { return _i2cHealthy; }

#ifdef DRUMDISPLAY_TEST
    void debugExpireBackoff() { _i2cLastAttempt = millis() - I2C_RETRY_MS - 1; }
#endif

protected:
    virtual bool i2cProbe() = 0;

    bool i2cReachable() {
        const uint32_t now = millis();
        if (!_i2cHealthy && (now - _i2cLastAttempt) < I2C_RETRY_MS) return false;  // tripped → back off
        _i2cLastAttempt = now;
        return (_i2cHealthy = i2cProbe());
    }

    static constexpr uint32_t I2C_RETRY_MS = 2000;

    ~I2cHealth() = default;  // protected, non-virtual: a mixin, never deleted through this type

private:
    bool     _i2cHealthy     = true;
    uint32_t _i2cLastAttempt = 0;
};

}  // namespace OpenSkyhawk

#endif  // ARDUINO_ARCH_STM32