@@ -5,7 +5,7 @@
float pressureToAltitude ( float pressure ) {
if ( ! pressure ) return 0 ; // If the pressure module has been disabled return '0'
pressure / = 100.0F ; // Convert [Pa] to [hPa]
return 44330.0 * ( 1.0 - pow ( pressure / 1013.25 , 0.1903 ) ) ; // С alculate altitude
return 44330.0f * ( 1.0f - pow ( pressure / 1013.25f , 0.1903f ) ) ; // С alculate altitude
}
float pressureToMmHg ( float pressure ) {
@@ -14,14 +14,17 @@ float pressureToMmHg(float pressure) {
/* ============ Setup & begin ============ */
bool GyverBME280 : : begin ( void ) {
GyverBME280 : : GyverBME280 ( TwoWire & wire ) {
_wire = & wire ;
}
bool GyverBME280 : : begin ( ) {
return begin ( 0x76 ) ;
}
bool GyverBME280 : : begin ( uint8_t address ) {
_i2c_address = address ;
/* === Start I2C bus & check BME280 === */
Wire . begin ( ) ; // Start I2C bus
if ( ! reset ( ) ) return false ; // BME280 software reset & ack check
uint8_t ID = readRegister ( 0xD0 ) ;
if ( ID ! = 0x60 & & ID ! = 0x58 ) return false ; // Check chip ID (bme/bmp280)
@@ -108,11 +111,11 @@ float GyverBME280::readPressure(void) {
float GyverBME280 : : readHumidity ( void ) {
W ire. beginTransmission ( _i2c_address ) ; // Start I2C transmission
W ire. write ( 0xFD ) ; // Request humidity data register
if ( W ire. endTransmission ( ) ! = 0 ) return 0 ;
W ire. requestFrom ( _i2c_address , 2 ) ; // Request humidity data
int32_t hum_raw = ( ( uint16_t ) W ire. read ( ) < < 8 ) | ( uint16_t ) W ire. read ( ) ; // Read humidity data
_w ire- > beginTransmission ( _i2c_address ) ; // Start I2C transmission
_w ire- > write ( 0xFD ) ; // Request humidity data register
if ( _w ire- > endTransmission ( ) ! = 0 ) return 0 ;
_w ire- > requestFrom ( _i2c_address , 2 ) ; // Request humidity data
int32_t hum_raw = ( ( uint16_t ) _w ire- > read ( ) < < 8 ) | ( uint16_t ) _w ire- > read ( ) ; // Read humidity data
if ( hum_raw = = 0x8000 ) return 0 ; // If the humidity module has been disabled return '0'
int32_t value = ( readTempInt ( ) - ( ( int32_t ) 76800 ) ) ; // Start humidity converting
@@ -139,8 +142,6 @@ void GyverBME280::oneMeasurement(void) {
writeRegister ( 0xF4 , ( ( readRegister ( 0xF4 ) & 0xFC ) | 0x02 ) ) ; // Set the operating mode to FORCED_MODE
}
GyverBME280 : : GyverBME280 ( ) { }
/* ============ Private ============ */
/* = BME280 software reset = */
@@ -153,66 +154,66 @@ bool GyverBME280::reset(void) {
/* = Read and combine three BME280 registers = */
uint32_t GyverBME280 : : readRegister24 ( uint8_t address ) {
W ire. beginTransmission ( _i2c_address ) ;
W ire. write ( address ) ;
if ( W ire. endTransmission ( ) ! = 0 ) return 0x800000 ;
W ire. requestFrom ( _i2c_address , 3 ) ;
return ( ( ( uint32_t ) W ire. read ( ) < < 16 ) | ( ( uint32_t ) W ire. read ( ) < < 8 ) | ( uint32_t ) W ire. read ( ) ) ;
_w ire- > beginTransmission ( _i2c_address ) ;
_w ire- > write ( address ) ;
if ( _w ire- > endTransmission ( ) ! = 0 ) return 0x800000 ;
_w ire- > requestFrom ( _i2c_address , 3 ) ;
return ( ( ( uint32_t ) _w ire- > read ( ) < < 16 ) | ( ( uint32_t ) _w ire- > read ( ) < < 8 ) | ( uint32_t ) _w ire- > read ( ) ) ;
}
/* = Write one 8-bit BME280 register = */
bool GyverBME280 : : writeRegister ( uint8_t address , uint8_t data ) {
W ire. beginTransmission ( _i2c_address ) ;
W ire. write ( address ) ;
W ire. write ( data ) ;
if ( W ire. endTransmission ( ) ! = 0 ) return false ;
_w ire- > beginTransmission ( _i2c_address ) ;
_w ire- > write ( address ) ;
_w ire- > write ( data ) ;
if ( _w ire- > endTransmission ( ) ! = 0 ) return false ;
return true ;
}
/* = Read one 8-bit BME280 register = */
uint8_t GyverBME280 : : readRegister ( uint8_t address ) {
W ire. beginTransmission ( _i2c_address ) ;
W ire. write ( address ) ;
if ( W ire. endTransmission ( ) ! = 0 ) return 0 ;
W ire. requestFrom ( _i2c_address , 1 ) ;
return W ire. read ( ) ;
_w ire- > beginTransmission ( _i2c_address ) ;
_w ire- > write ( address ) ;
if ( _w ire- > endTransmission ( ) ! = 0 ) return 0 ;
_w ire- > requestFrom ( _i2c_address , 1 ) ;
return _w ire- > read ( ) ;
}
/* = Structure to store all calibration values = */
void GyverBME280 : : readCalibrationData ( void ) {
/* first part request*/
W ire. beginTransmission ( _i2c_address ) ;
W ire. write ( 0x88 ) ;
if ( W ire. endTransmission ( ) ! = 0 ) return ;
W ire. requestFrom ( _i2c_address , 25 ) ;
_w ire- > beginTransmission ( _i2c_address ) ;
_w ire- > write ( 0x88 ) ;
if ( _w ire- > endTransmission ( ) ! = 0 ) return ;
_w ire- > requestFrom ( _i2c_address , 25 ) ;
/* reading */
CalibrationData . _T1 = ( W ire. read ( ) | ( W ire. read ( ) < < 8 ) ) ;
CalibrationData . _T2 = ( W ire. read ( ) | ( W ire. read ( ) < < 8 ) ) ;
CalibrationData . _T3 = ( W ire. read ( ) | ( W ire. read ( ) < < 8 ) ) ;
CalibrationData . _P1 = ( W ire. read ( ) | ( W ire. read ( ) < < 8 ) ) ;
CalibrationData . _P2 = ( W ire. read ( ) | ( W ire. read ( ) < < 8 ) ) ;
CalibrationData . _P3 = ( W ire. read ( ) | ( W ire. read ( ) < < 8 ) ) ;
CalibrationData . _P4 = ( W ire. read ( ) | ( W ire. read ( ) < < 8 ) ) ;
CalibrationData . _P5 = ( W ire. read ( ) | ( W ire. read ( ) < < 8 ) ) ;
CalibrationData . _P6 = ( W ire. read ( ) | ( W ire. read ( ) < < 8 ) ) ;
CalibrationData . _P7 = ( W ire. read ( ) | ( W ire. read ( ) < < 8 ) ) ;
CalibrationData . _P8 = ( W ire. read ( ) | ( W ire. read ( ) < < 8 ) ) ;
CalibrationData . _P9 = ( W ire. read ( ) | ( W ire. read ( ) < < 8 ) ) ;
CalibrationData . _H1 = W ire. read ( ) ;
CalibrationData . _T1 = ( _w ire- > read ( ) | ( _w ire- > read ( ) < < 8 ) ) ;
CalibrationData . _T2 = ( _w ire- > read ( ) | ( _w ire- > read ( ) < < 8 ) ) ;
CalibrationData . _T3 = ( _w ire- > read ( ) | ( _w ire- > read ( ) < < 8 ) ) ;
CalibrationData . _P1 = ( _w ire- > read ( ) | ( _w ire- > read ( ) < < 8 ) ) ;
CalibrationData . _P2 = ( _w ire- > read ( ) | ( _w ire- > read ( ) < < 8 ) ) ;
CalibrationData . _P3 = ( _w ire- > read ( ) | ( _w ire- > read ( ) < < 8 ) ) ;
CalibrationData . _P4 = ( _w ire- > read ( ) | ( _w ire- > read ( ) < < 8 ) ) ;
CalibrationData . _P5 = ( _w ire- > read ( ) | ( _w ire- > read ( ) < < 8 ) ) ;
CalibrationData . _P6 = ( _w ire- > read ( ) | ( _w ire- > read ( ) < < 8 ) ) ;
CalibrationData . _P7 = ( _w ire- > read ( ) | ( _w ire- > read ( ) < < 8 ) ) ;
CalibrationData . _P8 = ( _w ire- > read ( ) | ( _w ire- > read ( ) < < 8 ) ) ;
CalibrationData . _P9 = ( _w ire- > read ( ) | ( _w ire- > read ( ) < < 8 ) ) ;
CalibrationData . _H1 = _w ire- > read ( ) ;
/* second part request*/
W ire. beginTransmission ( _i2c_address ) ;
W ire. write ( 0xE1 ) ;
W ire. endTransmission ( ) ;
W ire. requestFrom ( _i2c_address , 8 ) ;
_w ire- > beginTransmission ( _i2c_address ) ;
_w ire- > write ( 0xE1 ) ;
_w ire- > endTransmission ( ) ;
_w ire- > requestFrom ( _i2c_address , 8 ) ;
/* reading */
CalibrationData . _H2 = ( W ire. read ( ) | ( W ire. read ( ) < < 8 ) ) ;
CalibrationData . _H3 = W ire. read ( ) ;
CalibrationData . _H4 = ( W ire. read ( ) < < 4 ) ;
uint8_t interVal = W ire. read ( ) ;
CalibrationData . _H2 = ( _w ire- > read ( ) | ( _w ire- > read ( ) < < 8 ) ) ;
CalibrationData . _H3 = _w ire- > read ( ) ;
CalibrationData . _H4 = ( _w ire- > read ( ) < < 4 ) ;
uint8_t interVal = _w ire- > read ( ) ;
CalibrationData . _H4 | = ( interVal & 0xF ) ;
CalibrationData . _H5 = ( ( ( interVal & 0xF0 ) > > 4 ) | ( W ire. read ( ) < < 4 ) ) ;
CalibrationData . _H6 = W ire. read ( ) ;
CalibrationData . _H5 = ( ( ( interVal & 0xF0 ) > > 4 ) | ( _w ire- > read ( ) < < 4 ) ) ;
CalibrationData . _H6 = _w ire- > read ( ) ;
}