8static const char *
const TAG =
"max44009.sensor";
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;
15static const uint8_t MAX44009_CFG_CONTINUOUS = 0x80;
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;
24 bool state_ok =
false;
47 ESP_LOGCONFIG(TAG,
"MAX44009:");
50 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
57 if (this->
error_ != MAX44009_OK) {
67 uint8_t datahigh = this->
read_(MAX44009_LUX_READING_HIGH);
68 if (
error_ != MAX44009_OK) {
69 this->
error_ = MAX44009_ERROR_HIGH_BYTE;
72 uint8_t datalow = this->
read_(MAX44009_LUX_READING_LOW);
73 if (
error_ != MAX44009_OK) {
74 this->
error_ = MAX44009_ERROR_LOW_BYTE;
77 uint8_t exponent = datahigh >> 4;
78 if (exponent == 0x0F) {
79 this->
error_ = MAX44009_ERROR_OVERFLOW;
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;
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");
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);
112 ESP_LOGV(TAG,
"set to low power mode");
123 this->
error_ = MAX44009_ERROR_WIRE_REQUEST;
125 this->
error_ = MAX44009_OK;
132 this->
error_ = MAX44009_ERROR_WIRE_REQUEST;
134 this->
error_ = MAX44009_OK;
BedjetMode mode
BedJet operating mode.
virtual void mark_failed()
Mark this component as failed.
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
bool read_byte(uint8_t a_register, uint8_t *data)
float read_illuminance_()
Read the illuminance value.
void write_(uint8_t reg, uint8_t value)
void set_mode(MAX44009Mode mode)
void dump_config() override
bool set_continuous_mode()
bool set_low_power_mode()
float convert_to_lux_(uint8_t data_high, uint8_t data_low)
uint8_t read_(uint8_t reg)
void publish_state(float state)
Publish a new state to the front-end.
@ MAX44009_MODE_LOW_POWER
@ MAX44009_MODE_CONTINUOUS
Providing packet encoding functions for exchanging data with a remote host.