From 5f447e38a785cfc7103d5d044a891954eda97140 Mon Sep 17 00:00:00 2001 From: Nagy Attila Gabor Date: Fri, 21 Aug 2020 15:28:16 +0200 Subject: [PATCH] Ads1115 refactoring to avoid code duplication Signed-off-by: Nagy Attila Gabor --- tasmota/xsns_12_ads1115.ino | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/tasmota/xsns_12_ads1115.ino b/tasmota/xsns_12_ads1115.ino index 4de58ebc2..d6a251f79 100644 --- a/tasmota/xsns_12_ads1115.ino +++ b/tasmota/xsns_12_ads1115.ino @@ -180,6 +180,17 @@ void Ads1115Detect(void) } } +// Create the identifier of the the selected sensor +void Ads1115Label(char* label, uint32_t maxsize, uint8_t address) { + if (1 == Ads1115.count) { + // "ADS1115":{"A0":3240,"A1":3235,"A2":3269,"A3":3269} + snprintf_P(label, maxsize, PSTR("ADS1115")); + } else { + // "ADS1115-48":{"A0":3240,"A1":3235,"A2":3269,"A3":3269},"ADS1115-49":{"A0":3240,"A1":3235,"A2":3269,"A3":3269} + snprintf_P(label, maxsize, PSTR("ADS1115%c%02x"), IndexSeparator(), address); + } +} + #ifdef USE_RULES // Check every 250ms if there are relevant changes in any of the analog inputs // and if so then trigger a message @@ -208,13 +219,7 @@ void AdsEvery250ms(void) Ads1115.address = old_address; if (changed) { char label[15]; - if (1 == Ads1115.count) { - // "ADS1115":{"A0":3240,"A1":3235,"A2":3269,"A3":3269} - snprintf_P(label, sizeof(label), PSTR("ADS1115")); - } else { - // "ADS1115-48":{"A0":3240,"A1":3235,"A2":3269,"A3":3269},"ADS1115-49":{"A0":3240,"A1":3235,"A2":3269,"A3":3269} - snprintf_P(label, sizeof(label), PSTR("ADS1115%c%02x"), IndexSeparator(), Ads1115.addresses[t]); - } + Ads1115Label(label, sizeof(label), Ads1115.addresses[t]); Response_P(PSTR("{\"%s\":{"), label); @@ -252,13 +257,7 @@ void Ads1115Show(bool json) Ads1115.address = old_address; char label[15]; - if (1 == Ads1115.count) { - // "ADS1115":{"A0":3240,"A1":3235,"A2":3269,"A3":3269} - snprintf_P(label, sizeof(label), PSTR("ADS1115")); - } else { - // "ADS1115-48":{"A0":3240,"A1":3235,"A2":3269,"A3":3269},"ADS1115-49":{"A0":3240,"A1":3235,"A2":3269,"A3":3269} - snprintf_P(label, sizeof(label), PSTR("ADS1115%c%02x"), IndexSeparator(), Ads1115.addresses[t]); - } + Ads1115Label(label, sizeof(label), Ads1115.addresses[t]); if (json) { ResponseAppend_P(PSTR(",\"%s\":{"), label);