From 953a8c265d9e4f51d943393632c99e2146c3368f Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Thu, 17 Nov 2022 18:29:24 -0500 Subject: [PATCH] add rt_sensor_float_t --- components/drivers/Kconfig | 6 +- components/drivers/include/drivers/sensor.h | 67 +++++++++++---------- components/drivers/sensors/sensor.c | 8 +-- 3 files changed, 43 insertions(+), 38 deletions(-) diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 481d9c68c1..66014b25a3 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -322,8 +322,12 @@ config RT_USING_SENSOR if RT_USING_SENSOR config RT_USING_SENSOR_CMD - bool "Using Sensor cmd" + bool "Enable sensor commands" default y + + config RT_USING_SENSOR_DOUBLE_FLOAT + bool "Using double floating as sensor data type" + default n endif config RT_USING_TOUCH diff --git a/components/drivers/include/drivers/sensor.h b/components/drivers/include/drivers/sensor.h index 71376e1018..a1de110a08 100644 --- a/components/drivers/include/drivers/sensor.h +++ b/components/drivers/include/drivers/sensor.h @@ -30,7 +30,6 @@ extern "C" { #define RT_SENSOR_MODULE_MAX (3) /* The maximum number of members of a sensor module */ /* Sensor types */ - #define RT_SENSOR_CLASS_NONE (0) #define RT_SENSOR_CLASS_ACCE (1) /* Accelerometer */ #define RT_SENSOR_CLASS_GYRO (2) /* Gyroscope */ @@ -55,7 +54,6 @@ extern "C" { #define RT_SENSOR_CLASS_BP (21) /* Blood Pressure */ /* Sensor vendor types */ - #define RT_SENSOR_VENDOR_UNKNOWN (0) #define RT_SENSOR_VENDOR_STM (1) /* STMicroelectronics */ #define RT_SENSOR_VENDOR_BOSCH (2) /* Bosch */ @@ -74,7 +72,6 @@ extern "C" { #define RT_SENSOR_VENDOR_MELEXIS (15) /* Melexis */ /* Sensor unit types */ - #define RT_SENSOR_UNIT_NONE (0) #define RT_SENSOR_UNIT_MG (1) /* Accelerometer unit: mG */ #define RT_SENSOR_UNIT_MDPS (2) /* Gyroscope unit: mdps */ @@ -95,15 +92,14 @@ extern "C" { #define RT_SENSOR_UNIT_DD (17) /* Coordinates unit: DD */ #define RT_SENSOR_UNIT_MGM3 (18) /* Concentration unit: mg/m3 */ #define RT_SENSOR_UNIT_MMHG (19) /* Blood Pressure unit: mmHg */ -/* Sensor communication interface types */ +/* Sensor communication interface types */ #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) /* Sensor power mode types */ - #define RT_SENSOR_POWER_NONE (0) #define RT_SENSOR_POWER_DOWN (1) /* power down mode */ #define RT_SENSOR_POWER_NORMAL (2) /* normal-power mode */ @@ -111,14 +107,12 @@ extern "C" { #define RT_SENSOR_POWER_HIGH (4) /* high-power mode */ /* 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 */ /* 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 */ @@ -129,6 +123,13 @@ extern "C" { #define RT_SENSOR_CTRL_USER_CMD_START 0x100 /* User commands should be greater than 0x100 */ +/* sensor floating data type */ +#ifdef RT_USING_SENSOR_DOUBLE_FLOAT +typedef double rt_sensor_float_t; +#else +typedef float rt_sensor_float_t; +#endif /* RT_USING_SENSOR_DOUBLE_FLOAT */ + struct rt_sensor_info { rt_uint8_t type; /* The sensor type */ @@ -190,22 +191,22 @@ struct rt_sensor_module /* 3-axis Data Type */ struct sensor_3_axis { - float x; - float y; - float z; + rt_sensor_float_t x; + rt_sensor_float_t y; + rt_sensor_float_t z; }; /* Blood Pressure Data Type */ struct sensor_bp { - float sbp; /* SBP : systolic pressure */ - float dbp; /* DBP : diastolic pressure */ + rt_sensor_float_t sbp; /* SBP : systolic pressure */ + rt_sensor_float_t dbp; /* DBP : diastolic pressure */ }; struct coordinates { - float longitude; - float latitude; + rt_sensor_float_t longitude; + rt_sensor_float_t latitude; }; struct rt_sensor_data @@ -219,21 +220,21 @@ struct rt_sensor_data struct sensor_3_axis mag; /* Magnetometer. unit: mGauss */ struct coordinates coord; /* Coordinates unit: degrees */ struct sensor_bp bp; /* BloodPressure. unit: mmHg */ - float temp; /* Temperature. unit: dCelsius */ - float humi; /* Relative humidity. unit: permillage */ - float baro; /* Pressure. unit: pascal (Pa) */ - float light; /* Light. unit: lux */ - float proximity; /* Distance. unit: centimeters */ - float hr; /* Heart rate. unit: bpm */ - float tvoc; /* TVOC. unit: permillage */ - float noise; /* Noise Loudness. unit: HZ */ - float step; /* Step sensor. unit: 1 */ - float force; /* Force sensor. unit: mN */ - float dust; /* Dust sensor. unit: ug/m3 */ - float eco2; /* eCO2 sensor. unit: ppm */ - float spo2; /* SpO2 sensor. unit: permillage */ - float iaq; /* IAQ sensor. unit: 1 */ - float etoh; /* EtOH sensor. unit: ppm */ + rt_sensor_float_t temp; /* Temperature. unit: dCelsius */ + rt_sensor_float_t humi; /* Relative humidity. unit: permillage */ + rt_sensor_float_t baro; /* Pressure. unit: pascal (Pa) */ + rt_sensor_float_t light; /* Light. unit: lux */ + rt_sensor_float_t proximity; /* Distance. unit: centimeters */ + rt_sensor_float_t hr; /* Heart rate. unit: bpm */ + rt_sensor_float_t tvoc; /* TVOC. unit: permillage */ + rt_sensor_float_t noise; /* Noise Loudness. unit: HZ */ + rt_sensor_float_t step; /* Step sensor. unit: 1 */ + rt_sensor_float_t force; /* Force sensor. unit: mN */ + rt_sensor_float_t dust; /* Dust sensor. unit: ug/m3 */ + rt_sensor_float_t eco2; /* eCO2 sensor. unit: ppm */ + rt_sensor_float_t spo2; /* SpO2 sensor. unit: permillage */ + rt_sensor_float_t iaq; /* IAQ sensor. unit: 1 */ + rt_sensor_float_t etoh; /* EtOH sensor. unit: ppm */ } data; }; @@ -243,10 +244,10 @@ struct rt_sensor_ops rt_err_t (*control)(rt_sensor_t sensor, int cmd, void *arg); }; -int rt_hw_sensor_register(rt_sensor_t sensor, - const char *name, - rt_uint32_t flag, - void *data); +int rt_hw_sensor_register(rt_sensor_t sensor, + const char *name, + rt_uint32_t flag, + void *data); #ifdef __cplusplus } diff --git a/components/drivers/sensors/sensor.c b/components/drivers/sensors/sensor.c index 763de38cf2..3a5a0421df 100644 --- a/components/drivers/sensors/sensor.c +++ b/components/drivers/sensors/sensor.c @@ -421,10 +421,10 @@ const static struct rt_device_ops rt_sensor_ops = /* * sensor register */ -int rt_hw_sensor_register(rt_sensor_t sensor, - const char *name, - rt_uint32_t flag, - void *data) +int rt_hw_sensor_register(rt_sensor_t sensor, + const char *name, + rt_uint32_t flag, + void *data) { rt_int8_t result; rt_device_t device;