ESPHome  2022.9.1
i2c.cpp
Go to the documentation of this file.
1 #include "i2c.h"
2 #include "esphome/core/log.h"
3 #include <memory>
4 
5 namespace esphome {
6 namespace i2c {
7 
8 static const char *const TAG = "i2c";
9 
10 bool I2CDevice::write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len) {
11  // we have to copy in order to be able to change byte order
12  std::unique_ptr<uint16_t[]> temp{new uint16_t[len]};
13  for (size_t i = 0; i < len; i++)
14  temp[i] = htoi2cs(data[i]);
15  return write_register(a_register, reinterpret_cast<const uint8_t *>(temp.get()), len * 2) == ERROR_OK;
16 }
17 
19  this->parent_->write_register(this->register_, &value, 1);
20  return *this;
21 }
22 I2CRegister &I2CRegister::operator&=(uint8_t value) {
23  value &= get();
24  this->parent_->write_register(this->register_, &value, 1);
25  return *this;
26 }
28  value |= get();
29  this->parent_->write_register(this->register_, &value, 1);
30  return *this;
31 }
32 
33 uint8_t I2CRegister::get() const {
34  uint8_t value = 0x00;
35  this->parent_->read_register(this->register_, &value, 1);
36  return value;
37 }
38 
39 } // namespace i2c
40 } // namespace esphome
uint8_t get() const
Definition: i2c.cpp:33
bool write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len)
Definition: i2c.cpp:10
uint16_t htoi2cs(uint16_t hostshort)
Definition: i2c.h:37
I2CRegister & operator|=(uint8_t value)
Definition: i2c.cpp:27
std::string size_t len
Definition: helpers.h:281
Definition: a4988.cpp:4
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop=true)
Definition: i2c.h:57
I2CRegister & operator &=(uint8_t value)
I2CRegister & operator=(uint8_t value)
Definition: i2c.cpp:18