ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
max44009.cpp
Go to the documentation of this file.
1#include "max44009.h"
2
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace max44009 {
7
8static const char *const TAG = "max44009.sensor";
9
10// REGISTERS
11static const uint8_t MAX44009_REGISTER_CONFIGURATION = 0x02;
12static const uint8_t MAX44009_LUX_READING_HIGH = 0x03;
13static const uint8_t MAX44009_LUX_READING_LOW = 0x04;
14// CONFIGURATION MASKS
15static const uint8_t MAX44009_CFG_CONTINUOUS = 0x80;
16// ERROR CODES
17static const uint8_t MAX44009_OK = 0;
18static const uint8_t MAX44009_ERROR_WIRE_REQUEST = -10;
19static const uint8_t MAX44009_ERROR_OVERFLOW = -20;
20static const uint8_t MAX44009_ERROR_HIGH_BYTE = -30;
21static const uint8_t MAX44009_ERROR_LOW_BYTE = -31;
22
24 bool state_ok = false;
26 state_ok = this->set_low_power_mode();
28 state_ok = this->set_continuous_mode();
29 } else {
30 /*
31 * Mode AUTO: Set mode depending on update interval
32 * - On low power mode, the IC measures lux intensity only once every 800ms
33 * regardless of integration time
34 * - On continuous mode, the IC continuously measures lux intensity
35 */
36 if (this->get_update_interval() < 800) {
37 state_ok = this->set_continuous_mode();
38 } else {
39 state_ok = this->set_low_power_mode();
40 }
41 }
42 if (!state_ok)
43 this->mark_failed();
44}
45
47 ESP_LOGCONFIG(TAG, "MAX44009:");
48 LOG_I2C_DEVICE(this);
49 if (this->is_failed()) {
50 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
51 }
52}
53
55
57 // update sensor illuminance value
58 float lux = this->read_illuminance_();
59 if (this->error_ != MAX44009_OK) {
60 this->status_set_warning();
61 this->publish_state(NAN);
62 } else {
64 this->publish_state(lux);
65 }
66}
67
69 uint8_t datahigh = this->read_(MAX44009_LUX_READING_HIGH);
70 if (error_ != MAX44009_OK) {
71 this->error_ = MAX44009_ERROR_HIGH_BYTE;
72 return this->error_;
73 }
74 uint8_t datalow = this->read_(MAX44009_LUX_READING_LOW);
75 if (error_ != MAX44009_OK) {
76 this->error_ = MAX44009_ERROR_LOW_BYTE;
77 return this->error_;
78 }
79 uint8_t exponent = datahigh >> 4;
80 if (exponent == 0x0F) {
81 this->error_ = MAX44009_ERROR_OVERFLOW;
82 return this->error_;
83 }
84
85 return this->convert_to_lux_(datahigh, datalow);
86}
87
88float MAX44009Sensor::convert_to_lux_(uint8_t data_high, uint8_t data_low) {
89 uint8_t exponent = data_high >> 4;
90 uint32_t mantissa = ((data_high & 0x0F) << 4) + (data_low & 0x0F);
91 return ((0x0001 << exponent) * 0.045) * mantissa;
92}
93
95 uint8_t config = this->read_(MAX44009_REGISTER_CONFIGURATION);
96 if (this->error_ == MAX44009_OK) {
97 config |= MAX44009_CFG_CONTINUOUS;
98 this->write_(MAX44009_REGISTER_CONFIGURATION, config);
100 ESP_LOGV(TAG, "set to continuous mode");
101 return true;
102 } else {
103 this->status_set_warning();
104 return false;
105 }
106}
107
109 uint8_t config = this->read_(MAX44009_REGISTER_CONFIGURATION);
110 if (this->error_ == MAX44009_OK) {
111 config &= ~MAX44009_CFG_CONTINUOUS;
112 this->write_(MAX44009_REGISTER_CONFIGURATION, config);
113 this->status_clear_warning();
114 ESP_LOGV(TAG, "set to low power mode");
115 return true;
116 } else {
117 this->status_set_warning();
118 return false;
119 }
120}
121
122uint8_t MAX44009Sensor::read_(uint8_t reg) {
123 uint8_t data = 0;
124 if (!this->read_byte(reg, &data)) {
125 this->error_ = MAX44009_ERROR_WIRE_REQUEST;
126 } else {
127 this->error_ = MAX44009_OK;
128 }
129 return data;
130}
131
132void MAX44009Sensor::write_(uint8_t reg, uint8_t value) {
133 if (!this->write_byte(reg, value)) {
134 this->error_ = MAX44009_ERROR_WIRE_REQUEST;
135 } else {
136 this->error_ = MAX44009_OK;
137 }
138}
139
141
142} // namespace max44009
143} // namespace esphome
BedjetMode mode
BedJet operating mode.
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition i2c.h:266
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition i2c.h:239
float get_setup_priority() const override
Definition max44009.cpp:54
float read_illuminance_()
Read the illuminance value.
Definition max44009.cpp:68
void write_(uint8_t reg, uint8_t value)
Definition max44009.cpp:132
void set_mode(MAX44009Mode mode)
Definition max44009.cpp:140
float convert_to_lux_(uint8_t data_high, uint8_t data_low)
Definition max44009.cpp:88
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
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