[sensor] add setting power/accuracy/fetch data mode commands

This commit is contained in:
Meco Man 2022-12-25 20:48:32 -05:00 committed by Man, Jianting (Meco)
parent 3a7ee55780
commit b1763e6903
3 changed files with 112 additions and 14 deletions

View File

@ -222,11 +222,11 @@ extern "C" {
#define RT_SENSOR_MODE_ACCURACY_HIGH_STR "Accuracy High" #define RT_SENSOR_MODE_ACCURACY_HIGH_STR "Accuracy High"
#define RT_SENSOR_MODE_ACCURACY_MEDIUM (2) #define RT_SENSOR_MODE_ACCURACY_MEDIUM (2)
#define RT_SENSOR_MODE_ACCURACY_MEDIUM_STR "Accuracy Medium" #define RT_SENSOR_MODE_ACCURACY_MEDIUM_STR "Accuracy Medium"
#define RT_SENSOR_MODE_ACCURACY_LOW (4) #define RT_SENSOR_MODE_ACCURACY_LOW (3)
#define RT_SENSOR_MODE_ACCURACY_LOW_STR "Accuracy Low" #define RT_SENSOR_MODE_ACCURACY_LOW_STR "Accuracy Low"
#define RT_SENSOR_MODE_ACCURACY_LOWEST (5) #define RT_SENSOR_MODE_ACCURACY_LOWEST (4)
#define RT_SENSOR_MODE_ACCURACY_LOWEST_STR "Accuracy Lowest" #define RT_SENSOR_MODE_ACCURACY_LOWEST_STR "Accuracy Lowest"
#define RT_SENSOR_MODE_ACCURACY_NOTRUST (6) #define RT_SENSOR_MODE_ACCURACY_NOTRUST (5)
#define RT_SENSOR_MODE_ACCURACY_NOTRUST_STR "Accuracy No Trust" #define RT_SENSOR_MODE_ACCURACY_NOTRUST_STR "Accuracy No Trust"
/* Sensor mode: power */ /* Sensor mode: power */

View File

@ -358,7 +358,7 @@ static rt_err_t _sensor_control(rt_device_t dev, int cmd, void *args)
break; break;
case RT_SENSOR_CTRL_SET_ACCURACY_MODE: case RT_SENSOR_CTRL_SET_ACCURACY_MODE:
/* Configuration sensor power mode */ /* Configuration sensor power mode */
mode = (rt_uint32_t)args; mode = (rt_uint32_t)args & 0x000F;
if (!(mode == RT_SENSOR_MODE_ACCURACY_HIGHEST || mode == RT_SENSOR_MODE_ACCURACY_HIGH ||\ if (!(mode == RT_SENSOR_MODE_ACCURACY_HIGHEST || mode == RT_SENSOR_MODE_ACCURACY_HIGH ||\
mode == RT_SENSOR_MODE_ACCURACY_MEDIUM || mode == RT_SENSOR_MODE_ACCURACY_LOW ||\ mode == RT_SENSOR_MODE_ACCURACY_MEDIUM || mode == RT_SENSOR_MODE_ACCURACY_LOW ||\
mode == RT_SENSOR_MODE_ACCURACY_LOWEST || mode == RT_SENSOR_MODE_ACCURACY_NOTRUST)) mode == RT_SENSOR_MODE_ACCURACY_LOWEST || mode == RT_SENSOR_MODE_ACCURACY_NOTRUST))
@ -369,13 +369,13 @@ static rt_err_t _sensor_control(rt_device_t dev, int cmd, void *args)
result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_ACCURACY_MODE, args); result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_ACCURACY_MODE, args);
if (result == RT_EOK) if (result == RT_EOK)
{ {
RT_SENSOR_MODE_SET_ACCURACY(sensor->info.mode, (rt_uint32_t)args & 0x0F); RT_SENSOR_MODE_SET_ACCURACY(sensor->info.mode, mode);
LOG_D("set accuracy mode code: %d", RT_SENSOR_MODE_GET_ACCURACY(sensor->info.mode)); LOG_D("set accuracy mode code: %d", RT_SENSOR_MODE_GET_ACCURACY(sensor->info.mode));
} }
break; break;
case RT_SENSOR_CTRL_SET_POWER_MODE: case RT_SENSOR_CTRL_SET_POWER_MODE:
/* Configuration sensor power mode */ /* Configuration sensor power mode */
mode = (rt_uint32_t)args; mode = (rt_uint32_t)args & 0x000F;
if (!(mode == RT_SENSOR_MODE_POWER_HIGHEST || mode == RT_SENSOR_MODE_POWER_HIGH ||\ if (!(mode == RT_SENSOR_MODE_POWER_HIGHEST || mode == RT_SENSOR_MODE_POWER_HIGH ||\
mode == RT_SENSOR_MODE_POWER_MEDIUM || mode == RT_SENSOR_MODE_POWER_LOW ||\ mode == RT_SENSOR_MODE_POWER_MEDIUM || mode == RT_SENSOR_MODE_POWER_LOW ||\
mode == RT_SENSOR_MODE_POWER_LOWEST || mode == RT_SENSOR_MODE_POWER_DOWN)) mode == RT_SENSOR_MODE_POWER_LOWEST || mode == RT_SENSOR_MODE_POWER_DOWN))
@ -386,13 +386,13 @@ static rt_err_t _sensor_control(rt_device_t dev, int cmd, void *args)
result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER_MODE, args); result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER_MODE, args);
if (result == RT_EOK) if (result == RT_EOK)
{ {
RT_SENSOR_MODE_SET_POWER(sensor->info.mode, (rt_uint32_t)args & 0x0F); RT_SENSOR_MODE_SET_POWER(sensor->info.mode, mode);
LOG_D("set power mode code: %d", RT_SENSOR_MODE_GET_POWER(sensor->info.mode)); LOG_D("set power mode code: %d", RT_SENSOR_MODE_GET_POWER(sensor->info.mode));
} }
break; break;
case RT_SENSOR_CTRL_SET_FETCH_MODE: case RT_SENSOR_CTRL_SET_FETCH_MODE:
/* Configuration sensor power mode */ /* Configuration sensor power mode */
mode = (rt_uint32_t)args; mode = (rt_uint32_t)args & 0x000F;
if (!(mode == RT_SENSOR_MODE_FETCH_POLLING || mode == RT_SENSOR_MODE_FETCH_INT ||\ if (!(mode == RT_SENSOR_MODE_FETCH_POLLING || mode == RT_SENSOR_MODE_FETCH_INT ||\
mode == RT_SENSOR_MODE_FETCH_FIFO)) mode == RT_SENSOR_MODE_FETCH_FIFO))
{ {
@ -402,7 +402,7 @@ static rt_err_t _sensor_control(rt_device_t dev, int cmd, void *args)
result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_FETCH_MODE, args); result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_FETCH_MODE, args);
if (result == RT_EOK) if (result == RT_EOK)
{ {
RT_SENSOR_MODE_SET_FETCH(sensor->info.mode, (rt_uint32_t)args & 0x0F); RT_SENSOR_MODE_SET_FETCH(sensor->info.mode, mode);
LOG_D("set fetch mode code: %d", RT_SENSOR_MODE_GET_FETCH(sensor->info.mode)); LOG_D("set fetch mode code: %d", RT_SENSOR_MODE_GET_FETCH(sensor->info.mode));
} }
break; break;
@ -517,9 +517,10 @@ int rt_hw_sensor_register(rt_sensor_t sensor,
LOG_I("sensor[%s] init success", device_name); LOG_I("sensor[%s] init success", device_name);
rt_free(device_name); rt_free(device_name);
/* set sensor accuracy and power as the hightest */ /* set sensor accuracy and power as the hightest, and polling data as default */
rt_device_control(device, RT_SENSOR_CTRL_SET_ACCURACY_MODE, RT_SENSOR_MODE_ACCURACY_HIGHEST); rt_device_control(device, RT_SENSOR_CTRL_SET_ACCURACY_MODE, RT_SENSOR_MODE_ACCURACY_HIGHEST);
rt_device_control(device, RT_SENSOR_CTRL_SET_POWER_MODE, RT_SENSOR_MODE_POWER_HIGHEST); rt_device_control(device, RT_SENSOR_CTRL_SET_POWER_MODE, RT_SENSOR_MODE_POWER_HIGHEST);
rt_device_control(device, RT_SENSOR_CTRL_SET_FETCH_MODE, RT_SENSOR_MODE_FETCH_POLLING);
return RT_EOK; return RT_EOK;
} }

View File

@ -541,6 +541,7 @@ static void sensor_cmd_warning_unknown(void)
rt_kprintf(" read [num] read [num] times sensor (default 5)\n"); rt_kprintf(" read [num] read [num] times sensor (default 5)\n");
rt_kprintf(" power [mode] set or get power mode\n"); rt_kprintf(" power [mode] set or get power mode\n");
rt_kprintf(" accuracy [mode] set or get accuracy mode\n"); rt_kprintf(" accuracy [mode] set or get accuracy mode\n");
rt_kprintf(" fetch [mode] set or get fetch data mode\n");
} }
static void sensor_cmd_warning_probe(void) static void sensor_cmd_warning_probe(void)
@ -562,7 +563,7 @@ static void sensor(int argc, char **argv)
sensor_cmd_warning_unknown(); sensor_cmd_warning_unknown();
return; return;
} }
else if (!strcmp(argv[1], "info")) else if (!rt_strcmp(argv[1], "info"))
{ {
if (dev == RT_NULL) if (dev == RT_NULL)
{ {
@ -586,7 +587,7 @@ static void sensor(int argc, char **argv)
rt_kprintf("interface type :%s\n", sensor_get_intf_name(sensor)); rt_kprintf("interface type :%s\n", sensor_get_intf_name(sensor));
rt_kprintf("interface device :%s\n", sensor->config.intf.dev_name); rt_kprintf("interface device :%s\n", sensor->config.intf.dev_name);
} }
else if (!strcmp(argv[1], "read")) else if (!rt_strcmp(argv[1], "read"))
{ {
rt_uint16_t num = 5; rt_uint16_t num = 5;
@ -617,7 +618,7 @@ static void sensor(int argc, char **argv)
rt_thread_mdelay(delay); rt_thread_mdelay(delay);
} }
} }
else if (!strcmp(argv[1], "list")) else if (!rt_strcmp(argv[1], "list"))
{ {
struct rt_object *object; struct rt_object *object;
struct rt_list_node *node; struct rt_list_node *node;
@ -648,7 +649,7 @@ static void sensor(int argc, char **argv)
2, sensor_dev->info.scale.range_min, 2, sensor_dev->info.scale.range_max, 5, sensor_get_unit_name(&sensor_dev->info)); 2, sensor_dev->info.scale.range_min, 2, sensor_dev->info.scale.range_max, 5, sensor_get_unit_name(&sensor_dev->info));
} }
} }
else if (!strcmp(argv[1], "probe")) else if (!rt_strcmp(argv[1], "probe"))
{ {
rt_uint8_t reg = 0xFF; rt_uint8_t reg = 0xFF;
rt_device_t new_dev; rt_device_t new_dev;
@ -680,6 +681,102 @@ static void sensor(int argc, char **argv)
} }
dev = new_dev; dev = new_dev;
} }
else if (!rt_strcmp(argv[1], "power"))
{
rt_uint32_t mode;
if (dev == RT_NULL)
{
sensor_cmd_warning_probe();
return;
}
sensor = (rt_sensor_t)dev;
if (argc == 2)
{
rt_kprintf("current power mode: %s\n", sensor_get_power_mode_name(&sensor->info));
}
else if (argc == 3)
{
mode = atoi(argv[2]);
if (rt_device_control(dev, RT_SENSOR_CTRL_SET_POWER_MODE, (void *)mode) == RT_EOK)
{
rt_kprintf("set new power mode as: %s\n", sensor_get_power_mode_name(&sensor->info));
}
else
{
LOG_E("Don't support! Set new power mode error!");
}
}
else
{
sensor_cmd_warning_unknown();
}
}
else if (!rt_strcmp(argv[1], "accuracy"))
{
rt_uint32_t mode;
if (dev == RT_NULL)
{
sensor_cmd_warning_probe();
return;
}
sensor = (rt_sensor_t)dev;
if (argc == 2)
{
rt_kprintf("current accuracy mode: %s\n", sensor_get_accuracy_mode_name(&sensor->info));
}
else if (argc == 3)
{
mode = atoi(argv[2]);
if (rt_device_control(dev, RT_SENSOR_CTRL_SET_ACCURACY_MODE, (void *)mode) == RT_EOK)
{
rt_kprintf("set new accuracy mode as: %s\n", sensor_get_accuracy_mode_name(&sensor->info));
}
else
{
LOG_E("Don't support! Set new accuracy mode error!");
}
}
else
{
sensor_cmd_warning_unknown();
}
}
else if (!rt_strcmp(argv[1], "fetch"))
{
rt_uint32_t mode;
if (dev == RT_NULL)
{
sensor_cmd_warning_probe();
return;
}
sensor = (rt_sensor_t)dev;
if (argc == 2)
{
rt_kprintf("current fetch data mode: %s\n", sensor_get_fetch_mode_name(&sensor->info));
}
else if (argc == 3)
{
mode = atoi(argv[2]);
if (rt_device_control(dev, RT_SENSOR_CTRL_SET_FETCH_MODE, (void *)mode) == RT_EOK)
{
rt_kprintf("set new fetch data mode as: %s\n", sensor_get_fetch_mode_name(&sensor->info));
}
else
{
LOG_E("Don't support! Set new fetch data mode error!");
}
}
else
{
sensor_cmd_warning_unknown();
}
}
else else
{ {
sensor_cmd_warning_unknown(); sensor_cmd_warning_unknown();