/*
 * bme280.c
 *
 *  Created on: Nov 30, 2022
 *      Author: thomm
 */


//#include <stdint.h>

/* resources...
 *  https://github.com/boschsensortec/BME280_driver/blob/master/bme280.c
 *  https://github.com/eziya/STM32_HAL_BME280/blob/master/Src/BME280/bme280.c
 *  https://github.com/vuquangtrong/tiva-c/blob/master/7_Sensors/Sensor/BMP280.cpp
 *  https://github.com/khoitd1997/BMP280_driver_TivaC/blob/master/src/TivaC_I2C.c
 *  https://github.com/JVKran/Forced-BME280/blob/master/src/forcedClimate.cpp
 *  https://drive.google.com/file/d/1mXrVDlCyYWgZkumhETqR3W0fhO84YPFa/view
 *  https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf
 *
 *
 * Pressure range: 300 … 1100 hPa (equivalent to +9000…-500m above/below sea level)
        Pressure resolution: 0.01 hPa ( < 10 cm)
        Temperature range: -40 … 85 °C
        Temperature resolution: 0.01 °C
        Humidity range: 0 … 100 %
        Interface: I2C and SPI
        Supply voltage range: 1.71 … 3.6 V

        Pressure is measured in hectoPascals (hPa), also called millibars.
        Standard pressure at sea level is defined as 1013hPa, but we can see large areas of either high or low pressure.


 */

#include <stdint.h>
#include <stdbool.h>
#include "system.h"
#include "user_io.h"
#include "uartstdio.h"  // User local version with larger RX buffer
#include "inc/hw_i2c.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "driverlib/i2c.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"

#include "board_util.h"
#include "i2c0.h"
#include "i2c_bme280.h"

extern void delay_msec(unsigned int msec);

#if BOARD_MSC == 3
extern int mmsDbgFlg;
#endif

// From Schematic: bme280 i2c address: 1110110x
//#define BME280_I2C_ADDRESS  0xEC        // Tiva API requires this to be bit shifted for 7bit address

struct bme280_data bme280_data;

void bme280_soak_test(void)
{
    int ret;
    float tempr, pressr;
    uint32_t humid;
    uint32_t cnt;
    // char input;

    prn_nl();
    cnt=0;
    while(1)
    {
        //uprintf("Imp es nada bme280_soak_test in i2c_bme280.c"); prn_nl();
        ret = bme280_getValues(&humid, &tempr, &pressr);


        uprintf("%07u, H=%u, T=%7.2f, P=%7.1f", cnt, humid, tempr, pressr);   prn_nl();
        cnt++;

        if( UARTRxBytesAvail() )
        {
            // input =
            get_key();
            break;    // break from while(1)
        }

        if(ret <= 0)
        {
            delay_msec(100);
        }

        wait_consoleTx();
    }



    //bme280_test();

}

void bme280_dev_test(void)
{
    //i2c0_busScan();

    //bme280_init(&bme280_data);

    bme280_readChipID(1);
    bme280_readStatus(1);
    bme280_readConfig(1);
    bme280_readCtrlMeas(1);

    bme280_test();
}


void bme280_test(void)
{
    int ret;

    // char input;
    int32_t temperature;
    int32_t pressure, humidity;

    while(1)
    {
        ret = bme280_sample_fetch(&bme280_data);
        if(ret <= 0)
        {
            temperature = 0;
            pressure    = 0;
            humidity    = 0;
        }
        else
        {
            temperature = bme280_get_temperature(&bme280_data);
            pressure = bme280_get_pressure(&bme280_data);
            humidity = bme280_get_humidity(&bme280_data);
        }

        uprintf("bme280 temperature:   %4d.%02d  C\r\n", temperature/100, temperature%100);
        uprintf("bme280 humidity:      %4d       percent RH\r\n", humidity/1024);
        uprintf("bme280 pressure:      %4d.%02d  hPa\r\n", (pressure/256)/100, (pressure/256)%100);

        if( UARTRxBytesAvail() )
        {
            // input =
            get_key();
            break;    // break from while(1)
        }

        delay_msec(1000);
        wait_consoleTx();
    }
}

//int bme280_init(struct bme280_data *data)
// return 0 if OK
int bme280_init(void)
{
    int ret;
    struct bme280_data *data;

    pwr_env_mon_on();       //   // Turn on Load switch for ENV monitoring, PB5

    data = &bme280_data;
    data->chip_id = 0x00;

    ret = bme280_read_byte(BME280_REG_CHIPID, &data->chip_id);
    if(ret <= 0)
    {
        return 0;
    }

    if (data->chip_id == BME280_CHIPID_VAL)
    {
        //uprintf("BME280 chip detected\r\n");
    }
    else if ( (data->chip_id == BMP280_CHIP_ID_MP) || (data->chip_id == BMP280_CHIP_ID_SAMPLE_1) )
    {
        //uprintf("BMP280 chip detected\r\n");
    }
    else
    {
        // FUTURE: create a bme280 disable flag
        //uprintf("BME280: bad chip id 0x%x\r\n", data->chip_id);
        return 0;
    }

    ret = bme280_read_compensation(data);
    if(ret <= 0)
    {
        return ret;
    }

    if (data->chip_id == BME280_CHIPID_VAL)
    {
        ret = bme280_write_byte(BME280_REG_CTRL_HUM, BME280_HUMIDITY_OVER);       // 1x oversample (011 written into reg)
        if(ret <= 0)
        {
            return ret;
        }
    }

    ret = bme280_setup();
    if(ret <= 0)
    {
        return ret;
    }

    return 1;
}

int bme280_setup(void)
{
    int ret;

    // Setup the BME280
    ret = bme280_write_byte(BME280_REG_CTRL_MEAS, BME280_CTRL_MEAS_VAL);      // Press Over, Temp Over, MODE Forced
    if(ret <= 0)
    {
        return ret;
    }
    ret = bme280_write_byte(BME280_REG_CONFIG, BME280_CONFIG_VAL);
    if(ret <= 0)
    {
        return ret;
    }

    return 1;
}


// called by bme_init
int bme280_read_compensation(struct bme280_data *data)
{
    int ret;

    uint16_t buf[16];       //12
    uint8_t hbuf[8];        //7
    uint8_t bVal;

    while(1)
    {
        // Poll status reg
        ret = bme280_readStatus2(&bVal);
        if(ret <= 0)
        {
            return ret;
        }

        // is a copy to registers in progress (bit.0=1)?
        if ((bVal & 0x01) == 0x00 )
        {
            break;
        }
    }

    ret = bme280_read_buf(BME280_REG_COMP_START, (uint8_t *)buf, sizeof(buf));
    if(ret <= 0)
    {
        return -EIO;
    }

    data->dig_t1 = buf[0];
    data->dig_t2 = buf[1];
    data->dig_t3 = buf[2];
    data->dig_p1 = buf[3];
    data->dig_p2 = buf[4];
    data->dig_p3 = buf[5];
    data->dig_p4 = buf[6];
    data->dig_p5 = buf[7];
    data->dig_p6 = buf[8];
    data->dig_p7 = buf[9];
    data->dig_p8 = buf[10];
    data->dig_p9 = buf[11];

    if (data->chip_id == BME280_CHIPID_VAL)
    {
        ret = bme280_read_byte(BME280_REG_HUM_COMP_PART1, &data->dig_h1);
        if(ret <= 0)
        {
            return -EIO;
        }
        ret = bme280_read_buf(BME280_REG_HUM_COMP_PART2, hbuf, 7);
        if(ret <= 0)
        {
            return -EIO;
        }

        data->dig_h2 = (hbuf[1] << 8) | hbuf[0];
        data->dig_h3 = hbuf[2];
        data->dig_h4 = (hbuf[3] << 4) | (hbuf[4] & 0x0F);
        data->dig_h5 = ((hbuf[4] >> 4) & 0x0F) | (hbuf[5] << 4);
        data->dig_h6 = hbuf[6];
    }

    return 1;
}


/*
 * Compensation code taken from BME280 datasheet, Section 4.2.3
 * "Compensation formula".
 */
void bme280_calc_temperature(struct bme280_data *data, int32_t adc_temp)
{
    int32_t var1, var2;

    var1 = (((adc_temp >> 3) - ((int32_t)data->dig_t1 << 1)) *
        ((int32_t)data->dig_t2)) >> 11;
    var2 = (((((adc_temp >> 4) - ((int32_t)data->dig_t1)) *
          ((adc_temp >> 4) - ((int32_t)data->dig_t1))) >> 12) *
        ((int32_t)data->dig_t3)) >> 14;

    data->t_fine = var1 + var2;
    data->comp_temp = (data->t_fine * 5 + 128) >> 8;
}

void bme280_calc_pressure(struct bme280_data *data, int32_t adc_press)
{
    int64_t var1, var2, p;

    var1 = ((int64_t)data->t_fine) - 128000;
    var2 = var1 * var1 * (int64_t)data->dig_p6;
    var2 = var2 + ((var1 * (int64_t)data->dig_p5) << 17);
    var2 = var2 + (((int64_t)data->dig_p4) << 35);
    var1 = ((var1 * var1 * (int64_t)data->dig_p3) >> 8) +
        ((var1 * (int64_t)data->dig_p2) << 12);
    var1 = (((((int64_t)1) << 47) + var1)) * ((int64_t)data->dig_p1) >> 33;

    /* Avoid exception caused by division by zero. */
    if (var1 == 0) {
        data->comp_press = 0;
        return;
    }

    p = 1048576 - adc_press;
    p = (((p << 31) - var2) * 3125) / var1;
    var1 = (((int64_t)data->dig_p9) * (p >> 13) * (p >> 13)) >> 25;
    var2 = (((int64_t)data->dig_p8) * p) >> 19;
    p = ((p + var1 + var2) >> 8) + (((int64_t)data->dig_p7) << 4);

    data->comp_press = (uint32_t)p;
}

void bme280_calc_humidity(struct bme280_data *data, int32_t adc_humidity)
{
    int32_t h;

    h = (data->t_fine - ((int32_t)76800));
    h = ((((adc_humidity << 14) - (((int32_t)data->dig_h4) << 20) -
        (((int32_t)data->dig_h5) * h)) + ((int32_t)16384)) >> 15) *
        (((((((h * ((int32_t)data->dig_h6)) >> 10) * (((h *
        ((int32_t)data->dig_h3)) >> 11) + ((int32_t)32768))) >> 10) +
        ((int32_t)2097152)) * ((int32_t)data->dig_h2) + 8192) >> 14);
    h = (h - (((((h >> 15) * (h >> 15)) >> 7) *
        ((int32_t)data->dig_h1)) >> 4));
    h = (h > 419430400 ? 419430400 : h);

    data->comp_humidity = (uint32_t)(h >> 12);
}


int bme280_getValues(uint32_t *humid, float *tempr, float *pressr)
{
    int ret;
#if BOARD_MSC == 3
    if(mmsDbgFlg == 1)
#endif
        {uprintf("Imp Es Nada bme280_getValues() i2c_bme280.c, hangs on revA baseboard, hardware prob?"); prn_nl(); }

    ret = bme280_sample_fetch(&bme280_data);
    if(ret <= 0)
    {
        *humid  = 0;
        *tempr  = 0;
        *pressr = 0;
    }
    else
    {
        *humid  = bme280_get_uiHumidity();
        *tempr  = bme280_get_fTemperature();
        *pressr = bme280_get_fPressure();
    }

    return ret;
}

#ifdef NEWCODE
int bme280_read_data(float *humid, float *tempr, float *pressr)
{
    bme280_sample_fetch(&bme280_data);
    *humid = (float)bme280_data.comp_humidity;
    *tempr = (float)bme280_data.comp_temp;
    *pressr = (float)bme280_data.comp_press;
    return 0;
}
#endif

int bme280_read_data(void)
{
    int ret = bme280_sample_fetch(&bme280_data);
    return ret;
}

int bme280_sample_fetch(struct bme280_data *data)
{
    int ret;
    uint8_t buf[8];
    int32_t adc_press, adc_temp, adc_humidity;
    int size = 6;       // for bmp280
    uint8_t bVal;

    if (data->chip_id == BME280_CHIPID_VAL) {
        size = 8;
    }

    // Operating in FORCED_MODE requires the FORCED mode setting to be written
    ret = bme280_write_byte(BME280_REG_CTRL_MEAS, BME280_CTRL_MEAS_VAL);
    if(ret <= 0)
    {
        return ret;
    }

    delay_msec(500);

    while(1)
    {
        // is a conversion running (bit.3=1)?
        ret = bme280_read_byte(BME280_REG_STATUS, &bVal);
        if(ret <= 0)
        {
            return ret;
        }

        if ((bVal & 0x03) == 0x00 )
        {
            break;
        }
    }

    if (bme280_read_buf(BME280_REG_PRESS_MSB, buf, size) <= 0)
    {
        return ret;
    }

    adc_press = (buf[0] << 12) | (buf[1] << 4) | (buf[2] >> 4);
    adc_temp = (buf[3] << 12) | (buf[4] << 4) | (buf[5] >> 4);

    bme280_calc_temperature(data, adc_temp);
    bme280_calc_pressure(data, adc_press);

    if (data->chip_id == BME280_CHIPID_VAL)
    {
        adc_humidity = (buf[6] << 8) | buf[7];
        bme280_calc_humidity(data, adc_humidity);
    }

    return 1;
}

/* returns temperature in 0.01 degC from struct, assumes bme280_sample_fetch has been called*/
int32_t bme280_get_temperature(struct bme280_data *data)
{
    return data->comp_temp;
}

/* returns pressure in 1/256 Pa */
uint32_t bme280_get_pressure(struct bme280_data *data)
{
    return data->comp_press;
}

/* returns humidity in 1/1024 %RH */
uint32_t bme280_get_humidity(struct bme280_data *data)
{
    return data->comp_humidity;
}



/* returns temperature in 0.01 degC from struct, assumes bme280_sample_fetch has been called*/
float bme280_get_fTemperature(void)
{
    int32_t temperature;
    float T;

    temperature = bme280_get_temperature(&bme280_data);
    T = (float)temperature;
    T /= 100;

    //uprintf("bme280 temperature:   %4d.%02d C  %8.2f\r\n", temperature/100, temperature%100, T);

    return(T);
}

/* returns pressure in hPa, assumes bme280_sample_fetch has been called*/
float bme280_get_fPressure(void)
{
    uint32_t pressure;
    float P;

    pressure = bme280_get_pressure(&bme280_data);

    //uprintf("bme280 pressure:      %4d.%02d  hPa\r\n", (pressure/256)/100, (pressure/256)%100);

    P = (float)pressure;
    P /= 256.0;
    P /= 100.0;


    return(P);
}

/* returns humidity in percent relative humidity, assumes bme280_sample_fetch has been called*/
uint32_t bme280_get_uiHumidity(void)
{
    uint32_t humidity;

    humidity = bme280_get_humidity(&bme280_data);

    //uprintf("bme280 humidity:      %4d       percent RH\r\n", humidity/1024);

    return(humidity/1024);
}

//-----------------------------------------//
//Main API (aside from bme280_init() for APF usecase
//was int HIH6130_ReadTempHumidity(&temp, &humi);
int bme280_ReadTempHumidity(float *Temp, float *Humi)
{
    int ret = 0;

    float fTempr;
    float fHumid;

    ret = bme280_read_data();     //read bme280 data into struct
    if(ret <= 0)
    {
        *Temp = -100.0f;
        *Humi = -1.0f;
        return 0;
    }

    fTempr = bme280_get_fTemperature();
    if( (fTempr > -20.0f) && (fTempr < 125.0f))
    {
        *Temp = fTempr;
    }
    else
    {
        *Temp = -100.0f;  //CF2 sets it to -1 which is a realistic temperature for the APF
        ret = 0;
    }

    fHumid = (float)bme280_get_uiHumidity();
    if( (fHumid >= 0.0f) && (fHumid < 100.0f))
    {
        *Humi = fHumid;
    }
    else
    {
        *Humi = fHumid = -1.0f;
        ret = 0;
    }

    // indicate if we had an oob temp or humidity
    if(ret <= 0)
    {
        return 0;
    }

    return 1;
}

//
// calculates dew point
// input:   humidity [%RH], temperature [C]
// output:  dew point [C]
// feed args in the same order as ReadTempHumidity above
// was calc_dewpoint(float h, float t) in CF2
float bme280_calc_dewpoint(float tempr, float humid)
{
    float logEx,dew_point;

    logEx = ((0.66077+7.5*tempr)/(237.3+tempr)) + (log10(humid)-2);

    dew_point = ((logEx - 0.66077) * 237.3) / (0.66077 + 7.5 - logEx);

    return( dew_point );

}



// writes 1 byte '_data' to register 'reg_addr'
int bme280_write_byte(uint8_t reg_addr, uint8_t device_data)
{
    return i2c0_write_reg8(BME280_I2C_7BIT_ADDR, reg_addr, device_data);
}

// reads 8 bits from register 'reg_addr'
//uint8_t bme280_read_byte(uint8_t device_register)
int bme280_read_byte(uint8_t reg_addr, uint8_t *data)
{
    return i2c0_read_reg8(BME280_I2C_7BIT_ADDR, reg_addr, data);
}

int bme280_read_buf(uint8_t reg_addr, uint8_t *data, uint8_t len)
{
    return i2c0_write_then_read(BME280_I2C_7BIT_ADDR, &reg_addr, 1, data, len);
}

uint8_t bme280_readChipID(int prnFlg)
{
    int ret;
    uint8_t  chipId;

    ret = bme280_read_byte(BME280_REG_CHIPID, &chipId);

    //TODO: handle errors
    if(ret <= 0)
    {
        chipId = 0;
    }

    if(prnFlg) uprintf("\r\nBME280 Chip ID = 0x%02x, reading reg returns: 0x%02x\r\n", BME280_CHIPID_VAL, chipId );

    return(chipId);
}

uint8_t bme280_readStatus(int prnFlg)
{
    int ret;
    uint8_t  status;

    ret = bme280_read_byte(BME280_REG_STATUS, &status);

    //TODO: handle errors
    if(ret <= 0)
    {
        status = 0;
    }

    if(prnFlg) uprintf("\r\nBME280 Status = 0x%02x\r\n", status );

    return(status);
}


uint8_t bme280_readConfig(int prnFlg)
{
    int ret;
    uint8_t  config;

    ret = bme280_read_byte(BME280_REG_CONFIG, &config);

    //TODO: handle errors
    if(ret <= 0)
    {
        config = 0;
    }

    if(prnFlg) uprintf("\r\nBME280 Config = 0x%02x\r\n", config );

    return(config);
}


uint8_t bme280_readCtrlMeas(int prnFlg)
{
    int ret;
    uint8_t  bVal;

    ret = bme280_read_byte(BME280_REG_CONFIG, &bVal);

    //TODO: handle errors
    if(ret <= 0)
    {
        bVal = 0;
    }

    if(prnFlg) uprintf("\r\nBME280 CTRL MEAS = 0x%02x\r\n", bVal );

    return(bVal);
}

int bme280_readStatus2(uint8_t* out_status)
{
    int ret;
    ret = bme280_read_byte(BME280_REG_STATUS, out_status);
    if(ret <= 0)
    {
        return 0;
    }

    return 1;
}









#ifdef JUNKCODE
/*!
 * @brief This API is used to calculate the maximum delay in milliseconds required for the
 * temperature/pressure/humidity(which ever at enabled) measurement to complete.
 */
uint32_t bme280_calc_meas_delay(const struct bme280_settings *settings)
{
    uint32_t max_delay;
    uint8_t temp_osr;
    uint8_t pres_osr;
    uint8_t hum_osr;

    /*Array to map OSR config register value to actual OSR */
    uint8_t osr_sett_to_act_osr[] = { 0, 1, 2, 4, 8, 16 };

    /* Mapping osr settings to the actual osr values e.g. 0b101 -> osr X16  */
    if (settings->osr_t <= 5)
    {
        temp_osr = osr_sett_to_act_osr[settings->osr_t];
    }
    else
    {
        temp_osr = 16;
    }

    if (settings->osr_p <= 5)
    {
        pres_osr = osr_sett_to_act_osr[settings->osr_p];
    }
    else
    {
        pres_osr = 16;
    }

    if (settings->osr_h <= 5)
    {
        hum_osr = osr_sett_to_act_osr[settings->osr_h];
    }
    else
    {
        hum_osr = 16;
    }

    max_delay =
        (uint32_t)((BME280_MEAS_OFFSET + (BME280_MEAS_DUR * temp_osr) +
                    ((BME280_MEAS_DUR * pres_osr) + BME280_PRES_HUM_MEAS_OFFSET) +
                    ((BME280_MEAS_DUR * hum_osr) + BME280_PRES_HUM_MEAS_OFFSET)) / BME280_MEAS_SCALING_FACTOR);

    return max_delay;
}


// BME280 sensor setup function.
void bme280_setup(uint8_t mode, uint8_t T_sampling, uint8_t H_sampling, uint8_t P_sampling, uint8_t filter, uint8_t standby)
{
    uint8_t _ctrl_hum, _ctrl_meas, _config;

    _ctrl_hum = H_sampling;
    _config = ((standby << 5) | (filter << 2)) & 0xFC;
    _ctrl_meas = (T_sampling << 5) | (P_sampling << 2) | mode;

    bme280_write_uint8(BME280_REG_CTRLHUM, _ctrl_hum);
    bme280_write_uint8(BME280_REG_CONFIG,  _config);
    bme280_write_uint8(BME280_REG_CONTROL, _ctrl_meas);
}

/*!
 * @brief This internal API puts the device to sleep mode.
 */
int8_t bme280_sleep(const struct bme280_dev *dev)
{
    int8_t rslt;
    uint8_t reg_data[4];

    struct bme280_settings settings;


    rslt = bme280_get_regs(BME280_CTRL_HUM_ADDR, reg_data, 4, dev);
    if (rslt == BME280_OK) {
        parse_device_settings(reg_data, &settings);
        rslt = bme280_soft_reset(dev);
        if (rslt == BME280_OK)
            rslt = reload_device_settings(&settings, dev);
    }

    return rslt;
}


/*!
 * @brief This internal API puts the device to sleep mode.
 */
static int8_t put_device_to_sleep(struct bme280_dev *dev)
{
    int8_t rslt;
    uint8_t reg_data[4];
    struct bme280_settings settings;

    rslt = bme280_get_regs(BME280_CTRL_HUM_ADDR, reg_data, 4, dev);

    if (rslt == BME280_OK)
    {
        parse_device_settings(reg_data, &settings);
        rslt = bme280_soft_reset(dev);

        if (rslt == BME280_OK)
        {
            rslt = reload_device_settings(&settings, dev);
        }
    }

    return rslt;
}


/*
 * Compensation code taken from BME280 datasheet, Section 4.2.3
 * "Compensation formula".
 */
static void bme280_compensate_temp(struct bme280_data *data, int32_t adc_temp)
{
    int32_t var1, var2;

    var1 = (((adc_temp >> 3) - ((int32_t)data->dig_t1 << 1)) *
        ((int32_t)data->dig_t2)) >> 11;
    var2 = (((((adc_temp >> 4) - ((int32_t)data->dig_t1)) *
          ((adc_temp >> 4) - ((int32_t)data->dig_t1))) >> 12) *
        ((int32_t)data->dig_t3)) >> 14;

    data->t_fine = var1 + var2;
    data->comp_temp = (data->t_fine * 5 + 128) >> 8;
}

static void bme280_compensate_press(struct bme280_data *data, int32_t adc_press)
{
    int64_t var1, var2, p;

    var1 = ((int64_t)data->t_fine) - 128000;
    var2 = var1 * var1 * (int64_t)data->dig_p6;
    var2 = var2 + ((var1 * (int64_t)data->dig_p5) << 17);
    var2 = var2 + (((int64_t)data->dig_p4) << 35);
    var1 = ((var1 * var1 * (int64_t)data->dig_p3) >> 8) +
        ((var1 * (int64_t)data->dig_p2) << 12);
    var1 = (((((int64_t)1) << 47) + var1)) * ((int64_t)data->dig_p1) >> 33;

    /* Avoid exception caused by division by zero. */
    if (var1 == 0) {
        data->comp_press = 0;
        return;
    }

    p = 1048576 - adc_press;
    p = (((p << 31) - var2) * 3125) / var1;
    var1 = (((int64_t)data->dig_p9) * (p >> 13) * (p >> 13)) >> 25;
    var2 = (((int64_t)data->dig_p8) * p) >> 19;
    p = ((p + var1 + var2) >> 8) + (((int64_t)data->dig_p7) << 4);

    data->comp_press = (uint32_t)p;
}

static void bme280_compensate_humidity(struct bme280_data *data,
                       int32_t adc_humidity)
{
    int32_t h;

    h = (data->t_fine - ((int32_t)76800));
    h = ((((adc_humidity << 14) - (((int32_t)data->dig_h4) << 20) -
        (((int32_t)data->dig_h5) * h)) + ((int32_t)16384)) >> 15) *
        (((((((h * ((int32_t)data->dig_h6)) >> 10) * (((h *
        ((int32_t)data->dig_h3)) >> 11) + ((int32_t)32768))) >> 10) +
        ((int32_t)2097152)) * ((int32_t)data->dig_h2) + 8192) >> 14);
    h = (h - (((((h >> 15) * (h >> 15)) >> 7) *
        ((int32_t)data->dig_h1)) >> 4));
    h = (h > 419430400 ? 419430400 : h);

    data->comp_humidity = (uint32_t)(h >> 12);
}


float bme280_calcAltitude(float pressure)
{
    float A, B, C;

    A = pressure / 101325;
    B = 1 / 5.25588;
    C = pow(A, B);
    C = 1.0 - C;
    C = C / 0.0000225577;

    return C;
}


#endif





