blob: 77f0799ef110b2666f5464db408e26fc6888c68b [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
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cinttypes>
Parker Schuh58b39e82019-02-22 22:32:46 -08005#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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080013namespace y2019::camera {
Parker Schuh58b39e82019-02-22 22:32:46 -080014
15aos::vision::CameraParams MakeCameraParams(int32_t width, int32_t height,
16 int32_t exposure, int32_t brightness,
17 int32_t gain, int32_t fps);
18
19class Reader {
20 public:
21 using ProcessCb = std::function<void(
22 aos::vision::DataRef data, aos::monotonic_clock::time_point timestamp)>;
23 Reader(const std::string &dev_name, ProcessCb process,
24 aos::vision::CameraParams params);
25
26 aos::vision::ImageFormat get_format();
27
28 void HandleFrame();
29 void StartAsync() {
30 MMapBuffers();
31 Start();
32 }
33 int fd() { return fd_; }
34
Alex Perry5b1e8e32019-04-07 13:25:31 -070035 bool SetCameraControl(uint32_t id, const char *name, int value);
36
Parker Schuh58b39e82019-02-22 22:32:46 -080037 private:
38 void QueueBuffer(v4l2_buffer *buf);
39 void InitMMap();
Parker Schuh58b39e82019-02-22 22:32:46 -080040 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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080067} // namespace y2019::camera
Parker Schuh58b39e82019-02-22 22:32:46 -080068
69#endif // AOS_VISION_IMAGE_READER_H_