ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
m5stack_8angle.cpp
Go to the documentation of this file.
1#include "m5stack_8angle.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace m5stack_8angle {
7
8static const char *const TAG = "m5stack_8angle";
9
12
13 err = this->read(nullptr, 0);
14 if (err != i2c::NO_ERROR) {
15 ESP_LOGE(TAG, "I2C error %02X", err);
16 this->mark_failed();
17 return;
18 };
19
20 err = this->read_register(M5STACK_8ANGLE_REGISTER_FW_VERSION, &this->fw_version_, 1);
21 if (err != i2c::NO_ERROR) {
22 ESP_LOGE(TAG, "I2C error %02X", err);
23 this->mark_failed();
24 return;
25 };
26}
27
29 ESP_LOGCONFIG(TAG, "M5STACK_8ANGLE:");
30 LOG_I2C_DEVICE(this);
31 ESP_LOGCONFIG(TAG, " Firmware version: %d ", this->fw_version_);
32}
33
35 int32_t raw_pos = this->read_knob_pos_raw(channel, bits);
36 if (raw_pos == -1) {
37 return NAN;
38 }
39 return (float) raw_pos / ((1 << bits) - 1);
40}
41
43 uint16_t knob_pos = 0;
45 if (bits == BITS_8) {
46 err = this->read_register(M5STACK_8ANGLE_REGISTER_ANALOG_INPUT_8B + channel, (uint8_t *) &knob_pos, 1);
47 } else if (bits == BITS_12) {
48 err = this->read_register(M5STACK_8ANGLE_REGISTER_ANALOG_INPUT_12B + (channel * 2), (uint8_t *) &knob_pos, 2);
49 } else {
50 ESP_LOGE(TAG, "Invalid number of bits: %d", bits);
51 return -1;
52 }
53 if (err == i2c::NO_ERROR) {
54 return knob_pos;
55 } else {
56 return -1;
57 }
58}
59
61 uint8_t out;
62 i2c::ErrorCode err = this->read_register(M5STACK_8ANGLE_REGISTER_DIGITAL_INPUT, (uint8_t *) &out, 1);
63 if (err == i2c::NO_ERROR) {
64 return out ? 1 : 0;
65 } else {
66 return -1;
67 }
68}
69
71
72} // namespace m5stack_8angle
73} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:10
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
int32_t read_knob_pos_raw(uint8_t channel, AnalogBits bits=AnalogBits::BITS_8)
float read_knob_pos(uint8_t channel, AnalogBits bits=AnalogBits::BITS_8)
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:11
@ NO_ERROR
No error found during execution of method.
Definition i2c_bus.h:12
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7