8 static const char *
const TAG =
"bmi160";
13 enum class Cmd : uint8_t {
122 ESP_LOGCONFIG(TAG,
"Setting up BMI160...");
124 if (!this->read_byte(BMI160_REGISTER_CHIPID, &chipid) || (chipid != 0b11010001)) {
129 ESP_LOGV(TAG,
" Bringing accelerometer out of sleep...");
134 ESP_LOGV(TAG,
" Waiting for accelerometer to wake up...");
137 this->set_timeout(10, [
this]() { this->internal_setup_(1); });
141 ESP_LOGV(TAG,
" Bringing gyroscope out of sleep...");
146 ESP_LOGV(TAG,
" Waiting for gyroscope to wake up...");
148 this->set_timeout(10, [
this]() { this->internal_setup_(2); });
152 ESP_LOGV(TAG,
" Setting up Gyro Config...");
154 ESP_LOGV(TAG,
" Output gyro_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config));
155 if (!this->write_byte(BMI160_REGISTER_GYRO_CONFIG, gyro_config)) {
159 ESP_LOGV(TAG,
" Setting up Gyro Range...");
161 ESP_LOGV(TAG,
" Output gyro_range: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_range));
162 if (!this->write_byte(BMI160_REGISTER_GYRO_RANGE, gyro_range)) {
167 ESP_LOGV(TAG,
" Setting up Accel Config...");
168 uint8_t accel_config =
170 ESP_LOGV(TAG,
" Output accel_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config));
171 if (!this->write_byte(BMI160_REGISTER_ACCEL_CONFIG, accel_config)) {
175 ESP_LOGV(TAG,
" Setting up Accel Range...");
177 ESP_LOGV(TAG,
" Output accel_range: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_range));
178 if (!this->write_byte(BMI160_REGISTER_ACCEL_RANGE, accel_range)) {
183 this->setup_complete_ =
true;
189 ESP_LOGCONFIG(TAG,
"BMI160:");
190 LOG_I2C_DEVICE(
this);
191 if (this->is_failed()) {
192 ESP_LOGE(TAG,
"Communication with BMI160 failed!");
194 LOG_UPDATE_INTERVAL(
this);
195 LOG_SENSOR(
" ",
"Acceleration X", this->accel_x_sensor_);
196 LOG_SENSOR(
" ",
"Acceleration Y", this->accel_y_sensor_);
197 LOG_SENSOR(
" ",
"Acceleration Z", this->accel_z_sensor_);
198 LOG_SENSOR(
" ",
"Gyro X", this->gyro_x_sensor_);
199 LOG_SENSOR(
" ",
"Gyro Y", this->gyro_y_sensor_);
200 LOG_SENSOR(
" ",
"Gyro Z", this->gyro_z_sensor_);
201 LOG_SENSOR(
" ",
"Temperature", this->temperature_sensor_);
205 uint8_t raw_data[len * 2];
207 i2c::ErrorCode err = this->read_register(reg, raw_data, len * 2,
true);
211 for (
int i = 0; i <
len; i++) {
212 value[i] = (int16_t) ((uint16_t) raw_data[i * 2] | ((uint16_t) raw_data[i * 2 + 1] << 8));
218 if (!this->setup_complete_) {
222 ESP_LOGV(TAG,
" Updating BMI160...");
224 if (this->read_le_int16_(BMI160_REGISTER_DATA_GYRO_X_LSB, data, 6) !=
i2c::ERROR_OK) {
225 this->status_set_warning();
229 float gyro_x = (float) data[0] / (
float) INT16_MAX * 2000.f;
230 float gyro_y = (float) data[1] / (
float) INT16_MAX * 2000.f;
231 float gyro_z = (float) data[2] / (
float) INT16_MAX * 2000.f;
232 float accel_x = (float) data[3] / (
float) INT16_MAX * 16 *
GRAVITY_EARTH;
233 float accel_y = (float) data[4] / (
float) INT16_MAX * 16 *
GRAVITY_EARTH;
234 float accel_z = (float) data[5] / (
float) INT16_MAX * 16 *
GRAVITY_EARTH;
236 int16_t raw_temperature;
237 if (this->read_le_int16_(BMI160_REGISTER_DATA_TEMP_LSB, &raw_temperature, 1) !=
i2c::ERROR_OK) {
238 this->status_set_warning();
241 float temperature = (float) raw_temperature / (
float) INT16_MAX * 64.5f + 23.f;
244 "Got accel={x=%.3f m/s², y=%.3f m/s², z=%.3f m/s²}, " 245 "gyro={x=%.3f °/s, y=%.3f °/s, z=%.3f °/s}, temp=%.3f°C",
246 accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z, temperature);
248 if (this->accel_x_sensor_ !=
nullptr)
249 this->accel_x_sensor_->publish_state(accel_x);
250 if (this->accel_y_sensor_ !=
nullptr)
251 this->accel_y_sensor_->publish_state(accel_y);
252 if (this->accel_z_sensor_ !=
nullptr)
253 this->accel_z_sensor_->publish_state(accel_z);
255 if (this->temperature_sensor_ !=
nullptr)
256 this->temperature_sensor_->publish_state(temperature);
258 if (this->gyro_x_sensor_ !=
nullptr)
259 this->gyro_x_sensor_->publish_state(gyro_x);
260 if (this->gyro_y_sensor_ !=
nullptr)
261 this->gyro_y_sensor_->publish_state(gyro_y);
262 if (this->gyro_z_sensor_ !=
nullptr)
263 this->gyro_z_sensor_->publish_state(gyro_z);
265 this->status_clear_warning();
const float DATA
For components that import data from directly connected sensors like DHT.
float get_setup_priority() const override
const uint8_t BMI160_REGISTER_ACCEL_CONFIG
const uint8_t BMI160_REGISTER_DATA_ACCEL_Z_MSB
const uint8_t BMI160_REGISTER_CMD
const uint8_t BMI160_REGISTER_DATA_TEMP_MSB
const uint8_t BMI160_REGISTER_CHIPID
const uint8_t BMI160_REGISTER_DATA_ACCEL_X_LSB
const uint8_t BMI160_REGISTER_DATA_GYRO_Y_LSB
const uint8_t BMI160_REGISTER_DATA_GYRO_Z_LSB
const uint8_t BMI160_REGISTER_DATA_ACCEL_Z_LSB
i2c::ErrorCode read_le_int16_(uint8_t reg, int16_t *value, uint8_t len)
reads len 16-bit little-endian integers from the given i2c register
const uint8_t BMI160_REGISTER_DATA_GYRO_Z_MSB
No error found during execution of method.
void dump_config() override
const uint8_t BMI160_REGISTER_DATA_ACCEL_X_MSB
const uint8_t BMI160_REGISTER_GYRO_CONFIG
const uint8_t BMI160_REGISTER_DATA_ACCEL_Y_LSB
const uint8_t BMI160_REGISTER_DATA_GYRO_X_MSB
const uint8_t BMI160_REGISTER_DATA_TEMP_LSB
const uint8_t BMI160_REGISTER_DATA_GYRO_Y_MSB
const uint8_t BMI160_REGISTER_DATA_ACCEL_Y_MSB
Implementation of SPI Controller mode.
void internal_setup_(int stage)
const float GRAVITY_EARTH
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
const uint8_t BMI160_REGISTER_ACCEL_RANGE
const uint8_t BMI160_REGISTER_DATA_GYRO_X_LSB
const uint8_t BMI160_REGISTER_GYRO_RANGE