Skip to content

Commit ddaabc4

Browse files
committed
samples: drivers: video: capture: use DEVICE_DT_GET_OR_NULL for display
Allow the display to also use if() instead of #if by leveraging the DEVICE_DT_GET_OR_NULL() that permit display_dev to always be defined. The compiler will const-fold all the unused variables and functions. Signed-off-by: Josuah Demangeon <[email protected]>
1 parent 980e884 commit ddaabc4

File tree

1 file changed

+11
-14
lines changed
  • samples/drivers/video/capture/src

1 file changed

+11
-14
lines changed

samples/drivers/video/capture/src/main.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ LOG_MODULE_REGISTER(main, CONFIG_LOG_DEFAULT_LEVEL);
1919
#error No camera chosen in devicetree. Missing "--shield" or "--snippet video-sw-generator" flag?
2020
#endif
2121

22-
#if DT_HAS_CHOSEN(zephyr_display)
2322
static inline int app_setup_display(const struct device *const display_dev, const uint32_t pixfmt)
2423
{
2524
struct display_capabilities capabilities;
@@ -85,7 +84,6 @@ static int app_display_frame(const struct device *const display_dev,
8584

8685
return display_write(display_dev, 0, vbuf->line_offset, &buf_desc, vbuf->buffer);
8786
}
88-
#endif
8987

9088
static int app_setup_video_selection(const struct device *const video_dev,
9189
const struct video_format *const fmt)
@@ -339,6 +337,7 @@ static int app_setup_video_buffers(const struct device *const video_dev,
339337
int main(void)
340338
{
341339
const struct device *const video_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera));
340+
const struct device *const display_dev = DEVICE_DT_GET_OR_NULL(DT_CHOSEN(zephyr_display));
342341
struct video_buffer *vbuf = &(struct video_buffer){};
343342
struct video_format fmt = {
344343
.type = VIDEO_BUF_TYPE_OUTPUT,
@@ -380,14 +379,12 @@ int main(void)
380379
goto err;
381380
}
382381

383-
#if DT_HAS_CHOSEN(zephyr_display)
384-
const struct device *const display_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
385-
386-
ret = app_setup_display(display_dev, fmt.pixelformat);
387-
if (ret < 0) {
388-
goto err;
382+
if (DT_HAS_CHOSEN(zephyr_display)) {
383+
ret = app_setup_display(display_dev, fmt.pixelformat);
384+
if (ret < 0) {
385+
goto err;
386+
}
389387
}
390-
#endif
391388

392389
ret = app_setup_video_buffers(video_dev, &caps, &fmt);
393390
if (ret < 0) {
@@ -413,12 +410,12 @@ int main(void)
413410
LOG_INF("Got frame %u! size: %u; timestamp %u ms",
414411
frame++, vbuf->bytesused, vbuf->timestamp);
415412

416-
#if DT_HAS_CHOSEN(zephyr_display)
417-
ret = app_display_frame(display_dev, vbuf, &fmt);
418-
if (ret != 0) {
419-
LOG_WRN("Failed to display this frame");
413+
if (DT_HAS_CHOSEN(zephyr_display)) {
414+
ret = app_display_frame(display_dev, vbuf, &fmt);
415+
if (ret != 0) {
416+
LOG_WRN("Failed to display this frame");
417+
}
420418
}
421-
#endif
422419

423420
ret = video_enqueue(video_dev, vbuf);
424421
if (ret < 0) {

0 commit comments

Comments
 (0)