Skip to content

Commit

Permalink
sensors: add new channel SENSOR_CHAN_POS_DXYZ
Browse files Browse the repository at this point in the history
Add new channel: `SENSOR_CHAN_POS_DXYZ`, so that it is
consistent with other 3-axis channels.

Updated pytest, `sensor_shell` & `fake_sensor` accordingly.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
  • Loading branch information
ycsin authored and nashif committed May 24, 2024
1 parent 4e71680 commit e993e99
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 8 deletions.
5 changes: 5 additions & 0 deletions drivers/sensor/default_rtio_sensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ static int get_frame_count(const uint8_t *buffer, struct sensor_chan_spec channe
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;
break;
default:
break;
}
Expand Down Expand Up @@ -343,6 +346,7 @@ int sensor_natively_supported_channel_size_info(struct sensor_chan_spec channel,
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);
return 0;
Expand Down Expand Up @@ -481,6 +485,7 @@ static int decode(const uint8_t *buffer, struct sensor_chan_spec chan_spec,
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,
chan_spec.chan_idx);
Expand Down
6 changes: 4 additions & 2 deletions drivers/sensor/sensor_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ static const char *sensor_channel_name[SENSOR_CHAN_COMMON_COUNT] = {
[SENSOR_CHAN_POS_DX] = "pos_dx",
[SENSOR_CHAN_POS_DY] = "pos_dy",
[SENSOR_CHAN_POS_DZ] = "pos_dz",
[SENSOR_CHAN_POS_DXYZ] = "pos_dxyz",
[SENSOR_CHAN_RPM] = "rpm",
[SENSOR_CHAN_GAUGE_VOLTAGE] = "gauge_voltage",
[SENSOR_CHAN_GAUGE_AVG_CURRENT] = "gauge_avg_current",
Expand Down Expand Up @@ -364,6 +365,7 @@ void sensor_shell_processing_callback(int result, uint8_t *buf, uint32_t buf_len
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;
Expand Down Expand Up @@ -392,7 +394,7 @@ void sensor_shell_processing_callback(int result, uint8_t *buf, uint32_t buf_len
case SENSOR_CHAN_ACCEL_XYZ:
case SENSOR_CHAN_GYRO_XYZ:
case SENSOR_CHAN_MAGN_XYZ:
case SENSOR_CHAN_POS_DX: {
case SENSOR_CHAN_POS_DXYZ: {
struct sensor_three_axis_data *data =
(struct sensor_three_axis_data *)decoded_buffer;

Expand Down Expand Up @@ -446,7 +448,7 @@ void sensor_shell_processing_callback(int result, uint8_t *buf, uint32_t buf_len
case SENSOR_CHAN_ACCEL_XYZ:
case SENSOR_CHAN_GYRO_XYZ:
case SENSOR_CHAN_MAGN_XYZ:
case SENSOR_CHAN_POS_DX: {
case SENSOR_CHAN_POS_DXYZ: {
struct sensor_three_axis_data *data =
(struct sensor_three_axis_data *)decoded_buffer;

Expand Down
6 changes: 4 additions & 2 deletions include/zephyr/drivers/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ enum sensor_channel {
SENSOR_CHAN_POS_DY,
/** Position change on the Z axis, in points. */
SENSOR_CHAN_POS_DZ,
/** Position change on the X, Y and Z axis, in points. */
SENSOR_CHAN_POS_DXYZ,

/** Revolutions per minute, in RPM. */
SENSOR_CHAN_RPM,
Expand Down Expand Up @@ -929,12 +931,12 @@ struct __attribute__((__packed__)) sensor_data_generic_header {
*
* @param[in] chan The channel to check
* @retval true if @p chan is any of @ref SENSOR_CHAN_ACCEL_XYZ, @ref SENSOR_CHAN_GYRO_XYZ, or
* @ref SENSOR_CHAN_MAGN_XYZ
* @ref SENSOR_CHAN_MAGN_XYZ, or @ref SENSOR_CHAN_POS_DXYZ
* @retval false otherwise
*/
#define SENSOR_CHANNEL_3_AXIS(chan) \
((chan) == SENSOR_CHAN_ACCEL_XYZ || (chan) == SENSOR_CHAN_GYRO_XYZ || \
(chan) == SENSOR_CHAN_MAGN_XYZ)
(chan) == SENSOR_CHAN_MAGN_XYZ || (chan) == SENSOR_CHAN_POS_DXYZ)

/**
* @brief Get the sensor's decoder API
Expand Down
1 change: 1 addition & 0 deletions include/zephyr/drivers/sensor_data_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct sensor_data_header {
* - :c:enum:`SENSOR_CHAN_POS_DX`
* - :c:enum:`SENSOR_CHAN_POS_DY`
* - :c:enum:`SENSOR_CHAN_POS_DZ`
* - :c:enum:`SENSOR_CHAN_POS_DXYZ`
*/
struct sensor_three_axis_data {
struct sensor_data_header header;
Expand Down
8 changes: 4 additions & 4 deletions samples/sensor/sensor_shell/pytest/test_sensor_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ def test_sensor_shell_get(shell: Shell):
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_time_to_empty)' in line for line in lines]), 'expected response not found'
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=58(gauge_desired_charging_current)' in line for line in lines]), 'expected response not found'
assert any(['channel type=59(gauge_desired_charging_current)' in line for line in lines]), 'expected response not found'

logger.info('response is valid')

Expand All @@ -42,7 +42,7 @@ def test_sensor_shell_attr_get(shell: Shell):
assert any(['sensor@0(channel=co2, attr=sampling_frequency)' in line for line in lines]), 'expected response not found'

lines = shell.exec_command('sensor attr_get sensor@1 53 3')
assert any(['sensor@1(channel=gauge_time_to_empty, attr=slope_th)' in line for line in lines]), 'expected response not found'
assert any(['sensor@1(channel=gauge_state_of_health, attr=slope_th)' in line for line in lines]), 'expected response not found'

logger.info('response is valid')

Expand All @@ -55,7 +55,7 @@ def test_sensor_shell_attr_set(shell: Shell):
assert any([expected_line in line for line in lines]), 'expected response not found'

lines = shell.exec_command('sensor attr_set sensor@1 53 3 1')
expected_line = 'sensor@1 channel=gauge_time_to_empty, attr=slope_th set to value=1'
expected_line = 'sensor@1 channel=gauge_state_of_health, attr=slope_th set to value=1'
assert any([expected_line in line for line in lines]), 'expected response not found'

logger.info('response is valid')
Expand Down
2 changes: 2 additions & 0 deletions samples/sensor/sensor_shell/src/fake_sensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ static int channel_get(const struct device *dev, enum sensor_channel chan, struc
case SENSOR_CHAN_GYRO_XYZ:
__fallthrough;
case SENSOR_CHAN_MAGN_XYZ:
__fallthrough;
case SENSOR_CHAN_POS_DXYZ:
for (int i = 0; i < 3; i++, val++) {
val->val1 = chan;
val->val2 = 1;
Expand Down

0 comments on commit e993e99

Please sign in to comment.