Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport v3.7-branch] sensor: shell: Allow individual axis data to be processed #83723

Merged
merged 4 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 32 additions & 37 deletions drivers/sensor/default_rtio_sensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,25 +313,44 @@ static int get_frame_count(const uint8_t *buffer, struct sensor_chan_spec channe

switch (channel.chan_type) {
case SENSOR_CHAN_ACCEL_XYZ:
channel.chan_type = SENSOR_CHAN_ACCEL_X;
break;
case SENSOR_CHAN_GYRO_XYZ:
channel.chan_type = SENSOR_CHAN_GYRO_X;
break;
case SENSOR_CHAN_MAGN_XYZ:
channel.chan_type = SENSOR_CHAN_MAGN_X;
break;
case SENSOR_CHAN_POS_DXYZ:
channel.chan_type = SENSOR_CHAN_POS_DX;
for (size_t i = 0 ; i < header->num_channels; ++i) {
/* For 3-axis channels, we need to verify we have each individual axis */
struct sensor_chan_spec channel_x = {
.chan_type = channel.chan_type - 3,
.chan_idx = channel.chan_idx,
};
struct sensor_chan_spec channel_y = {
.chan_type = channel.chan_type - 2,
.chan_idx = channel.chan_idx,
};
struct sensor_chan_spec channel_z = {
.chan_type = channel.chan_type - 1,
.chan_idx = channel.chan_idx,
};

/** The three axes don't need to be at the beginning of the header, but
* they should be consecutive.
*/
if (((header->num_channels - i) >= 3) &&
sensor_chan_spec_eq(header->channels[i], channel_x) &&
sensor_chan_spec_eq(header->channels[i + 1], channel_y) &&
sensor_chan_spec_eq(header->channels[i + 2], channel_z)) {
*frame_count = 1;
return 0;
}
}
break;
default:
break;
}
for (size_t i = 0; i < header->num_channels; ++i) {
if (sensor_chan_spec_eq(header->channels[i], channel)) {
*frame_count = 1;
return 0;
for (size_t i = 0; i < header->num_channels; ++i) {
if (sensor_chan_spec_eq(header->channels[i], channel)) {
*frame_count = 1;
return 0;
}
}
break;
}

return -ENOTSUP;
Expand All @@ -348,21 +367,9 @@ int sensor_natively_supported_channel_size_info(struct sensor_chan_spec channel,
}

switch (channel.chan_type) {
case SENSOR_CHAN_ACCEL_X:
case SENSOR_CHAN_ACCEL_Y:
case SENSOR_CHAN_ACCEL_Z:
case SENSOR_CHAN_ACCEL_XYZ:
case SENSOR_CHAN_GYRO_X:
case SENSOR_CHAN_GYRO_Y:
case SENSOR_CHAN_GYRO_Z:
case SENSOR_CHAN_GYRO_XYZ:
case SENSOR_CHAN_MAGN_X:
case SENSOR_CHAN_MAGN_Y:
case SENSOR_CHAN_MAGN_Z:
case SENSOR_CHAN_MAGN_XYZ:
case SENSOR_CHAN_POS_DX:
case SENSOR_CHAN_POS_DY:
case SENSOR_CHAN_POS_DZ:
case SENSOR_CHAN_POS_DXYZ:
*base_size = sizeof(struct sensor_three_axis_data);
*frame_size = sizeof(struct sensor_three_axis_sample_data);
Expand Down Expand Up @@ -475,33 +482,21 @@ static int decode(const uint8_t *buffer, struct sensor_chan_spec chan_spec,

/* Check for 3d channel mappings */
switch (chan_spec.chan_type) {
case SENSOR_CHAN_ACCEL_X:
case SENSOR_CHAN_ACCEL_Y:
case SENSOR_CHAN_ACCEL_Z:
case SENSOR_CHAN_ACCEL_XYZ:
count = decode_three_axis(header, q, data_out, SENSOR_CHAN_ACCEL_X,
SENSOR_CHAN_ACCEL_Y, SENSOR_CHAN_ACCEL_Z,
chan_spec.chan_idx);
break;
case SENSOR_CHAN_GYRO_X:
case SENSOR_CHAN_GYRO_Y:
case SENSOR_CHAN_GYRO_Z:
case SENSOR_CHAN_GYRO_XYZ:
count = decode_three_axis(header, q, data_out, SENSOR_CHAN_GYRO_X,
SENSOR_CHAN_GYRO_Y, SENSOR_CHAN_GYRO_Z,
chan_spec.chan_idx);
break;
case SENSOR_CHAN_MAGN_X:
case SENSOR_CHAN_MAGN_Y:
case SENSOR_CHAN_MAGN_Z:
case SENSOR_CHAN_MAGN_XYZ:
count = decode_three_axis(header, q, data_out, SENSOR_CHAN_MAGN_X,
SENSOR_CHAN_MAGN_Y, SENSOR_CHAN_MAGN_Z,
chan_spec.chan_idx);
break;
case SENSOR_CHAN_POS_DX:
case SENSOR_CHAN_POS_DY:
case SENSOR_CHAN_POS_DZ:
case SENSOR_CHAN_POS_DXYZ:
count = decode_three_axis(header, q, data_out, SENSOR_CHAN_POS_DX,
SENSOR_CHAN_POS_DY, SENSOR_CHAN_POS_DZ,
Expand Down
17 changes: 0 additions & 17 deletions drivers/sensor/sensor_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,23 +354,6 @@ void sensor_shell_processing_callback(int result, uint8_t *buf, uint32_t buf_len
size_t frame_size;
uint16_t frame_count;

/* Channels with multi-axis equivalents are skipped */
switch (ch.chan_type) {
case SENSOR_CHAN_ACCEL_X:
case SENSOR_CHAN_ACCEL_Y:
case SENSOR_CHAN_ACCEL_Z:
case SENSOR_CHAN_GYRO_X:
case SENSOR_CHAN_GYRO_Y:
case SENSOR_CHAN_GYRO_Z:
case SENSOR_CHAN_MAGN_X:
case SENSOR_CHAN_MAGN_Y:
case SENSOR_CHAN_MAGN_Z:
case SENSOR_CHAN_POS_DX:
case SENSOR_CHAN_POS_DY:
case SENSOR_CHAN_POS_DZ:
continue;
}

rc = decoder->get_size_info(ch, &base_size, &frame_size);
if (rc != 0) {
LOG_DBG("skipping unsupported channel %s:%d",
Expand Down
12 changes: 4 additions & 8 deletions samples/sensor/sensor_shell/pytest/test_sensor_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@ def test_sensor_shell_info(shell: Shell):
def test_sensor_shell_get(shell: Shell):
logger.info('send "sensor get" command')

lines = shell.exec_command('sensor get sensor@0 voltage')
assert any(['channel type=31(voltage)' in line for line in lines]), 'expected response not found'

lines = shell.exec_command('sensor get sensor@1 53')
assert any(['channel type=53(gauge_state_of_health)' in line for line in lines]), 'expected response not found'

# Channel should be the last one before 'all' (because 'all' doesn't print anything) so that the
# for-loop in `parse_named_int()` will go through everything
lines = shell.exec_command('sensor get sensor@0 gauge_desired_charging_current')
assert any(['channel type=59(gauge_desired_charging_current)' in line for line in lines]), 'expected response not found'
for channel in range(59):
logger.info(f'channel {channel}')
lines = shell.exec_command(f'sensor get sensor@0 {channel}')
assert any([f'channel type={channel}' in line for line in lines]), 'expected response not found'

logger.info('response is valid')

Expand Down
29 changes: 2 additions & 27 deletions tests/drivers/build_all/sensor/src/generic_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,33 +216,8 @@ static void run_generic_test(const struct device *dev)
q31_t q;
int8_t shift;

switch (ch) {
/* Special handling to break out triplet samples. */
case SENSOR_CHAN_MAGN_X:
case SENSOR_CHAN_ACCEL_X:
case SENSOR_CHAN_GYRO_X:
q = decoded_data.three_axis.readings[0].x;
shift = decoded_data.three_axis.shift;
break;
case SENSOR_CHAN_MAGN_Y:
case SENSOR_CHAN_ACCEL_Y:
case SENSOR_CHAN_GYRO_Y:
q = decoded_data.three_axis.readings[0].y;
shift = decoded_data.three_axis.shift;
break;
case SENSOR_CHAN_MAGN_Z:
case SENSOR_CHAN_ACCEL_Z:
case SENSOR_CHAN_GYRO_Z:
q = decoded_data.three_axis.readings[0].z;
shift = decoded_data.three_axis.shift;
break;

/* Default case for single Q31 samples */
default:
q = decoded_data.q31.readings[0].value;
shift = decoded_data.q31.shift;
break;
}
q = decoded_data.q31.readings[0].value;
shift = decoded_data.q31.shift;

/* Align everything to be a 64-bit Q32.32 number for comparison */
int64_t expected_shifted =
Expand Down
Loading