esp-idf-bmx280/include/bmx280.h

150 lines
4.2 KiB
C
Raw Normal View History

2020-11-06 11:04:05 +01:00
/**
* BMX280 - BME280 & BMP280 Driver for Esspressif ESP-32.
2020-12-11 19:23:00 +01:00
*
* MIT License
*
* Copyright (C) 2020 Halit Utku Maden
* Please contact at <utkumaden@hotmail.com>
2020-11-06 11:04:05 +01:00
*/
2020-12-11 19:23:00 +01:00
2020-11-06 11:04:05 +01:00
#ifndef _BMX280_H_
#define _BMX280_H_
#ifdef __cplusplus
extern "C" {
#endif
2020-11-06 11:04:05 +01:00
#include <stdint.h>
2020-11-19 11:33:02 +01:00
#include <limits.h>
2024-11-25 11:30:11 +01:00
#include <assert.h>
2020-11-06 11:04:05 +01:00
#include "sdkconfig.h"
2024-11-24 12:45:29 +01:00
#include "bmx280_bits.h"
2024-11-25 11:30:11 +01:00
#if !(CONFIG_USE_I2C_MASTER_DRIVER)
2024-11-24 12:45:29 +01:00
#include "driver/i2c.h"
#else
#include "driver/i2c_master.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#endif
#define BMXAPI extern
2020-11-06 11:04:05 +01:00
/**
* Anonymous structure to driver settings.
*/
typedef struct bmx280_t bmx280_t;
2024-11-25 11:30:11 +01:00
#if CONFIG_USE_I2C_MASTER_DRIVER
2020-11-19 11:33:02 +01:00
/**
* Create an instance of the BMX280 driver.
2024-11-25 11:30:11 +01:00
* @param bus_handle The I2C master handle via port.
2020-11-19 11:33:02 +01:00
* @return A non-null pointer to the driver structure on success.
*/
2024-11-25 11:30:11 +01:00
BMXAPI bmx280_t* bmx280_create_master(i2c_master_bus_handle_t bus_handle);
// legacy define for existing code bases
#define bmx280_create(port) bmx280_create_legacy(port)
#define bmx280_create_legacy(port) static_assert(0, "You have the wrong driver configuration for using the legacy I2C driver.")
2024-11-24 12:45:29 +01:00
#else
/**
* Create an instance of the BMX280 driver.
2024-11-25 11:30:11 +01:00
* @param port The I2C port to use.
2024-11-24 12:45:29 +01:00
* @return A non-null pointer to the driver structure on success.
*/
2024-11-25 11:30:11 +01:00
BMXAPI bmx280_t* bmx280_create_legacy(i2c_port_t port);
// legacy define for existing code bases
#define bmx280_create(port) bmx280_create_legacy(port)
#define bmx280_create_master(port) static_assert(0, "You have the wrong driver configuration for using the new I2C master driver.")
2024-11-24 12:45:29 +01:00
#endif
2024-11-25 11:30:11 +01:00
2020-11-19 11:33:02 +01:00
/**
* Destroy your the instance.
* @param bmx280 The instance to destroy.
*/
BMXAPI void bmx280_close(bmx280_t* bmx280);
2020-11-06 11:04:05 +01:00
2020-11-19 11:33:02 +01:00
/**
* Probe for the sensor and read calibration data.
* @param bmx280 Driver structure.
*/
BMXAPI esp_err_t bmx280_init(bmx280_t* bmx280);
2020-11-19 11:33:02 +01:00
/**
* Configure the sensor with the given parameters.
* @param bmx280 Driver structure.
* @param configuration The configuration to use.
*/
BMXAPI esp_err_t bmx280_configure(bmx280_t* bmx280, bmx280_config_t *cfg);
2020-11-19 11:33:02 +01:00
/**
* Set the sensor mode of operation.
* @param bmx280 Driver structure.
* @param mode The mode to set the sensor to.
*/
BMXAPI esp_err_t bmx280_setMode(bmx280_t* bmx280, bmx280_mode_t mode);
/**
* Get the sensor current mode of operation.
* @param bmx280 Driver structure.
* @param mode Pointer to write current mode to.
*/
BMXAPI esp_err_t bmx280_getMode(bmx280_t* bmx280, bmx280_mode_t* mode);
2020-11-19 11:33:02 +01:00
/**
* Returns true if sensor is currently sampling environment conditions.
* @param bmx280 Driver structure.
*/
BMXAPI bool bmx280_isSampling(bmx280_t* bmx280);
2020-11-19 11:33:02 +01:00
/**
* Read sensor values as fixed point numbers.
* @param bmx280 Driver structure.
* @param temperature The temperature in C (0.01 degree C increments)
* @param pressure The pressure in Pa (1/256 Pa increments)
* @param humidity The humidity in %RH (1/1024 %RH increments) (UINT32_MAX when invlaid.)
*/
BMXAPI esp_err_t bmx280_readout(bmx280_t *bmx280, int32_t *temperature, uint32_t *pressure, uint32_t *humidity);
2020-11-19 11:33:02 +01:00
/**
* Convert sensor readout to floating point values.
* @param tin Input temperature.
* @param pin Input pressure.
* @param hin Input humidity.
* @param tout Output temperature. (C)
* @param pout Output pressure. (Pa)
* @param hout Output humidity. (%Rh)
*/
static inline void bmx280_readout2float(int32_t* tin, uint32_t *pin, uint32_t *hin, float *tout, float *pout, float *hout)
2020-11-19 11:33:02 +01:00
{
2021-05-02 16:32:12 +02:00
if (tin && tout)
*tout = (float)*tin * 0.01f;
if (pin && pout)
*pout = (float)*pin * (1.0f/256.0f);
if (hin && hout)
*hout = (*hin == UINT32_MAX) ? -1.0f : (float)*hin * (1.0f/1024.0f);
2020-11-19 11:33:02 +01:00
}
/**
* Read sensor values as floating point numbers.
* @param bmx280 Driver structure.
* @param temperature The temperature in C.
* @param pressure The pressure in Pa.
* @param humidity The humidity in %RH.
*/
2020-11-19 11:33:02 +01:00
static inline esp_err_t bmx280_readoutFloat(bmx280_t *bmx280, float* temperature, float* pressure, float* humidity)
{
int32_t t; uint32_t p, h;
esp_err_t err = bmx280_readout(bmx280, &t, &p, &h);
2020-11-06 11:04:05 +01:00
2020-11-19 11:33:02 +01:00
if (err == ESP_OK)
{
bmx280_readout2float(&t, &p, &h, temperature, pressure, humidity);
2020-11-19 11:33:02 +01:00
}
2020-11-06 11:04:05 +01:00
2020-11-19 11:33:02 +01:00
return err;
}
2020-11-06 11:04:05 +01:00
#ifdef __cplusplus
};
#endif
2020-11-06 11:04:05 +01:00
#endif