Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added check connection state for i2c devices #120

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/BME280.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,23 @@ bool BME280::Initialize()
{
bool success(true);

success &= ReadChipID();
if(IsConnected()) {
success &= ReadChipID();

if(success)
{
success &= ReadTrim();

if(m_settings.filter != Filter_Off)
if(success)
{
InitializeFilter();
success &= ReadTrim();

if(m_settings.filter != Filter_Off)
{
InitializeFilter();
}

WriteSettings();
}

WriteSettings();
}

m_initialized = success;
m_initialized = success;
}

return m_initialized;
}
Expand Down
6 changes: 5 additions & 1 deletion src/BME280.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class BME280
virtual bool Initialize();

///////////////////////////////////////////////////////////////
/// Force a unfiltered measurement to populate the filter
/// Force a unfiltered measurement to populate the filter
/// buffer.
bool InitializeFilter();

Expand Down Expand Up @@ -282,6 +282,10 @@ class BME280
uint8_t data[],
uint8_t length)=0;

/////////////////////////////////////////////////////////////////
/// check if BME280 is connected and available.
virtual bool IsConnected(void)=0;


/*****************************************************************/
/* WORKER FUNCTIONS */
Expand Down
13 changes: 13 additions & 0 deletions src/BME280I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ BME280I2C::BME280I2C
):BME280(settings),
m_settings(settings)
{
Wire.begin();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is on the user, per previous issue

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My investigations showed that Wire.begin() is necessary to have a non blocking behaviour for Wire.endTransmission().
I just tested it here again.

}


Expand Down Expand Up @@ -100,3 +101,15 @@ bool BME280I2C::ReadRegister

return ord == length;
}


/****************************************************************/
bool BME280I2C::IsConnected(void)
{
bool connected = false;
Wire.beginTransmission(m_settings.bme280Addr);
if(0 == Wire.endTransmission())
connected = true;

return connected;
}
4 changes: 4 additions & 0 deletions src/BME280I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,9 @@ class BME280I2C: public BME280
uint8_t data[],
uint8_t length);

/////////////////////////////////////////////////////////////////
/// check if BME280 is connected and available.
virtual bool IsConnected(void);

};
#endif // TG_BME_280_I2C_H
12 changes: 12 additions & 0 deletions src/BME280I2C_BRZO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,16 @@ bool BME280I2C_BRZO::ReadRegister
return (brzo_i2c_end_transaction()==0);
}


/****************************************************************/
bool BME280I2C_BRZO::IsConnected(void)
{
bool connected = false;
brzo_i2c_start_transaction(m_settings.bme280Addr, m_settings.i2cClockRate);
if(0 == brzo_i2c_end_transaction())
connected = true;

return connected;
}

#endif
4 changes: 4 additions & 0 deletions src/BME280I2C_BRZO.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,9 @@ class BME280I2C_BRZO : public BME280I2C
uint8_t data[],
uint8_t length);

/////////////////////////////////////////////////////////////////
/// check if BME280 is connected and available.
virtual bool IsConnected(void);

};
#endif // BME280I2C_BRZO_H
8 changes: 8 additions & 0 deletions src/BME280Spi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,11 @@ bool BME280Spi::WriteRegister

return true;
}


/****************************************************************/
bool BME280Spi::IsConnected(void) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these are defaulted to true, this indicates to me that IsConnected shouldn't be in the base class.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct. Now it's implemented much simpler! Thank you for your annotation.

// not possible here. Connection state can be discovered by reading the
// chip ID which is done during initalization
return true;
}
4 changes: 4 additions & 0 deletions src/BME280Spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,9 @@ class BME280Spi: public BME280
uint8_t addr,
uint8_t data);

/////////////////////////////////////////////////////////////////
/// check if BME280 is connected and available.
virtual bool IsConnected(void);

};
#endif // TG_BME_280_SPI_H
9 changes: 8 additions & 1 deletion src/BME280SpiSw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ bool BME280SpiSw::WriteRegister
// de-select the device
digitalWrite(m_settings.spiCsPin, HIGH);

return true;
return true;
}


/****************************************************************/
bool BME280SpiSw::IsConnected(void) {
// not possible here. Connection state can be discovered by reading the
// chip ID which is done during initalization
return true;
}
4 changes: 4 additions & 0 deletions src/BME280SpiSw.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,9 @@ class BME280SpiSw: public BME280{
uint8_t addr,
uint8_t data);

/////////////////////////////////////////////////////////////////
/// check if BME280 is connected and available.
virtual bool IsConnected(void);

};
#endif // TG_BME_280_SPI_H