[sensor] draft new sensor-hal framework (#6746)
* [sensor] new sensor framework 针对老版本sensor框架的诸多不合理设计进行重构 之前的PR中已经重构了浮点数相关的问题 本次PR主要围绕sensor的整体架构予以重构,对过于理想化的参数和模式予以删除 * [sensor] 增加can modbus总线类型 * [stm32l745] 完善sensor对接 * [sensor] fix the onchip ID print
This commit is contained in:
parent
c930b4f623
commit
262392a69c
|
@ -11,36 +11,36 @@
|
|||
#include <board.h>
|
||||
|
||||
#ifdef BSP_USING_ICM20608
|
||||
#include "sensor_inven_mpu6xxx.h"
|
||||
#include <sensor_inven_mpu6xxx.h>
|
||||
|
||||
int sensor_init(void)
|
||||
static int rt_hw_icm20608_port(void)
|
||||
{
|
||||
struct rt_sensor_config cfg;
|
||||
|
||||
cfg.intf.type = RT_SENSOR_INTF_I2C;
|
||||
cfg.intf.dev_name = "i2c3";
|
||||
cfg.intf.type = RT_SENSOR_INTF_I2C;
|
||||
cfg.intf.arg = (void *)MPU6XXX_ADDR_DEFAULT;
|
||||
cfg.irq_pin.pin = RT_PIN_NONE;
|
||||
|
||||
rt_hw_mpu6xxx_init("icm", &cfg);
|
||||
|
||||
return 0;
|
||||
return RT_EOK;
|
||||
}
|
||||
INIT_ENV_EXPORT(sensor_init);
|
||||
INIT_ENV_EXPORT(rt_hw_icm20608_port);
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef BSP_USING_AHT10
|
||||
#include "sensor_asair_aht10.h"
|
||||
#include <sensor_asair_aht10.h>
|
||||
|
||||
#define AHT10_I2C_BUS "i2c4"
|
||||
|
||||
int rt_hw_aht10_port(void)
|
||||
static int rt_hw_aht10_port(void)
|
||||
{
|
||||
struct rt_sensor_config cfg;
|
||||
|
||||
cfg.intf.dev_name = AHT10_I2C_BUS;
|
||||
cfg.intf.dev_name = "i2c4";
|
||||
cfg.intf.type = RT_SENSOR_INTF_I2C;
|
||||
cfg.intf.arg = (void *)AHT10_I2C_ADDR;
|
||||
cfg.irq_pin.pin = RT_PIN_NONE;
|
||||
|
||||
rt_hw_aht10_init("aht10", &cfg);
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2019-01-31 flybreak first version
|
||||
* 2022-12-17 Meco Man re-implement sensor framework
|
||||
*/
|
||||
|
||||
#ifndef __SENSOR_H__
|
||||
|
@ -32,54 +33,54 @@ extern "C" {
|
|||
#define RT_SENSOR_MACRO_GET_NAME(macro) (macro##_STR)
|
||||
|
||||
/* Sensor types */
|
||||
#define RT_SENSOR_CLASS_NONE (0)
|
||||
#define RT_SENSOR_CLASS_NONE_STR "None"
|
||||
#define RT_SENSOR_CLASS_ACCE (1)
|
||||
#define RT_SENSOR_CLASS_ACCE_STR "Accelerometer"
|
||||
#define RT_SENSOR_CLASS_GYRO (2)
|
||||
#define RT_SENSOR_CLASS_GYRO_STR "Gyroscope"
|
||||
#define RT_SENSOR_CLASS_MAG (3)
|
||||
#define RT_SENSOR_CLASS_MAG_STR "Magnetometer"
|
||||
#define RT_SENSOR_CLASS_TEMP (4)
|
||||
#define RT_SENSOR_CLASS_TEMP_STR "Temperature"
|
||||
#define RT_SENSOR_CLASS_HUMI (5)
|
||||
#define RT_SENSOR_CLASS_HUMI_STR "Relative Humidity"
|
||||
#define RT_SENSOR_CLASS_BARO (6)
|
||||
#define RT_SENSOR_CLASS_BARO_STR "Barometer"
|
||||
#define RT_SENSOR_CLASS_LIGHT (7)
|
||||
#define RT_SENSOR_CLASS_LIGHT_STR "Ambient light"
|
||||
#define RT_SENSOR_CLASS_PROXIMITY (8)
|
||||
#define RT_SENSOR_CLASS_PROXIMITY_STR "Proximity"
|
||||
#define RT_SENSOR_CLASS_HR (9)
|
||||
#define RT_SENSOR_CLASS_HR_STR "Heart Rate"
|
||||
#define RT_SENSOR_CLASS_TVOC (10)
|
||||
#define RT_SENSOR_CLASS_TVOC_STR "TVOC Level"
|
||||
#define RT_SENSOR_CLASS_NOISE (11)
|
||||
#define RT_SENSOR_CLASS_NOISE_STR "Noise Loudness"
|
||||
#define RT_SENSOR_CLASS_STEP (12)
|
||||
#define RT_SENSOR_CLASS_STEP_STR "Step"
|
||||
#define RT_SENSOR_CLASS_FORCE (13)
|
||||
#define RT_SENSOR_CLASS_FORCE_STR "Force"
|
||||
#define RT_SENSOR_CLASS_DUST (14)
|
||||
#define RT_SENSOR_CLASS_DUST_STR "Dust"
|
||||
#define RT_SENSOR_CLASS_ECO2 (15)
|
||||
#define RT_SENSOR_CLASS_ECO2_STR "eCO2"
|
||||
#define RT_SENSOR_CLASS_GNSS (16)
|
||||
#define RT_SENSOR_CLASS_GNSS_STR "GNSS"
|
||||
#define RT_SENSOR_CLASS_TOF (17)
|
||||
#define RT_SENSOR_CLASS_TOF_STR "ToF"
|
||||
#define RT_SENSOR_CLASS_SPO2 (18)
|
||||
#define RT_SENSOR_CLASS_SPO2_STR "SpO2"
|
||||
#define RT_SENSOR_CLASS_IAQ (19)
|
||||
#define RT_SENSOR_CLASS_IAQ_STR "IAQ"
|
||||
#define RT_SENSOR_CLASS_ETOH (20)
|
||||
#define RT_SENSOR_CLASS_ETOH_STR "EtOH"
|
||||
#define RT_SENSOR_CLASS_BP (21)
|
||||
#define RT_SENSOR_CLASS_BP_STR "Blood Pressure"
|
||||
#define RT_SENSOR_CLASS_VOLTAGE (22)
|
||||
#define RT_SENSOR_CLASS_VOLTAGE_STR "Voltage"
|
||||
#define RT_SENSOR_CLASS_CURRENT (23)
|
||||
#define RT_SENSOR_CLASS_CURRENT_STR "Current"
|
||||
#define RT_SENSOR_TYPE_NONE (0)
|
||||
#define RT_SENSOR_TYPE_NONE_STR "None"
|
||||
#define RT_SENSOR_TYPE_ACCE (1)
|
||||
#define RT_SENSOR_TYPE_ACCE_STR "Accelerometer"
|
||||
#define RT_SENSOR_TYPE_GYRO (2)
|
||||
#define RT_SENSOR_TYPE_GYRO_STR "Gyroscope"
|
||||
#define RT_SENSOR_TYPE_MAG (3)
|
||||
#define RT_SENSOR_TYPE_MAG_STR "Magnetometer"
|
||||
#define RT_SENSOR_TYPE_TEMP (4)
|
||||
#define RT_SENSOR_TYPE_TEMP_STR "Temperature"
|
||||
#define RT_SENSOR_TYPE_HUMI (5)
|
||||
#define RT_SENSOR_TYPE_HUMI_STR "Relative Humidity"
|
||||
#define RT_SENSOR_TYPE_BARO (6)
|
||||
#define RT_SENSOR_TYPE_BARO_STR "Barometer"
|
||||
#define RT_SENSOR_TYPE_LIGHT (7)
|
||||
#define RT_SENSOR_TYPE_LIGHT_STR "Ambient light"
|
||||
#define RT_SENSOR_TYPE_PROXIMITY (8)
|
||||
#define RT_SENSOR_TYPE_PROXIMITY_STR "Proximity"
|
||||
#define RT_SENSOR_TYPE_HR (9)
|
||||
#define RT_SENSOR_TYPE_HR_STR "Heart Rate"
|
||||
#define RT_SENSOR_TYPE_TVOC (10)
|
||||
#define RT_SENSOR_TYPE_TVOC_STR "TVOC Level"
|
||||
#define RT_SENSOR_TYPE_NOISE (11)
|
||||
#define RT_SENSOR_TYPE_NOISE_STR "Noise Loudness"
|
||||
#define RT_SENSOR_TYPE_STEP (12)
|
||||
#define RT_SENSOR_TYPE_STEP_STR "Step"
|
||||
#define RT_SENSOR_TYPE_FORCE (13)
|
||||
#define RT_SENSOR_TYPE_FORCE_STR "Force"
|
||||
#define RT_SENSOR_TYPE_DUST (14)
|
||||
#define RT_SENSOR_TYPE_DUST_STR "Dust"
|
||||
#define RT_SENSOR_TYPE_ECO2 (15)
|
||||
#define RT_SENSOR_TYPE_ECO2_STR "eCO2"
|
||||
#define RT_SENSOR_TYPE_GNSS (16)
|
||||
#define RT_SENSOR_TYPE_GNSS_STR "GNSS"
|
||||
#define RT_SENSOR_TYPE_TOF (17)
|
||||
#define RT_SENSOR_TYPE_TOF_STR "ToF"
|
||||
#define RT_SENSOR_TYPE_SPO2 (18)
|
||||
#define RT_SENSOR_TYPE_SPO2_STR "SpO2"
|
||||
#define RT_SENSOR_TYPE_IAQ (19)
|
||||
#define RT_SENSOR_TYPE_IAQ_STR "IAQ"
|
||||
#define RT_SENSOR_TYPE_ETOH (20)
|
||||
#define RT_SENSOR_TYPE_ETOH_STR "EtOH"
|
||||
#define RT_SENSOR_TYPE_BP (21)
|
||||
#define RT_SENSOR_TYPE_BP_STR "Blood Pressure"
|
||||
#define RT_SENSOR_TYPE_VOLTAGE (22)
|
||||
#define RT_SENSOR_TYPE_VOLTAGE_STR "Voltage"
|
||||
#define RT_SENSOR_TYPE_CURRENT (23)
|
||||
#define RT_SENSOR_TYPE_CURRENT_STR "Current"
|
||||
|
||||
/* Sensor vendor types */
|
||||
#define RT_SENSOR_VENDOR_UNKNOWN (0)
|
||||
|
@ -186,33 +187,76 @@ extern "C" {
|
|||
#define RT_SENSOR_INTF_UART_STR "UART"
|
||||
#define RT_SENSOR_INTF_ONEWIRE (1 << 3)
|
||||
#define RT_SENSOR_INTF_ONEWIRE_STR "1-Wire"
|
||||
#define RT_SENSOR_INTF_CAN (1 << 4)
|
||||
#define RT_SENSOR_INTF_CAN_STR "CAN"
|
||||
#define RT_SENSOR_INTF_MODBUS (1 << 5)
|
||||
#define RT_SENSOR_INTF_MODBUS_STR "Modbus"
|
||||
|
||||
/* Sensor power mode types */
|
||||
#define RT_SENSOR_POWER_NONE (0)
|
||||
#define RT_SENSOR_POWER_NONE_STR "None"
|
||||
#define RT_SENSOR_POWER_DOWN (1) /* power down mode */
|
||||
#define RT_SENSOR_POWER_DOWN_STR "Down"
|
||||
#define RT_SENSOR_POWER_NORMAL (2) /* normal-power mode */
|
||||
#define RT_SENSOR_POWER_NORMAL_STR "Normal"
|
||||
#define RT_SENSOR_POWER_LOW (3) /* low-power mode */
|
||||
#define RT_SENSOR_POWER_LOW_STR "Low"
|
||||
#define RT_SENSOR_POWER_HIGH (4) /* high-power mode */
|
||||
#define RT_SENSOR_POWER_HIGH_STR "High"
|
||||
/**
|
||||
* Sensor mode
|
||||
* rt_uint16_t mode
|
||||
* 0000 | 0000 | 0000 | 0000
|
||||
* unused accuracy power fetch data
|
||||
*/
|
||||
#define RT_SENSOR_MODE_ACCURACY_BIT_OFFSET (8)
|
||||
#define RT_SENSOR_MODE_POWER_BIT_OFFSET (4)
|
||||
#define RT_SENSOR_MODE_FETCH_BIT_OFFSET (0)
|
||||
|
||||
/* Sensor work mode types */
|
||||
#define RT_SENSOR_MODE_NONE (0)
|
||||
#define RT_SENSOR_MODE_POLLING (1) /* One shot only read a data */
|
||||
#define RT_SENSOR_MODE_INT (2) /* TODO: One shot interrupt only read a data */
|
||||
#define RT_SENSOR_MODE_FIFO (3) /* TODO: One shot interrupt read all fifo data */
|
||||
#define RT_SENSOR_MODE_GET_ACCURACY(mode) (rt_uint8_t)((mode >> RT_SENSOR_MODE_ACCURACY_BIT_OFFSET) & 0x0F)
|
||||
#define RT_SENSOR_MODE_GET_POWER(mode) (rt_uint8_t)((mode >> RT_SENSOR_MODE_POWER_BIT_OFFSET) & 0x0F)
|
||||
#define RT_SENSOR_MODE_GET_FETCH(mode) (rt_uint8_t)((mode >> RT_SENSOR_MODE_FETCH_BIT_OFFSET) & 0x0F)
|
||||
|
||||
#define RT_SENSOR_MODE_CLEAR_ACCURACY(mode) (mode &= ((rt_uint16_t)~((rt_uint16_t)0x0F << RT_SENSOR_MODE_ACCURACY_BIT_OFFSET)))
|
||||
#define RT_SENSOR_MODE_CLEAR_POWER(mode) (mode &= ((rt_uint16_t)~((rt_uint16_t)0x0F << RT_SENSOR_MODE_POWER_BIT_OFFSET)))
|
||||
#define RT_SENSOR_MODE_CLEAR_FETCH(mode) (mode &= ((rt_uint16_t)~((rt_uint16_t)0x0F << RT_SENSOR_MODE_FETCH_BIT_OFFSET)))
|
||||
|
||||
#define RT_SENSOR_MODE_SET_ACCURACY(mode, accuracy_mode) RT_SENSOR_MODE_CLEAR_ACCURACY(mode); (mode |= (accuracy_mode << RT_SENSOR_MODE_ACCURACY_BIT_OFFSET))
|
||||
#define RT_SENSOR_MODE_SET_POWER(mode, power_mode) RT_SENSOR_MODE_CLEAR_POWER(mode); (mode |= (power_mode << RT_SENSOR_MODE_POWER_BIT_OFFSET))
|
||||
#define RT_SENSOR_MODE_SET_FETCH(mode, fetch_mode) RT_SENSOR_MODE_CLEAR_FETCH(mode); (mode |= (fetch_mode << RT_SENSOR_MODE_FETCH_BIT_OFFSET))
|
||||
|
||||
/* Sensor mode: accuracy */
|
||||
#define RT_SENSOR_MODE_ACCURACY_HIGHEST (0)
|
||||
#define RT_SENSOR_MODE_ACCURACY_HIGHEST_STR "Accuracy Highest"
|
||||
#define RT_SENSOR_MODE_ACCURACY_HIGH (1)
|
||||
#define RT_SENSOR_MODE_ACCURACY_HIGH_STR "Accuracy High"
|
||||
#define RT_SENSOR_MODE_ACCURACY_MEDIUM (2)
|
||||
#define RT_SENSOR_MODE_ACCURACY_MEDIUM_STR "Accuracy Medium"
|
||||
#define RT_SENSOR_MODE_ACCURACY_LOW (4)
|
||||
#define RT_SENSOR_MODE_ACCURACY_LOW_STR "Accuracy Low"
|
||||
#define RT_SENSOR_MODE_ACCURACY_LOWEST (5)
|
||||
#define RT_SENSOR_MODE_ACCURACY_LOWEST_STR "Accuracy Lowest"
|
||||
#define RT_SENSOR_MODE_ACCURACY_NOTRUST (6)
|
||||
#define RT_SENSOR_MODE_ACCURACY_NOTRUST_STR "Accuracy No Trust"
|
||||
|
||||
/* Sensor mode: power */
|
||||
#define RT_SENSOR_MODE_POWER_HIGHEST (0)
|
||||
#define RT_SENSOR_MODE_POWER_HIGHEST_STR "Power Highest"
|
||||
#define RT_SENSOR_MODE_POWER_HIGH (1)
|
||||
#define RT_SENSOR_MODE_POWER_HIGH_STR "Power High"
|
||||
#define RT_SENSOR_MODE_POWER_MEDIUM (2)
|
||||
#define RT_SENSOR_MODE_POWER_MEDIUM_STR "Power Medium"
|
||||
#define RT_SENSOR_MODE_POWER_LOW (3)
|
||||
#define RT_SENSOR_MODE_POWER_LOW_STR "Power Low"
|
||||
#define RT_SENSOR_MODE_POWER_LOWEST (4)
|
||||
#define RT_SENSOR_MODE_POWER_LOWEST_STR "Power Lowest"
|
||||
#define RT_SENSOR_MODE_POWER_DOWN (5)
|
||||
#define RT_SENSOR_MODE_POWER_DOWN_STR "Power Down"
|
||||
|
||||
/* Sensor mode: fetch data */
|
||||
#define RT_SENSOR_MODE_FETCH_POLLING (0) /* One shot only read a data */
|
||||
#define RT_SENSOR_MODE_FETCH_POLLING_STR "Polling Mode"
|
||||
#define RT_SENSOR_MODE_FETCH_INT (1) /* TODO: One shot interrupt only read a data */
|
||||
#define RT_SENSOR_MODE_FETCH_INT_STR "Interrupt Mode"
|
||||
#define RT_SENSOR_MODE_FETCH_FIFO (2) /* TODO: One shot interrupt read all fifo data */
|
||||
#define RT_SENSOR_MODE_FETCH_FIFO_STR "FIFO Mode"
|
||||
|
||||
/* Sensor control cmd types */
|
||||
#define RT_SENSOR_CTRL_GET_ID (RT_DEVICE_CTRL_BASE(Sensor) + 0) /* Get device id */
|
||||
#define RT_SENSOR_CTRL_GET_INFO (RT_DEVICE_CTRL_BASE(Sensor) + 1) /* Get sensor info */
|
||||
#define RT_SENSOR_CTRL_SET_RANGE (RT_DEVICE_CTRL_BASE(Sensor) + 2) /* Set the measure range of sensor. unit is info of sensor */
|
||||
#define RT_SENSOR_CTRL_SET_ODR (RT_DEVICE_CTRL_BASE(Sensor) + 3) /* Set output date rate. unit is HZ */
|
||||
#define RT_SENSOR_CTRL_SET_MODE (RT_DEVICE_CTRL_BASE(Sensor) + 4) /* Set sensor's work mode. ex. RT_SENSOR_MODE_POLLING,RT_SENSOR_MODE_INT */
|
||||
#define RT_SENSOR_CTRL_SET_POWER (RT_DEVICE_CTRL_BASE(Sensor) + 5) /* Set power mode. args type of sensor power mode. ex. RT_SENSOR_POWER_DOWN,RT_SENSOR_POWER_NORMAL */
|
||||
#define RT_SENSOR_CTRL_SELF_TEST (RT_DEVICE_CTRL_BASE(Sensor) + 6) /* Take a self test */
|
||||
#define RT_SENSOR_CTRL_SELF_TEST (RT_DEVICE_CTRL_BASE(Sensor) + 1) /* Take a self test */
|
||||
#define RT_SENSOR_CTRL_SOFT_RESET (RT_DEVICE_CTRL_BASE(Sensor) + 2) /* soft reset sensor */
|
||||
#define RT_SENSOR_CTRL_SET_FETCH_MODE (RT_DEVICE_CTRL_BASE(Sensor) + 3) /* set fetch data mode */
|
||||
#define RT_SENSOR_CTRL_SET_POWER_MODE (RT_DEVICE_CTRL_BASE(Sensor) + 4) /* set power mode */
|
||||
#define RT_SENSOR_CTRL_SET_ACCURACY_MODE (RT_DEVICE_CTRL_BASE(Sensor) + 5) /* set accuracy mode */
|
||||
|
||||
#define RT_SENSOR_CTRL_USER_CMD_START 0x100 /* User commands should be greater than 0x100 */
|
||||
|
||||
|
@ -223,17 +267,30 @@ typedef double rt_sensor_float_t;
|
|||
typedef float rt_sensor_float_t;
|
||||
#endif /* RT_USING_SENSOR_DOUBLE_FLOAT */
|
||||
|
||||
struct rt_sensor_accuracy
|
||||
{
|
||||
rt_sensor_float_t resolution; /* resolution of sesnor measurement */
|
||||
rt_sensor_float_t error; /* error of sesnor measurement */
|
||||
};
|
||||
|
||||
struct rt_sensor_scale
|
||||
{
|
||||
rt_sensor_float_t range_max; /* maximum range of this sensor's value. unit is 'unit' */
|
||||
rt_sensor_float_t range_min; /* minimum range of this sensor's value. unit is 'unit' */
|
||||
};
|
||||
|
||||
struct rt_sensor_info
|
||||
{
|
||||
rt_uint8_t type; /* The sensor type */
|
||||
rt_uint8_t vendor; /* Vendor of sensors */
|
||||
const char *model; /* model name of sensor */
|
||||
rt_uint8_t type; /* sensor type */
|
||||
rt_uint8_t vendor; /* sensors vendor */
|
||||
const char *name; /* name of sensor */
|
||||
rt_uint8_t unit; /* unit of measurement */
|
||||
rt_uint8_t intf_type; /* Communication interface type */
|
||||
rt_int32_t range_max; /* maximum range of this sensor's value. unit is 'unit' */
|
||||
rt_int32_t range_min; /* minimum range of this sensor's value. unit is 'unit' */
|
||||
rt_uint32_t period_min; /* Minimum measurement period,unit:ms. zero = not a constant rate */
|
||||
rt_uint8_t intf_type; /* communication interface type */
|
||||
rt_uint16_t mode; /* sensor work mode */
|
||||
rt_uint8_t fifo_max;
|
||||
rt_sensor_float_t acquire_min; /* minimum acquirement period, unit:ms. zero = not a constant rate */
|
||||
struct rt_sensor_accuracy accuracy; /* sensor current measure accuracy */
|
||||
struct rt_sensor_scale scale; /* sensor current scale range */
|
||||
};
|
||||
|
||||
struct rt_sensor_intf
|
||||
|
@ -247,15 +304,13 @@ struct rt_sensor_config
|
|||
{
|
||||
struct rt_sensor_intf intf; /* sensor interface config */
|
||||
struct rt_device_pin_mode irq_pin; /* Interrupt pin, The purpose of this pin is to notification read data */
|
||||
rt_uint8_t mode; /* sensor work mode */
|
||||
rt_uint8_t power; /* sensor power mode */
|
||||
rt_uint16_t odr; /* sensor out data rate */
|
||||
rt_int32_t range; /* sensor range of measurement */
|
||||
};
|
||||
|
||||
typedef struct rt_sensor_device *rt_sensor_t;
|
||||
typedef struct rt_sensor_data *rt_sensor_data_t;
|
||||
typedef struct rt_sensor_info *rt_sensor_info_t;
|
||||
typedef struct rt_sensor_accuracy *rt_sensor_accuracy_t;
|
||||
typedef struct rt_sensor_scale *rt_sensor_scale_t;
|
||||
|
||||
struct rt_sensor_device
|
||||
{
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
* Date Author Notes
|
||||
* 2019-01-31 flybreak first version
|
||||
* 2020-02-22 luhuadong support custom commands
|
||||
* 2022-12-17 Meco Man re-implement sensor framework
|
||||
*/
|
||||
|
||||
#include <drivers/sensor.h>
|
||||
|
@ -61,12 +62,12 @@ static void _sensor_cb(rt_sensor_t sen)
|
|||
{
|
||||
sen->parent.rx_indicate(&sen->parent, sen->data_len / sizeof(struct rt_sensor_data));
|
||||
}
|
||||
else if (sen->config.mode == RT_SENSOR_MODE_INT)
|
||||
else if (RT_SENSOR_MODE_GET_FETCH(sen->info.mode) == RT_SENSOR_MODE_FETCH_INT)
|
||||
{
|
||||
/* The interrupt mode only produces one data at a time */
|
||||
sen->parent.rx_indicate(&sen->parent, 1);
|
||||
}
|
||||
else if (sen->config.mode == RT_SENSOR_MODE_FIFO)
|
||||
else if (RT_SENSOR_MODE_GET_FETCH(sen->info.mode) == RT_SENSOR_MODE_FETCH_FIFO)
|
||||
{
|
||||
sen->parent.rx_indicate(&sen->parent, sen->info.fifo_max);
|
||||
}
|
||||
|
@ -168,30 +169,32 @@ static rt_err_t _sensor_open(rt_device_t dev, rt_uint16_t oflag)
|
|||
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);
|
||||
if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_FETCH_MODE, (void *)RT_SENSOR_MODE_FETCH_POLLING) == RT_EOK)
|
||||
{
|
||||
RT_SENSOR_MODE_SET_FETCH(sensor->info.mode, RT_SENSOR_MODE_FETCH_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)
|
||||
if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_FETCH_MODE, (void *)RT_SENSOR_MODE_FETCH_INT) == RT_EOK)
|
||||
{
|
||||
/* Initialization sensor interrupt */
|
||||
_sensor_irq_init(sensor);
|
||||
sensor->config.mode = RT_SENSOR_MODE_INT;
|
||||
RT_SENSOR_MODE_SET_FETCH(sensor->info.mode, RT_SENSOR_MODE_FETCH_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)
|
||||
if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_FETCH_MODE, (void *)RT_SENSOR_MODE_FETCH_FIFO) == RT_EOK)
|
||||
{
|
||||
/* Initialization sensor interrupt */
|
||||
_sensor_irq_init(sensor);
|
||||
sensor->config.mode = RT_SENSOR_MODE_FIFO;
|
||||
RT_SENSOR_MODE_SET_FETCH(sensor->info.mode, RT_SENSOR_MODE_FETCH_FIFO);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -200,10 +203,16 @@ static rt_err_t _sensor_open(rt_device_t dev, rt_uint16_t oflag)
|
|||
goto __exit;
|
||||
}
|
||||
|
||||
/* Configure power mode to normal mode */
|
||||
if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_NORMAL) == RT_EOK)
|
||||
/* Configure power mode to highest mode */
|
||||
if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER_MODE, (void *)RT_SENSOR_MODE_POWER_HIGHEST) == RT_EOK)
|
||||
{
|
||||
sensor->config.power = RT_SENSOR_POWER_NORMAL;
|
||||
RT_SENSOR_MODE_SET_POWER(sensor->info.mode, RT_SENSOR_MODE_POWER_HIGHEST);
|
||||
}
|
||||
|
||||
/* Configure accuracy mode to highest mode */
|
||||
if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_ACCURACY_MODE, (void *)RT_SENSOR_MODE_ACCURACY_HIGHEST) == RT_EOK)
|
||||
{
|
||||
RT_SENSOR_MODE_SET_ACCURACY(sensor->info.mode, RT_SENSOR_MODE_ACCURACY_HIGHEST);
|
||||
}
|
||||
|
||||
__exit:
|
||||
|
@ -234,9 +243,9 @@ static rt_err_t _sensor_close(rt_device_t dev)
|
|||
}
|
||||
|
||||
/* Configure power mode to power down mode */
|
||||
if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_DOWN) == RT_EOK)
|
||||
if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER_MODE, (void *)RT_SENSOR_MODE_POWER_DOWN) == RT_EOK)
|
||||
{
|
||||
sensor->config.power = RT_SENSOR_POWER_DOWN;
|
||||
RT_SENSOR_MODE_SET_POWER(sensor->info.mode, RT_SENSOR_MODE_POWER_DOWN);
|
||||
}
|
||||
|
||||
if (sensor->module != RT_NULL && sensor->info.fifo_max > 0 && sensor->data_buf != RT_NULL)
|
||||
|
@ -257,7 +266,7 @@ static rt_err_t _sensor_close(rt_device_t dev)
|
|||
}
|
||||
}
|
||||
}
|
||||
if (sensor->config.mode != RT_SENSOR_MODE_POLLING)
|
||||
if (RT_SENSOR_MODE_GET_FETCH(sensor->info.mode) != RT_SENSOR_MODE_FETCH_POLLING)
|
||||
{
|
||||
/* Sensor disable interrupt */
|
||||
if (sensor->config.irq_pin.pin != RT_PIN_NONE)
|
||||
|
@ -346,45 +355,42 @@ static rt_err_t _sensor_control(rt_device_t dev, int cmd, void *args)
|
|||
result = local_ctrl(sensor, RT_SENSOR_CTRL_GET_ID, args);
|
||||
}
|
||||
break;
|
||||
case RT_SENSOR_CTRL_GET_INFO:
|
||||
if (args)
|
||||
{
|
||||
rt_memcpy(args, &sensor->info, sizeof(struct rt_sensor_info));
|
||||
}
|
||||
break;
|
||||
case RT_SENSOR_CTRL_SET_RANGE:
|
||||
/* Configuration measurement range */
|
||||
result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_RANGE, args);
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
sensor->config.range = (rt_int32_t)args;
|
||||
LOG_D("set range %d", sensor->config.range);
|
||||
}
|
||||
break;
|
||||
case RT_SENSOR_CTRL_SET_ODR:
|
||||
/* Configuration data output rate */
|
||||
result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_ODR, args);
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
sensor->config.odr = (rt_uint32_t)args & 0xFFFF;
|
||||
LOG_D("set odr %d", sensor->config.odr);
|
||||
}
|
||||
break;
|
||||
case RT_SENSOR_CTRL_SET_POWER:
|
||||
case RT_SENSOR_CTRL_SET_ACCURACY_MODE:
|
||||
/* Configuration sensor power mode */
|
||||
result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, args);
|
||||
result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_ACCURACY_MODE, args);
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
sensor->config.power = (rt_uint32_t)args & 0xFF;
|
||||
LOG_D("set power mode code:", sensor->config.power);
|
||||
RT_SENSOR_MODE_SET_ACCURACY(sensor->info.mode, (rt_uint32_t)args & 0x0F);
|
||||
LOG_D("set accuracy mode code: %d", RT_SENSOR_MODE_GET_ACCURACY(sensor->info.mode));
|
||||
}
|
||||
break;
|
||||
case RT_SENSOR_CTRL_SET_POWER_MODE:
|
||||
/* Configuration sensor power mode */
|
||||
result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER_MODE, args);
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
RT_SENSOR_MODE_SET_POWER(sensor->info.mode, (rt_uint32_t)args & 0x0F);
|
||||
LOG_D("set power mode code: %d", RT_SENSOR_MODE_GET_POWER(sensor->info.mode));
|
||||
}
|
||||
break;
|
||||
case RT_SENSOR_CTRL_SET_FETCH_MODE:
|
||||
/* Configuration sensor power mode */
|
||||
result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_FETCH_MODE, args);
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
RT_SENSOR_MODE_SET_FETCH(sensor->info.mode, (rt_uint32_t)args & 0x0F);
|
||||
LOG_D("set fetch mode code: %d", RT_SENSOR_MODE_GET_FETCH(sensor->info.mode));
|
||||
}
|
||||
break;
|
||||
case RT_SENSOR_CTRL_SELF_TEST:
|
||||
/* Device self-test */
|
||||
/* device self test */
|
||||
result = local_ctrl(sensor, RT_SENSOR_CTRL_SELF_TEST, args);
|
||||
break;
|
||||
case RT_SENSOR_CTRL_SOFT_RESET:
|
||||
/* device soft reset */
|
||||
result = local_ctrl(sensor, RT_SENSOR_CTRL_SOFT_RESET, args);
|
||||
break;
|
||||
default:
|
||||
|
||||
if (cmd > RT_SENSOR_CTRL_USER_CMD_START)
|
||||
{
|
||||
/* Custom commands */
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
* 2019-01-31 flybreak first version
|
||||
* 2019-07-16 WillianChan Increase the output of sensor information
|
||||
* 2020-02-22 luhuadong Add vendor info and sensor types for cmd
|
||||
* 2022-12-17 Meco Man re-implement sensor framework
|
||||
*/
|
||||
|
||||
#include <drivers/sensor.h>
|
||||
|
@ -25,55 +26,55 @@ static const char *sensor_get_type_name(rt_sensor_info_t info)
|
|||
{
|
||||
switch(info->type)
|
||||
{
|
||||
case RT_SENSOR_CLASS_ACCE:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_ACCE);
|
||||
case RT_SENSOR_CLASS_GYRO:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_GYRO);
|
||||
case RT_SENSOR_CLASS_MAG:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_MAG);
|
||||
case RT_SENSOR_CLASS_TEMP:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_TEMP);
|
||||
case RT_SENSOR_CLASS_HUMI:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_HUMI);
|
||||
case RT_SENSOR_CLASS_BARO:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_BARO);
|
||||
case RT_SENSOR_CLASS_LIGHT:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_LIGHT);
|
||||
case RT_SENSOR_CLASS_PROXIMITY:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_PROXIMITY);
|
||||
case RT_SENSOR_CLASS_HR:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_HR);
|
||||
case RT_SENSOR_CLASS_TVOC:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_TVOC);
|
||||
case RT_SENSOR_CLASS_NOISE:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_NOISE);
|
||||
case RT_SENSOR_CLASS_STEP:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_STEP);
|
||||
case RT_SENSOR_CLASS_FORCE:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_FORCE);
|
||||
case RT_SENSOR_CLASS_DUST:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_DUST);
|
||||
case RT_SENSOR_CLASS_ECO2:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_ECO2);
|
||||
case RT_SENSOR_CLASS_GNSS:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_GNSS);
|
||||
case RT_SENSOR_CLASS_TOF:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_TOF);
|
||||
case RT_SENSOR_CLASS_SPO2:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_SPO2);
|
||||
case RT_SENSOR_CLASS_IAQ:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_IAQ);
|
||||
case RT_SENSOR_CLASS_ETOH:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_ETOH);
|
||||
case RT_SENSOR_CLASS_BP:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_BP);
|
||||
case RT_SENSOR_CLASS_VOLTAGE:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_VOLTAGE);
|
||||
case RT_SENSOR_CLASS_CURRENT:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_CURRENT);
|
||||
case RT_SENSOR_CLASS_NONE:
|
||||
case RT_SENSOR_TYPE_ACCE:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_ACCE);
|
||||
case RT_SENSOR_TYPE_GYRO:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_GYRO);
|
||||
case RT_SENSOR_TYPE_MAG:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_MAG);
|
||||
case RT_SENSOR_TYPE_TEMP:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_TEMP);
|
||||
case RT_SENSOR_TYPE_HUMI:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_HUMI);
|
||||
case RT_SENSOR_TYPE_BARO:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_BARO);
|
||||
case RT_SENSOR_TYPE_LIGHT:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_LIGHT);
|
||||
case RT_SENSOR_TYPE_PROXIMITY:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_PROXIMITY);
|
||||
case RT_SENSOR_TYPE_HR:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_HR);
|
||||
case RT_SENSOR_TYPE_TVOC:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_TVOC);
|
||||
case RT_SENSOR_TYPE_NOISE:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_NOISE);
|
||||
case RT_SENSOR_TYPE_STEP:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_STEP);
|
||||
case RT_SENSOR_TYPE_FORCE:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_FORCE);
|
||||
case RT_SENSOR_TYPE_DUST:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_DUST);
|
||||
case RT_SENSOR_TYPE_ECO2:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_ECO2);
|
||||
case RT_SENSOR_TYPE_GNSS:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_GNSS);
|
||||
case RT_SENSOR_TYPE_TOF:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_TOF);
|
||||
case RT_SENSOR_TYPE_SPO2:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_SPO2);
|
||||
case RT_SENSOR_TYPE_IAQ:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_IAQ);
|
||||
case RT_SENSOR_TYPE_ETOH:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_ETOH);
|
||||
case RT_SENSOR_TYPE_BP:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_BP);
|
||||
case RT_SENSOR_TYPE_VOLTAGE:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_VOLTAGE);
|
||||
case RT_SENSOR_TYPE_CURRENT:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_CURRENT);
|
||||
case RT_SENSOR_TYPE_NONE:
|
||||
default:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_CLASS_NONE);
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_TYPE_NONE);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -185,76 +186,170 @@ static const char *sensor_get_unit_name(rt_sensor_info_t info)
|
|||
}
|
||||
}
|
||||
|
||||
static const char* sensor_get_accuracy_mode_name(rt_sensor_info_t info)
|
||||
{
|
||||
switch(RT_SENSOR_MODE_GET_ACCURACY(info->mode))
|
||||
{
|
||||
case RT_SENSOR_MODE_ACCURACY_HIGHEST:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_MODE_ACCURACY_HIGHEST);
|
||||
case RT_SENSOR_MODE_ACCURACY_HIGH:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_MODE_ACCURACY_HIGH);
|
||||
case RT_SENSOR_MODE_ACCURACY_MEDIUM:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_MODE_ACCURACY_MEDIUM);
|
||||
case RT_SENSOR_MODE_ACCURACY_LOW:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_MODE_ACCURACY_LOW);
|
||||
case RT_SENSOR_MODE_ACCURACY_LOWEST:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_MODE_ACCURACY_LOWEST);
|
||||
case RT_SENSOR_MODE_ACCURACY_NOTRUST:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_MODE_ACCURACY_NOTRUST);
|
||||
default:
|
||||
LOG_E("accuracy mode illegal!");
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
static const char* sensor_get_power_mode_name(rt_sensor_info_t info)
|
||||
{
|
||||
switch(RT_SENSOR_MODE_GET_POWER(info->mode))
|
||||
{
|
||||
case RT_SENSOR_MODE_POWER_HIGHEST:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_MODE_POWER_HIGHEST);
|
||||
case RT_SENSOR_MODE_POWER_HIGH:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_MODE_POWER_HIGH);
|
||||
case RT_SENSOR_MODE_POWER_MEDIUM:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_MODE_POWER_MEDIUM);
|
||||
case RT_SENSOR_MODE_POWER_LOW:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_MODE_POWER_LOW);
|
||||
case RT_SENSOR_MODE_POWER_LOWEST:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_MODE_POWER_LOWEST);
|
||||
case RT_SENSOR_MODE_POWER_DOWN:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_MODE_POWER_DOWN);
|
||||
default:
|
||||
LOG_E("power mode illegal!");
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
static const char* sensor_get_fetch_mode_name(rt_sensor_info_t info)
|
||||
{
|
||||
switch(RT_SENSOR_MODE_GET_FETCH(info->mode))
|
||||
{
|
||||
case RT_SENSOR_MODE_FETCH_POLLING:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_MODE_FETCH_POLLING);
|
||||
case RT_SENSOR_MODE_FETCH_INT:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_MODE_FETCH_INT);
|
||||
case RT_SENSOR_MODE_FETCH_FIFO:
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_MODE_FETCH_FIFO);
|
||||
default:
|
||||
LOG_E("fetch data mode illegal!");
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
static void sensor_show_data(rt_size_t num, rt_sensor_t sensor, struct rt_sensor_data *sensor_data)
|
||||
{
|
||||
const char *unit_name = sensor_get_unit_name(&sensor->info);
|
||||
switch (sensor->info.type)
|
||||
{
|
||||
case RT_SENSOR_CLASS_ACCE:
|
||||
case RT_SENSOR_TYPE_ACCE:
|
||||
LOG_I("num:%d, x:%f, y:%f, z:%f %s, timestamp:%u", num, sensor_data->data.acce.x, sensor_data->data.acce.y, sensor_data->data.acce.z, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_GYRO:
|
||||
case RT_SENSOR_TYPE_GYRO:
|
||||
LOG_I("num:%d, x:%f, y:%f, z:%f %s, timestamp:%u", num, sensor_data->data.gyro.x, sensor_data->data.gyro.y, sensor_data->data.gyro.z, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_MAG:
|
||||
case RT_SENSOR_TYPE_MAG:
|
||||
LOG_I("num:%d, x:%f, y:%f, z:%f %s, timestamp:%u", num, sensor_data->data.mag.x, sensor_data->data.mag.y, sensor_data->data.mag.z, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_GNSS:
|
||||
case RT_SENSOR_TYPE_GNSS:
|
||||
LOG_I("num:%d, lon:%f, lat:%f %s, timestamp:%u", num, sensor_data->data.coord.longitude, sensor_data->data.coord.latitude, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_TEMP:
|
||||
case RT_SENSOR_TYPE_TEMP:
|
||||
LOG_I("num:%d, temp:%f%s, timestamp:%u", num, sensor_data->data.temp, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_HUMI:
|
||||
case RT_SENSOR_TYPE_HUMI:
|
||||
LOG_I("num:%d, humi:%f%s, timestamp:%u", num, sensor_data->data.humi, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_BARO:
|
||||
case RT_SENSOR_TYPE_BARO:
|
||||
LOG_I("num:%d, press:%f%s, timestamp:%u", num, sensor_data->data.baro, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_LIGHT:
|
||||
case RT_SENSOR_TYPE_LIGHT:
|
||||
LOG_I("num:%d, light:%f%s, timestamp:%u", num, sensor_data->data.light, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_PROXIMITY:
|
||||
case RT_SENSOR_CLASS_TOF:
|
||||
case RT_SENSOR_TYPE_PROXIMITY:
|
||||
case RT_SENSOR_TYPE_TOF:
|
||||
LOG_I("num:%d, distance:%f%s, timestamp:%u", num, sensor_data->data.proximity, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_HR:
|
||||
case RT_SENSOR_TYPE_HR:
|
||||
LOG_I("num:%d, heart rate:%f%s, timestamp:%u", num, sensor_data->data.hr, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_TVOC:
|
||||
case RT_SENSOR_TYPE_TVOC:
|
||||
LOG_I("num:%d, tvoc:%f%s, timestamp:%u", num, sensor_data->data.tvoc, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_NOISE:
|
||||
case RT_SENSOR_TYPE_NOISE:
|
||||
LOG_I("num:%d, noise:%f%s, timestamp:%u", num, sensor_data->data.noise, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_STEP:
|
||||
case RT_SENSOR_TYPE_STEP:
|
||||
LOG_I("num:%d, step:%f%s, timestamp:%u", num, sensor_data->data.step, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_FORCE:
|
||||
case RT_SENSOR_TYPE_FORCE:
|
||||
LOG_I("num:%d, force:%f%s, timestamp:%u", num, sensor_data->data.force, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_DUST:
|
||||
case RT_SENSOR_TYPE_DUST:
|
||||
LOG_I("num:%d, dust:%f%s, timestamp:%u", num, sensor_data->data.dust, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_ECO2:
|
||||
case RT_SENSOR_TYPE_ECO2:
|
||||
LOG_I("num:%d, eco2:%f%s, timestamp:%u", num, sensor_data->data.eco2, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_IAQ:
|
||||
case RT_SENSOR_TYPE_IAQ:
|
||||
LOG_I("num:%d, IAQ:%f%s, timestamp:%u", num, sensor_data->data.iaq, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_ETOH:
|
||||
case RT_SENSOR_TYPE_ETOH:
|
||||
LOG_I("num:%d, EtOH:%f%s, timestamp:%u", num, sensor_data->data.etoh, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_BP:
|
||||
case RT_SENSOR_TYPE_BP:
|
||||
LOG_I("num:%d, bp.sbp:%f, bp.dbp:%f %s, timestamp:%u", num, sensor_data->data.bp.sbp, sensor_data->data.bp.dbp, unit_name, sensor_data->timestamp);
|
||||
break;
|
||||
case RT_SENSOR_CLASS_NONE:
|
||||
case RT_SENSOR_TYPE_NONE:
|
||||
default:
|
||||
LOG_E("Unknown type of sensor!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static const char* sensor_get_intf_name(rt_sensor_t sensor)
|
||||
{
|
||||
rt_uint8_t type = sensor->config.intf.type;
|
||||
|
||||
if (type | RT_SENSOR_INTF_I2C)
|
||||
{
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_INTF_I2C);
|
||||
}
|
||||
else if (type | RT_SENSOR_INTF_SPI)
|
||||
{
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_INTF_SPI);
|
||||
}
|
||||
else if (type | RT_SENSOR_INTF_UART)
|
||||
{
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_INTF_UART);
|
||||
}
|
||||
else if (type | RT_SENSOR_INTF_ONEWIRE)
|
||||
{
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_INTF_ONEWIRE);
|
||||
}
|
||||
else if (type | RT_SENSOR_INTF_CAN)
|
||||
{
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_INTF_CAN);
|
||||
}
|
||||
else if (type | RT_SENSOR_INTF_MODBUS)
|
||||
{
|
||||
return RT_SENSOR_MACRO_GET_NAME(RT_SENSOR_INTF_MODBUS);
|
||||
}
|
||||
else
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
static rt_err_t rx_callback(rt_device_t dev, rt_size_t size)
|
||||
{
|
||||
rt_sem_release(sensor_rx_sem);
|
||||
|
@ -263,15 +358,11 @@ static rt_err_t rx_callback(rt_device_t dev, rt_size_t size)
|
|||
|
||||
static void sensor_fifo_rx_entry(void *parameter)
|
||||
{
|
||||
rt_device_t dev = (rt_device_t)parameter;
|
||||
rt_sensor_t sensor = (rt_sensor_t)parameter;
|
||||
struct rt_sensor_data *data = RT_NULL;
|
||||
struct rt_sensor_info info;
|
||||
rt_size_t res, i;
|
||||
|
||||
rt_device_control(dev, RT_SENSOR_CTRL_GET_INFO, &info);
|
||||
|
||||
data = (struct rt_sensor_data *)rt_malloc(sizeof(struct rt_sensor_data) * info.fifo_max);
|
||||
data = (struct rt_sensor_data *)rt_malloc(sizeof(struct rt_sensor_data) * sensor->info.fifo_max);
|
||||
if (data == RT_NULL)
|
||||
{
|
||||
LOG_E("Memory allocation failed!");
|
||||
|
@ -281,7 +372,7 @@ static void sensor_fifo_rx_entry(void *parameter)
|
|||
{
|
||||
rt_sem_take(sensor_rx_sem, RT_WAITING_FOREVER);
|
||||
|
||||
res = rt_device_read(dev, 0, data, info.fifo_max);
|
||||
res = rt_device_read((rt_device_t)sensor, 0, data, sensor->info.fifo_max);
|
||||
for (i = 0; i < res; i++)
|
||||
{
|
||||
sensor_show_data(i, sensor, &data[i]);
|
||||
|
@ -328,8 +419,6 @@ static void sensor_fifo(int argc, char **argv)
|
|||
rt_thread_startup(tid1);
|
||||
|
||||
rt_device_set_rx_indicate(dev, rx_callback);
|
||||
|
||||
rt_device_control(dev, RT_SENSOR_CTRL_SET_ODR, (void *)20);
|
||||
}
|
||||
#ifdef RT_USING_FINSH
|
||||
MSH_CMD_EXPORT(sensor_fifo, Sensor fifo mode test function);
|
||||
|
@ -393,7 +482,6 @@ static void sensor_int(int argc, char **argv)
|
|||
LOG_E("open device failed!");
|
||||
return;
|
||||
}
|
||||
rt_device_control(dev, RT_SENSOR_CTRL_SET_ODR, (void *)20);
|
||||
}
|
||||
#ifdef RT_USING_FINSH
|
||||
MSH_CMD_EXPORT(sensor_int, Sensor interrupt mode test function);
|
||||
|
@ -419,7 +507,7 @@ static void sensor_polling(int argc, char **argv)
|
|||
num = atoi(argv[2]);
|
||||
|
||||
sensor = (rt_sensor_t)dev;
|
||||
delay = sensor->info.period_min > 100 ? sensor->info.period_min : 100;
|
||||
delay = sensor->info.acquire_min > 100 ? sensor->info.acquire_min : 100;
|
||||
|
||||
result = rt_device_open(dev, RT_DEVICE_FLAG_RDONLY);
|
||||
if (result != RT_EOK)
|
||||
|
@ -427,7 +515,6 @@ static void sensor_polling(int argc, char **argv)
|
|||
LOG_E("open device failed! error code : %d", result);
|
||||
return;
|
||||
}
|
||||
rt_device_control(dev, RT_SENSOR_CTRL_SET_ODR, (void *)100);
|
||||
|
||||
for (i = 0; i < num; i++)
|
||||
{
|
||||
|
@ -461,32 +548,33 @@ static void sensor(int argc, char **argv)
|
|||
{
|
||||
rt_kprintf("\n");
|
||||
rt_kprintf("sensor [OPTION] [PARAM]\n");
|
||||
rt_kprintf(" probe <dev_name> Probe sensor by given name\n");
|
||||
rt_kprintf(" info Get sensor info\n");
|
||||
rt_kprintf(" range <var> Set range to var\n");
|
||||
rt_kprintf(" mode <var> Set work mode to var\n");
|
||||
rt_kprintf(" power <var> Set power mode to var\n");
|
||||
rt_kprintf(" rate <var> Set output date rate to var\n");
|
||||
rt_kprintf(" read [num] Read [num] times sensor (default 5)\n");
|
||||
rt_kprintf(" probe <dev_name> probe sensor by given name\n");
|
||||
rt_kprintf(" info get sensor information\n");
|
||||
rt_kprintf(" read [num] read [num] times sensor (default 5)\n");
|
||||
return ;
|
||||
}
|
||||
else if (!strcmp(argv[1], "info"))
|
||||
{
|
||||
struct rt_sensor_info info;
|
||||
if (dev == RT_NULL)
|
||||
{
|
||||
LOG_W("Please probe sensor device first!");
|
||||
return ;
|
||||
}
|
||||
rt_device_control(dev, RT_SENSOR_CTRL_GET_INFO, &info);
|
||||
rt_kprintf("model :%s\n", info.model);
|
||||
rt_kprintf("type: :%s\n", sensor_get_type_name(&info));
|
||||
rt_kprintf("vendor :%s\n", sensor_get_vendor_name(&info));
|
||||
rt_kprintf("unit :%s\n", sensor_get_unit_name(&info));
|
||||
rt_kprintf("range_max :%d\n", info.range_max);
|
||||
rt_kprintf("range_min :%d\n", info.range_min);
|
||||
rt_kprintf("period_min:%dms\n", info.period_min);
|
||||
rt_kprintf("fifo_max :%d\n", info.fifo_max);
|
||||
sensor = (rt_sensor_t)dev;
|
||||
rt_kprintf("name :%s\n", sensor->info.name);
|
||||
rt_kprintf("type: :%s\n", sensor_get_type_name(&sensor->info));
|
||||
rt_kprintf("vendor :%s\n", sensor_get_vendor_name(&sensor->info));
|
||||
rt_kprintf("interface :%s\n", sensor_get_intf_name(sensor));
|
||||
rt_kprintf("unit :%s\n", sensor_get_unit_name(&sensor->info));
|
||||
rt_kprintf("fetch data:%s\n", sensor_get_fetch_mode_name(&sensor->info));
|
||||
rt_kprintf("power :%s\n", sensor_get_power_mode_name(&sensor->info));
|
||||
rt_kprintf("accuracy :%s\n", sensor_get_accuracy_mode_name(&sensor->info));
|
||||
rt_kprintf("range max :%f\n", sensor->info.scale.range_max);
|
||||
rt_kprintf("range min :%f\n", sensor->info.scale.range_min);
|
||||
rt_kprintf("resolution:%f\n", sensor->info.accuracy.resolution);
|
||||
rt_kprintf("error :%f\n", sensor->info.accuracy.error);
|
||||
rt_kprintf("acquire min:%fms\n", sensor->info.acquire_min);
|
||||
rt_kprintf("fifo max :%d\n", sensor->info.fifo_max);
|
||||
}
|
||||
else if (!strcmp(argv[1], "read"))
|
||||
{
|
||||
|
@ -503,7 +591,7 @@ static void sensor(int argc, char **argv)
|
|||
}
|
||||
|
||||
sensor = (rt_sensor_t)dev;
|
||||
delay = sensor->info.period_min > 100 ? sensor->info.period_min : 100;
|
||||
delay = sensor->info.acquire_min > 100 ? sensor->info.acquire_min : 100;
|
||||
|
||||
for (i = 0; i < num; i++)
|
||||
{
|
||||
|
@ -537,8 +625,10 @@ static void sensor(int argc, char **argv)
|
|||
LOG_E("open device failed!");
|
||||
return;
|
||||
}
|
||||
rt_device_control(new_dev, RT_SENSOR_CTRL_GET_ID, ®);
|
||||
LOG_I("device id: 0x%x!", reg);
|
||||
if (rt_device_control(new_dev, RT_SENSOR_CTRL_GET_ID, ®) == RT_EOK)
|
||||
{
|
||||
LOG_I("Sensor Chip ID: %#x", reg);
|
||||
}
|
||||
if (dev)
|
||||
{
|
||||
rt_device_close(dev);
|
||||
|
@ -550,22 +640,6 @@ static void sensor(int argc, char **argv)
|
|||
LOG_W("Please probe sensor first!");
|
||||
return ;
|
||||
}
|
||||
else if (!strcmp(argv[1], "range"))
|
||||
{
|
||||
rt_device_control(dev, RT_SENSOR_CTRL_SET_RANGE, (void *)atoi(argv[2]));
|
||||
}
|
||||
else if (!strcmp(argv[1], "mode"))
|
||||
{
|
||||
rt_device_control(dev, RT_SENSOR_CTRL_SET_MODE, (void *)atoi(argv[2]));
|
||||
}
|
||||
else if (!strcmp(argv[1], "power"))
|
||||
{
|
||||
rt_device_control(dev, RT_SENSOR_CTRL_SET_POWER, (void *)atoi(argv[2]));
|
||||
}
|
||||
else if (!strcmp(argv[1], "rate"))
|
||||
{
|
||||
rt_device_control(dev, RT_SENSOR_CTRL_SET_ODR, (void *)atoi(argv[2]));
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_W("Unknown command, please enter 'sensor' get help information!");
|
||||
|
|
Loading…
Reference in New Issue