ESPHome  2024.10.2
as5600.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/hal.h"
8 
9 namespace esphome {
10 namespace as5600 {
11 
12 static const uint16_t POSITION_COUNT = 4096;
13 static const float RAW_TO_DEGREES = 360.0 / POSITION_COUNT;
14 static const float DEGREES_TO_RAW = POSITION_COUNT / 360.0;
15 
16 enum 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 
27 enum 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 
40 enum 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 
47 class AS5600Component : public Component, public i2c::I2CDevice {
48  public:
50  void setup() override;
51  void dump_config() override;
53  float get_setup_priority() const override { return setup_priority::DATA; }
54 
55  // configuration setters
56  void set_dir_pin(InternalGPIOPin *pin) { this->dir_pin_ = pin; }
57  void set_direction(uint8_t direction) { this->direction_ = direction; }
58  void set_fast_filter(uint8_t fast_filter) { this->fast_filter_ = fast_filter; }
59  void set_hysteresis(uint8_t hysteresis) { this->hysteresis_ = hysteresis; }
60  void set_power_mode(uint8_t power_mode) { this->power_mode_ = power_mode; }
61  void set_slow_filter(uint8_t slow_filter) { this->slow_filter_ = slow_filter; }
62  void set_watchdog(bool watchdog) { this->watchdog_ = watchdog; }
63  bool get_watchdog() { return this->watchdog_; }
64  void set_start_position(uint16_t start_position) { this->start_position_ = start_position % POSITION_COUNT; }
65  void set_end_position(uint16_t end_position) {
66  this->end_position_ = end_position % POSITION_COUNT;
68  }
69  void set_range(uint16_t range) {
70  this->end_position_ = range % POSITION_COUNT;
71  this->end_mode_ = END_MODE_RANGE;
72  }
73 
74  // Gets the scale value for the configured range.
75  // For example, if configured to start at 0deg and end at 180deg, the
76  // range is 50% of the native/raw range, so the range scale would be 0.5.
77  // If configured to use the full 360deg, the range scale would be 1.0.
78  float get_range_scale() { return this->range_scale_; }
79 
80  // Indicates whether the given *raw* position is within the configured range
81  bool in_range(uint16_t raw_position);
82 
86 
87  protected:
89  uint8_t direction_;
90  uint8_t fast_filter_;
91  uint8_t hysteresis_;
92  uint8_t power_mode_;
93  uint8_t slow_filter_;
94  uint8_t pwm_frequency_{0};
95  uint8_t output_mode_{0};
96  bool watchdog_;
97  uint16_t start_position_;
98  uint16_t end_position_{0};
99  uint16_t raw_max_;
101  float range_scale_{1.0};
102 };
103 
104 } // namespace as5600
105 } // namespace esphome
void set_direction(uint8_t direction)
Definition: as5600.h:57
InternalGPIOPin * dir_pin_
Definition: as5600.h:88
EndPositionMode end_mode_
Definition: as5600.h:100
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void set_dir_pin(InternalGPIOPin *pin)
Definition: as5600.h:56
void set_hysteresis(uint8_t hysteresis)
Definition: as5600.h:59
void set_fast_filter(uint8_t fast_filter)
Definition: as5600.h:58
void dump_config() override
Definition: as5600.cpp:89
uint8_t power_mode
Definition: qmp6988.h:70
AS5600MagnetStatus read_magnet_status()
Definition: as5600.cpp:116
void set_start_position(uint16_t start_position)
Definition: as5600.h:64
void setup() override
Set up the internal sensor array.
Definition: as5600.cpp:25
float get_setup_priority() const override
HARDWARE_LATE setup priority.
Definition: as5600.h:53
FanDirection direction
Definition: fan.h:37
void set_watchdog(bool watchdog)
Definition: as5600.h:62
void set_end_position(uint16_t end_position)
Definition: as5600.h:65
void set_slow_filter(uint8_t slow_filter)
Definition: as5600.h:61
optional< uint16_t > read_position()
Definition: as5600.cpp:121
optional< uint16_t > read_raw_position()
Definition: as5600.cpp:129
void set_range(uint16_t range)
Definition: as5600.h:69
bool in_range(uint16_t raw_position)
Definition: as5600.cpp:110
void set_power_mode(uint8_t power_mode)
Definition: as5600.h:60
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
This Class provides the methods to read/write bytes from/to an i2c device.
Definition: i2c.h:133