ESPHome 2026.3.0-dev
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 LOG_SENSOR("", "AS5600 Sensor", this);
27 ESP_LOGCONFIG(TAG, " Out of Range Mode: %u", this->out_of_range_mode_);
28 if (this->angle_sensor_ != nullptr) {
29 LOG_SENSOR(" ", "Angle Sensor", this->angle_sensor_);
30 }
31 if (this->raw_angle_sensor_ != nullptr) {
32 LOG_SENSOR(" ", "Raw Angle Sensor", this->raw_angle_sensor_);
33 }
34 if (this->position_sensor_ != nullptr) {
35 LOG_SENSOR(" ", "Position Sensor", this->position_sensor_);
36 }
37 if (this->raw_position_sensor_ != nullptr) {
38 LOG_SENSOR(" ", "Raw Position Sensor", this->raw_position_sensor_);
39 }
40 if (this->gain_sensor_ != nullptr) {
41 LOG_SENSOR(" ", "Gain Sensor", this->gain_sensor_);
42 }
43 if (this->magnitude_sensor_ != nullptr) {
44 LOG_SENSOR(" ", "Magnitude Sensor", this->magnitude_sensor_);
45 }
46 if (this->status_sensor_ != nullptr) {
47 LOG_SENSOR(" ", "Status Sensor", this->status_sensor_);
48 }
49 LOG_UPDATE_INTERVAL(this);
50}
51
53 if (this->gain_sensor_ != nullptr) {
54 this->gain_sensor_->publish_state(this->parent_->reg(REGISTER_AGC).get());
55 }
56
57 if (this->magnitude_sensor_ != nullptr) {
58 uint16_t value = 0;
59 this->parent_->read_byte_16(REGISTER_MAGNITUDE, &value);
60 this->magnitude_sensor_->publish_state(value);
61 }
62
63 // 2 = magnet not detected
64 // 4 = magnet just right
65 // 5 = magnet too strong
66 // 6 = magnet too weak
67 if (this->status_sensor_ != nullptr) {
68 this->status_sensor_->publish_state(this->parent_->read_magnet_status());
69 }
70
71 auto pos = this->parent_->read_position();
72 if (!pos.has_value()) {
73 this->status_set_warning();
74 return;
75 }
76
77 auto raw = this->parent_->read_raw_position();
78 if (!raw.has_value()) {
79 this->status_set_warning();
80 return;
81 }
82
84 this->publish_state(this->parent_->in_range(raw.value()) ? pos.value() : NAN);
85 } else {
86 this->publish_state(pos.value());
87 }
88
89 if (this->raw_position_sensor_ != nullptr) {
91 }
93}
94
95} // namespace as5600
96} // namespace esphome
uint8_t raw[35]
Definition bl0939.h:0
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
sensor::Sensor * magnitude_sensor_
sensor::Sensor * raw_position_sensor_
sensor::Sensor * raw_angle_sensor_
sensor::Sensor * position_sensor_
sensor::Sensor * status_sensor_
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
@ OUT_RANGE_MODE_NAN
Definition as5600.h:37
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
size_t size_t pos
Definition helpers.h:854