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