Skip to content

Commit 3ea45bd

Browse files
Yuval Peressyperess
authored andcommitted
sensors: Migrate use cases
Migrate a few samples in order to establish a new pattern to replace fetch/get. Signed-off-by: Yuval Peress <[email protected]>
1 parent 025687a commit 3ea45bd

File tree

9 files changed

+235
-176
lines changed

9 files changed

+235
-176
lines changed

tests/boards/nrf/qdec/src/main.c

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,13 @@ static void qenc_emulate_verify_reading(struct qdec_qenc_loopback *loopback,
130130
int rc;
131131
struct sensor_value val = {0};
132132
int32_t expected_steps = emulation_duration_ms / emulator_period_ms;
133-
int32_t expected_reading = 360 * expected_steps / loopback->qdec_config_step;
134-
int32_t delta = expected_reading / 4;
133+
int32_t expected_reading = 360 * expected_steps / config_step;
134+
int32_t delta = expected_reading / 5;
135+
int8_t rotation_shift;
136+
q31_t rotation_value;
137+
struct sensor_chan_spec channels[1] = {
138+
{.chan_type = SENSOR_CHAN_ROTATION, .chan_idx = 0},
139+
};
135140

136141
if (!forward) {
137142
expected_reading *= -1;
@@ -142,16 +147,16 @@ static void qenc_emulate_verify_reading(struct qdec_qenc_loopback *loopback,
142147
/* wait for some readings */
143148
k_msleep(emulation_duration_ms);
144149

145-
rc = sensor_sample_fetch(loopback->qdec);
150+
rc = sensor_read_and_decode(loopback->qdec, channels, ARRAY_SIZE(channels), &rotation_shift,
151+
&rotation_value, 1);
146152

147153
if (!overflow_expected) {
148154
zassert_true(rc == 0, "Failed to fetch sample (%d)", rc);
149155
} else {
150156
zassert_true(rc == -EOVERFLOW, "Failed to detect overflow");
151157
}
152158

153-
rc = sensor_channel_get(loopback->qdec, SENSOR_CHAN_ROTATION, &val);
154-
zassert_true(rc == 0, "Failed to get sample (%d)", rc);
159+
q31_to_sensor_value(rotation_value, rotation_shift, &val);
155160

156161
TC_PRINT("Expected reading: %d, actual value: %d, delta: %d\n",
157162
expected_reading, val.val1, delta);
@@ -165,11 +170,9 @@ static void qenc_emulate_verify_reading(struct qdec_qenc_loopback *loopback,
165170
/* wait and get readings to clear state */
166171
k_msleep(100);
167172

168-
rc = sensor_sample_fetch(loopback->qdec);
169-
zassert_true(rc == 0, "Failed to fetch sample (%d)", rc);
170-
171-
rc = sensor_channel_get(loopback->qdec, SENSOR_CHAN_ROTATION, &val);
172-
zassert_true(rc == 0, "Failed to get sample (%d)", rc);
173+
rc = sensor_read_and_decode(loopback->qdec, channels, ARRAY_SIZE(channels), &rotation_shift,
174+
&rotation_value, 1);
175+
zassert_true(rc == 0, "Failed to read sample (%d)", rc);
173176
}
174177

175178
static void sensor_trigger_set_and_disable(struct qdec_qenc_loopback *loopback)
@@ -247,6 +250,11 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_and_disable)
247250
static void sensor_trigger_set_test(struct qdec_qenc_loopback *loopback)
248251
{
249252
int rc;
253+
int8_t rotation_shift;
254+
q31_t rotation_value;
255+
struct sensor_chan_spec channels[1] = {
256+
{.chan_type = SENSOR_CHAN_ROTATION, .chan_idx = 0},
257+
};
250258
struct sensor_value val = {0};
251259

252260
if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
@@ -266,11 +274,11 @@ static void sensor_trigger_set_test(struct qdec_qenc_loopback *loopback)
266274
rc = k_sem_take(&sem, K_MSEC(200));
267275
zassert_true(rc == 0, "qdec handler should be triggered (%d)", rc);
268276

269-
rc = sensor_sample_fetch(loopback->qdec);
270-
zassert_true(rc == 0, "Failed to fetch sample (%d)", rc);
277+
rc = sensor_read_and_decode(loopback->qdec, channels, ARRAY_SIZE(channels), &rotation_shift,
278+
&rotation_value, 1);
279+
zassert_true(rc == 0, "Failed to read sample (%d)", rc);
271280

272-
rc = sensor_channel_get(loopback->qdec, SENSOR_CHAN_ROTATION, &val);
273-
zassert_true(rc == 0, "Failed to fetch sample (%d)", rc);
281+
q31_to_sensor_value(rotation_value, rotation_shift, &val);
274282

275283
TC_PRINT("QDEC reading: %d\n", val.val1);
276284
zassert_true(val.val1 != 0, "No readings from QDEC");
@@ -368,6 +376,11 @@ ZTEST(qdec_sensor, test_qdec_readings)
368376
static void sensor_channel_get_empty(const struct device *const dev)
369377
{
370378
int rc;
379+
int8_t rotation_shift;
380+
q31_t rotation_value;
381+
struct sensor_chan_spec channels[1] = {
382+
{.chan_type = SENSOR_CHAN_ROTATION, .chan_idx = 0},
383+
};
371384
struct sensor_value val = {0};
372385

373386
if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
@@ -377,24 +390,19 @@ static void sensor_channel_get_empty(const struct device *const dev)
377390
/* wait for potential new readings */
378391
k_msleep(100);
379392

380-
rc = sensor_sample_fetch(dev);
381-
zassert_true(rc == 0, "Failed to fetch sample (%d)", rc);
382-
383-
/* get readings but ignore them, as they may include reading from time
384-
* when emulation was still working (i.e. during previous test)
385-
*/
386-
rc = sensor_channel_get(dev, SENSOR_CHAN_ROTATION, &val);
387-
zassert_true(rc == 0, "Failed to get sample (%d)", rc);
393+
rc = sensor_read_and_decode(dev, channels, ARRAY_SIZE(channels), &rotation_shift,
394+
&rotation_value, 1);
395+
zassert_true(rc == 0, "Failed to read sample (%d)", rc);
388396

389397
/* wait for potential new readings */
390398
k_msleep(100);
391399

392-
rc = sensor_sample_fetch(dev);
400+
rc = sensor_read_and_decode(dev, channels, ARRAY_SIZE(channels), &rotation_shift,
401+
&rotation_value, 1);
393402
zassert_true(rc == 0, "Failed to fetch sample (%d)", rc);
394403

395404
/* emulation was not working, expect no readings */
396-
rc = sensor_channel_get(dev, SENSOR_CHAN_ROTATION, &val);
397-
zassert_true(rc == 0, "Failed to get sample (%d)", rc);
405+
q31_to_sensor_value(rotation_value, rotation_shift, &val);
398406
zassert_true(val.val1 == 0, "Expected no readings but got: %d", val.val1);
399407
zassert_true(val.val2 == 0, "Expected no readings but got: %d", val.val2);
400408

@@ -420,8 +428,12 @@ ZTEST(qdec_sensor, test_sensor_channel_get_empty)
420428
static void sensor_channel_get_test(struct qdec_qenc_loopback *loopback)
421429
{
422430
int rc;
423-
struct sensor_value val_first = {0};
424-
struct sensor_value val_second = {0};
431+
int8_t rotation_shift;
432+
q31_t rotation_value;
433+
struct sensor_chan_spec channels[1] = {
434+
{.chan_type = SENSOR_CHAN_ROTATION, .chan_idx = 0},
435+
};
436+
struct sensor_value val = {0};
425437

426438
if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
427439
pm_device_runtime_get(loopback->qdec);
@@ -432,32 +444,12 @@ static void sensor_channel_get_test(struct qdec_qenc_loopback *loopback)
432444
/* wait for some readings*/
433445
k_msleep(100);
434446

435-
rc = sensor_sample_fetch(loopback->qdec);
436-
zassert_true(rc == 0, "Failed to fetch sample (%d)", rc);
437-
438-
rc = sensor_channel_get(loopback->qdec, SENSOR_CHAN_ROTATION, &val_first);
439-
zassert_true(rc == 0, "Failed to get sample (%d)", rc);
440-
zassert_true(val_first.val1 != 0, "No readings from QDEC");
447+
rc = sensor_read_and_decode(loopback->qdec, channels, ARRAY_SIZE(channels), &rotation_shift,
448+
&rotation_value, 1);
449+
zassert_true(rc == 0, "Failed to read sample (%d)", rc);
441450

442-
/* wait for more readings*/
443-
k_msleep(200);
444-
445-
rc = sensor_channel_get(loopback->qdec, SENSOR_CHAN_ROTATION, &val_second);
446-
zassert_true(rc == 0, "Failed to fetch sample (%d)", rc);
447-
zassert_true(val_second.val1 != 0, "No readings from QDEC");
448-
449-
/* subsequent calls of sensor_channel_get without calling sensor_sample_fetch
450-
* should yield the same value
451-
*/
452-
zassert_true(val_first.val1 == val_second.val1,
453-
"Expected the same readings: %d vs %d",
454-
val_first.val1,
455-
val_second.val1);
456-
457-
zassert_true(val_first.val2 == val_second.val2,
458-
"Expected the same readings: %d vs %d",
459-
val_first.val2,
460-
val_second.val2);
451+
q31_to_sensor_value(rotation_value, rotation_shift, &val);
452+
zassert_true(val.val1 != 0, "No readings from QDEC");
461453

462454
if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
463455
pm_device_runtime_put(loopback->qdec);
@@ -481,6 +473,11 @@ ZTEST(qdec_sensor, test_sensor_channel_get)
481473
static void sensor_channel_get_negative(struct qdec_qenc_loopback *loopback)
482474
{
483475
int rc;
476+
int8_t max_shift;
477+
q31_t max_value;
478+
struct sensor_chan_spec channels[1] = {
479+
{.chan_type = SENSOR_CHAN_MAX, .chan_idx = 0},
480+
};
484481
struct sensor_value val = {0};
485482

486483
if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
@@ -492,11 +489,11 @@ static void sensor_channel_get_negative(struct qdec_qenc_loopback *loopback)
492489
/* wait for some readings*/
493490
k_msleep(100);
494491

495-
rc = sensor_sample_fetch(loopback->qdec);
496-
zassert_true(rc == 0, "Failed to fetch sample (%d)", rc);
492+
rc = sensor_read_and_decode(loopback->qdec, channels, ARRAY_SIZE(channels), &max_shift,
493+
&max_value, 1);
494+
zassert_true(rc == 0, "Failed to read sample (%d)", rc);
497495

498-
rc = sensor_channel_get(loopback->qdec, SENSOR_CHAN_MAX, &val);
499-
zassert_true(rc < 0, "Should failed to get sample (%d)", rc);
496+
q31_to_sensor_value(max_value, max_shift, &val);
500497
zassert_true(val.val1 == 0, "Some readings from QDEC: %d", val.val1);
501498
zassert_true(val.val2 == 0, "Some readings from QDEC: %d", val.val2);
502499

@@ -522,18 +519,21 @@ ZTEST(qdec_sensor, test_sensor_channel_get_negative)
522519
static void sensor_sample_fetch_test(const struct device *const dev)
523520
{
524521
int rc;
522+
int8_t shift;
523+
q31_t value;
524+
struct sensor_chan_spec channels[2] = {
525+
{.chan_type = SENSOR_CHAN_ROTATION, .chan_idx = 0},
526+
{.chan_type = SENSOR_CHAN_MAX, .chan_idx = 0},
527+
};
525528

526529
if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
527530
pm_device_runtime_get(dev);
528531
}
529532

530-
rc = sensor_sample_fetch(dev);
531-
zassert_true(rc == 0, "Failed to fetch sample (%d)", rc);
532-
533-
rc = sensor_sample_fetch_chan(dev, SENSOR_CHAN_ROTATION);
533+
rc = sensor_read_and_decode(dev, &channels[0], 1, &shift, &value, 1);
534534
zassert_true(rc == 0, "Failed to fetch sample (%d)", rc);
535535

536-
rc = sensor_sample_fetch_chan(dev, SENSOR_CHAN_MAX);
536+
rc = sensor_read_and_decode(dev, &channels[1], 1, &shift, &value, 1);
537537
zassert_true(rc < 0, "Should fail to fetch sample from invalid channel (%d)", rc);
538538

539539
if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
@@ -542,7 +542,7 @@ static void sensor_sample_fetch_test(const struct device *const dev)
542542
}
543543

544544
/**
545-
* @brief sensor_sample_fetch(_chan) test
545+
* @brief Reading sample test
546546
*
547547
* Confirm fetching work with QDEC specific channel - rotation
548548
*
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
CONFIG_ZTEST=y
2+
CONFIG_SENSOR=y
3+
CONFIG_SENSOR_LOG_LEVEL_DBG=y
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
CONFIG_ZTEST=y
22
CONFIG_FPU=y
3+
CONFIG_SENSOR=y
4+
CONFIG_SENSOR_LOG_LEVEL_DBG=y

tests/drivers/sensor/generic/src/dummy_sensor.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ static const struct dummy_sensor_config dummy_config = {
2020
.i2c_address = 123
2121
};
2222

23-
static int dummy_sensor_sample_fetch(const struct device *dev,
24-
enum sensor_channel chan)
23+
static int dummy_sensor_sample_fetch_impl(const struct device *dev, enum sensor_channel chan)
2524
{
2625
ARG_UNUSED(dev);
2726
ARG_UNUSED(chan);
@@ -30,8 +29,7 @@ static int dummy_sensor_sample_fetch(const struct device *dev,
3029
return 0;
3130
}
3231

33-
static int dummy_sensor_channel_get(const struct device *dev,
34-
enum sensor_channel chan,
32+
static int dummy_sensor_channel_get_impl(const struct device *dev, enum sensor_channel chan,
3533
struct sensor_value *val)
3634
{
3735
struct dummy_sensor_data *data = dev->data;
@@ -96,7 +94,7 @@ static int dummy_sensor_init(const struct device *dev)
9694
/* initialize the channels value for dummy driver */
9795
for (int i = 0; i < SENSOR_CHANNEL_NUM; i++) {
9896
data->val[i].val1 = i;
99-
data->val[i].val2 = i*i;
97+
data->val[i].val2 = i * i * 1000;
10098
}
10199

102100
return 0;
@@ -162,16 +160,16 @@ int dummy_sensor_trigger_set(const struct device *dev,
162160
}
163161

164162
static DEVICE_API(sensor, dummy_sensor_api) = {
165-
.sample_fetch = &dummy_sensor_sample_fetch,
166-
.channel_get = &dummy_sensor_channel_get,
163+
.sample_fetch = &dummy_sensor_sample_fetch_impl,
164+
.channel_get = &dummy_sensor_channel_get_impl,
167165
.attr_set = dummy_sensor_attr_set,
168166
.attr_get = dummy_sensor_attr_get,
169167
.trigger_set = dummy_sensor_trigger_set,
170168
};
171169

172170
static DEVICE_API(sensor, dummy_sensor_no_trig_api) = {
173-
.sample_fetch = &dummy_sensor_sample_fetch,
174-
.channel_get = &dummy_sensor_channel_get,
171+
.sample_fetch = &dummy_sensor_sample_fetch_impl,
172+
.channel_get = &dummy_sensor_channel_get_impl,
175173
.attr_set = NULL,
176174
.attr_get = NULL,
177175
.trigger_set = NULL,

0 commit comments

Comments
 (0)