使用气体传感器,对接到sensor框架中使用。
device框架 --》sensor框架 --》具体的设备
查找设备时的名称为sensor.c文件中的sensor_name_str变量定义的头部分。
static char *const sensor_name_str[] =
{
"none",
"acce_", /* Accelerometer */
"gyro_", /* Gyroscope */
"mag_", /* Magnetometer */
"temp_", /* Temperature */
"humi_", /* Relative Humidity */
"baro_", /* Barometer */
"li_", /* Ambient light */
"pr_", /* Proximity */
"hr_", /* Heart Rate */
"tvoc_", /* TVOC Level */
"noi_", /* Noise Loudness */
"step_", /* Step sensor */
"forc_", /* Force sensor */
"dust_", /* Dust sensor */
"eco2_", /* eCO2 sensor */
"gnss_", /* GPS/GNSS sensor */
// "tof_", /* TOF sensor */
// "spo2_" /* SpO2 sensor */
"ch4_" /* CH4 sensor */
};
在注册的时候,将后半部分的名称写入进去
result = rt_hw_sensor_register(sensor_temp,"gas", RT_DEVICE_FLAG_RDONLY, RT_NULL);
在该函数中通过调用初始化的时候的设备类型地址,对设备名称的前部分进行查表注册。
sensor_name = sensor_name_str[sensor->info. Type];
#define RT_SENSOR_CLASS_NONE (0)
#define RT_SENSOR_CLASS_ACCE (1) /* Accelerometer */
#define RT_SENSOR_CLASS_GYRO (2) /* Gyroscope */
#define RT_SENSOR_CLASS_MAG (3) /* Magnetometer */
#define RT_SENSOR_CLASS_TEMP (4) /* Temperature */
#define RT_SENSOR_CLASS_HUMI (5) /* Relative Humidity */
#define RT_SENSOR_CLASS_BARO (6) /* Barometer */
#define RT_SENSOR_CLASS_LIGHT (7) /* Ambient light */
#define RT_SENSOR_CLASS_PROXIMITY (8) /* Proximity */
#define RT_SENSOR_CLASS_HR (9) /* Heart Rate */
#define RT_SENSOR_CLASS_TVOC (10) /* TVOC Level */
#define RT_SENSOR_CLASS_NOISE (11) /* Noise Loudness */
#define RT_SENSOR_CLASS_STEP (12) /* Step sensor */
#define RT_SENSOR_CLASS_FORCE (13) /* Force sensor */
#define RT_SENSOR_CLASS_DUST (14) /* Dust sensor */
#define RT_SENSOR_CLASS_ECO2 (15) /* eCO2 sensor */
#define RT_SENSOR_CLASS_GNSS (16) /* GPS/GNSS sensor */
#define RT_SENSOR_CLASS_CH4 (17) /* CH4 sensor */
该参数通过sensor,c中的下列函数实现的
/* RT-Thread Device Interface */
static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag)
{
rt_sensor_t sensor = (rt_sensor_t)dev;
RT_ASSERT(dev != RT_NULL);
rt_err_t res = RT_EOK;
rt_err_t (*local_ctrl)(struct rt_sensor_device *sensor, int cmd, void *arg) = local_control;
if (sensor->module)
{
/* take the module mutex */
rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
}
if (sensor->module != RT_NULL && sensor->info.fifo_max > 0 && sensor->data_buf == RT_NULL)
{
/* Allocate memory for the sensor buffer */
sensor->data_buf = rt_malloc(sizeof(struct rt_sensor_data) * sensor->info.fifo_max);
if (sensor->data_buf == RT_NULL)
{
res = -RT_ENOMEM;
goto __exit;
}
}
if (sensor->ops->control != RT_NULL)
{
local_ctrl = sensor->ops->control;
}
sensor->config.mode = RT_SENSOR_MODE_POLLING;
if (oflag & RT_DEVICE_FLAG_RDONLY && dev->flag & RT_DEVICE_FLAG_RDONLY)
{
/* If polling mode is supported, configure it to polling mode */
local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_POLLING);
}
else if (oflag & RT_DEVICE_FLAG_INT_RX && dev->flag & RT_DEVICE_FLAG_INT_RX)
{
/* If interrupt mode is supported, configure it to interrupt mode */
if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_INT) == RT_EOK)
{
/* Initialization sensor interrupt */
rt_sensor_irq_init(sensor);
sensor->config.mode = RT_SENSOR_MODE_INT;
}
}
else if (oflag & RT_DEVICE_FLAG_FIFO_RX && dev->flag & RT_DEVICE_FLAG_FIFO_RX)
{
/* If fifo mode is supported, configure it to fifo mode */
if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_FIFO) == RT_EOK)
{
/* Initialization sensor interrupt */
rt_sensor_irq_init(sensor);
sensor->config.mode = RT_SENSOR_MODE_FIFO;
}
}
else
{
res = -RT_EINVAL;
goto __exit;
}
/* Configure power mode to normal mode */
if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_NORMAL) == RT_EOK)
{
sensor->config.power = RT_SENSOR_POWER_NORMAL;
}
__exit:
if (sensor->module)
{
/* release the module mutex */
rt_mutex_release(sensor->module->lock);
}
return res;
}
可以看到实际上,开启函数是对control函数进行的控制,实现的开启和关闭。
该函数的输入参数是struct rt_sensor_data data;
struct rt_sensor_data
{
rt_uint32_t timestamp; /* The timestamp when the data was received */
rt_uint8_t type; /* The sensor type of the data */
rt_uint8_t state; /* The sensor state*/
union
{
struct sensor_3_axis acce; /* Accelerometer. unit: mG */
struct sensor_3_axis gyro; /* Gyroscope. unit: mdps */
struct sensor_3_axis mag; /* Magnetometer. unit: mGauss */
struct coordinates coord; /* Coordinates unit: degrees */
float temp; /* Temperature. unit: dCelsius */
float humi; /* Relative humidity. unit: permillage */
rt_int32_t baro; /* Pressure. unit: pascal (Pa) */
rt_int32_t light; /* Light. unit: lux */
rt_int32_t proximity; /* Distance. unit: centimeters */
rt_int32_t hr; /* Heart rate. unit: bpm */
rt_int32_t tvoc; /* TVOC. unit: permillage */
rt_int32_t noise; /* Noise Loudness. unit: HZ */
rt_uint32_t step; /* Step sensor. unit: 1 */
rt_int32_t force; /* Force sensor. unit: mN */
rt_uint32_t dust; /* Dust sensor. unit: ug/m3 */
rt_uint32_t eco2; /* eCO2 sensor. unit: ppm */
rt_uint8_t ch4; /* CH4 sensor. unit: LEL */
} data;
};
在实际使用的时候,根据具体的传感器选择具体的数据类型进行选择。在编写底层对接函数的时候,在获取数据部分也需要同步将数据写入到相同的传感器类型的数据中去。