ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
as5600.h
Go to the documentation of this file.
1#pragma once
2
4#include "esphome/core/hal.h"
8
9namespace esphome {
10namespace as5600 {
11
12static const uint16_t POSITION_COUNT = 4096;
13static const float RAW_TO_DEGREES = 360.0 / POSITION_COUNT;
14static const float DEGREES_TO_RAW = POSITION_COUNT / 360.0;
15
16enum EndPositionMode : uint8_t {
17 // In this mode, the end position is calculated by taking the start position
18 // and adding the range/positions. For example, you could say start at 90deg,
19 // and have a range of 180deg and effectively the sensor will report values
20 // from the physical 90deg thru 270deg.
22 // In this mode, the end position is explicitly set, and changing the start
23 // position will NOT change the end position.
25};
26
27enum OutRangeMode : uint8_t {
28 // In this mode, the AS5600 chip itself actually reports these values, but
29 // effectively it splits the out-of-range values in half, and when positioned
30 // over the half closest to the min/start position, it will report 0 and when
31 // positioned over the half closes to the max/end position, it will report the
32 // max/end value.
34 // In this mode, when the magnet is positioned outside the configured
35 // range, the sensor will report NAN, which translates to "Unknown"
36 // in Home Assistant.
38};
39
40enum AS5600MagnetStatus : uint8_t {
41 MAGNET_GONE = 2, // 0b010 / magnet not detected
42 MAGNET_OK = 4, // 0b100 / magnet just right
43 MAGNET_STRONG = 5, // 0b101 / magnet too strong
44 MAGNET_WEAK = 6, // 0b110 / magnet too weak
45};
46
48 public:
50 void setup() override;
51 void dump_config() override;
53
54 // configuration setters
55 void set_dir_pin(InternalGPIOPin *pin) { this->dir_pin_ = pin; }
56 void set_direction(uint8_t direction) { this->direction_ = direction; }
57 void set_fast_filter(uint8_t fast_filter) { this->fast_filter_ = fast_filter; }
58 void set_hysteresis(uint8_t hysteresis) { this->hysteresis_ = hysteresis; }
60 void set_slow_filter(uint8_t slow_filter) { this->slow_filter_ = slow_filter; }
61 void set_watchdog(bool watchdog) { this->watchdog_ = watchdog; }
62 bool get_watchdog() { return this->watchdog_; }
63 void set_start_position(uint16_t start_position) { this->start_position_ = start_position % POSITION_COUNT; }
64 void set_end_position(uint16_t end_position) {
65 this->end_position_ = end_position % POSITION_COUNT;
67 }
68 void set_range(uint16_t range) {
69 this->end_position_ = range % POSITION_COUNT;
71 }
72
73 // Gets the scale value for the configured range.
74 // For example, if configured to start at 0deg and end at 180deg, the
75 // range is 50% of the native/raw range, so the range scale would be 0.5.
76 // If configured to use the full 360deg, the range scale would be 1.0.
77 float get_range_scale() { return this->range_scale_; }
78
79 // Indicates whether the given *raw* position is within the configured range
80 bool in_range(uint16_t raw_position);
81
85
86 protected:
88 uint8_t direction_;
89 uint8_t fast_filter_;
90 uint8_t hysteresis_;
91 uint8_t power_mode_;
92 uint8_t slow_filter_;
93 uint8_t pwm_frequency_{0};
94 uint8_t output_mode_{0};
97 uint16_t end_position_{0};
98 uint16_t raw_max_;
100 float range_scale_{1.0};
101};
102
103} // namespace as5600
104} // namespace esphome
optional< uint16_t > read_position()
Definition as5600.cpp:121
AS5600MagnetStatus read_magnet_status()
Definition as5600.cpp:116
void set_start_position(uint16_t start_position)
Definition as5600.h:63
optional< uint16_t > read_raw_position()
Definition as5600.cpp:129
bool in_range(uint16_t raw_position)
Definition as5600.cpp:110
void set_fast_filter(uint8_t fast_filter)
Definition as5600.h:57
void set_slow_filter(uint8_t slow_filter)
Definition as5600.h:60
void setup() override
Set up the internal sensor array.
Definition as5600.cpp:25
void set_direction(uint8_t direction)
Definition as5600.h:56
void set_hysteresis(uint8_t hysteresis)
Definition as5600.h:58
void set_range(uint16_t range)
Definition as5600.h:68
EndPositionMode end_mode_
Definition as5600.h:99
void set_watchdog(bool watchdog)
Definition as5600.h:61
void set_power_mode(uint8_t power_mode)
Definition as5600.h:59
void set_end_position(uint16_t end_position)
Definition as5600.h:64
void set_dir_pin(InternalGPIOPin *pin)
HARDWARE_LATE setup priority.
Definition as5600.h:55
InternalGPIOPin * dir_pin_
Definition as5600.h:87
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:133
FanDirection direction
Definition fan.h:3
PowerMode power_mode
Definition msa3xx.h:3
Range range
Definition msa3xx.h:0
@ OUT_RANGE_MODE_NAN
Definition as5600.h:37
@ OUT_RANGE_MODE_MIN_MAX
Definition as5600.h:33
@ END_MODE_POSITION
Definition as5600.h:24
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7