APM:Libraries
CameraSensor.cpp
Go to the documentation of this file.
1 /*
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License as published by
4  the Free Software Foundation, either version 3 of the License, or
5  (at your option) any later version.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 #include "CameraSensor.h"
16 
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <linux/v4l2-subdev.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/ioctl.h>
24 #include <unistd.h>
25 
26 using namespace Linux;
27 
28 bool CameraSensor::set_format(uint32_t width, uint32_t height, uint32_t format)
29 {
30  struct v4l2_subdev_format fmt;
31  int ret, fd;
32 
33  fd = open(_device_path, O_RDWR | O_CLOEXEC);
34  if (fd < 0) {
35  return false;
36  }
37 
38  memset(&fmt, 0, sizeof(fmt));
39  fmt.pad = 0;
40  fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
41  fmt.format.width = width;
42  fmt.format.height = height;
43  fmt.format.code = format;
44 
45  ret = ioctl(fd, VIDIOC_SUBDEV_S_FMT, &fmt);
46  if (ret < 0) {
47  return false;
48  }
49 
50  return true;
51 }
int open(const char *pathname, int flags)
POSIX Open a file with integer mode flags.
Definition: posix.c:885
bool set_format(uint32_t width, uint32_t height, uint32_t format)
const char * _device_path
Definition: CameraSensor.h:29