在 ov5640 上使用 V4L2 设置 ctrl
Setting ctrl with V4L2 on ov5640
我想通过以下方式使用 V4L2 中的 ioctl
和 VIDIOC_S_CTRL
来控制各种 ov5640
相机参数:
#include <string>
#include <iostream>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <malloc.h>
#include <cstring>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#define IOCTL_TRIES 3
#define CLEAR(x) memset (&(x), 0, sizeof (x))
static int xioctl(int fd, int request, void *arg)
{
int r;
int tries = IOCTL_TRIES;
do {
r = ioctl(fd, request, arg);
} while (--tries > 0 && r == -1 && EINTR == errno);
return r;
}
bool v4l2_ctrl_set(int fd, uint32_t id, int val)
{
struct v4l2_control ctrl;
CLEAR(ctrl);
ctrl.id = id;
ctrl.value = val;
if (xioctl(fd, VIDIOC_S_CTRL, &ctrl) == -1) {
std::cout << "Failed to set ctrl with id "
<< id << " to value " << val
<< "\nerror (" << errno << "): " << strerror(errno) << std::endl;
return false;
}
return true;
}
int main()
{
int fd = open("/dev/video0", O_RDWR | O_NONBLOCK);
if (fd == -1) {
std::cout << "Failed to open the camera" << std::endl;
return -1;
}
v4l2_ctrl_set(fd, V4L2_CID_SATURATION, 100);
return 0;
}
不幸的是 ioctl
失败了,我得到了 error (25): Inappropriate ioctl for device
。我在 if (sensor->power_count == 0) {
之前使用 Intrinsyc Open-Q 820 µSOM with linaro 4.14. I've managed to add some debugs prints to ov5640 driver file in ov5640_s_ctrl function(以防省电模式出现问题)并重新编译内核。我 运行 代码,但是查看 dmesg
我的 printk
消息没有被打印出来,所以这意味着 ov5640_s_ctrl
即使设置了回调也没有被调用:
static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
.g_volatile_ctrl = ov5640_g_volatile_ctrl,
.s_ctrl = ov5640_s_ctrl,
};
我是不是用错了V4L2?我应该在设置控件之前启用某些功能吗?这更令人困惑,因为我设法使用 v4l2 从相机获取图像,但我无法 set/get 任何控件。
在您提供的 ov5640.c 的内核源代码中,驱动程序被分配了标志 V4L2_SUBDEV_FL_HAS_DEVNODE
,这意味着它可能提供一个子开发节点 /dev/v4l-subdevX
。根据内核文档:
Device nodes named v4l-subdevX can be created in /dev to access sub-devices directly. If a sub-device supports direct userspace configuration it must set the V4L2_SUBDEV_FL_HAS_DEVNODE flag before being registered.`
因此您可以尝试直接从 v4l-subdevX 节点设置控件(如果存在)。
我想通过以下方式使用 V4L2 中的 ioctl
和 VIDIOC_S_CTRL
来控制各种 ov5640
相机参数:
#include <string>
#include <iostream>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <malloc.h>
#include <cstring>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#define IOCTL_TRIES 3
#define CLEAR(x) memset (&(x), 0, sizeof (x))
static int xioctl(int fd, int request, void *arg)
{
int r;
int tries = IOCTL_TRIES;
do {
r = ioctl(fd, request, arg);
} while (--tries > 0 && r == -1 && EINTR == errno);
return r;
}
bool v4l2_ctrl_set(int fd, uint32_t id, int val)
{
struct v4l2_control ctrl;
CLEAR(ctrl);
ctrl.id = id;
ctrl.value = val;
if (xioctl(fd, VIDIOC_S_CTRL, &ctrl) == -1) {
std::cout << "Failed to set ctrl with id "
<< id << " to value " << val
<< "\nerror (" << errno << "): " << strerror(errno) << std::endl;
return false;
}
return true;
}
int main()
{
int fd = open("/dev/video0", O_RDWR | O_NONBLOCK);
if (fd == -1) {
std::cout << "Failed to open the camera" << std::endl;
return -1;
}
v4l2_ctrl_set(fd, V4L2_CID_SATURATION, 100);
return 0;
}
不幸的是 ioctl
失败了,我得到了 error (25): Inappropriate ioctl for device
。我在 if (sensor->power_count == 0) {
之前使用 Intrinsyc Open-Q 820 µSOM with linaro 4.14. I've managed to add some debugs prints to ov5640 driver file in ov5640_s_ctrl function(以防省电模式出现问题)并重新编译内核。我 运行 代码,但是查看 dmesg
我的 printk
消息没有被打印出来,所以这意味着 ov5640_s_ctrl
即使设置了回调也没有被调用:
static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
.g_volatile_ctrl = ov5640_g_volatile_ctrl,
.s_ctrl = ov5640_s_ctrl,
};
我是不是用错了V4L2?我应该在设置控件之前启用某些功能吗?这更令人困惑,因为我设法使用 v4l2 从相机获取图像,但我无法 set/get 任何控件。
在您提供的 ov5640.c 的内核源代码中,驱动程序被分配了标志 V4L2_SUBDEV_FL_HAS_DEVNODE
,这意味着它可能提供一个子开发节点 /dev/v4l-subdevX
。根据内核文档:
Device nodes named v4l-subdevX can be created in /dev to access sub-devices directly. If a sub-device supports direct userspace configuration it must set the V4L2_SUBDEV_FL_HAS_DEVNODE flag before being registered.`
因此您可以尝试直接从 v4l-subdevX 节点设置控件(如果存在)。