ESPHome 2026.11.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
6
7ESPHOME_LOG_TAG(TAG, "max44009.sensor");
8
9// REGISTERS
10static constexpr uint8_t MAX44009_REGISTER_CONFIGURATION = 0x02;
11static constexpr uint8_t MAX44009_LUX_READING_HIGH = 0x03;
12static constexpr uint8_t MAX44009_LUX_READING_LOW = 0x04;
13// CONFIGURATION MASKS
14static constexpr uint8_t MAX44009_CFG_CONTINUOUS = 0x80;
15// ERROR CODES
16static constexpr int8_t MAX44009_OK = 0;
17static constexpr int8_t MAX44009_ERROR_WIRE_REQUEST = -10;
18static constexpr int8_t MAX44009_ERROR_OVERFLOW = -20;
19static constexpr int8_t MAX44009_ERROR_HIGH_BYTE = -30;
20static constexpr int8_t MAX44009_ERROR_LOW_BYTE = -31;
21
23 bool state_ok = false;
25 state_ok = this->set_low_power_mode();
27 state_ok = this->set_continuous_mode();
28 } else {
29 /*
30 * Mode AUTO: Set mode depending on update interval
31 * - On low power mode, the IC measures lux intensity only once every 800ms
32 * regardless of integration time
33 * - On continuous mode, the IC continuously measures lux intensity
34 */
35 if (this->get_update_interval() < 800) {
36 state_ok = this->set_continuous_mode();
37 } else {
38 state_ok = this->set_low_power_mode();
39 }
40 }
41 if (!state_ok)
42 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
43}
44
46 ESP_LOGCONFIG(TAG, "MAX44009:");
47 LOG_I2C_DEVICE(this);
48}
49
51 // update sensor illuminance value
52 float lux = this->read_illuminance_();
53 if (this->error_ != MAX44009_OK) {
54 this->status_set_warning();
55 this->publish_state(NAN);
56 } else {
58 this->publish_state(lux);
59 }
60}
61
63 uint8_t datahigh = this->read_(MAX44009_LUX_READING_HIGH);
64 if (error_ != MAX44009_OK) {
65 this->error_ = MAX44009_ERROR_HIGH_BYTE;
66 return this->error_;
67 }
68 uint8_t datalow = this->read_(MAX44009_LUX_READING_LOW);
69 if (error_ != MAX44009_OK) {
70 this->error_ = MAX44009_ERROR_LOW_BYTE;
71 return this->error_;
72 }
73 uint8_t exponent = datahigh >> 4;
74 if (exponent == 0x0F) {
75 this->error_ = MAX44009_ERROR_OVERFLOW;
76 return this->error_;
77 }
78
79 return this->convert_to_lux_(datahigh, datalow);
80}
81
82float MAX44009Sensor::convert_to_lux_(uint8_t data_high, uint8_t data_low) {
83 uint8_t exponent = data_high >> 4;
84 uint32_t mantissa = ((data_high & 0x0F) << 4) + (data_low & 0x0F);
85 return ((0x0001 << exponent) * 0.045) * mantissa;
86}
87
89 uint8_t config = this->read_(MAX44009_REGISTER_CONFIGURATION);
90 if (this->error_ == MAX44009_OK) {
91 config |= MAX44009_CFG_CONTINUOUS;
92 this->write_(MAX44009_REGISTER_CONFIGURATION, config);
94 ESP_LOGV(TAG, "set to continuous mode");
95 return true;
96 } else {
97 this->status_set_warning();
98 return false;
99 }
100}
101
103 uint8_t config = this->read_(MAX44009_REGISTER_CONFIGURATION);
104 if (this->error_ == MAX44009_OK) {
105 config &= ~MAX44009_CFG_CONTINUOUS;
106 this->write_(MAX44009_REGISTER_CONFIGURATION, config);
107 this->status_clear_warning();
108 ESP_LOGV(TAG, "set to low power mode");
109 return true;
110 } else {
111 this->status_set_warning();
112 return false;
113 }
114}
115
116uint8_t MAX44009Sensor::read_(uint8_t reg) {
117 uint8_t data = 0;
118 if (!this->read_byte(reg, &data)) {
119 this->error_ = MAX44009_ERROR_WIRE_REQUEST;
120 } else {
121 this->error_ = MAX44009_OK;
122 }
123 return data;
124}
125
126void MAX44009Sensor::write_(uint8_t reg, uint8_t value) {
127 if (!this->write_byte(reg, value)) {
128 this->error_ = MAX44009_ERROR_WIRE_REQUEST;
129 } else {
130 this->error_ = MAX44009_OK;
131 }
132}
133
134} // namespace esphome::max44009
void mark_failed()
Mark this component as failed.
void status_clear_warning()
Definition component.h:296
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) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
float read_illuminance_()
Read the illuminance value.
Definition max44009.cpp:62
void write_(uint8_t reg, uint8_t value)
Definition max44009.cpp:126
float convert_to_lux_(uint8_t data_high, uint8_t data_low)
Definition max44009.cpp:82
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
@ MAX44009_MODE_LOW_POWER
Definition max44009.h:9
@ MAX44009_MODE_CONTINUOUS
Definition max44009.h:9
ESPHOME_LOG_TAG(TAG, "max44009.sensor")
static void uint32_t