AP_HAL_Linux: VideoIn: follow coding style and minor fixes

- No need to if/else if just returning
 - Sort includes
 - Fix missing space in log message
 - When closing the fd, set it to -1. It's better to later fail the
   operation than to operate on another random file descriptor
 - Add some spaces to improve readability
 - Use pragma once
 - Do not initialize members to zero, it's already the behavior for our
   custom allocator
This commit is contained in:
Lucas De Marchi 2015-12-14 12:18:43 -02:00 committed by Andrew Tridgell
parent 25df4d03ff
commit c75704bde3
2 changed files with 29 additions and 34 deletions

View File

@ -21,22 +21,21 @@
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BEBOP
#include "VideoIn.h"
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sched.h>
#include <time.h>
#include <fcntl.h>
#include <linux/videodev2.h>
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <linux/videodev2.h>
#include <time.h>
#include <unistd.h>
extern const AP_HAL::HAL& hal;
@ -52,11 +51,7 @@ bool VideoIn::get_frame(Frame &frame)
_streaming = true;
}
if (_dequeue_frame(frame)) {
return true;
} else {
return false;
}
return _dequeue_frame(frame);
}
void VideoIn::put_frame(Frame &frame)
@ -88,9 +83,10 @@ bool VideoIn::open_device(const char *device_path, uint32_t memtype)
}
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
hal.console->printf("Error opening device %s: is not a video capture"
"device\n", device_path);
hal.console->printf("Error opening device %s: is not a video capture device\n",
device_path);
close(_fd);
_fd = -1;
return false;
}
@ -112,8 +108,7 @@ bool VideoIn::allocate_buffers(uint32_t nbufs)
ret = ioctl(_fd, VIDIOC_REQBUFS, &rb);
if (ret < 0) {
printf("Unable to request buffers: %s (%d).\n", strerror(errno),
errno);
printf("Unable to request buffers: %s (%d).\n", strerror(errno), errno);
return ret;
}
@ -138,7 +133,7 @@ bool VideoIn::allocate_buffers(uint32_t nbufs)
switch (_memtype) {
case V4L2_MEMORY_MMAP:
buffers[i].mem = mmap(0, buf.length, PROT_READ | PROT_WRITE,
MAP_SHARED, _fd, buf.m.offset);
MAP_SHARED, _fd, buf.m.offset);
if (buffers[i].mem == MAP_FAILED) {
hal.console->printf("Unable to map buffer %u: %s (%d)\n", i,
strerror(errno), errno);
@ -249,6 +244,7 @@ void VideoIn::_queue_buffer(int index)
{
int ret;
struct v4l2_buffer buf;
memset(&buf, 0, sizeof buf);
buf.index = index;
buf.type = V4L2_CAP_VIDEO_CAPTURE;
@ -257,6 +253,7 @@ void VideoIn::_queue_buffer(int index)
if (_memtype == V4L2_MEMORY_USERPTR) {
buf.m.userptr = (unsigned long) _buffers[index].mem;
}
ret = ioctl(_fd, VIDIOC_QBUF, &buf);
if (ret < 0) {
hal.console->printf("Unable to queue buffer : %s (%d).\n",

View File

@ -12,8 +12,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __VIDEOIN_H__
#define __VIDEOIN_H__
#pragma once
#include "AP_HAL_Linux.h"
#include <linux/videodev2.h>
@ -31,9 +30,9 @@ public:
class Frame {
friend class VideoIn;
public:
uint32_t timestamp = 0;
uint32_t sequence = 0;
void *data = NULL;
uint32_t timestamp;
uint32_t sequence;
void *data;
private:
uint32_t buf_index;
};
@ -56,14 +55,13 @@ private:
bool _dequeue_frame(Frame &frame);
uint32_t _timeval_to_us(struct timeval& tv);
int _fd = -1;
struct buffer *_buffers = NULL;
unsigned int _nbufs = 0;
bool _streaming = false;
uint32_t _width = 0;
uint32_t _height = 0;
uint32_t _format = 0;
uint32_t _bytesperline = 0;
uint32_t _sizeimage = 0;
struct buffer *_buffers;
unsigned int _nbufs;
bool _streaming;
uint32_t _width;
uint32_t _height;
uint32_t _format;
uint32_t _bytesperline;
uint32_t _sizeimage;
uint32_t _memtype = V4L2_MEMORY_MMAP;
};
#endif