|
@ -40,28 +40,32 @@ class I2CSensor : public BaseSensor { |
|
|
// Specific for I2C sensors |
|
|
// Specific for I2C sensors |
|
|
unsigned char _begin_i2c(unsigned char address, size_t size, unsigned char * addresses) { |
|
|
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 |
|
|
// Check if we should release a previously locked address |
|
|
if (_previous_address != address) { |
|
|
|
|
|
|
|
|
if ((_previous_address > 0) && (_previous_address != address)) { |
|
|
i2cReleaseLock(_previous_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; |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|