Merge pull request #5083 from kevinbaluha/ads1115_multi

add functionality to use multiple ads1115's on one i2cbus
This commit is contained in:
Theo Arends 2019-02-08 12:29:15 +01:00 committed by GitHub
commit f587510d2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 34 deletions

View File

@ -119,7 +119,8 @@ CONFIG REGISTER
uint8_t ads1115_type = 0; uint8_t ads1115_type = 0;
uint8_t ads1115_address; uint8_t ads1115_address;
uint8_t ads1115_addresses[] = { ADS1115_ADDRESS_ADDR_GND, ADS1115_ADDRESS_ADDR_VDD, ADS1115_ADDRESS_ADDR_SDA, ADS1115_ADDRESS_ADDR_SCL }; uint8_t ads1115_addresses[] = { ADS1115_ADDRESS_ADDR_GND, ADS1115_ADDRESS_ADDR_VDD, ADS1115_ADDRESS_ADDR_SDA, ADS1115_ADDRESS_ADDR_SCL };
uint8_t ads1115_found[] = {false,false,false,false};
int16_t ads1115_values[4];
//Ads1115StartComparator(channel, ADS1115_REG_CONFIG_MODE_SINGLE); //Ads1115StartComparator(channel, ADS1115_REG_CONFIG_MODE_SINGLE);
//Ads1115StartComparator(channel, ADS1115_REG_CONFIG_MODE_CONTIN); //Ads1115StartComparator(channel, ADS1115_REG_CONFIG_MODE_CONTIN);
void Ads1115StartComparator(uint8_t channel, uint16_t mode) void Ads1115StartComparator(uint8_t channel, uint16_t mode)
@ -160,52 +161,84 @@ int16_t Ads1115GetConversion(uint8_t channel)
void Ads1115Detect(void) void Ads1115Detect(void)
{ {
uint16_t buffer; uint16_t buffer;
if (ads1115_type) {
return;
}
for (uint8_t i = 0; i < sizeof(ads1115_addresses); i++) { for (uint8_t i = 0; i < sizeof(ads1115_addresses); i++) {
ads1115_address = ads1115_addresses[i]; if (!ads1115_found[i]) {
if (I2cValidRead16(&buffer, ads1115_address, ADS1115_REG_POINTER_CONVERT)) { ads1115_address = ads1115_addresses[i];
Ads1115StartComparator(i, ADS1115_REG_CONFIG_MODE_CONTIN); if (I2cValidRead16(&buffer, ads1115_address, ADS1115_REG_POINTER_CONVERT) &&
ads1115_type = 1; I2cValidRead16(&buffer, ads1115_address, ADS1115_REG_POINTER_CONFIG)) {
snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "ADS1115", ads1115_address); Ads1115StartComparator(i, ADS1115_REG_CONFIG_MODE_CONTIN);
AddLog(LOG_LEVEL_DEBUG); ads1115_type = 1;
break; ads1115_found[i] = 1;
snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "ADS1115", ads1115_address);
AddLog(LOG_LEVEL_DEBUG);
}
} }
} }
} }
void Ads1115GetValues(uint8_t address)
{
uint8_t old_address = ads1115_address;
ads1115_address = address;
for (uint8_t i = 0; i < 4; i++) {
ads1115_values[i] = Ads1115GetConversion(i);
//snprintf_P(log_data, sizeof(log_data), "Logging ADS1115 %02x (%i) = %i", address, i, ads1115_values[i] );
//AddLog(LOG_LEVEL_INFO);
}
ads1115_address = old_address;
}
void Ads1115toJSON(char *comma_j)
{
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%s{"), mqtt_data,comma_j);
char *comma = (char*)"";
for (uint8_t i = 0; i < 4; i++) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%s\"A%d\":%d"), mqtt_data, comma, i, ads1115_values[i]);
comma = (char*)",";
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}"), mqtt_data);
}
void Ads1115toString(uint8_t address)
{
char label[15];
snprintf_P(label, sizeof(label), "ADS1115(%02x)", address);
for (uint8_t i = 0; i < 4; i++) {
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_ANALOG, mqtt_data, label, i, ads1115_values[i]);
}
}
void Ads1115Show(bool json) void Ads1115Show(bool json)
{ {
if (ads1115_type) { if (!ads1115_type) { return; }
char stemp[10];
uint8_t dsxflg = 0; if (json) {
for (uint8_t i = 0; i < 4; i++) { snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"ADS1115\":["), mqtt_data);
int16_t adc_value = Ads1115GetConversion(i); }
char *comma = (char*)"";
for (uint8_t t = 0; t < sizeof(ads1115_addresses); t++) {
//snprintf_P(log_data, sizeof(log_data), "Logging ADS1115 %02x", ads1115_addresses[t]);
//AddLog(LOG_LEVEL_INFO);
if (ads1115_found[t]) {
Ads1115GetValues(ads1115_addresses[t]);
if (json) { if (json) {
if (!dsxflg ) { Ads1115toJSON(comma);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"ADS1115\":{"), mqtt_data); comma = (char*)",";
stemp[0] = '\0'; }
}
dsxflg++;
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%s\"A%d\":%d"), mqtt_data, stemp, i, adc_value);
strlcpy(stemp, ",", sizeof(stemp));
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
} else { else {
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_ANALOG, mqtt_data, "ADS1115", i, adc_value); Ads1115toString(ads1115_addresses[t]);
}
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
}
}
if (json) {
if (dsxflg) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}"), mqtt_data);
}
} }
} }
if (json) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s]"), mqtt_data);
}
} }
/*********************************************************************************************\ /*********************************************************************************************\

View File

@ -64,12 +64,14 @@ bool Tsl2561Read(void)
void Tsl2561Detect(void) void Tsl2561Detect(void)
{ {
if (tsl2561_type) { return; } if (tsl2561_type) { return; }
uint8_t id;
if (I2cDevice(0x29) || I2cDevice(0x39) || I2cDevice(0x49)) { if (I2cDevice(0x29) || I2cDevice(0x39) || I2cDevice(0x49)) {
if (!Tsl.id(id)) return;
Tsl.begin(); Tsl.begin();
if (Tsl.on()) { if (Tsl.on()) {
tsl2561_type = 1; tsl2561_type = 1;
snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, tsl2561_types, Tsl.address()); snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, tsl2561_types, Tsl.address(), id);
AddLog(LOG_LEVEL_DEBUG); AddLog(LOG_LEVEL_DEBUG);
} }
} }