ESPHome 2026.5.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 LOG_SENSOR(" ", "Raw Position Sensor", this->raw_position_sensor_);
29 LOG_SENSOR(" ", "Gain Sensor", this->gain_sensor_);
30 LOG_SENSOR(" ", "Magnitude Sensor", this->magnitude_sensor_);
31 LOG_SENSOR(" ", "Status Sensor", this->status_sensor_);
32 LOG_UPDATE_INTERVAL(this);
33}
34
36 if (this->gain_sensor_ != nullptr) {
37 this->gain_sensor_->publish_state(this->parent_->reg(REGISTER_AGC).get());
38 }
39
40 if (this->magnitude_sensor_ != nullptr) {
41 uint16_t value = 0;
42 this->parent_->read_byte_16(REGISTER_MAGNITUDE, &value);
43 this->magnitude_sensor_->publish_state(value);
44 }
45
46 // 2 = magnet not detected
47 // 4 = magnet just right
48 // 5 = magnet too strong
49 // 6 = magnet too weak
50 if (this->status_sensor_ != nullptr) {
51 this->status_sensor_->publish_state(this->parent_->read_magnet_status());
52 }
53
54 auto pos = this->parent_->read_position();
55 if (!pos.has_value()) {
56 this->status_set_warning();
57 return;
58 }
59
60 auto raw = this->parent_->read_raw_position();
61 if (!raw.has_value()) {
62 this->status_set_warning();
63 return;
64 }
65
67 this->publish_state(this->parent_->in_range(raw.value()) ? pos.value() : NAN);
68 } else {
69 this->publish_state(pos.value());
70 }
71
72 if (this->raw_position_sensor_ != nullptr) {
74 }
76}
77
78} // namespace as5600
79} // namespace esphome
uint8_t raw[35]
Definition bl0939.h:0
void status_clear_warning()
Definition component.h:306
sensor::Sensor * magnitude_sensor_
sensor::Sensor * raw_position_sensor_
sensor::Sensor * status_sensor_
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
@ 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:1082