2018-12-07 17:31:34 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Diego Sueiro
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <shell/shell.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <device.h>
|
2020-01-25 19:35:02 +08:00
|
|
|
#include <drivers/sensor.h>
|
2018-12-07 17:31:34 +08:00
|
|
|
|
2020-01-03 18:16:30 +08:00
|
|
|
#define SENSOR_GET_HELP \
|
|
|
|
"Get sensor data. Channel names are optional. All channels are read " \
|
|
|
|
"when no channels are provided. Syntax:\n" \
|
|
|
|
"<device_name> <channel name 0> .. <channel name N>"
|
|
|
|
|
2018-12-07 17:31:34 +08:00
|
|
|
const char *sensor_channel_name[SENSOR_CHAN_ALL] = {
|
|
|
|
[SENSOR_CHAN_ACCEL_X] = "accel_x",
|
|
|
|
[SENSOR_CHAN_ACCEL_Y] = "accel_y",
|
|
|
|
[SENSOR_CHAN_ACCEL_Z] = "accel_z",
|
|
|
|
[SENSOR_CHAN_ACCEL_XYZ] = "accel_xyz",
|
|
|
|
[SENSOR_CHAN_GYRO_X] = "gyro_x",
|
|
|
|
[SENSOR_CHAN_GYRO_Y] = "gyro_y",
|
|
|
|
[SENSOR_CHAN_GYRO_Z] = "gyro_z",
|
|
|
|
[SENSOR_CHAN_GYRO_XYZ] = "gyro_xyz",
|
|
|
|
[SENSOR_CHAN_MAGN_X] = "magn_x",
|
|
|
|
[SENSOR_CHAN_MAGN_Y] = "magn_y",
|
|
|
|
[SENSOR_CHAN_MAGN_Z] = "magn_z",
|
|
|
|
[SENSOR_CHAN_MAGN_XYZ] = "magn_xyz",
|
|
|
|
[SENSOR_CHAN_DIE_TEMP] = "die_temp",
|
|
|
|
[SENSOR_CHAN_AMBIENT_TEMP] = "ambient_temp",
|
|
|
|
[SENSOR_CHAN_PRESS] = "press",
|
|
|
|
[SENSOR_CHAN_PROX] = "prox",
|
|
|
|
[SENSOR_CHAN_HUMIDITY] = "humidity",
|
|
|
|
[SENSOR_CHAN_LIGHT] = "light",
|
|
|
|
[SENSOR_CHAN_IR] = "ir",
|
|
|
|
[SENSOR_CHAN_RED] = "red",
|
|
|
|
[SENSOR_CHAN_GREEN] = "green",
|
|
|
|
[SENSOR_CHAN_BLUE] = "blue",
|
|
|
|
[SENSOR_CHAN_ALTITUDE] = "altitude",
|
|
|
|
[SENSOR_CHAN_PM_1_0] = "pm_1_0",
|
|
|
|
[SENSOR_CHAN_PM_2_5] = "pm_2_5",
|
|
|
|
[SENSOR_CHAN_PM_10] = "pm_10",
|
|
|
|
[SENSOR_CHAN_DISTANCE] = "distance",
|
|
|
|
[SENSOR_CHAN_CO2] = "co2",
|
|
|
|
[SENSOR_CHAN_VOC] = "voc",
|
|
|
|
[SENSOR_CHAN_VOLTAGE] = "voltage",
|
|
|
|
[SENSOR_CHAN_CURRENT] = "current",
|
|
|
|
[SENSOR_CHAN_ROTATION] = "rotation",
|
|
|
|
};
|
|
|
|
|
2020-01-03 18:16:30 +08:00
|
|
|
static int handle_channel_by_name(const struct shell *shell, struct device *dev,
|
|
|
|
const char *channel_name)
|
2018-12-07 17:31:34 +08:00
|
|
|
{
|
|
|
|
struct sensor_value value[3];
|
2020-05-22 19:51:12 +08:00
|
|
|
char *endptr;
|
2020-01-03 18:16:30 +08:00
|
|
|
int err;
|
2020-05-22 19:51:12 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Attempt to parse channel name as a number first */
|
|
|
|
i = strtoul(channel_name, &endptr, 0);
|
2018-12-07 17:31:34 +08:00
|
|
|
|
2020-05-22 19:51:12 +08:00
|
|
|
if (*endptr != '\0') {
|
|
|
|
/* Channel name is not a number, look it up */
|
|
|
|
for (i = 0; i < ARRAY_SIZE(sensor_channel_name); i++) {
|
|
|
|
if (strcmp(channel_name, sensor_channel_name[i]) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2018-12-07 17:31:34 +08:00
|
|
|
}
|
|
|
|
|
2020-05-22 19:51:12 +08:00
|
|
|
if (i == ARRAY_SIZE(sensor_channel_name)) {
|
|
|
|
shell_error(shell, "Channel not supported (%s)",
|
|
|
|
channel_name);
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2020-01-03 18:16:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err = sensor_channel_get(dev, i, value);
|
|
|
|
if (err < 0) {
|
|
|
|
return err;
|
|
|
|
}
|
2018-12-07 17:31:34 +08:00
|
|
|
|
2020-05-22 19:51:12 +08:00
|
|
|
if (i >= ARRAY_SIZE(sensor_channel_name)) {
|
|
|
|
shell_print(shell, "channel idx=%d value = %10.6f", i,
|
|
|
|
sensor_value_to_double(&value[0]));
|
|
|
|
} else if (i != SENSOR_CHAN_ACCEL_XYZ &&
|
2020-01-03 18:16:30 +08:00
|
|
|
i != SENSOR_CHAN_GYRO_XYZ &&
|
|
|
|
i != SENSOR_CHAN_MAGN_XYZ) {
|
|
|
|
shell_print(shell,
|
|
|
|
"channel idx=%d %s = %10.6f", i,
|
|
|
|
sensor_channel_name[i],
|
|
|
|
sensor_value_to_double(&value[0]));
|
2018-12-07 17:31:34 +08:00
|
|
|
} else {
|
2020-01-03 18:16:30 +08:00
|
|
|
shell_print(shell,
|
|
|
|
"channel idx=%d %s x = %10.6f y = %10.6f z = %10.6f",
|
|
|
|
i, sensor_channel_name[i],
|
|
|
|
sensor_value_to_double(&value[0]),
|
|
|
|
sensor_value_to_double(&value[1]),
|
|
|
|
sensor_value_to_double(&value[2]));
|
2018-12-07 17:31:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static int cmd_get_sensor(const struct shell *shell, size_t argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct device *dev;
|
2020-01-03 18:16:30 +08:00
|
|
|
int err;
|
2018-12-07 17:31:34 +08:00
|
|
|
|
|
|
|
dev = device_get_binding(argv[1]);
|
2020-01-03 18:16:30 +08:00
|
|
|
if (dev == NULL) {
|
|
|
|
shell_error(shell, "Device unknown (%s)", argv[1]);
|
2020-03-11 02:43:00 +08:00
|
|
|
return -ENODEV;
|
2020-01-03 18:16:30 +08:00
|
|
|
}
|
2018-12-07 17:31:34 +08:00
|
|
|
|
2020-01-03 18:16:30 +08:00
|
|
|
err = sensor_sample_fetch(dev);
|
|
|
|
if (err < 0) {
|
|
|
|
shell_error(shell, "Failed to read sensor");
|
|
|
|
}
|
2018-12-07 17:31:34 +08:00
|
|
|
|
2020-01-03 18:16:30 +08:00
|
|
|
if (argc == 2) {
|
|
|
|
/* read all channels */
|
|
|
|
for (int i = 0; i < ARRAY_SIZE(sensor_channel_name); i++) {
|
|
|
|
if (sensor_channel_name[i]) {
|
|
|
|
handle_channel_by_name(shell, dev,
|
|
|
|
sensor_channel_name[i]);
|
2018-12-07 17:31:34 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-03 18:16:30 +08:00
|
|
|
} else {
|
|
|
|
for (int i = 2; i < argc; i++) {
|
|
|
|
err = handle_channel_by_name(shell, dev, argv[i]);
|
|
|
|
if (err < 0) {
|
|
|
|
shell_error(shell,
|
|
|
|
"Failed to read channel (%s)", argv[i]);
|
2018-12-07 17:31:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-03 18:16:30 +08:00
|
|
|
static void channel_name_get(size_t idx, struct shell_static_entry *entry);
|
|
|
|
|
|
|
|
SHELL_DYNAMIC_CMD_CREATE(dsub_channel_name, channel_name_get);
|
|
|
|
|
|
|
|
static void channel_name_get(size_t idx, struct shell_static_entry *entry)
|
2018-12-07 17:31:34 +08:00
|
|
|
{
|
2020-01-03 18:16:30 +08:00
|
|
|
int cnt = 0;
|
|
|
|
|
|
|
|
entry->syntax = NULL;
|
|
|
|
entry->handler = NULL;
|
|
|
|
entry->help = NULL;
|
|
|
|
entry->subcmd = &dsub_channel_name;
|
|
|
|
|
|
|
|
for (int i = 0; i < SENSOR_CHAN_ALL; i++) {
|
|
|
|
if (sensor_channel_name[i] != NULL) {
|
|
|
|
if (cnt == idx) {
|
|
|
|
entry->syntax = sensor_channel_name[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void device_name_get(size_t idx, struct shell_static_entry *entry);
|
|
|
|
|
|
|
|
SHELL_DYNAMIC_CMD_CREATE(dsub_device_name, device_name_get);
|
|
|
|
|
|
|
|
static void device_name_get(size_t idx, struct shell_static_entry *entry)
|
|
|
|
{
|
2020-06-22 22:56:19 +08:00
|
|
|
struct device *dev = shell_device_lookup(idx, NULL);
|
2018-12-07 17:31:34 +08:00
|
|
|
|
2020-06-22 22:56:19 +08:00
|
|
|
entry->syntax = (dev != NULL) ? dev->name : NULL;
|
2020-01-03 18:16:30 +08:00
|
|
|
entry->handler = NULL;
|
|
|
|
entry->help = NULL;
|
|
|
|
entry->subcmd = &dsub_channel_name;
|
2018-12-07 17:31:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SHELL_STATIC_SUBCMD_SET_CREATE(sub_sensor,
|
2020-01-03 18:16:30 +08:00
|
|
|
SHELL_CMD_ARG(get, &dsub_device_name, SENSOR_GET_HELP, cmd_get_sensor,
|
|
|
|
2, 255),
|
2018-12-07 17:31:34 +08:00
|
|
|
SHELL_SUBCMD_SET_END
|
|
|
|
);
|
|
|
|
|
|
|
|
SHELL_CMD_REGISTER(sensor, &sub_sensor, "Sensor commands", NULL);
|