Skip to content

Commit 606ecd7

Browse files
committed
drivers: video: sw_generator: use video_format_caps_index()
Use the video_format_caps_index() function to check if a format matches any entry of the format caps. Signed-off-by: Josuah Demangeon <me@josuah.net>
1 parent 7eea58d commit 606ecd7

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

drivers/video/video_sw_generator.c

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <zephyr/drivers/video.h>
1111
#include <zephyr/drivers/video-controls.h>
1212
#include <zephyr/logging/log.h>
13+
#include <zephyr/sys/util.h>
1314

1415
#include "video_ctrls.h"
1516
#include "video_device.h"
@@ -69,23 +70,16 @@ static const struct video_format_cap fmts[] = {
6970
static int video_sw_generator_set_fmt(const struct device *dev, struct video_format *fmt)
7071
{
7172
struct video_sw_generator_data *data = dev->data;
72-
int i = 0;
73+
size_t idx;
74+
int ret;
7375

74-
for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
75-
if (fmt->pixelformat == fmts[i].pixelformat && fmt->width >= fmts[i].width_min &&
76-
fmt->width <= fmts[i].width_max && fmt->height >= fmts[i].height_min &&
77-
fmt->height <= fmts[i].height_max) {
78-
break;
79-
}
80-
}
81-
82-
if (i == ARRAY_SIZE(fmts)) {
76+
ret = video_format_caps_index(fmts, fmt, &idx);
77+
if (ret < 0) {
8378
LOG_ERR("Unsupported pixel format or resolution");
84-
return -ENOTSUP;
79+
return ret;
8580
}
8681

8782
data->fmt = *fmt;
88-
8983
return 0;
9084
}
9185

@@ -277,17 +271,13 @@ static int video_sw_generator_get_frmival(const struct device *dev, struct video
277271

278272
static int video_sw_generator_enum_frmival(const struct device *dev, struct video_frmival_enum *fie)
279273
{
280-
int i = 0;
274+
size_t idx;
275+
int ret;
281276

282-
while (fmts[i].pixelformat && (fmts[i].pixelformat != fie->format->pixelformat)) {
283-
i++;
284-
}
285-
286-
if ((i == ARRAY_SIZE(fmts)) || (fie->format->width > fmts[i].width_max) ||
287-
(fie->format->width < fmts[i].width_min) ||
288-
(fie->format->height > fmts[i].height_max) ||
289-
(fie->format->height < fmts[i].height_min)) {
290-
return -EINVAL;
277+
ret = video_format_caps_index(fmts, fie->format, &idx);
278+
if (ret < 0) {
279+
LOG_ERR("Unsupported pixel format or resolution");
280+
return ret;
291281
}
292282

293283
fie->type = VIDEO_FRMIVAL_TYPE_STEPWISE;

0 commit comments

Comments
 (0)