2019-01-12 21:09:50 +00:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xsns_41_max44009.ino - MAX44009 ambient light sensor support for Tasmota
|
2019-01-12 21:09:50 +00:00
|
|
|
|
|
|
|
Copyright (C) 2019 Theo Arends
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
#ifdef USE_MAX44009
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* MAX44009 - Ambient Light Intensity
|
|
|
|
*
|
2019-01-23 15:06:06 +00:00
|
|
|
* I2C Address: 0x4a or 0x4b
|
2019-01-12 21:09:50 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-24 10:41:52 +00:00
|
|
|
#define XSNS_41 41
|
2019-11-03 16:54:39 +00:00
|
|
|
#define XI2C_28 28 // See I2CDEVICES.md
|
2019-01-12 21:09:50 +00:00
|
|
|
|
|
|
|
#define MAX44009_ADDR1 0x4A
|
|
|
|
#define MAX44009_ADDR2 0x4B
|
2019-01-19 22:27:12 +00:00
|
|
|
#define MAX44009_NO_REGISTERS 8
|
2019-01-23 21:20:29 +00:00
|
|
|
#define REG_CONFIG 0x02
|
|
|
|
#define REG_LUMINANCE 0x03
|
|
|
|
#define REG_LOWER_THRESHOLD 0x06
|
|
|
|
#define REG_THRESHOLD_TIMER 0x07
|
|
|
|
|
2019-01-12 21:09:50 +00:00
|
|
|
#define MAX44009_CONTINUOUS_AUTO_MODE 0x80 // Start measurement in automatic, continous mode
|
|
|
|
|
|
|
|
uint8_t max44009_address;
|
2019-01-19 22:27:12 +00:00
|
|
|
uint8_t max44009_addresses[] = { MAX44009_ADDR1, MAX44009_ADDR2, 0 }; //0 terminated list
|
|
|
|
uint8_t max44009_found = 0;
|
2019-01-12 21:09:50 +00:00
|
|
|
uint8_t max44009_valid = 0;
|
2019-01-24 10:41:52 +00:00
|
|
|
float max44009_illuminance = 0;
|
2019-01-12 21:09:50 +00:00
|
|
|
char max44009_types[] = "MAX44009";
|
|
|
|
|
|
|
|
bool Max4409Read_lum(void)
|
|
|
|
{
|
2019-01-19 22:27:12 +00:00
|
|
|
max44009_valid = 0;
|
2019-01-23 21:20:29 +00:00
|
|
|
uint8_t regdata[2];
|
|
|
|
|
|
|
|
/* Read 2 bytes luminance */
|
|
|
|
if (I2cValidRead16((uint16_t *)®data, max44009_address, REG_LUMINANCE)) {
|
|
|
|
int exponent = (regdata[0] & 0xF0) >> 4;
|
|
|
|
int mantissa = ((regdata[0] & 0x0F) << 4) | (regdata[1] & 0x0F);
|
2019-01-24 10:41:52 +00:00
|
|
|
max44009_illuminance = (float)(((0x00000001 << exponent) * (float)mantissa) * 0.045);
|
2019-01-23 21:20:29 +00:00
|
|
|
max44009_valid = 1;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2019-01-19 22:27:12 +00:00
|
|
|
}
|
2019-01-12 21:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
void Max4409Detect(void)
|
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
uint8_t reg[8];
|
2019-01-19 22:27:12 +00:00
|
|
|
bool failed = false;
|
|
|
|
|
|
|
|
if (max44009_found) {
|
2019-01-12 21:09:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-23 21:20:29 +00:00
|
|
|
uint8_t buffer1;
|
|
|
|
uint8_t buffer2;
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; 0 != max44009_addresses[i]; i++) {
|
2019-01-23 21:20:29 +00:00
|
|
|
|
2019-01-12 21:09:50 +00:00
|
|
|
max44009_address = max44009_addresses[i];
|
2019-01-19 22:27:12 +00:00
|
|
|
|
2019-01-23 21:20:29 +00:00
|
|
|
if ((I2cValidRead8(&buffer1, max44009_address, REG_LOWER_THRESHOLD)) &&
|
|
|
|
(I2cValidRead8(&buffer2, max44009_address, REG_THRESHOLD_TIMER))) {
|
|
|
|
//snprintf(log_data, sizeof(log_data), "MAX44009 %x: %x, %x", max44009_address, (int)buffer1, (int)buffer2);
|
|
|
|
//AddLog(LOG_LEVEL_DEBUG_MORE);
|
|
|
|
if ((0x00 == buffer1) &&
|
|
|
|
(0xFF == buffer2)) {
|
2019-01-24 10:41:52 +00:00
|
|
|
|
|
|
|
// looks like a MAX44009, try to initialize
|
|
|
|
|
2019-01-23 21:20:29 +00:00
|
|
|
Wire.beginTransmission(max44009_address);
|
|
|
|
|
2019-01-24 10:41:52 +00:00
|
|
|
// select configuration register and set mode
|
2019-01-23 21:20:29 +00:00
|
|
|
Wire.write(REG_CONFIG);
|
|
|
|
Wire.write(MAX44009_CONTINUOUS_AUTO_MODE);
|
|
|
|
if (0 == Wire.endTransmission()) {
|
|
|
|
max44009_found = 1;
|
2019-03-08 14:15:42 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, max44009_types, max44009_address);
|
2019-01-23 21:20:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-01-19 22:27:12 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 21:09:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Max4409EverySecond(void)
|
|
|
|
{
|
2019-01-23 15:06:06 +00:00
|
|
|
if (max44009_found) {
|
|
|
|
Max4409Read_lum();
|
2019-01-12 21:09:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void Max4409Show(bool json)
|
2019-01-12 21:09:50 +00:00
|
|
|
{
|
|
|
|
char illum_str[8];
|
|
|
|
|
|
|
|
if (max44009_valid) {
|
|
|
|
|
2019-01-23 15:06:06 +00:00
|
|
|
/* convert illuminance to string with suitable accuracy */
|
2019-01-23 21:20:29 +00:00
|
|
|
|
|
|
|
uint8_t prec = 0;
|
2019-01-24 10:41:52 +00:00
|
|
|
if (10 > max44009_illuminance ) {
|
2019-01-23 21:20:29 +00:00
|
|
|
prec = 3;
|
|
|
|
} else if (100 > max44009_illuminance) {
|
|
|
|
prec = 2;
|
|
|
|
} else if (1000 > max44009_illuminance) {
|
|
|
|
prec = 1;
|
2019-01-12 21:09:50 +00:00
|
|
|
}
|
2019-01-23 21:20:29 +00:00
|
|
|
dtostrf(max44009_illuminance, sizeof(illum_str) -1, prec, illum_str);
|
2019-01-12 21:09:50 +00:00
|
|
|
|
|
|
|
if (json) {
|
2019-03-23 16:57:31 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_ILLUMINANCE "\":%s}"), max44009_types, illum_str);
|
2019-01-12 21:09:50 +00:00
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
if (0 == tele_period) {
|
|
|
|
DomoticzSensor(DZ_ILLUMINANCE, illum_str);
|
|
|
|
}
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2019-01-23 15:06:06 +00:00
|
|
|
// show integer value for lx on web-server
|
2019-03-19 16:31:43 +00:00
|
|
|
WSContentSend_PD(HTTP_SNS_ILLUMINANCE, max44009_types, (int)max44009_illuminance);
|
2019-01-12 21:09:50 +00:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xsns41(uint8_t function)
|
2019-01-12 21:09:50 +00:00
|
|
|
{
|
2019-11-04 09:38:05 +00:00
|
|
|
if (!I2cEnabled(XI2C_28)) { return false; }
|
2019-11-03 16:54:39 +00:00
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2019-01-12 21:09:50 +00:00
|
|
|
|
2019-11-04 09:38:05 +00:00
|
|
|
switch (function) {
|
|
|
|
case FUNC_INIT:
|
|
|
|
Max4409Detect();
|
|
|
|
break;
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
Max4409EverySecond();
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
Max4409Show(1);
|
|
|
|
break;
|
2019-01-12 21:09:50 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
2019-11-04 09:38:05 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
Max4409Show(0);
|
|
|
|
break;
|
2019-01-12 21:09:50 +00:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_MAX44009
|
|
|
|
#endif // USE_I2C
|