ESPHome 2026.5.0
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
7static const char *const 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();
43}
44
46 ESP_LOGCONFIG(TAG, "MAX44009:");
47 LOG_I2C_DEVICE(this);
48 if (this->is_failed()) {
49 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
50 }
51}
52
54 // update sensor illuminance value
55 float lux = this->read_illuminance_();
56 if (this->error_ != MAX44009_OK) {
57 this->status_set_warning();
58 this->publish_state(NAN);
59 } else {
61 this->publish_state(lux);
62 }
63}
64
66 uint8_t datahigh = this->read_(MAX44009_LUX_READING_HIGH);
67 if (error_ != MAX44009_OK) {
68 this->error_ = MAX44009_ERROR_HIGH_BYTE;
69 return this->error_;
70 }
71 uint8_t datalow = this->read_(MAX44009_LUX_READING_LOW);
72 if (error_ != MAX44009_OK) {
73 this->error_ = MAX44009_ERROR_LOW_BYTE;
74 return this->error_;
75 }
76 uint8_t exponent = datahigh >> 4;
77 if (exponent == 0x0F) {
78 this->error_ = MAX44009_ERROR_OVERFLOW;
79 return this->error_;
80 }
81
82 return this->convert_to_lux_(datahigh, datalow);
83}
84
85float MAX44009Sensor::convert_to_lux_(uint8_t data_high, uint8_t data_low) {
86 uint8_t exponent = data_high >> 4;
87 uint32_t mantissa = ((data_high & 0x0F) << 4) + (data_low & 0x0F);
88 return ((0x0001 << exponent) * 0.045) * mantissa;
89}
90
92 uint8_t config = this->read_(MAX44009_REGISTER_CONFIGURATION);
93 if (this->error_ == MAX44009_OK) {
94 config |= MAX44009_CFG_CONTINUOUS;
95 this->write_(MAX44009_REGISTER_CONFIGURATION, config);
97 ESP_LOGV(TAG, "set to continuous mode");
98 return true;
99 } else {
100 this->status_set_warning();
101 return false;
102 }
103}
104
106 uint8_t config = this->read_(MAX44009_REGISTER_CONFIGURATION);
107 if (this->error_ == MAX44009_OK) {
108 config &= ~MAX44009_CFG_CONTINUOUS;
109 this->write_(MAX44009_REGISTER_CONFIGURATION, config);
110 this->status_clear_warning();
111 ESP_LOGV(TAG, "set to low power mode");
112 return true;
113 } else {
114 this->status_set_warning();
115 return false;
116 }
117}
118
119uint8_t MAX44009Sensor::read_(uint8_t reg) {
120 uint8_t data = 0;
121 if (!this->read_byte(reg, &data)) {
122 this->error_ = MAX44009_ERROR_WIRE_REQUEST;
123 } else {
124 this->error_ = MAX44009_OK;
125 }
126 return data;
127}
128
129void MAX44009Sensor::write_(uint8_t reg, uint8_t value) {
130 if (!this->write_byte(reg, value)) {
131 this->error_ = MAX44009_ERROR_WIRE_REQUEST;
132 } else {
133 this->error_ = MAX44009_OK;
134 }
135}
136
138
139} // namespace esphome::max44009
BedjetMode mode
BedJet operating mode.
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
void status_clear_warning()
Definition component.h:306
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:265
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:240
float read_illuminance_()
Read the illuminance value.
Definition max44009.cpp:65
void write_(uint8_t reg, uint8_t value)
Definition max44009.cpp:129
void set_mode(MAX44009Mode mode)
Definition max44009.cpp:137
float convert_to_lux_(uint8_t data_high, uint8_t data_low)
Definition max44009.cpp:85
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
@ MAX44009_MODE_LOW_POWER
Definition max44009.h:9
@ MAX44009_MODE_CONTINUOUS
Definition max44009.h:9
static void uint32_t