mirror of
https://github.com/esphome/esphome.git
synced 2024-11-22 23:18:10 +01:00
Move i2c scan to setup (#2869)
Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>
This commit is contained in:
parent
8375e1d64d
commit
b2f05faee0
5 changed files with 46 additions and 27 deletions
|
@ -1,6 +1,8 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace i2c {
|
namespace i2c {
|
||||||
|
@ -40,6 +42,20 @@ class I2CBus {
|
||||||
return writev(address, &buf, 1);
|
return writev(address, &buf, 1);
|
||||||
}
|
}
|
||||||
virtual ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt) = 0;
|
virtual ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt) = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void i2c_scan_() {
|
||||||
|
for (uint8_t address = 8; address < 120; address++) {
|
||||||
|
auto err = writev(address, nullptr, 0);
|
||||||
|
if (err == ERROR_OK) {
|
||||||
|
scan_results_.emplace_back(address, true);
|
||||||
|
} else if (err == ERROR_UNKNOWN) {
|
||||||
|
scan_results_.emplace_back(address, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::vector<std::pair<uint8_t, bool>> scan_results_;
|
||||||
|
bool scan_{false};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace i2c
|
} // namespace i2c
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include "i2c_bus_arduino.h"
|
#include "i2c_bus_arduino.h"
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
#include "esphome/core/helpers.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
@ -27,6 +28,10 @@ void ArduinoI2CBus::setup() {
|
||||||
wire_->begin(sda_pin_, scl_pin_);
|
wire_->begin(sda_pin_, scl_pin_);
|
||||||
wire_->setClock(frequency_);
|
wire_->setClock(frequency_);
|
||||||
initialized_ = true;
|
initialized_ = true;
|
||||||
|
if (this->scan_) {
|
||||||
|
ESP_LOGV(TAG, "Scanning i2c bus for active devices...");
|
||||||
|
this->i2c_scan_();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
void ArduinoI2CBus::dump_config() {
|
void ArduinoI2CBus::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG, "I2C Bus:");
|
ESP_LOGCONFIG(TAG, "I2C Bus:");
|
||||||
|
@ -45,22 +50,20 @@ void ArduinoI2CBus::dump_config() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (this->scan_) {
|
if (this->scan_) {
|
||||||
ESP_LOGI(TAG, "Scanning i2c bus for active devices...");
|
ESP_LOGI(TAG, "Results from i2c bus scan:");
|
||||||
uint8_t found = 0;
|
if (scan_results_.empty()) {
|
||||||
for (uint8_t address = 8; address < 120; address++) {
|
|
||||||
auto err = writev(address, nullptr, 0);
|
|
||||||
if (err == ERROR_OK) {
|
|
||||||
ESP_LOGI(TAG, "Found i2c device at address 0x%02X", address);
|
|
||||||
found++;
|
|
||||||
} else if (err == ERROR_UNKNOWN) {
|
|
||||||
ESP_LOGI(TAG, "Unknown error at address 0x%02X", address);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (found == 0) {
|
|
||||||
ESP_LOGI(TAG, "Found no i2c devices!");
|
ESP_LOGI(TAG, "Found no i2c devices!");
|
||||||
|
} else {
|
||||||
|
for (const auto &s : scan_results_) {
|
||||||
|
if (s.second)
|
||||||
|
ESP_LOGI(TAG, "Found i2c device at address 0x%02X", s.first);
|
||||||
|
else
|
||||||
|
ESP_LOGE(TAG, "Unknown error at address 0x%02X", s.first);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ErrorCode ArduinoI2CBus::readv(uint8_t address, ReadBuffer *buffers, size_t cnt) {
|
ErrorCode ArduinoI2CBus::readv(uint8_t address, ReadBuffer *buffers, size_t cnt) {
|
||||||
// logging is only enabled with vv level, if warnings are shown the caller
|
// logging is only enabled with vv level, if warnings are shown the caller
|
||||||
// should log them
|
// should log them
|
||||||
|
|
|
@ -34,7 +34,6 @@ class ArduinoI2CBus : public I2CBus, public Component {
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
TwoWire *wire_;
|
TwoWire *wire_;
|
||||||
bool scan_;
|
|
||||||
uint8_t sda_pin_;
|
uint8_t sda_pin_;
|
||||||
uint8_t scl_pin_;
|
uint8_t scl_pin_;
|
||||||
uint32_t frequency_;
|
uint32_t frequency_;
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include "i2c_bus_esp_idf.h"
|
#include "i2c_bus_esp_idf.h"
|
||||||
#include "esphome/core/hal.h"
|
#include "esphome/core/hal.h"
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
#include "esphome/core/helpers.h"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
|
@ -37,6 +38,10 @@ void IDFI2CBus::setup() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
initialized_ = true;
|
initialized_ = true;
|
||||||
|
if (this->scan_) {
|
||||||
|
ESP_LOGV(TAG, "Scanning i2c bus for active devices...");
|
||||||
|
this->i2c_scan_();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
void IDFI2CBus::dump_config() {
|
void IDFI2CBus::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG, "I2C Bus:");
|
ESP_LOGCONFIG(TAG, "I2C Bus:");
|
||||||
|
@ -55,23 +60,20 @@ void IDFI2CBus::dump_config() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (this->scan_) {
|
if (this->scan_) {
|
||||||
ESP_LOGI(TAG, "Scanning i2c bus for active devices...");
|
ESP_LOGI(TAG, "Results from i2c bus scan:");
|
||||||
uint8_t found = 0;
|
if (scan_results_.empty()) {
|
||||||
for (uint8_t address = 8; address < 120; address++) {
|
|
||||||
auto err = writev(address, nullptr, 0);
|
|
||||||
|
|
||||||
if (err == ERROR_OK) {
|
|
||||||
ESP_LOGI(TAG, "Found i2c device at address 0x%02X", address);
|
|
||||||
found++;
|
|
||||||
} else if (err == ERROR_UNKNOWN) {
|
|
||||||
ESP_LOGI(TAG, "Unknown error at address 0x%02X", address);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (found == 0) {
|
|
||||||
ESP_LOGI(TAG, "Found no i2c devices!");
|
ESP_LOGI(TAG, "Found no i2c devices!");
|
||||||
|
} else {
|
||||||
|
for (const auto &s : scan_results_) {
|
||||||
|
if (s.second)
|
||||||
|
ESP_LOGI(TAG, "Found i2c device at address 0x%02X", s.first);
|
||||||
|
else
|
||||||
|
ESP_LOGE(TAG, "Unknown error at address 0x%02X", s.first);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ErrorCode IDFI2CBus::readv(uint8_t address, ReadBuffer *buffers, size_t cnt) {
|
ErrorCode IDFI2CBus::readv(uint8_t address, ReadBuffer *buffers, size_t cnt) {
|
||||||
// logging is only enabled with vv level, if warnings are shown the caller
|
// logging is only enabled with vv level, if warnings are shown the caller
|
||||||
// should log them
|
// should log them
|
||||||
|
|
|
@ -36,7 +36,6 @@ class IDFI2CBus : public I2CBus, public Component {
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
i2c_port_t port_;
|
i2c_port_t port_;
|
||||||
bool scan_;
|
|
||||||
uint8_t sda_pin_;
|
uint8_t sda_pin_;
|
||||||
bool sda_pullup_enabled_;
|
bool sda_pullup_enabled_;
|
||||||
uint8_t scl_pin_;
|
uint8_t scl_pin_;
|
||||||
|
|
Loading…
Reference in a new issue