Skip to content

Commit 93e1560

Browse files
diegosueironashif
authored andcommitted
subsys/shell: Introduce the Sensor Shell Module
Adds a Shell Module for retrieving Sensor data. The following commands were added: sensor - Device commands Options: -h, --help :Show command help. Subcommands: get :<device_name> [chanel_idx] list :List configured sensors list_channels :<device_name> Signed-off-by: Diego Sueiro <[email protected]>
1 parent bb95dce commit 93e1560

File tree

3 files changed

+202
-0
lines changed

3 files changed

+202
-0
lines changed

drivers/sensor/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,4 @@ add_subdirectory_ifdef(CONFIG_TMP116 tmp116)
6262
add_subdirectory_ifdef(CONFIG_VL53L0X vl53l0x)
6363

6464
zephyr_sources_ifdef(CONFIG_USERSPACE sensor_handlers.c)
65+
zephyr_sources_ifdef(CONFIG_SENSOR_SHELL sensor_shell.c)

drivers/sensor/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ config SENSOR_INIT_PRIORITY
2020
help
2121
Sensor initialization priority.
2222

23+
config SENSOR_SHELL
24+
bool "Enable sensor shell"
25+
help
26+
This shell provides access to basic sensor data.
27+
2328
comment "Device Drivers"
2429

2530
source "drivers/sensor/adt7420/Kconfig"

drivers/sensor/sensor_shell.c

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/*
2+
* Copyright (c) 2018 Diego Sueiro
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <shell/shell.h>
8+
#include <stdlib.h>
9+
#include <string.h>
10+
#include <ctype.h>
11+
#include <device.h>
12+
#include <sensor.h>
13+
14+
extern struct device __device_init_start[];
15+
extern struct device __device_init_end[];
16+
17+
const char *sensor_channel_name[SENSOR_CHAN_ALL] = {
18+
[SENSOR_CHAN_ACCEL_X] = "accel_x",
19+
[SENSOR_CHAN_ACCEL_Y] = "accel_y",
20+
[SENSOR_CHAN_ACCEL_Z] = "accel_z",
21+
[SENSOR_CHAN_ACCEL_XYZ] = "accel_xyz",
22+
[SENSOR_CHAN_GYRO_X] = "gyro_x",
23+
[SENSOR_CHAN_GYRO_Y] = "gyro_y",
24+
[SENSOR_CHAN_GYRO_Z] = "gyro_z",
25+
[SENSOR_CHAN_GYRO_XYZ] = "gyro_xyz",
26+
[SENSOR_CHAN_MAGN_X] = "magn_x",
27+
[SENSOR_CHAN_MAGN_Y] = "magn_y",
28+
[SENSOR_CHAN_MAGN_Z] = "magn_z",
29+
[SENSOR_CHAN_MAGN_XYZ] = "magn_xyz",
30+
[SENSOR_CHAN_DIE_TEMP] = "die_temp",
31+
[SENSOR_CHAN_AMBIENT_TEMP] = "ambient_temp",
32+
[SENSOR_CHAN_PRESS] = "press",
33+
[SENSOR_CHAN_PROX] = "prox",
34+
[SENSOR_CHAN_HUMIDITY] = "humidity",
35+
[SENSOR_CHAN_LIGHT] = "light",
36+
[SENSOR_CHAN_IR] = "ir",
37+
[SENSOR_CHAN_RED] = "red",
38+
[SENSOR_CHAN_GREEN] = "green",
39+
[SENSOR_CHAN_BLUE] = "blue",
40+
[SENSOR_CHAN_ALTITUDE] = "altitude",
41+
[SENSOR_CHAN_PM_1_0] = "pm_1_0",
42+
[SENSOR_CHAN_PM_2_5] = "pm_2_5",
43+
[SENSOR_CHAN_PM_10] = "pm_10",
44+
[SENSOR_CHAN_DISTANCE] = "distance",
45+
[SENSOR_CHAN_CO2] = "co2",
46+
[SENSOR_CHAN_VOC] = "voc",
47+
[SENSOR_CHAN_VOLTAGE] = "voltage",
48+
[SENSOR_CHAN_CURRENT] = "current",
49+
[SENSOR_CHAN_ROTATION] = "rotation",
50+
};
51+
52+
static int cmd_list_sensor_channels(const struct shell *shell,
53+
size_t argc, char *argv[])
54+
{
55+
int err;
56+
struct device *dev;
57+
struct sensor_value value[3];
58+
59+
dev = device_get_binding(argv[1]);
60+
61+
if (dev != NULL) {
62+
63+
err = sensor_sample_fetch(dev);
64+
if (err) {
65+
shell_error(shell, "sensor_sample_fetch failed err=%d",
66+
err);
67+
return err;
68+
}
69+
70+
for (unsigned int i = 0; i < SENSOR_CHAN_ALL; i++) {
71+
if (!sensor_channel_get(dev, i, value)) {
72+
shell_print(shell, "idx=%d name=%s",
73+
i, sensor_channel_name[i]);
74+
}
75+
}
76+
77+
} else {
78+
shell_error(shell, "Could not bind the %s sensor", argv[1]);
79+
return -ENXIO;
80+
}
81+
82+
return 0;
83+
}
84+
85+
static int cmd_get_sensor(const struct shell *shell, size_t argc, char *argv[])
86+
{
87+
int err;
88+
struct device *dev;
89+
struct sensor_value value[3];
90+
int idx;
91+
92+
dev = device_get_binding(argv[1]);
93+
94+
if (dev != NULL) {
95+
err = sensor_sample_fetch(dev);
96+
if (err) {
97+
shell_error(shell, "sensor_sample_fetch failed err=%d",
98+
err);
99+
return err;
100+
}
101+
102+
if (isdigit((unsigned char)argv[2][0])) {
103+
idx = (u8_t)atoi(argv[2]);
104+
if (!sensor_channel_get(dev, idx, value)) {
105+
if (idx != SENSOR_CHAN_ACCEL_XYZ &&
106+
idx != SENSOR_CHAN_GYRO_XYZ &&
107+
idx != SENSOR_CHAN_MAGN_XYZ) {
108+
shell_print(shell,
109+
"channel idx=%d %s = %10.6f", idx,
110+
sensor_channel_name[idx],
111+
sensor_value_to_double(&value[0]));
112+
} else {
113+
shell_print(shell,
114+
"channel idx=%d %s = %10.6f", idx,
115+
sensor_channel_name[idx],
116+
sensor_value_to_double(&value[0]));
117+
shell_print(shell,
118+
"channel idx=%d %s = %10.6f", idx,
119+
sensor_channel_name[idx],
120+
sensor_value_to_double(&value[1]));
121+
shell_print(shell,
122+
"channel idx=%d %s = %10.6f", idx,
123+
sensor_channel_name[idx],
124+
sensor_value_to_double(&value[2]));
125+
}
126+
} else {
127+
shell_error(shell,
128+
"Could not read channel idx %d=%s",
129+
idx, sensor_channel_name[idx]);
130+
return -EIO;
131+
}
132+
return 0;
133+
}
134+
135+
for (unsigned int i = 0; i < SENSOR_CHAN_ALL; i++) {
136+
if (!sensor_channel_get(dev, i, value)) {
137+
if (i != SENSOR_CHAN_ACCEL_XYZ &&
138+
i != SENSOR_CHAN_GYRO_XYZ &&
139+
i != SENSOR_CHAN_MAGN_XYZ) {
140+
shell_print(shell,
141+
"channel idx=%d %s = %10.6f", i,
142+
sensor_channel_name[i],
143+
sensor_value_to_double(&value[0]));
144+
} else {
145+
shell_print(shell,
146+
"channel idx=%d %s = %10.6f", i,
147+
sensor_channel_name[i],
148+
sensor_value_to_double(&value[0]));
149+
shell_print(shell,
150+
"channel idx=%d %s = %10.6f", i,
151+
sensor_channel_name[i],
152+
sensor_value_to_double(&value[1]));
153+
shell_print(shell,
154+
"channel idx=%d %s = %10.6f", i,
155+
sensor_channel_name[i],
156+
sensor_value_to_double(&value[2]));
157+
}
158+
}
159+
}
160+
161+
} else {
162+
shell_error(shell, "Could not bind the %s sensor", argv[1]);
163+
return -ENXIO;
164+
}
165+
166+
return 0;
167+
}
168+
169+
static int cmd_list_sensors(const struct shell *shell,
170+
size_t argc, char **argv)
171+
{
172+
struct device *dev;
173+
174+
ARG_UNUSED(argc);
175+
ARG_UNUSED(argv);
176+
177+
shell_print(shell, "devices:");
178+
for (dev = __device_init_start; dev != __device_init_end; dev++) {
179+
if (dev->driver_api != NULL) {
180+
shell_print(shell, "- %s", dev->config->name);
181+
}
182+
}
183+
184+
return 0;
185+
}
186+
187+
SHELL_STATIC_SUBCMD_SET_CREATE(sub_sensor,
188+
SHELL_CMD_ARG(get, NULL, "<device_name> [chanel_idx]", cmd_get_sensor,
189+
2, 1),
190+
SHELL_CMD(list, NULL, "List configured sensors", cmd_list_sensors),
191+
SHELL_CMD_ARG(list_channels, NULL, "<device_name>",
192+
cmd_list_sensor_channels, 2, 0),
193+
SHELL_SUBCMD_SET_END
194+
);
195+
196+
SHELL_CMD_REGISTER(sensor, &sub_sensor, "Sensor commands", NULL);

0 commit comments

Comments
 (0)