Skip to content

Commit a3465f8

Browse files
josuahkartben
authored andcommitted
drivers: video: sw_generator: convert to use the devicetree
Make the video software generator a devicetree node, which allows enabling several instances, and select it as chosen { zephyr,camera = &... }; node. It can be enabled via a `video-sw-generator` snippet. Signed-off-by: Josuah Demangeon <me@josuah.net>
1 parent cc66467 commit a3465f8

File tree

16 files changed

+98
-58
lines changed

16 files changed

+98
-58
lines changed

drivers/video/Kconfig.sw_generator

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
# MT9m114
2-
31
# Copyright (c) 2016 Linaro Limited
42
# SPDX-License-Identifier: Apache-2.0
53

6-
DT_CHOSEN_ZEPHYR_CAMERA := zephyr,camera
7-
84
config VIDEO_SW_GENERATOR
95
bool "Video Software Generator"
10-
depends on !$(dt_chosen_enabled,$(DT_CHOSEN_ZEPHYR_CAMERA))
6+
depends on DT_HAS_ZEPHYR_VIDEO_SW_GENERATOR_ENABLED
7+
default y
118
help
129
Enable video pattern generator (for testing purposes).

drivers/video/video_sw_generator.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#define DT_DRV_COMPAT zephyr_sw_generator
7+
#define DT_DRV_COMPAT zephyr_video_sw_generator
88

99
#include <zephyr/drivers/video-controls.h>
1010
#include <zephyr/drivers/video.h>
@@ -451,14 +451,6 @@ static DEVICE_API(video, video_sw_generator_driver_api) = {
451451
#endif
452452
};
453453

454-
static struct video_sw_generator_data video_sw_generator_data_0 = {
455-
.fmt.width = 320,
456-
.fmt.height = 160,
457-
.fmt.pitch = 320 * 2,
458-
.fmt.pixelformat = VIDEO_PIX_FMT_RGB565,
459-
.frame_rate = DEFAULT_FRAME_RATE,
460-
};
461-
462454
static int video_sw_generator_init_controls(const struct device *dev)
463455
{
464456
struct video_sw_generator_data *data = dev->data;
@@ -479,8 +471,19 @@ static int video_sw_generator_init(const struct device *dev)
479471
return video_sw_generator_init_controls(dev);
480472
}
481473

482-
DEVICE_DEFINE(video_sw_generator, "VIDEO_SW_GENERATOR", &video_sw_generator_init, NULL,
483-
&video_sw_generator_data_0, NULL, POST_KERNEL, CONFIG_VIDEO_INIT_PRIORITY,
484-
&video_sw_generator_driver_api);
485-
486-
VIDEO_DEVICE_DEFINE(video_sw_generator, DEVICE_GET(video_sw_generator), NULL);
474+
#define VIDEO_SW_GENERATOR_DEFINE(n) \
475+
static struct video_sw_generator_data video_sw_generator_data_##n = { \
476+
.fmt.width = 320, \
477+
.fmt.height = 160, \
478+
.fmt.pitch = 320 * 2, \
479+
.fmt.pixelformat = VIDEO_PIX_FMT_RGB565, \
480+
.frame_rate = DEFAULT_FRAME_RATE, \
481+
}; \
482+
\
483+
DEVICE_DT_INST_DEFINE(n, &video_sw_generator_init, NULL, &video_sw_generator_data_##n, \
484+
NULL, POST_KERNEL, CONFIG_VIDEO_INIT_PRIORITY, \
485+
&video_sw_generator_driver_api); \
486+
\
487+
VIDEO_DEVICE_DEFINE(video_sw_generator_##n, DEVICE_DT_INST_GET(n), NULL);
488+
489+
DT_INST_FOREACH_STATUS_OKAY(VIDEO_SW_GENERATOR_DEFINE)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright 2025 tinyVision.ai Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: Emulated Video test pattern generator
5+
6+
compatible: "zephyr,video-sw-generator"

samples/drivers/video/capture/README.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,16 @@ For :ref:`native_sim`, build this sample application with the following commands
9191
:goals: build
9292
:compact:
9393

94-
For testing purpose without the need of any real video capture and/or display hardwares,
94+
For testing purpose and without the need of any real video capture and/or display hardwares,
9595
a video software pattern generator is supported by the above build commands without
96-
specifying the shields.
96+
specifying the shields, and using :ref:`snippet-video-sw-generator`:
97+
98+
.. zephyr-app-commands::
99+
:zephyr-app: samples/drivers/video/capture
100+
:board: native_sim/native/64
101+
:snippets: video-sw-generator
102+
:goals: build
103+
:compact:
97104

98105
For controlling the camera device using shell commands instead of continuously capturing the data,
99106
append ``-DCONFIG_VIDEO_SHELL=y`` to the build command:

samples/drivers/video/capture/prj.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
CONFIG_VIDEO=y
2-
CONFIG_VIDEO_SW_GENERATOR=y
32
CONFIG_SHELL=y
43
CONFIG_DEVICE_SHELL=y
54
CONFIG_PRINTK=y

samples/drivers/video/capture/sample.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ tests:
4545
- CONFIG_VIDEO_SHELL=y
4646
- CONFIG_FPU=y
4747
- CONFIG_DISPLAY=n
48+
extra_args:
49+
- platform:native_sim/native/64:SNIPPET="video-sw-generator"
4850
harness: console
4951
harness_config:
5052
type: one_line

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ LOG_MODULE_REGISTER(main);
2323
#define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL
2424
#endif
2525

26-
#define VIDEO_DEV_SW "VIDEO_SW_GENERATOR"
27-
2826
#if DT_HAS_CHOSEN(zephyr_display)
2927
static inline int display_setup(const struct device *const display_dev, const uint32_t pixfmt)
3028
{
@@ -92,6 +90,7 @@ int main(void)
9290
{
9391
struct video_buffer *buffers[CONFIG_VIDEO_BUFFER_POOL_NUM_MAX];
9492
struct video_buffer *vbuf = &(struct video_buffer){};
93+
const struct device *video_dev;
9594
struct video_format fmt;
9695
struct video_caps caps;
9796
struct video_frmival frmival;
@@ -108,21 +107,11 @@ int main(void)
108107
return 0;
109108
}
110109

111-
#if DT_HAS_CHOSEN(zephyr_camera)
112-
const struct device *const video_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera));
113-
110+
video_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera));
114111
if (!device_is_ready(video_dev)) {
115112
LOG_ERR("%s: video device is not ready", video_dev->name);
116113
return 0;
117114
}
118-
#else
119-
const struct device *const video_dev = device_get_binding(VIDEO_DEV_SW);
120-
121-
if (video_dev == NULL) {
122-
LOG_ERR("%s: video device not found or failed to initialized", VIDEO_DEV_SW);
123-
return 0;
124-
}
125-
#endif
126115

127116
LOG_INF("Video device: %s", video_dev->name);
128117

samples/drivers/video/capture_to_lvgl/README.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ For :zephyr:board:`mini_stm32h743`, build this sample application with the follo
3737
:gen-args: -DCONFIG_BOOT_DELAY=2000
3838
:compact:
3939

40+
For testing purpose and without the need of any real video capture hardware,
41+
a video software pattern generator is supported by using :ref:`snippet-video-sw-generator`:
42+
43+
.. zephyr-app-commands::
44+
:zephyr-app: samples/drivers/video/capture
45+
:board: native_sim/native/64
46+
:snippets: video-sw-generator
47+
:goals: build
48+
:compact:
49+
4050
Sample Output
4151
=============
4252

samples/drivers/video/capture_to_lvgl/prj.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
CONFIG_VIDEO=y
2-
CONFIG_VIDEO_SW_GENERATOR=y
32

43
CONFIG_PRINTK=y
54
CONFIG_LOG=y

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@
1515
#include <zephyr/logging/log.h>
1616
LOG_MODULE_REGISTER(main);
1717

18-
#define VIDEO_DEV_SW "VIDEO_SW_GENERATOR"
19-
2018
int main(void)
2119
{
2220
struct video_buffer *buffers[2];
2321
struct video_buffer *vbuf = &(struct video_buffer){};
2422
const struct device *display_dev;
23+
const struct device *video_dev;
2524
struct video_format fmt;
2625
struct video_caps caps;
2726
enum video_buf_type type = VIDEO_BUF_TYPE_OUTPUT;
28-
const struct device *video_dev;
2927
size_t bsize;
3028
int i = 0;
3129

@@ -35,19 +33,11 @@ int main(void)
3533
return 0;
3634
}
3735

38-
#if DT_HAS_CHOSEN(zephyr_camera)
3936
video_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera));
4037
if (!device_is_ready(video_dev)) {
4138
LOG_ERR("%s device is not ready", video_dev->name);
4239
return 0;
4340
}
44-
#else
45-
video_dev = device_get_binding(VIDEO_DEV_SW);
46-
if (video_dev == NULL) {
47-
LOG_ERR("%s device not found", VIDEO_DEV_SW);
48-
return 0;
49-
}
50-
#endif
5141

5242
LOG_INF("- Device name: %s", video_dev->name);
5343

0 commit comments

Comments
 (0)