#ifndef __SENSOR_H__
#define __SENSOR_H__
#include <rtthread.h>
#include <rtdevice.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifdef RT_USING_RTC
#define rt_sensor_get_ts() time(RT_NULL)
#else
#define rt_sensor_get_ts() rt_tick_get()
#endif
#define RT_PIN_NONE 0xFFFF
#define RT_DEVICE_FLAG_FIFO_RX 0x200
#define RT_SENSOR_MODULE_MAX (3)
#define RT_SENSOR_CLASS_NONE (0)
#define RT_SENSOR_CLASS_ACCE (1)
#define RT_SENSOR_CLASS_GYRO (2)
#define RT_SENSOR_CLASS_MAG (3)
#define RT_SENSOR_CLASS_TEMP (4)
#define RT_SENSOR_CLASS_HUMI (5)
#define RT_SENSOR_CLASS_BARO (6)
#define RT_SENSOR_CLASS_LIGHT (7)
#define RT_SENSOR_CLASS_PROXIMITY (8)
#define RT_SENSOR_CLASS_HR (9)
#define RT_SENSOR_CLASS_TVOC (10)
#define RT_SENSOR_CLASS_NOISE (11)
#define RT_SENSOR_CLASS_STEP (12)
#define RT_SENSOR_CLASS_FORCE (13)
#define RT_SENSOR_CLASS_DUST (14)
#define RT_SENSOR_CLASS_ECO2 (15)
#define RT_SENSOR_VENDOR_UNKNOWN (0)
#define RT_SENSOR_VENDOR_STM (1)
#define RT_SENSOR_VENDOR_BOSCH (2)
#define RT_SENSOR_VENDOR_INVENSENSE (3)
#define RT_SENSOR_VENDOR_SEMTECH (4)
#define RT_SENSOR_VENDOR_GOERTEK (5)
#define RT_SENSOR_VENDOR_MIRAMEMS (6)
#define RT_SENSOR_VENDOR_DALLAS (7)
#define RT_SENSOR_VENDOR_ASAIR (8)
#define RT_SENSOR_VENDOR_SHARP (9)
#define RT_SENSOR_VENDOR_SENSIRION (10)
#define RT_SENSOR_VENDOR_TI (11)
#define RT_SENSOR_VENDOR_PLANTOWER (12)
#define RT_SENSOR_VENDOR_AMS (13)
#define RT_SENSOR_UNIT_NONE (0)
#define RT_SENSOR_UNIT_MG (1)
#define RT_SENSOR_UNIT_MDPS (2)
#define RT_SENSOR_UNIT_MGAUSS (3)
#define RT_SENSOR_UNIT_LUX (4)
#define RT_SENSOR_UNIT_CM (5)
#define RT_SENSOR_UNIT_PA (6)
#define RT_SENSOR_UNIT_PERMILLAGE (7)
#define RT_SENSOR_UNIT_DCELSIUS (8)
#define RT_SENSOR_UNIT_HZ (9)
#define RT_SENSOR_UNIT_ONE (10)
#define RT_SENSOR_UNIT_BPM (11)
#define RT_SENSOR_UNIT_MM (12)
#define RT_SENSOR_UNIT_MN (13)
#define RT_SENSOR_UNIT_PPM (14)
#define RT_SENSOR_UNIT_PPB (15)
#define RT_SENSOR_INTF_I2C (1 << 0)
#define RT_SENSOR_INTF_SPI (1 << 1)
#define RT_SENSOR_INTF_UART (1 << 2)
#define RT_SENSOR_INTF_ONEWIRE (1 << 3)
#define RT_SENSOR_POWER_NONE (0)
#define RT_SENSOR_POWER_DOWN (1)
#define RT_SENSOR_POWER_NORMAL (2)
#define RT_SENSOR_POWER_LOW (3)
#define RT_SENSOR_POWER_HIGH (4)
#define RT_SENSOR_MODE_NONE (0)
#define RT_SENSOR_MODE_POLLING (1)
#define RT_SENSOR_MODE_INT (2)
#define RT_SENSOR_MODE_FIFO (3)
#define RT_SENSOR_CTRL_GET_ID (0)
#define RT_SENSOR_CTRL_GET_INFO (1)
#define RT_SENSOR_CTRL_SET_RANGE (2)
#define RT_SENSOR_CTRL_SET_ODR (3)
#define RT_SENSOR_CTRL_SET_MODE (4)
#define RT_SENSOR_CTRL_SET_POWER (5)
#define RT_SENSOR_CTRL_SELF_TEST (6)
#define RT_SENSOR_CTRL_USER_CMD_START 0x100
struct rt_sensor_info
{
rt_uint8_t type;
rt_uint8_t vendor;
const char *model;
rt_uint8_t unit;
rt_uint8_t intf_type;
rt_int32_t range_max;
rt_int32_t range_min;
rt_uint32_t period_min;
rt_uint8_t fifo_max;
};
struct rt_sensor_intf
{
char *dev_name;
rt_uint8_t type;
void *user_data;
};
struct rt_sensor_config
{
struct rt_sensor_intf intf;
struct rt_device_pin_mode irq_pin;
rt_uint8_t mode;
rt_uint8_t power;
rt_uint16_t odr;
rt_int32_t range;
};
typedef struct rt_sensor_device *rt_sensor_t;
struct rt_sensor_device
{
struct rt_device parent;
struct rt_sensor_info info;
struct rt_sensor_config config;
void *data_buf;
rt_size_t data_len;
const struct rt_sensor_ops *ops;
struct rt_sensor_module *module;
rt_err_t (*irq_handle)(rt_sensor_t sensor);
};
struct rt_sensor_module
{
rt_mutex_t lock;
rt_sensor_t sen[RT_SENSOR_MODULE_MAX];
rt_uint8_t sen_num;
};
struct sensor_3_axis
{
rt_int32_t x;
rt_int32_t y;
rt_int32_t z;
};
struct rt_sensor_data
{
rt_uint32_t timestamp;
rt_uint8_t type;
union
{
struct sensor_3_axis acce;
struct sensor_3_axis gyro;
struct sensor_3_axis mag;
rt_int32_t temp;
rt_int32_t humi;
rt_int32_t baro;
rt_int32_t light;
rt_int32_t proximity;
rt_int32_t hr;
rt_int32_t tvoc;
rt_int32_t noise;
rt_uint32_t step;
rt_int32_t force;
rt_uint32_t dust;
rt_uint32_t eco2;
} data;
};
struct rt_sensor_ops
{
rt_size_t (*fetch_data)(struct rt_sensor_device *sensor, void *buf, rt_size_t len);
rt_err_t (*control)(struct rt_sensor_device *sensor, int cmd, void *arg);
};
int rt_hw_sensor_register(rt_sensor_t sensor,
const char *name,
rt_uint32_t flag,
void *data);
#ifdef __cplusplus
}
#endif
#endif