ESPHome 2026.2.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
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 // update sensor illuminance value
56 float lux = this->read_illuminance_();
57 if (this->error_ != MAX44009_OK) {
58 this->status_set_warning();
59 this->publish_state(NAN);
60 } else {
62 this->publish_state(lux);
63 }
64}
65
67 uint8_t datahigh = this->read_(MAX44009_LUX_READING_HIGH);
68 if (error_ != MAX44009_OK) {
69 this->error_ = MAX44009_ERROR_HIGH_BYTE;
70 return this->error_;
71 }
72 uint8_t datalow = this->read_(MAX44009_LUX_READING_LOW);
73 if (error_ != MAX44009_OK) {
74 this->error_ = MAX44009_ERROR_LOW_BYTE;
75 return this->error_;
76 }
77 uint8_t exponent = datahigh >> 4;
78 if (exponent == 0x0F) {
79 this->error_ = MAX44009_ERROR_OVERFLOW;
80 return this->error_;
81 }
82
83 return this->convert_to_lux_(datahigh, datalow);
84}
85
86float MAX44009Sensor::convert_to_lux_(uint8_t data_high, uint8_t data_low) {
87 uint8_t exponent = data_high >> 4;
88 uint32_t mantissa = ((data_high & 0x0F) << 4) + (data_low & 0x0F);
89 return ((0x0001 << exponent) * 0.045) * mantissa;
90}
91
93 uint8_t config = this->read_(MAX44009_REGISTER_CONFIGURATION);
94 if (this->error_ == MAX44009_OK) {
95 config |= MAX44009_CFG_CONTINUOUS;
96 this->write_(MAX44009_REGISTER_CONFIGURATION, config);
98 ESP_LOGV(TAG, "set to continuous mode");
99 return true;
100 } else {
101 this->status_set_warning();
102 return false;
103 }
104}
105
107 uint8_t config = this->read_(MAX44009_REGISTER_CONFIGURATION);
108 if (this->error_ == MAX44009_OK) {
109 config &= ~MAX44009_CFG_CONTINUOUS;
110 this->write_(MAX44009_REGISTER_CONFIGURATION, config);
111 this->status_clear_warning();
112 ESP_LOGV(TAG, "set to low power mode");
113 return true;
114 } else {
115 this->status_set_warning();
116 return false;
117 }
118}
119
120uint8_t MAX44009Sensor::read_(uint8_t reg) {
121 uint8_t data = 0;
122 if (!this->read_byte(reg, &data)) {
123 this->error_ = MAX44009_ERROR_WIRE_REQUEST;
124 } else {
125 this->error_ = MAX44009_OK;
126 }
127 return data;
128}
129
130void MAX44009Sensor::write_(uint8_t reg, uint8_t value) {
131 if (!this->write_byte(reg, value)) {
132 this->error_ = MAX44009_ERROR_WIRE_REQUEST;
133 } else {
134 this->error_ = MAX44009_OK;
135 }
136}
137
139
140} // namespace max44009
141} // 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) const
Definition i2c.h:266
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:241
float read_illuminance_()
Read the illuminance value.
Definition max44009.cpp:66
void write_(uint8_t reg, uint8_t value)
Definition max44009.cpp:130
void set_mode(MAX44009Mode mode)
Definition max44009.cpp:138
float convert_to_lux_(uint8_t data_high, uint8_t data_low)
Definition max44009.cpp:86
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7