ESPHome 2026.3.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,
30 "M5STACK_8ANGLE:\n"
31 " Firmware version: %d",
32 this->fw_version_);
33 LOG_I2C_DEVICE(this);
34}
35
37 int32_t raw_pos = this->read_knob_pos_raw(channel, bits);
38 if (raw_pos == -1) {
39 return NAN;
40 }
41 return (float) raw_pos / ((1 << bits) - 1);
42}
43
45 uint16_t knob_pos = 0;
47 if (bits == BITS_8) {
48 err = this->read_register(M5STACK_8ANGLE_REGISTER_ANALOG_INPUT_8B + channel, (uint8_t *) &knob_pos, 1);
49 } else if (bits == BITS_12) {
50 err = this->read_register(M5STACK_8ANGLE_REGISTER_ANALOG_INPUT_12B + (channel * 2), (uint8_t *) &knob_pos, 2);
51 } else {
52 ESP_LOGE(TAG, "Invalid number of bits: %d", bits);
53 return -1;
54 }
55 if (err == i2c::NO_ERROR) {
56 return knob_pos;
57 } else {
58 return -1;
59 }
60}
61
63 uint8_t out;
64 i2c::ErrorCode err = this->read_register(M5STACK_8ANGLE_REGISTER_DIGITAL_INPUT, (uint8_t *) &out, 1);
65 if (err == i2c::NO_ERROR) {
66 return out ? 1 : 0;
67 } else {
68 return -1;
69 }
70}
71
72} // namespace m5stack_8angle
73} // namespace esphome
void mark_failed()
Mark this component as failed.
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:163
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:25
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:12
@ NO_ERROR
No error found during execution of method.
Definition i2c_bus.h:13
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7