ESPHome 2025.5.0
Loading...
Searching...
No Matches
as5600_sensor.cpp
Go to the documentation of this file.
1#include "as5600_sensor.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace as5600 {
6
7static const char *const TAG = "as5600.sensor";
8
9// Configuration registers
10static const uint8_t REGISTER_ZMCO = 0x00; // 8 bytes / R
11static const uint8_t REGISTER_ZPOS = 0x01; // 16 bytes / RW
12static const uint8_t REGISTER_MPOS = 0x03; // 16 bytes / RW
13static const uint8_t REGISTER_MANG = 0x05; // 16 bytes / RW
14static const uint8_t REGISTER_CONF = 0x07; // 16 bytes / RW
15
16// Output registers
17static const uint8_t REGISTER_ANGLE_RAW = 0x0C; // 16 bytes / R
18static const uint8_t REGISTER_ANGLE = 0x0E; // 16 bytes / R
19
20// Status registers
21static const uint8_t REGISTER_STATUS = 0x0B; // 8 bytes / R
22static const uint8_t REGISTER_AGC = 0x1A; // 8 bytes / R
23static 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) {
93 }
95}
96
97} // namespace as5600
98} // namespace esphome
uint8_t raw[35]
Definition bl0939.h:0
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
sensor::Sensor * magnitude_sensor_
sensor::Sensor * raw_position_sensor_
sensor::Sensor * raw_angle_sensor_
sensor::Sensor * position_sensor_
float get_setup_priority() const override
sensor::Sensor * status_sensor_
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
@ OUT_RANGE_MODE_NAN
Definition as5600.h:37
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:19
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7