Allow to configure options

This commit is contained in:
Kamil Trzcinski 2022-04-07 23:01:58 +02:00
parent 31a1f91e72
commit 386ad37b40
6 changed files with 136 additions and 21 deletions

View File

@ -60,7 +60,6 @@ camera_t *camera_open(camera_options_t *options)
if (camera->options.fps > 0) { if (camera->options.fps > 0) {
camera->camera->capture_list->fmt_interval_us = 1000 * 1000 / camera->options.fps; camera->camera->capture_list->fmt_interval_us = 1000 * 1000 / camera->options.fps;
printf("buffer_lock.c: frame interval: %d\n", camera->camera->capture_list->fmt_interval_us);
} }
switch (camera->camera->capture_list->fmt_format) { switch (camera->camera->capture_list->fmt_format) {
@ -108,6 +107,7 @@ error:
int camera_set_params(camera_t *camera) int camera_set_params(camera_t *camera)
{ {
device_set_fps(camera->camera, camera->options.fps); device_set_fps(camera->camera, camera->options.fps);
device_set_option_list(camera->camera, camera->options.options);
// DEVICE_SET_OPTION(camera->camera, EXPOSURE, 2684); // DEVICE_SET_OPTION(camera->camera, EXPOSURE, 2684);
// DEVICE_SET_OPTION(camera->camera, ANALOGUE_GAIN, 938); // DEVICE_SET_OPTION(camera->camera, ANALOGUE_GAIN, 938);

View File

@ -246,20 +246,3 @@ int device_set_fps(device_t *dev, int desired_fps)
error: error:
return -1; return -1;
} }
int device_set_option(device_t *dev, const char *name, uint32_t id, int32_t value)
{
struct v4l2_control ctl = {0};
if (!dev) {
return -1;
}
ctl.id = id;
ctl.value = value;
E_LOG_DEBUG(dev, "Configuring option %s (%08x) = %d", name, id, value);
E_XIOCTL(dev, dev->subdev_fd >= 0 ? dev->subdev_fd : dev->fd, VIDIOC_S_CTRL, &ctl, "Can't set option %s", name);
return 0;
error:
return -1;
}

View File

@ -30,9 +30,12 @@ int device_consume_event(device_t *device);
int device_set_stream(device_t *dev, bool do_on); int device_set_stream(device_t *dev, bool do_on);
int device_set_decoder_start(device_t *dev, bool do_on); int device_set_decoder_start(device_t *dev, bool do_on);
int device_video_force_key(device_t *dev); int device_video_force_key(device_t *dev);
int device_set_fps(device_t *dev, int desired_fps);
int device_set_pad_format(device_t *device, unsigned width, unsigned height, unsigned format); int device_set_pad_format(device_t *device, unsigned width, unsigned height, unsigned format);
int device_set_option(device_t *dev, const char *name, uint32_t id, int32_t value); int device_set_option(device_t *dev, const char *name, uint32_t id, int32_t value);
int device_set_fps(device_t *dev, int desired_fps); int device_set_option_string(device_t *dev, const char *option);
void device_set_option_list(device_t *dev, const char *option_list);
#define DEVICE_SET_OPTION(dev, name, value) \ #define DEVICE_SET_OPTION(dev, name, value) \
device_set_option(dev, #name, V4L2_CID_##name, value) device_set_option(dev, #name, V4L2_CID_##name, value)

129
hw/device_options.c Normal file
View File

@ -0,0 +1,129 @@
#include "hw/device.h"
#include <ctype.h>
int device_set_option(device_t *dev, const char *name, uint32_t id, int32_t value)
{
struct v4l2_control ctl = {0};
if (!dev) {
return -1;
}
ctl.id = id;
ctl.value = value;
E_LOG_DEBUG(dev, "Configuring option %s (%08x) = %d", name, id, value);
E_XIOCTL(dev, dev->subdev_fd >= 0 ? dev->subdev_fd : dev->fd, VIDIOC_S_CTRL, &ctl, "Can't set option %s", name);
return 0;
error:
return -1;
}
void device_option_normalize_name(char *in)
{
char *out = in;
while (*in) {
if (isalnum(*in)) {
*out++ = tolower(*in++);
} else if (isprint(*in)) {
*out++ = '_';
while (*++in && isprint(*in) && !isalnum(*in));
} else {
in++;
}
}
*out++ = 0;
}
int device_set_option_string_fd(device_t *dev, int fd, const char *name, const char *value)
{
struct v4l2_query_ext_ctrl qctrl = {0};
struct v4l2_control ctl = {0};
qctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
while (0 == ioctl (fd, VIDIOC_QUERY_EXT_CTRL, &qctrl)) {
ctl.id = qctrl.id;
qctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
device_option_normalize_name(qctrl.name);
if (strcmp(qctrl.name, name) != 0)
continue;
if (qctrl.flags & V4L2_CTRL_FLAG_DISABLED) {
E_LOG_INFO(dev, "The '%s' is disabled", name);
continue;
} else if (qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY) {
E_LOG_INFO(dev, "The '%s' is read-only", name);
continue;
}
switch(qctrl.type) {
case V4L2_CTRL_TYPE_INTEGER:
case V4L2_CTRL_TYPE_BOOLEAN:
case V4L2_CTRL_TYPE_MENU:
ctl.value = atoi(value);
E_LOG_VERBOSE(dev, "Configuring option %s (%08x) = %d", name, ctl.id, ctl.value);
E_XIOCTL(dev, fd, VIDIOC_S_CTRL, &ctl, "Can't set option %s", name);
return 1;
default:
E_LOG_INFO(dev, "The '%s' control type '%d' is not supported", name, qctrl.type);
return 0;
}
}
return 0;
error:
return -1;
}
int device_set_option_string(device_t *dev, const char *option)
{
char name[256];
strcpy(name, option);
char *value = strchr(name, '=');
if (!value) {
E_LOG_ERROR(dev, "Missing 'key=value': '%s'", option);
}
*value++ = 0;
device_option_normalize_name(name);
int ret = device_set_option_string_fd(dev, dev->subdev_fd, name, value);
if (ret > 0)
return 0;
ret = device_set_option_string_fd(dev, dev->fd, name, value);
if (ret == 0)
E_LOG_ERROR(dev, "The '%s' was failed to find.", option);
else if (ret < 0)
E_LOG_ERROR(dev, "The '%s' did fail to be set.", option);
return 0;
error:
return -1;
}
void device_set_option_list(device_t *dev, const char *option_list)
{
if (!dev || !option_list || !option_list[0]) {
return;
}
char *start = strdup(option_list);
char *string = start;
char *option;
while (option = strsep(&string, ",")) {
device_set_option_string(dev, option);
}
free(start);
}

View File

@ -8,8 +8,6 @@
static int option_handler_print(option_t *option, char *data); static int option_handler_print(option_t *option, char *data);
static int option_handler_set(option_t *option, char *data); static int option_handler_set(option_t *option, char *data);
#define OPTION_VALUE_LIST_SEP ","
static void print_help(option_t *options) static void print_help(option_t *options)
{ {
for (int i = 0; options[i].name; i++) { for (int i = 0; options[i].name; i++) {

View File

@ -31,6 +31,8 @@ typedef struct options_s {
__fn__; \ __fn__; \
}) })
#define OPTION_VALUE_LIST_SEP ","
#define OPTION_FORMAT_uint "%d" #define OPTION_FORMAT_uint "%d"
#define OPTION_FORMAT_hex "%08x" #define OPTION_FORMAT_hex "%08x"
#define OPTION_FORMAT_bool "%d" #define OPTION_FORMAT_bool "%d"