Add I2C bus2 support to BH1750

This commit is contained in:
Theo Arends 2023-10-19 18:01:12 +02:00
parent b39adacc41
commit e3253fea8f
3 changed files with 30 additions and 19 deletions

View File

@ -6,7 +6,8 @@ All notable changes to this project will be documented in this file.
## [13.2.0.1]
### Added
- I2C bus2 support to iAQ core sensor (#19799)
- I2C bus2 support to HTU temperature sensor
- I2C bus2 support to HTU temperature and humidity sensor
- I2C bus2 support to BH1750 ambient light sensor
### Breaking Changed

View File

@ -114,7 +114,8 @@ The latter links can be used for OTA upgrades too like ``OtaUrl https://ota.tasm
### Added
- Experimental support for ESP32-C2 and ESP32-C6 using Arduino core v3
- I2C bus2 support to iAQ core sensor [#19799](https://github.com/arendst/Tasmota/issues/19799)
- I2C bus2 support to HTU temperature sensor
- I2C bus2 support to HTU temperature and humidity sensor
- I2C bus2 support to BH1750 ambient light sensor
### Breaking Changed

View File

@ -62,6 +62,7 @@ struct {
struct {
uint8_t address;
uint8_t bus;
uint8_t valid = 0;
uint8_t mtreg = 69; // Default Measurement Time
uint16_t illuminance = 0;
@ -78,29 +79,33 @@ uint8_t Bh1750Resolution(uint32_t sensor_index) {
}
bool Bh1750SetResolution(uint32_t sensor_index) {
Wire.beginTransmission(Bh1750_sensors[sensor_index].address);
Wire.write(Bh1750.resolution[Bh1750Resolution(sensor_index)]);
return (!Wire.endTransmission());
TwoWire& myWire = I2cGetWire(Bh1750_sensors[sensor_index].bus);
myWire.beginTransmission(Bh1750_sensors[sensor_index].address);
myWire.write(Bh1750.resolution[Bh1750Resolution(sensor_index)]);
return (!myWire.endTransmission());
}
bool Bh1750SetMTreg(uint32_t sensor_index) {
Wire.beginTransmission(Bh1750_sensors[sensor_index].address);
TwoWire& myWire = I2cGetWire(Bh1750_sensors[sensor_index].bus);
if (&myWire == nullptr) { return false; } // No valid I2c bus
myWire.beginTransmission(Bh1750_sensors[sensor_index].address);
uint8_t data = BH1750_MEASUREMENT_TIME_HIGH | ((Bh1750_sensors[sensor_index].mtreg >> 5) & 0x07);
Wire.write(data);
if (Wire.endTransmission()) { return false; }
Wire.beginTransmission(Bh1750_sensors[sensor_index].address);
myWire.write(data);
if (myWire.endTransmission()) { return false; }
myWire.beginTransmission(Bh1750_sensors[sensor_index].address);
data = BH1750_MEASUREMENT_TIME_LOW | (Bh1750_sensors[sensor_index].mtreg & 0x1F);
Wire.write(data);
if (Wire.endTransmission()) { return false; }
myWire.write(data);
if (myWire.endTransmission()) { return false; }
return Bh1750SetResolution(sensor_index);
}
bool Bh1750Read(uint32_t sensor_index) {
if (Bh1750_sensors[sensor_index].valid) { Bh1750_sensors[sensor_index].valid--; }
if (2 != Wire.requestFrom(Bh1750_sensors[sensor_index].address, (uint8_t)2)) { return false; }
TwoWire& myWire = I2cGetWire(Bh1750_sensors[sensor_index].bus);
if (2 != myWire.requestFrom(Bh1750_sensors[sensor_index].address, (uint8_t)2)) { return false; }
float illuminance = (Wire.read() << 8) | Wire.read();
float illuminance = (myWire.read() << 8) | myWire.read();
illuminance *= 57.5 / (float)Bh1750_sensors[sensor_index].mtreg; // Fix #16022
if (1 == Bh1750Resolution(sensor_index)) {
illuminance /= 2;
@ -114,13 +119,17 @@ bool Bh1750Read(uint32_t sensor_index) {
/********************************************************************************************/
void Bh1750Detect(void) {
for (uint32_t bus = 0; bus < 2; bus++) {
for (uint32_t i = 0; i < sizeof(Bh1750.addresses); i++) {
if (!I2cSetDevice(Bh1750.addresses[i])) { continue; }
if (!I2cSetDevice(Bh1750.addresses[i], bus)) { continue; }
Bh1750_sensors[Bh1750.count].address = Bh1750.addresses[i];
Bh1750_sensors[Bh1750.count].bus = bus;
if (Bh1750SetMTreg(Bh1750.count)) {
I2cSetActiveFound(Bh1750_sensors[Bh1750.count].address, Bh1750.types);
I2cSetActiveFound(Bh1750_sensors[Bh1750.count].address, Bh1750.types, Bh1750_sensors[Bh1750.count].bus);
Bh1750.count++;
if (2 == Bh1750.count) { return; }
}
}
}
}