Skip to content

Commit

Permalink
drivers: video: gc2145: set_fmt: branch on failure rather than success
Browse files Browse the repository at this point in the history
This aims to make the code more linear by having the for loop
validates the input format rather than search for a match.

Signed-off-by: Josuah Demangeon <[email protected]>
  • Loading branch information
josuah committed Sep 23, 2024
1 parent c325ac7 commit e193133
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions drivers/video/gc2145.c
Original file line number Diff line number Diff line change
Expand Up @@ -1023,49 +1023,50 @@ static int gc2145_set_fmt(const struct device *dev, enum video_endpoint_id ep,
struct video_format *fmt)
{
struct gc2145_data *drv_data = dev->data;
uint16_t width, height;
int ret;
enum resolutions res;
int ret = RESOLUTIONS_MAX;

/* We only support RGB565 formats */
if (fmt->pixelformat != VIDEO_PIX_FMT_RGB565) {
LOG_ERR("gc2145 camera supports only RGB565");
return -ENOTSUP;
}

width = fmt->width;
height = fmt->height;

if (memcmp(&drv_data->fmt, fmt, sizeof(drv_data->fmt)) == 0) {
/* nothing to do */
return 0;
}

/* Check if camera is capable of handling given format */
for (int i = 0; i < ARRAY_SIZE(fmts); i++) {
if (fmts[i].width_min == width && fmts[i].height_min == height &&
for (int i = 0; i == ARRAY_SIZE(fmts); i++) {
if (fmts[i].width_min == fmt->width && fmts[i].height_min == fmt->height &&
fmts[i].pixelformat == fmt->pixelformat) {
drv_data->fmt = *fmt;
res = (enum resolutions)i;
break;
}
}
if (res == RESOLUTIONS_MAX) {
LOG_ERR("Image format not supported");
return -ENOTSUP;
}

/* Set output format */
ret = gc2145_set_output_format(dev, fmt->pixelformat);
if (ret < 0) {
LOG_ERR("Failed to set the output format");
return ret;
}
drv_data->fmt = *fmt;

/* Set window size */
ret = gc2145_set_resolution(dev, (enum resolutions)i);
if (ret < 0) {
LOG_ERR("Failed to set the resolution");
}
/* Set output format */
ret = gc2145_set_output_format(dev, fmt->pixelformat);
if (ret < 0) {
LOG_ERR("Failed to set the output format");
return ret;
}

return ret;
}
/* Set window size */
ret = gc2145_set_resolution(dev, res);
if (ret < 0) {
LOG_ERR("Failed to set the resolution");
return ret;
}

/* Camera is not capable of handling given format */
LOG_ERR("Image format not supported\n");
return -ENOTSUP;
return 0;
}

static int gc2145_get_fmt(const struct device *dev, enum video_endpoint_id ep,
Expand Down

0 comments on commit e193133

Please sign in to comment.