2021-09-20 11:47:51 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#ifdef USE_ARDUINO
|
|
|
|
|
|
|
|
#include "i2c_bus.h"
|
|
|
|
#include "esphome/core/component.h"
|
|
|
|
#include <Wire.h>
|
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace i2c {
|
|
|
|
|
2021-10-01 12:53:37 +02:00
|
|
|
enum RecoveryCode {
|
|
|
|
RECOVERY_FAILED_SCL_LOW,
|
|
|
|
RECOVERY_FAILED_SDA_LOW,
|
|
|
|
RECOVERY_COMPLETED,
|
|
|
|
};
|
|
|
|
|
2021-09-20 11:47:51 +02:00
|
|
|
class ArduinoI2CBus : public I2CBus, public Component {
|
|
|
|
public:
|
|
|
|
void setup() override;
|
|
|
|
void dump_config() override;
|
|
|
|
ErrorCode readv(uint8_t address, ReadBuffer *buffers, size_t cnt) override;
|
|
|
|
ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt) override;
|
|
|
|
float get_setup_priority() const override { return setup_priority::BUS; }
|
|
|
|
|
|
|
|
void set_scan(bool scan) { scan_ = scan; }
|
|
|
|
void set_sda_pin(uint8_t sda_pin) { sda_pin_ = sda_pin; }
|
|
|
|
void set_scl_pin(uint8_t scl_pin) { scl_pin_ = scl_pin; }
|
|
|
|
void set_frequency(uint32_t frequency) { frequency_ = frequency; }
|
|
|
|
|
2021-09-23 20:11:40 +02:00
|
|
|
private:
|
2021-09-24 18:02:28 +02:00
|
|
|
void recover_();
|
2021-10-01 12:53:37 +02:00
|
|
|
RecoveryCode recovery_result_;
|
2021-09-23 20:11:40 +02:00
|
|
|
|
2021-09-20 11:47:51 +02:00
|
|
|
protected:
|
|
|
|
TwoWire *wire_;
|
|
|
|
bool scan_;
|
|
|
|
uint8_t sda_pin_;
|
|
|
|
uint8_t scl_pin_;
|
|
|
|
uint32_t frequency_;
|
|
|
|
bool initialized_ = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace i2c
|
|
|
|
} // namespace esphome
|
|
|
|
|
|
|
|
#endif // USE_ARDUINO
|