/// © MiroZ 2024

#include "Bmp280.h"

#include <esp_log.h>

static const char *TAG = "bme280";

Bmp280::Bmp280(TwoWire & bus) : m_bus(bus)
{
	m_sensor = new BMP280_DEV(bus);
}

bool Bmp280::init()
{
	uint8_t status = m_sensor->begin(0x76);
	m_sensor->setPresOversampling(OVERSAMPLING_X4);    // Set the pressure oversampling to X4
	m_sensor->setTempOversampling(OVERSAMPLING_X1);  	// Set the temperature oversampling to X1
	m_sensor->setIIRFilter(IIR_FILTER_2);              // Set the IIR filter to setting 4
	m_sensor->setTimeStandby(TIME_STANDBY_62MS);
	m_sensor->startNormalConversion();                 // Start BMP280 continuous conversion in NORMAL_MODE

	m_operational = status == 1;

	return m_operational;
}

bool Bmp280::read()
{
	float temp, pressure;

	if(m_sensor->getTempPres(temp, pressure))
	{
		char buffer[64];

		static float prev = 1000;
		prev = pressure*0.1+prev*0.9;

		sprintf(buffer, "%0.3f,%0.3f\n", pressure, prev);
		printf(buffer);
	}
	//ESP_LOGI(TAG, "temp: %0.3f, pressure: %0.3f", temp*1.8+32, pressure);
	
	return true;
}