ESPHome 2026.5.1
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::as5600 {
5
6static const char *const TAG = "as5600.sensor";
7
8// Configuration registers
9static const uint8_t REGISTER_ZMCO = 0x00; // 8 bytes / R
10static const uint8_t REGISTER_ZPOS = 0x01; // 16 bytes / RW
11static const uint8_t REGISTER_MPOS = 0x03; // 16 bytes / RW
12static const uint8_t REGISTER_MANG = 0x05; // 16 bytes / RW
13static const uint8_t REGISTER_CONF = 0x07; // 16 bytes / RW
14
15// Output registers
16static const uint8_t REGISTER_ANGLE_RAW = 0x0C; // 16 bytes / R
17static const uint8_t REGISTER_ANGLE = 0x0E; // 16 bytes / R
18
19// Status registers
20static const uint8_t REGISTER_STATUS = 0x0B; // 8 bytes / R
21static const uint8_t REGISTER_AGC = 0x1A; // 8 bytes / R
22static const uint8_t REGISTER_MAGNITUDE = 0x1B; // 16 bytes / R
23
25 LOG_SENSOR("", "AS5600 Sensor", this);
26 ESP_LOGCONFIG(TAG, " Out of Range Mode: %u", this->out_of_range_mode_);
27 LOG_SENSOR(" ", "Raw Position Sensor", this->raw_position_sensor_);
28 LOG_SENSOR(" ", "Gain Sensor", this->gain_sensor_);
29 LOG_SENSOR(" ", "Magnitude Sensor", this->magnitude_sensor_);
30 LOG_SENSOR(" ", "Status Sensor", this->status_sensor_);
31 LOG_UPDATE_INTERVAL(this);
32}
33
35 if (this->gain_sensor_ != nullptr) {
36 this->gain_sensor_->publish_state(this->parent_->reg(REGISTER_AGC).get());
37 }
38
39 if (this->magnitude_sensor_ != nullptr) {
40 uint16_t value = 0;
41 this->parent_->read_byte_16(REGISTER_MAGNITUDE, &value);
42 this->magnitude_sensor_->publish_state(value);
43 }
44
45 // 2 = magnet not detected
46 // 4 = magnet just right
47 // 5 = magnet too strong
48 // 6 = magnet too weak
49 if (this->status_sensor_ != nullptr) {
50 this->status_sensor_->publish_state(this->parent_->read_magnet_status());
51 }
52
53 auto pos = this->parent_->read_position();
54 if (!pos.has_value()) {
55 this->status_set_warning();
56 return;
57 }
58
59 auto raw = this->parent_->read_raw_position();
60 if (!raw.has_value()) {
61 this->status_set_warning();
62 return;
63 }
64
66 this->publish_state(this->parent_->in_range(raw.value()) ? pos.value() : NAN);
67 } else {
68 this->publish_state(pos.value());
69 }
70
71 if (this->raw_position_sensor_ != nullptr) {
73 }
75}
76
77} // namespace esphome::as5600
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:36
size_t size_t pos
Definition helpers.h:1038