blob: c5498fe9010f4070d19760fa306dc0035b518225 [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
Alex Perry5b1e8e32019-04-07 13:25:31 -070036 bool SetCameraControl(uint32_t id, const char *name, int value);
37
Parker Schuh58b39e82019-02-22 22:32:46 -080038 private:
39 void QueueBuffer(v4l2_buffer *buf);
40 void InitMMap();
Parker Schuh58b39e82019-02-22 22:32:46 -080041 void Init();
42 void Start();
43 void MMapBuffers();
44 // File descriptor of the camera
45 int fd_;
46 // Name of the camera device.
47 std::string dev_name_;
48
49 ProcessCb process_;
50
51 int tick_id_ = 0;
52 // The number of buffers currently queued in v4l2.
53 uint32_t queued_;
54 struct Buffer;
55 // TODO(parker): This should be a smart pointer, but it cannot
56 // because the buffers are not ummapped.
57 Buffer *buffers_;
58
59 // TODO(parker): The timestamps should be queue insertion timestamps
60 // which will remove the impact of kNumBuffers.
61 // TODO(parker): Flush the queue (or tweak the FPS) if we fall behind.
62 static const unsigned int kNumBuffers = 5;
63
64 // set only at initialize
65 aos::vision::CameraParams params_;
66};
67
68} // namespace camera
69} // namespace y2019
70
71#endif // AOS_VISION_IMAGE_READER_H_