Files
esphome-dev/esphome/components/i2c/i2c.h
T
Otto Winter ac0d921413 ESP-IDF support and generic target platforms (#2303)
* Socket refactor and SSL

* esp-idf temp

* Fixes

* Echo component and noise

* Add noise API transport support

* Updates

* ESP-IDF

* Complete

* Fixes

* Fixes

* Versions update

* New i2c APIs

* Complete i2c refactor

* SPI migration

* Revert ESP Preferences migration, too complex for now

* OTA support

* Remove echo again

* Remove ssl again

* GPIOFlags updates

* Rename esphal and ICACHE_RAM_ATTR

* Make ESP32 arduino compilable again

* Fix GPIO flags

* Complete pin registry refactor and fixes

* Fixes to make test1 compile

* Remove sdkconfig file

* Ignore sdkconfig file

* Fixes in reviewing

* Make test2 compile

* Make test4 compile

* Make test5 compile

* Run clang-format

* Fix lint errors

* Use esp-idf APIs instead of btStart

* Another round of fixes

* Start implementing ESP8266

* Make test3 compile

* Guard esp8266 code

* Lint

* Reformat

* Fixes

* Fixes v2

* more fixes

* ESP-IDF tidy target

* Convert ARDUINO_ARCH_ESPxx

* Update WiFiSignalSensor

* Update time ifdefs

* OTA needs millis from hal

* RestartSwitch needs delay from hal

* ESP-IDF Uart

* Fix OTA blank password

* Allow setting sdkconfig

* Fix idf partitions and allow setting sdkconfig from yaml

* Re-add read/write compat APIs and fix esp8266 uart

* Fix esp8266 store log strings in flash

* Fix ESP32 arduino preferences not initialized

* Update ifdefs

* Change how sdkconfig change is detected

* Add checks to ci-custom and fix them

* Run clang-format

* Add esp-idf clang-tidy target and fix errors

* Fixes from clang-tidy idf round 2

* Fixes from compiling tests with esp-idf

* Run clang-format

* Switch test5.yaml to esp-idf

* Implement ESP8266 Preferences

* Lint

* Re-do PIO package version selection a bit

* Fix arduinoespressif32 package version

* Fix unit tests

* Lint

* Lint fixes

* Fix readv/writev not defined

* Fix graphing component

* Re-add all old options from core/config.py

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
2021-09-20 11:47:51 +02:00

137 lines
4.0 KiB
C++

#pragma once
#include "i2c_bus.h"
#include "esphome/core/optional.h"
#include <array>
#include <vector>
namespace esphome {
namespace i2c {
#define LOG_I2C_DEVICE(this) ESP_LOGCONFIG(TAG, " Address: 0x%02X", this->address_);
class I2CDevice;
class I2CRegister {
public:
I2CRegister(I2CDevice *parent, uint8_t a_register) : parent_(parent), register_(a_register) {}
I2CRegister &operator=(uint8_t value);
I2CRegister &operator&=(uint8_t value);
I2CRegister &operator|=(uint8_t value);
explicit operator uint8_t() const { return get(); }
uint8_t get() const;
protected:
I2CDevice *parent_;
uint8_t register_;
};
// like ntohs/htons but without including networking headers.
// ("i2c" byte order is big-endian)
inline uint16_t i2ctohs(uint16_t i2cshort) {
union {
uint16_t x;
uint8_t y[2];
} conv;
conv.x = i2cshort;
return ((uint16_t) conv.y[0] << 8) | ((uint16_t) conv.y[1] << 0);
}
inline uint16_t htoi2cs(uint16_t hostshort) { return i2ctohs(hostshort); }
class I2CDevice {
public:
I2CDevice() = default;
void set_i2c_address(uint8_t address) { address_ = address; }
void set_i2c_bus(I2CBus *bus) { bus_ = bus; }
I2CRegister reg(uint8_t a_register) { return {this, a_register}; }
ErrorCode read(uint8_t *data, size_t len) { return bus_->read(address_, data, len); }
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len) {
ErrorCode err = this->write(&a_register, 1);
if (err != ERROR_OK)
return err;
return this->read(data, len);
}
ErrorCode write(const uint8_t *data, uint8_t len) { return bus_->write(address_, data, len); }
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) {
WriteBuffer buffers[2];
buffers[0].data = &a_register;
buffers[0].len = 1;
buffers[1].data = data;
buffers[1].len = len;
return bus_->writev(address_, buffers, 2);
}
// Compat APIs
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len) {
return read_register(a_register, data, len) == ERROR_OK;
}
bool read_bytes_raw(uint8_t *data, uint8_t len) { return read(data, len) == ERROR_OK; }
template<size_t N> optional<std::array<uint8_t, N>> read_bytes(uint8_t a_register) {
std::array<uint8_t, N> res;
if (!this->read_bytes(a_register, res.data(), N)) {
return {};
}
return res;
}
template<size_t N> optional<std::array<uint8_t, N>> read_bytes_raw() {
std::array<uint8_t, N> res;
if (!this->read_bytes_raw(res.data(), N)) {
return {};
}
return res;
}
bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len) {
if (read_register(a_register, reinterpret_cast<uint8_t *>(data), len * 2) != ERROR_OK)
return false;
for (size_t i = 0; i < len; i++)
data[i] = i2ctohs(data[i]);
return true;
}
bool read_byte(uint8_t a_register, uint8_t *data) { return read_register(a_register, data, 1) == ERROR_OK; }
optional<uint8_t> read_byte(uint8_t a_register) {
uint8_t data;
if (!this->read_byte(a_register, &data))
return {};
return data;
}
bool read_byte_16(uint8_t a_register, uint16_t *data) { return read_bytes_16(a_register, data, 1); }
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) {
return write_register(a_register, data, len) == ERROR_OK;
}
bool write_bytes(uint8_t a_register, const std::vector<uint8_t> &data) {
return write_bytes(a_register, data.data(), data.size());
}
template<size_t N> bool write_bytes(uint8_t a_register, const std::array<uint8_t, N> &data) {
return write_bytes(a_register, data.data(), data.size());
}
bool write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len);
bool write_byte(uint8_t a_register, uint8_t data) { return write_bytes(a_register, &data, 1); }
bool write_byte_16(uint8_t a_register, uint16_t data) { return write_bytes_16(a_register, &data, 1); }
protected:
uint8_t address_{0x00};
I2CBus *bus_{nullptr};
};
} // namespace i2c
} // namespace esphome