After recently getting a custom i2c component working as a “custom sensor” (see Custom Sensor Component — ESPHome) I would like to get my hand onto making it into a real ESPHome components, so others can use it as well.
The component I’m working with is a BQ27441 (as used on the battery babysitter: SparkFun Battery Babysitter - LiPo Battery Manager - PRT-13777 - SparkFun Electronics).
I’ve read the documentation on Contributing — ESPHome and got the basics working,
I now have a component that publishes some hard coded values.
The next step is to communicate with the actual sensor. I’ve been looking at the datasheet and at the sparkfun library.
The sparkfun library used the Wire library, I would have to re-write it to use the ESPHome i2c library.
The first thing the Sparkfun library does is a check if the right device is connected:
// Read the device type - should be 0x0421
uint16_t BQ27441::deviceType(void)
{
return readControlWord(BQ27441_CONTROL_DEVICE_TYPE);
}
To do this, I have to send 2 bytes:
followed by another 2(?) bytes:
So that would basically be:
0x00 0x01 followed by 0x0001
Now the problem I have is that I don’t know what functions to use from the i2c library.
Should I use write_bytes 2x first and then read_bytes to get the response?
The documentation on ESPHome: esphome::i2c::I2CDevice Class Reference has this declariation:
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());
}
What should be the value of a_register?