2018-06-25 16:35:42 +01:00
|
|
|
/*
|
|
|
|
xsns_26_lm75ad.ino - Support for I2C LM75AD Temperature Sensor
|
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Andre Thomas and Theo Arends
|
2018-06-25 16:35:42 +01:00
|
|
|
|
|
|
|
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_LM75AD
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* LM75AD - Temperature
|
|
|
|
*
|
|
|
|
* Docs at https://www.nxp.com/docs/en/data-sheet/LM75A.pdf
|
|
|
|
*
|
|
|
|
* I2C Address: 0x48 - 0x4F
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-06 16:33:51 +00:00
|
|
|
#define XSNS_26 26
|
2019-11-03 16:54:39 +00:00
|
|
|
#define XI2C_20 20 // See I2CDEVICES.md
|
2018-11-06 16:33:51 +00:00
|
|
|
|
2018-06-25 16:35:42 +01:00
|
|
|
#define LM75AD_ADDRESS1 0x48
|
|
|
|
#define LM75AD_ADDRESS2 0x49
|
|
|
|
#define LM75AD_ADDRESS3 0x4A
|
|
|
|
#define LM75AD_ADDRESS4 0x4B
|
|
|
|
#define LM75AD_ADDRESS5 0x4C
|
|
|
|
#define LM75AD_ADDRESS6 0x4D
|
|
|
|
#define LM75AD_ADDRESS7 0x4E
|
|
|
|
#define LM75AD_ADDRESS8 0x4F
|
|
|
|
|
|
|
|
#define LM75_TEMP_REGISTER 0x00
|
|
|
|
#define LM75_CONF_REGISTER 0x01
|
|
|
|
#define LM75_THYST_REGISTER 0x02
|
|
|
|
#define LM75_TOS_REGISTER 0x03
|
|
|
|
|
2019-11-11 10:44:40 +00:00
|
|
|
bool lm75ad_type = false;
|
2018-06-25 16:35:42 +01:00
|
|
|
uint8_t lm75ad_address;
|
|
|
|
uint8_t lm75ad_addresses[] = { LM75AD_ADDRESS1, LM75AD_ADDRESS2, LM75AD_ADDRESS3, LM75AD_ADDRESS4, LM75AD_ADDRESS5, LM75AD_ADDRESS6, LM75AD_ADDRESS7, LM75AD_ADDRESS8 };
|
|
|
|
|
2018-09-20 19:44:17 +01:00
|
|
|
void LM75ADDetect(void)
|
2018-06-25 16:35:42 +01:00
|
|
|
{
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < sizeof(lm75ad_addresses); i++) {
|
2018-06-25 16:35:42 +01:00
|
|
|
lm75ad_address = lm75ad_addresses[i];
|
2019-12-31 13:23:34 +00:00
|
|
|
if (I2cActive(lm75ad_address)) {
|
2019-11-23 17:55:48 +00:00
|
|
|
continue; }
|
|
|
|
if (!I2cSetDevice(lm75ad_address)) {
|
2021-02-23 07:08:28 +00:00
|
|
|
continue; // do not make the next step without a confirmed device on the bus
|
2019-11-23 17:55:48 +00:00
|
|
|
}
|
2019-11-09 17:34:22 +00:00
|
|
|
uint16_t buffer;
|
2018-08-04 18:07:47 +01:00
|
|
|
if (I2cValidRead16(&buffer, lm75ad_address, LM75_THYST_REGISTER)) {
|
|
|
|
if (buffer == 0x4B00) {
|
2019-11-11 10:44:40 +00:00
|
|
|
lm75ad_type = true;
|
2019-11-09 17:34:22 +00:00
|
|
|
I2cSetActiveFound(lm75ad_address, "LM75AD");
|
2018-08-04 18:07:47 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-06-25 16:35:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-11 16:32:44 +00:00
|
|
|
float LM75ADGetTemp(void)
|
|
|
|
{
|
2018-06-25 16:35:42 +01:00
|
|
|
int16_t sign = 1;
|
|
|
|
|
|
|
|
uint16_t t = I2cRead16(lm75ad_address, LM75_TEMP_REGISTER);
|
|
|
|
if (t & 0x8000) { // we are getting a negative temperature value
|
|
|
|
t = (~t) +0x20;
|
|
|
|
sign = -1;
|
|
|
|
}
|
|
|
|
t = t >> 5; // shift value into place (5 LSB not used)
|
|
|
|
return ConvertTemp(sign * t * 0.125);
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void LM75ADShow(bool json)
|
2018-06-25 16:35:42 +01:00
|
|
|
{
|
2019-11-11 16:32:44 +00:00
|
|
|
float t = LM75ADGetTemp();
|
2018-06-25 16:35:42 +01:00
|
|
|
|
2019-11-11 16:32:44 +00:00
|
|
|
if (json) {
|
2021-06-11 17:14:12 +01:00
|
|
|
ResponseAppend_P(JSON_SNS_F_TEMP, "LM75AD", Settings->flag2.temperature_resolution, &t);
|
2018-06-25 16:35:42 +01:00
|
|
|
#ifdef USE_DOMOTICZ
|
2021-01-26 15:26:00 +00:00
|
|
|
if (0 == TasmotaGlobal.tele_period) DomoticzFloatSensor(DZ_TEMP, t);
|
2018-06-25 16:35:42 +01:00
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_WEBSERVER
|
2019-11-11 16:32:44 +00:00
|
|
|
} else {
|
2021-01-26 15:26:00 +00:00
|
|
|
WSContentSend_Temp("LM75AD", t);
|
2018-06-25 16:35:42 +01:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xsns26(uint8_t function)
|
2018-06-25 16:35:42 +01:00
|
|
|
{
|
2019-11-04 09:38:05 +00:00
|
|
|
if (!I2cEnabled(XI2C_20)) { return false; }
|
2019-11-03 16:54:39 +00:00
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2018-06-25 16:35:42 +01:00
|
|
|
|
2019-11-11 16:32:44 +00:00
|
|
|
if (FUNC_INIT == function) {
|
|
|
|
LM75ADDetect();
|
|
|
|
}
|
|
|
|
else if (lm75ad_type) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
LM75ADShow(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
LM75ADShow(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
2018-06-25 16:35:42 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_LM75AD
|
|
|
|
#endif // USE_I2C
|