7 static const char *
const TAG =
"mmc5603";
8 static const uint8_t MMC5603_ADDRESS = 0x30;
9 static const uint8_t MMC56X3_PRODUCT_ID = 0x39;
11 static const uint8_t MMC56X3_DEFAULT_ADDRESS = 0x30;
12 static const uint8_t MMC56X3_CHIP_ID = 0x10;
14 static const uint8_t MMC56X3_ADDR_XOUT0 = 0x00;
15 static const uint8_t MMC56X3_ADDR_XOUT1 = 0x01;
16 static const uint8_t MMC56X3_ADDR_XOUT2 = 0x06;
18 static const uint8_t MMC56X3_ADDR_YOUT0 = 0x02;
19 static const uint8_t MMC56X3_ADDR_YOUT1 = 0x03;
20 static const uint8_t MMC56X3_ADDR_YOUT2 = 0x07;
22 static const uint8_t MMC56X3_ADDR_ZOUT0 = 0x04;
23 static const uint8_t MMC56X3_ADDR_ZOUT1 = 0x05;
24 static const uint8_t MMC56X3_ADDR_ZOUT2 = 0x08;
26 static const uint8_t MMC56X3_OUT_TEMP = 0x09;
27 static const uint8_t MMC56X3_STATUS_REG = 0x18;
28 static const uint8_t MMC56X3_CTRL0_REG = 0x1B;
29 static const uint8_t MMC56X3_CTRL1_REG = 0x1C;
30 static const uint8_t MMC56X3_CTRL2_REG = 0x1D;
31 static const uint8_t MMC5603_ODR_REG = 0x1A;
34 ESP_LOGCONFIG(TAG,
"Setting up MMC5603...");
36 if (!this->
read_byte(MMC56X3_PRODUCT_ID, &
id)) {
42 if (
id != MMC56X3_CHIP_ID) {
43 ESP_LOGCONFIG(TAG,
"Chip Wrong");
49 if (!this->
write_byte(MMC56X3_CTRL1_REG, 0x80)) {
50 ESP_LOGCONFIG(TAG,
"Control 1 Failed for set bit");
56 if (!this->
write_byte(MMC56X3_CTRL0_REG, 0x08)) {
57 ESP_LOGCONFIG(TAG,
"Control 0 Failed for set bit");
63 if (!this->
write_byte(MMC56X3_CTRL0_REG, 0x10)) {
72 if (!this->
write_byte(MMC56X3_CTRL2_REG, ctrl_2)) {
79 ESP_LOGCONFIG(TAG,
"MMC5603:");
82 ESP_LOGE(TAG,
"Communication with MMC5603 failed!");
84 ESP_LOGE(TAG,
"The ID registers don't match - Is this really an MMC5603?");
86 LOG_UPDATE_INTERVAL(
this);
88 LOG_SENSOR(
" ",
"X Axis", this->
x_sensor_);
89 LOG_SENSOR(
" ",
"Y Axis", this->
y_sensor_);
90 LOG_SENSOR(
" ",
"Z Axis", this->
z_sensor_);
97 if (!this->
write_byte(MMC56X3_CTRL0_REG, 0x01)) {
102 if (!this->
read_byte(MMC56X3_STATUS_REG, &status)) {
107 uint8_t buffer[9] = {0};
109 if (!this->
read_byte(MMC56X3_ADDR_XOUT0, &buffer[0]) || !this->
read_byte(MMC56X3_ADDR_XOUT1, &buffer[1]) ||
110 !this->
read_byte(MMC56X3_ADDR_XOUT2, &buffer[2])) {
115 if (!this->
read_byte(MMC56X3_ADDR_YOUT0, &buffer[3]) || !this->
read_byte(MMC56X3_ADDR_YOUT1, &buffer[4]) ||
116 !this->
read_byte(MMC56X3_ADDR_YOUT2, &buffer[5])) {
121 if (!this->
read_byte(MMC56X3_ADDR_ZOUT0, &buffer[6]) || !this->
read_byte(MMC56X3_ADDR_ZOUT1, &buffer[7]) ||
122 !this->
read_byte(MMC56X3_ADDR_ZOUT2, &buffer[8])) {
128 raw_x |= buffer[0] << 12;
129 raw_x |= buffer[1] << 4;
130 raw_x |= buffer[2] << 0;
132 const float x = 0.0625 * (raw_x - 524288);
135 raw_y |= buffer[3] << 12;
136 raw_y |= buffer[4] << 4;
137 raw_y |= buffer[5] << 0;
139 const float y = 0.0625 * (raw_y - 524288);
142 raw_z |= buffer[6] << 12;
143 raw_z |= buffer[7] << 4;
144 raw_z |= buffer[8] << 0;
146 const float z = 0.0625 * (raw_z - 524288);
148 const float heading = atan2f(0.0f - x, y) * 180.0f / M_PI;
149 ESP_LOGD(TAG,
"Got x=%0.02fµT y=%0.02fµT z=%0.02fµT heading=%0.01f°", x, y, z, heading);
sensor::Sensor * y_sensor_
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
const float DATA
For components that import data from directly connected sensors like DHT.
void status_set_warning(const char *message="unspecified")
float get_setup_priority() const override
sensor::Sensor * x_sensor_
void publish_state(float state)
Publish a new state to the front-end.
void dump_config() override
enum esphome::mmc5603::MMC5603Component::ErrorCode error_code_
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
sensor::Sensor * z_sensor_
sensor::Sensor * heading_sensor_