Skip to content

Commit

Permalink
Moving address locking to init method in BMX280 sensor (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoseperez committed Mar 3, 2018
1 parent c63188b commit 812d8c2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
17 changes: 13 additions & 4 deletions code/espurna/sensors/BMX280Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ class BMX280Sensor : public I2CSensor {
_dirty = false;
_chip = 0;

// I2C auto-discover
_address = _begin_i2c(_address, sizeof(BMX280Sensor::addresses), BMX280Sensor::addresses);
if (_address == 0) return;

// Init
_init();

Expand Down Expand Up @@ -194,13 +190,26 @@ class BMX280Sensor : public I2CSensor {
// Make sure sensor had enough time to turn on. BMX280 requires 2ms to start up
delay(10);

// I2C auto-discover
_address = _begin_i2c(_address, sizeof(BMX280Sensor::addresses), BMX280Sensor::addresses);
if (_address == 0) return;

// Check sensor correctly initialized
_chip = i2c_read_uint8(_address, BMX280_REGISTER_CHIPID);
if ((_chip != BMX280_CHIP_BME280) && (_chip != BMX280_CHIP_BMP280)) {

_chip = 0;
i2cReleaseLock(_address);
_previous_address = 0;
_error = SENSOR_ERROR_UNKNOWN_ID;

// Setting _address to 0 forces auto-discover
// This might be necessary at this stage if there is a
// different sensor in the hardcoded address
_address = 0;

return;

}

_count = 0;
Expand Down
32 changes: 18 additions & 14 deletions code/espurna/sensors/I2CSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,32 @@ class I2CSensor : public BaseSensor {
// Specific for I2C sensors
unsigned char _begin_i2c(unsigned char address, size_t size, unsigned char * addresses) {

// If we have already locked this address for this sensor quit
if ((address > 0) && (address == _previous_address)) {
return _previous_address;
}

// Check if we should release a previously locked address
if (_previous_address != address) {
if ((_previous_address > 0) && (_previous_address != address)) {
i2cReleaseLock(_previous_address);
_previous_address = 0;
}

// If we have already an address, check it is not locked
if (address && !i2cGetLock(address)) {
_error = SENSOR_ERROR_I2C;

// If we don't have an address...
} else {

// Trigger auto-discover
address = i2cFindAndLock(size, addresses);
// If requesting a specific address, try to ger a lock to it
if ((0 < address) && i2cGetLock(address)) {
_previous_address = address;
return _previous_address;
}

// If still nothing exit with error
if (address == 0) _error = SENSOR_ERROR_I2C;
// If everything else fails, perform an auto-discover
_previous_address = i2cFindAndLock(size, addresses);

// Flag error
if (0 == _previous_address) {
_error = SENSOR_ERROR_I2C;
}

_previous_address = address;
return address;
return _previous_address;

}

Expand Down
10 changes: 9 additions & 1 deletion code/espurna/sensors/SI7021Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,17 @@ class SI7021Sensor : public I2CSensor {
_chip = i2c_read_uint8(_address);

if ((_chip != SI7021_CHIP_SI7021) & (_chip != SI7021_CHIP_HTU21D)) {

_count = 0;
i2cReleaseLock(_address);
_previous_address = 0;
_error = SENSOR_ERROR_UNKNOWN_ID;
_count = 0;

// Setting _address to 0 forces auto-discover
// This might be necessary at this stage if there is a
// different sensor in the hardcoded address
_address = 0;

} else {
_count = 2;
}
Expand Down

0 comments on commit 812d8c2

Please sign in to comment.