Update xdrv_26_lm75ad.ino to support C or F

Update xdrv_26_lm75ad.ino to support C or F depending on Settings.flag.temperature_conversion (#define TEMP_CONVERSION 0 or 1) as stored in [SetOption8] Return temperature in (0 = Celsius or 1 = Fahrenheit)
This commit is contained in:
andrethomas 2018-06-05 14:50:52 +02:00 committed by GitHub
parent c860927857
commit 289b662f10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 129 additions and 126 deletions

View File

@ -1,127 +1,130 @@
/* /*
xdrv_26_lm75ad.ino - Support for I2C LM75AD Temperature Sensor xdrv_26_lm75ad.ino - Support for I2C LM75AD Temperature Sensor
Copyright (C) 2018 Theo Arends Copyright (C) 2018 Theo Arends
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifdef USE_I2C #ifdef USE_I2C
#ifdef USE_LM75AD #ifdef USE_LM75AD
/*********************************************************************************************\ /*********************************************************************************************\
* LM75AD * LM75AD
\*********************************************************************************************/ \*********************************************************************************************/
#define LM75AD_ADDRESS1 0x48 #define LM75AD_ADDRESS1 0x48
#define LM75AD_ADDRESS2 0x49 #define LM75AD_ADDRESS2 0x49
#define LM75AD_ADDRESS3 0x4A #define LM75AD_ADDRESS3 0x4A
#define LM75AD_ADDRESS4 0x4B #define LM75AD_ADDRESS4 0x4B
#define LM75AD_ADDRESS5 0x4C #define LM75AD_ADDRESS5 0x4C
#define LM75AD_ADDRESS6 0x4D #define LM75AD_ADDRESS6 0x4D
#define LM75AD_ADDRESS7 0x4E #define LM75AD_ADDRESS7 0x4E
#define LM75AD_ADDRESS8 0x4F #define LM75AD_ADDRESS8 0x4F
#define LM75_TEMP_REGISTER 0x00 #define LM75_TEMP_REGISTER 0x00
#define LM75_CONF_REGISTER 0x01 #define LM75_CONF_REGISTER 0x01
#define LM75_THYST_REGISTER 0x02 #define LM75_THYST_REGISTER 0x02
#define LM75_TOS_REGISTER 0x03 #define LM75_TOS_REGISTER 0x03
uint8_t lm75ad_type = 0; uint8_t lm75ad_type = 0;
uint8_t lm75ad_address; uint8_t lm75ad_address;
uint8_t lm75ad_addresses[] = { LM75AD_ADDRESS1, LM75AD_ADDRESS2, LM75AD_ADDRESS3, LM75AD_ADDRESS4, LM75AD_ADDRESS5, LM75AD_ADDRESS6, LM75AD_ADDRESS7, LM75AD_ADDRESS8 }; uint8_t lm75ad_addresses[] = { LM75AD_ADDRESS1, LM75AD_ADDRESS2, LM75AD_ADDRESS3, LM75AD_ADDRESS4, LM75AD_ADDRESS5, LM75AD_ADDRESS6, LM75AD_ADDRESS7, LM75AD_ADDRESS8 };
int16_t LM75ADGetTempRegister(void) int16_t LM75ADGetTempRegister(void)
{ {
uint16_t res = I2cRead16(lm75ad_address, LM75_TEMP_REGISTER); uint16_t res = I2cRead16(lm75ad_address, LM75_TEMP_REGISTER);
return (int16_t)res; return (int16_t)res;
} }
void LM75ADDetect() void LM75ADDetect()
{ {
uint8_t buffer; uint8_t buffer;
if (lm75ad_type) { if (lm75ad_type) {
return; return;
} }
for (byte i = 0; i < sizeof(lm75ad_addresses); i++) { for (byte i = 0; i < sizeof(lm75ad_addresses); i++) {
lm75ad_address = lm75ad_addresses[i]; lm75ad_address = lm75ad_addresses[i];
if (I2cValidRead8(&buffer,lm75ad_address,LM75_CONF_REGISTER)) { if (I2cValidRead8(&buffer,lm75ad_address,LM75_CONF_REGISTER)) {
lm75ad_type = 1; lm75ad_type = 1;
snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "LM75AD", lm75ad_address); snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "LM75AD", lm75ad_address);
AddLog(LOG_LEVEL_DEBUG); AddLog(LOG_LEVEL_DEBUG);
break; break;
} }
} }
} }
float LM75ADConvertTemp(uint16_t t) { float LM75ADConvertTemp(uint16_t t) {
float tmpt; float tmpt;
if (t & 0x8000) { // we are getting a negative temperature value if (t & 0x8000) { // we are getting a negative temperature value
t=t<<1; // shift out the MSB t=t<<1; // shift out the MSB
t=t>>6; // shift value into place (5 LSB not used + the MSB we shifted out to make it zero) t=t>>6; // shift value into place (5 LSB not used + the MSB we shifted out to make it zero)
tmpt=t*0.125*-1; tmpt=t*0.125*-1;
} else { // we're getting a positive value } else { // we're getting a positive value
t=t>>5; // shift value into place (5 LSB not used) t=t>>5; // shift value into place (5 LSB not used)
tmpt=t*0.125; tmpt=t*0.125;
} }
return tmpt; if (!isnan(tmpt) && Settings.flag.temperature_conversion) {
} tmpt = tmpt * 1.8 + 32; // Fahrenheit
}
void LM75ADShow(boolean json) return tmpt;
{ }
if (lm75ad_type) {
char temperature[10]; void LM75ADShow(boolean json)
uint16_t lm75ad_value = LM75ADGetTempRegister(); {
float t=LM75ADConvertTemp(lm75ad_value); if (lm75ad_type) {
dtostrfd(t, Settings.flag2.temperature_resolution, temperature); char temperature[10];
if (json) { uint16_t lm75ad_value = LM75ADGetTempRegister();
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"LM75AD\":{\"" D_JSON_TEMPERATURE "\":%s}"), mqtt_data, temperature); float t=LM75ADConvertTemp(lm75ad_value);
#ifdef USE_WEBSERVER dtostrfd(t, Settings.flag2.temperature_resolution, temperature);
} else { if (json) {
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TEMP, mqtt_data, "LM75AD", temperature, TempUnit()); snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"LM75AD\":{\"" D_JSON_TEMPERATURE "\":%s}"), mqtt_data, temperature);
#endif // USE_WEBSERVER #ifdef USE_WEBSERVER
} } else {
} snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TEMP, mqtt_data, "LM75AD", temperature, TempUnit());
} #endif // USE_WEBSERVER
}
/*********************************************************************************************\ }
* Interface }
\*********************************************************************************************/
/*********************************************************************************************\
#define XSNS_26 * Interface
\*********************************************************************************************/
boolean Xsns26(byte function)
{ #define XSNS_26
boolean result = false;
boolean Xsns26(byte function)
if (i2c_flg) { {
switch (function) { boolean result = false;
case FUNC_PREP_BEFORE_TELEPERIOD:
LM75ADDetect(); if (i2c_flg) {
break; switch (function) {
case FUNC_JSON_APPEND: case FUNC_PREP_BEFORE_TELEPERIOD:
LM75ADShow(1); LM75ADDetect();
break; break;
#ifdef USE_WEBSERVER case FUNC_JSON_APPEND:
case FUNC_WEB_APPEND: LM75ADShow(1);
LM75ADShow(0); break;
break; #ifdef USE_WEBSERVER
#endif // USE_WEBSERVER case FUNC_WEB_APPEND:
} LM75ADShow(0);
} break;
return result; #endif // USE_WEBSERVER
} }
}
#endif // USE_LM75AD return result;
}
#endif // USE_LM75AD
#endif // USE_I2C #endif // USE_I2C