Browse Source

Merge pull request #3397 from luhuadong/develop

[Sensor] Support custom commands for rt_sensor_control
Bernard Xiong 5 years ago
parent
commit
beda0899f5
2 changed files with 15 additions and 2 deletions
  1. 13 2
      components/drivers/sensors/sensor.c
  2. 2 0
      components/drivers/sensors/sensor.h

+ 13 - 2
components/drivers/sensors/sensor.c

@@ -6,6 +6,7 @@
  * Change Logs:
  * Change Logs:
  * Date           Author       Notes
  * Date           Author       Notes
  * 2019-01-31     flybreak     first version
  * 2019-01-31     flybreak     first version
+ * 2020-02-22     luhuadong    support custom commands
  */
  */
 
 
 #include "sensor.h"
 #include "sensor.h"
@@ -307,7 +308,7 @@ static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args)
     case RT_SENSOR_CTRL_GET_ID:
     case RT_SENSOR_CTRL_GET_ID:
         if (args)
         if (args)
         {
         {
-            sensor->ops->control(sensor, RT_SENSOR_CTRL_GET_ID, args);
+            result = sensor->ops->control(sensor, RT_SENSOR_CTRL_GET_ID, args);
         }
         }
         break;
         break;
     case RT_SENSOR_CTRL_GET_INFO:
     case RT_SENSOR_CTRL_GET_INFO:
@@ -352,7 +353,17 @@ static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args)
         result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SELF_TEST, args);
         result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SELF_TEST, args);
         break;
         break;
     default:
     default:
-        return -RT_ERROR;
+
+        if (cmd > RT_SENSOR_CTRL_USER_CMD_START)
+        {
+            /* Custom commands */
+            result = sensor->ops->control(sensor, cmd, args);
+        }
+        else
+        {
+            result = -RT_ERROR;
+        }
+        break;
     }
     }
 
 
     if (sensor->module)
     if (sensor->module)

+ 2 - 0
components/drivers/sensors/sensor.h

@@ -111,6 +111,8 @@ extern "C" {
 #define  RT_SENSOR_CTRL_SET_POWER      (5)  /* Set power mode. args type of sensor power mode. ex. RT_SENSOR_POWER_DOWN,RT_SENSOR_POWER_NORMAL */
 #define  RT_SENSOR_CTRL_SET_POWER      (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      (6)  /* Take a self test */
 #define  RT_SENSOR_CTRL_SELF_TEST      (6)  /* Take a self test */
 
 
+#define  RT_SENSOR_CTRL_USER_CMD_START 0x100  /* User commands should be greater than 0x100 */
+
 struct rt_sensor_info
 struct rt_sensor_info
 {
 {
     rt_uint8_t     type;                    /* The sensor type */
     rt_uint8_t     type;                    /* The sensor type */