ESPHome  2022.6.3
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:96
I2CRegister reg(uint8_t a_register)
Definition: i2c.h:46
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)
Definition: i2c.h:48
ErrorCode write(const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:56
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:123
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:111
Definition: a4988.cpp:4
uint8_t read_register(uint8_t reg) override
Definition: as3935_i2c.cpp:26