ESPHome  2024.11.1
as5600_sensor.cpp
Go to the documentation of this file.
1 #include "as5600_sensor.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace as5600 {
6 
7 static const char *const TAG = "as5600.sensor";
8 
9 // Configuration registers
10 static const uint8_t REGISTER_ZMCO = 0x00; // 8 bytes / R
11 static const uint8_t REGISTER_ZPOS = 0x01; // 16 bytes / RW
12 static const uint8_t REGISTER_MPOS = 0x03; // 16 bytes / RW
13 static const uint8_t REGISTER_MANG = 0x05; // 16 bytes / RW
14 static const uint8_t REGISTER_CONF = 0x07; // 16 bytes / RW
15 
16 // Output registers
17 static const uint8_t REGISTER_ANGLE_RAW = 0x0C; // 16 bytes / R
18 static const uint8_t REGISTER_ANGLE = 0x0E; // 16 bytes / R
19 
20 // Status registers
21 static const uint8_t REGISTER_STATUS = 0x0B; // 8 bytes / R
22 static const uint8_t REGISTER_AGC = 0x1A; // 8 bytes / R
23 static const uint8_t REGISTER_MAGNITUDE = 0x1B; // 16 bytes / R
24 
26 
28  LOG_SENSOR("", "AS5600 Sensor", this);
29  ESP_LOGCONFIG(TAG, " Out of Range Mode: %u", this->out_of_range_mode_);
30  if (this->angle_sensor_ != nullptr) {
31  LOG_SENSOR(" ", "Angle Sensor", this->angle_sensor_);
32  }
33  if (this->raw_angle_sensor_ != nullptr) {
34  LOG_SENSOR(" ", "Raw Angle Sensor", this->raw_angle_sensor_);
35  }
36  if (this->position_sensor_ != nullptr) {
37  LOG_SENSOR(" ", "Position Sensor", this->position_sensor_);
38  }
39  if (this->raw_position_sensor_ != nullptr) {
40  LOG_SENSOR(" ", "Raw Position Sensor", this->raw_position_sensor_);
41  }
42  if (this->gain_sensor_ != nullptr) {
43  LOG_SENSOR(" ", "Gain Sensor", this->gain_sensor_);
44  }
45  if (this->magnitude_sensor_ != nullptr) {
46  LOG_SENSOR(" ", "Magnitude Sensor", this->magnitude_sensor_);
47  }
48  if (this->status_sensor_ != nullptr) {
49  LOG_SENSOR(" ", "Status Sensor", this->status_sensor_);
50  }
51  LOG_UPDATE_INTERVAL(this);
52 }
53 
55  if (this->gain_sensor_ != nullptr) {
56  this->gain_sensor_->publish_state(this->parent_->reg(REGISTER_AGC).get());
57  }
58 
59  if (this->magnitude_sensor_ != nullptr) {
60  uint16_t value = 0;
61  this->parent_->read_byte_16(REGISTER_MAGNITUDE, &value);
62  this->magnitude_sensor_->publish_state(value);
63  }
64 
65  // 2 = magnet not detected
66  // 4 = magnet just right
67  // 5 = magnet too strong
68  // 6 = magnet too weak
69  if (this->status_sensor_ != nullptr) {
70  this->status_sensor_->publish_state(this->parent_->read_magnet_status());
71  }
72 
73  auto pos = this->parent_->read_position();
74  if (!pos.has_value()) {
75  this->status_set_warning();
76  return;
77  }
78 
79  auto raw = this->parent_->read_raw_position();
80  if (!raw.has_value()) {
81  this->status_set_warning();
82  return;
83  }
84 
86  this->publish_state(this->parent_->in_range(raw.value()) ? pos.value() : NAN);
87  } else {
88  this->publish_state(pos.value());
89  }
90 
91  if (this->raw_position_sensor_ != nullptr) {
92  this->raw_position_sensor_->publish_state(raw.value());
93  }
94  this->status_clear_warning();
95 }
96 
97 } // namespace as5600
98 } // namespace esphome
sensor::Sensor * angle_sensor_
Definition: as5600_sensor.h:32
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
uint8_t raw[35]
Definition: bl0939.h:19
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
sensor::Sensor * position_sensor_
Definition: as5600_sensor.h:34
float get_setup_priority() const override
sensor::Sensor * gain_sensor_
Definition: as5600_sensor.h:36
void status_clear_warning()
Definition: component.cpp:166
sensor::Sensor * raw_position_sensor_
Definition: as5600_sensor.h:35
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
sensor::Sensor * raw_angle_sensor_
Definition: as5600_sensor.h:33
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
sensor::Sensor * magnitude_sensor_
Definition: as5600_sensor.h:37
sensor::Sensor * status_sensor_
Definition: as5600_sensor.h:38