Skip to content

Commit ea3fa88

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 e5ba362 commit ea3fa88

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)
@@ -340,6 +338,7 @@ static int app_setup_video_buffers(const struct device *const video_dev,
340338
int main(void)
341339
{
342340
const struct device *const video_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera));
341+
const struct device *const display_dev = DEVICE_DT_GET_OR_NULL(DT_CHOSEN(zephyr_display));
343342
struct video_buffer *vbuf = &(struct video_buffer){};
344343
struct video_format fmt = {
345344
.type = VIDEO_BUF_TYPE_OUTPUT,
@@ -381,14 +380,12 @@ int main(void)
381380
goto err;
382381
}
383382

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

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

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

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

0 commit comments

Comments
 (0)