ESPHome  2024.10.2
as3935_i2c.cpp
Go to the documentation of this file.
1 #include "as3935_i2c.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace as3935_i2c {
6 
7 static const char *const TAG = "as3935_i2c";
8 
9 void I2CAS3935Component::write_register(uint8_t reg, uint8_t mask, uint8_t bits, uint8_t start_pos) {
10  uint8_t write_reg;
11  if (!this->read_byte(reg, &write_reg)) {
12  this->mark_failed();
13  ESP_LOGW(TAG, "read_byte failed - increase log level for more details!");
14  return;
15  }
16 
17  write_reg &= (~mask);
18  write_reg |= (bits << start_pos);
19 
20  if (!this->write_byte(reg, write_reg)) {
21  ESP_LOGW(TAG, "write_byte failed - increase log level for more details!");
22  return;
23  }
24 }
25 
27  uint8_t value;
28  if (write(&reg, 1) != i2c::ERROR_OK) {
29  ESP_LOGW(TAG, "Writing register failed!");
30  return 0;
31  }
32  if (read(&value, 1) != i2c::ERROR_OK) {
33  ESP_LOGW(TAG, "Reading register failed!");
34  return 0;
35  }
36  return value;
37 }
39  AS3935Component::dump_config();
40  LOG_I2C_DEVICE(this);
41 }
42 
43 } // namespace as3935_i2c
44 } // namespace esphome
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition: i2c.h:235
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition: i2c.h:149
void write_register(uint8_t reg, uint8_t mask, uint8_t bits, uint8_t start_position) override
Definition: as3935_i2c.cpp:9
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition: i2c.h:160
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition: i2c.h:186
No error found during execution of method.
Definition: i2c_bus.h:13
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:262
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t read_register(uint8_t reg) override
Definition: as3935_i2c.cpp:26