12constexpr uint8_t IC0_ADDR = 0x09;
13constexpr uint8_t IC1_ADDR = 0x0a;
14constexpr uint8_t IC2_ADDR = 0x0b;
15constexpr uint8_t IC3_ADDR = 0x0c;
16constexpr uint8_t PRODUCT_ID_ADDR = 0x2f;
17constexpr uint8_t MMC5983_PRODUCT_ID = 0x30;
19float convert_data_to_millitesla(uint8_t data_17_10, uint8_t data_9_2, uint8_t data_1_0) {
20 int32_t counts = (data_17_10 << 10) | (data_9_2 << 2) | data_1_0;
24 return counts / 163840.0f;
33 const uint8_t ic0_value = 0b10000;
36 ESP_LOGW(TAG,
"Writing Internal Control 0 failed with i2c error %d", err);
44 ESP_LOGW(TAG,
"Reading data failed with i2c error %d", err);
58 PUBLISH_STATE(this->x_sensor_, convert_data_to_millitesla(data[0], data[1], (data[6] & 0b11000000) >> 6));
59 PUBLISH_STATE(this->y_sensor_, convert_data_to_millitesla(data[2], data[3], (data[6] & 0b00110000) >> 4));
60 PUBLISH_STATE(this->z_sensor_, convert_data_to_millitesla(data[4], data[5], (data[6] & 0b00001100) >> 2));
63void MMC5983Component::setup() {
64 if (!this->
check_chip_id(
this, PRODUCT_ID_ADDR, MMC5983_PRODUCT_ID))
69 const uint8_t zero = 0;
72 ESP_LOGE(TAG,
"Initializing Internal Control 0 failed with i2c error %d", err);
79 ESP_LOGE(TAG,
"Initializing Internal Control 1 failed with i2c error %d", err);
86 ESP_LOGE(TAG,
"Initializing Internal Control 2 failed with i2c error %d", err);
93 ESP_LOGE(TAG,
"Initializing Internal Control 3 failed with i2c error %d", err);
101 const uint8_t ic2_value = 0b00001101;
104 ESP_LOGE(TAG,
"Writing Internal Control 2 failed with i2c error %d", err);
110void MMC5983Component::dump_config() {
111 ESP_LOGD(TAG,
"MMC5983:");
112 LOG_I2C_DEVICE(
this);
113 LOG_SENSOR(
" ",
"X", this->x_sensor_);
114 LOG_SENSOR(
" ",
"Y", this->y_sensor_);
115 LOG_SENSOR(
" ",
"Z", this->z_sensor_);
bool check_chip_id(Component *component, uint8_t a_register, uint8_t expected, uint8_t mask=0xFF)
Read the 8-bit ID at a_register and accept it when it matches expected in the bits set in mask.