blob: eabb80a2ea06907b7cf126f99ee90c842c3dd224 [file] [log] [blame]
Parker Schuh44f86922017-01-03 23:59:50 -08001#ifndef AOS_VISION_IMAGE_READER_H_
2#define AOS_VISION_IMAGE_READER_H_
3#include <errno.h>
4#include <fcntl.h>
5#include <malloc.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <sys/mman.h>
10#include <sys/stat.h>
11#include <sys/time.h>
12#include <sys/types.h>
13#include <unistd.h>
14
15#include <inttypes.h>
16#include <functional>
17#include <string>
18
19#include "aos/common/time.h"
20#include "aos/vision/image/V4L2.h"
21#include "aos/vision/image/image_types.h"
22
23namespace camera {
24
25struct CameraParams {
26 int32_t width;
27 int32_t height;
28 int32_t exposure;
29 int32_t brightness;
30 int32_t gain;
31 int32_t fps;
32};
33
34class Reader {
35 public:
36 using ProcessCb = std::function<void(
37 aos::vision::DataRef data, aos::monotonic_clock::time_point timestamp)>;
38 Reader(const std::string &dev_name, ProcessCb process, CameraParams params);
39
40 aos::vision::ImageFormat get_format();
41
42 void HandleFrame();
43 void StartAsync() {
44 Start();
45 MMapBuffers();
46 }
47 int fd() { return fd_; }
48
49 private:
50 void QueueBuffer(v4l2_buffer *buf);
51 void InitMMap();
52 bool SetCameraControl(uint32_t id, const char *name, int value);
53 void Init();
54 void Start();
55 void MMapBuffers();
56 // File descriptor of the camera
57 int fd_;
58 // Name of the camera device.
59 std::string dev_name_;
60
61 ProcessCb process_;
62
63 // The number of buffers currently queued in v4l2.
64 uint32_t queued_;
65 struct Buffer;
66 // TODO(parker): This should be a smart pointer, but it cannot
67 // because the buffers are not ummapped.
68 Buffer *buffers_;
69
70 static const unsigned int kNumBuffers = 10;
71
72 // set only at initialize
73 CameraParams params_;
74};
75
76} // namespace camera
77
78#endif // AOS_VISION_IMAGE_READER_H_