ESPHome 2025.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
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 ESP_LOGCONFIG(TAG, "Setting up MAX44009...");
25 bool state_ok = false;
27 state_ok = this->set_low_power_mode();
29 state_ok = this->set_continuous_mode();
30 } else {
31 /*
32 * Mode AUTO: Set mode depending on update interval
33 * - On low power mode, the IC measures lux intensity only once every 800ms
34 * regardless of integration time
35 * - On continuous mode, the IC continuously measures lux intensity
36 */
37 if (this->get_update_interval() < 800) {
38 state_ok = this->set_continuous_mode();
39 } else {
40 state_ok = this->set_low_power_mode();
41 }
42 }
43 if (!state_ok)
44 this->mark_failed();
45}
46
48 ESP_LOGCONFIG(TAG, "MAX44009:");
49 LOG_I2C_DEVICE(this);
50 if (this->is_failed()) {
51 ESP_LOGE(TAG, "Communication with MAX44009 failed!");
52 }
53}
54
56
58 // update sensor illuminance value
59 float lux = this->read_illuminance_();
60 if (this->error_ != MAX44009_OK) {
61 this->status_set_warning();
62 this->publish_state(NAN);
63 } else {
65 this->publish_state(lux);
66 }
67}
68
70 uint8_t datahigh = this->read_(MAX44009_LUX_READING_HIGH);
71 if (error_ != MAX44009_OK) {
72 this->error_ = MAX44009_ERROR_HIGH_BYTE;
73 return this->error_;
74 }
75 uint8_t datalow = this->read_(MAX44009_LUX_READING_LOW);
76 if (error_ != MAX44009_OK) {
77 this->error_ = MAX44009_ERROR_LOW_BYTE;
78 return this->error_;
79 }
80 uint8_t exponent = datahigh >> 4;
81 if (exponent == 0x0F) {
82 this->error_ = MAX44009_ERROR_OVERFLOW;
83 return this->error_;
84 }
85
86 return this->convert_to_lux_(datahigh, datalow);
87}
88
89float MAX44009Sensor::convert_to_lux_(uint8_t data_high, uint8_t data_low) {
90 uint8_t exponent = data_high >> 4;
91 uint32_t mantissa = ((data_high & 0x0F) << 4) + (data_low & 0x0F);
92 return ((0x0001 << exponent) * 0.045) * mantissa;
93}
94
96 uint8_t config = this->read_(MAX44009_REGISTER_CONFIGURATION);
97 if (this->error_ == MAX44009_OK) {
98 config |= MAX44009_CFG_CONTINUOUS;
99 this->write_(MAX44009_REGISTER_CONFIGURATION, config);
100 this->status_clear_warning();
101 ESP_LOGV(TAG, "set to continuous mode");
102 return true;
103 } else {
104 this->status_set_warning();
105 return false;
106 }
107}
108
110 uint8_t config = this->read_(MAX44009_REGISTER_CONFIGURATION);
111 if (this->error_ == MAX44009_OK) {
112 config &= ~MAX44009_CFG_CONTINUOUS;
113 this->write_(MAX44009_REGISTER_CONFIGURATION, config);
114 this->status_clear_warning();
115 ESP_LOGV(TAG, "set to low power mode");
116 return true;
117 } else {
118 this->status_set_warning();
119 return false;
120 }
121}
122
123uint8_t MAX44009Sensor::read_(uint8_t reg) {
124 uint8_t data = 0;
125 if (!this->read_byte(reg, &data)) {
126 this->error_ = MAX44009_ERROR_WIRE_REQUEST;
127 } else {
128 this->error_ = MAX44009_OK;
129 }
130 return data;
131}
132
133void MAX44009Sensor::write_(uint8_t reg, uint8_t value) {
134 if (!this->write_byte(reg, value)) {
135 this->error_ = MAX44009_ERROR_WIRE_REQUEST;
136 } else {
137 this->error_ = MAX44009_OK;
138 }
139}
140
142
143} // namespace max44009
144} // 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="unspecified")
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:55
float read_illuminance_()
Read the illuminance value.
Definition max44009.cpp:69
void write_(uint8_t reg, uint8_t value)
Definition max44009.cpp:133
void set_mode(MAX44009Mode mode)
Definition max44009.cpp:141
float convert_to_lux_(uint8_t data_high, uint8_t data_low)
Definition max44009.cpp:89
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:19
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7