blob: 42832243d4f64e61ea06f08bedb0bf7317d7aeca [file] [log] [blame]
Parker Schuh44f86922017-01-03 23:59:50 -08001#ifndef AOS_VISION_IMAGE_READER_H_
2#define AOS_VISION_IMAGE_READER_H_
Parker Schuh44f86922017-01-03 23:59:50 -08003
4#include <inttypes.h>
5#include <functional>
6#include <string>
7
John Park33858a32018-09-28 23:05:48 -07008#include "aos/time/time.h"
Parker Schuh44f86922017-01-03 23:59:50 -08009#include "aos/vision/image/V4L2.h"
Parker Schuh24ee58d2017-03-11 16:13:23 -080010#include "aos/vision/image/camera_params.pb.h"
Parker Schuh44f86922017-01-03 23:59:50 -080011#include "aos/vision/image/image_types.h"
12
13namespace camera {
14
Parker Schuh24ee58d2017-03-11 16:13:23 -080015aos::vision::CameraParams MakeCameraParams(int32_t width, int32_t height,
16 int32_t exposure, int32_t brightness,
17 int32_t gain, int32_t fps);
Parker Schuh44f86922017-01-03 23:59:50 -080018
19class Reader {
20 public:
21 using ProcessCb = std::function<void(
22 aos::vision::DataRef data, aos::monotonic_clock::time_point timestamp)>;
Parker Schuh24ee58d2017-03-11 16:13:23 -080023 Reader(const std::string &dev_name, ProcessCb process,
24 aos::vision::CameraParams params);
Parker Schuh44f86922017-01-03 23:59:50 -080025
26 aos::vision::ImageFormat get_format();
27
28 void HandleFrame();
29 void StartAsync() {
Parker Schuh44f86922017-01-03 23:59:50 -080030 MMapBuffers();
Parker Schuh309dd722017-02-25 11:31:18 -080031 Start();
Parker Schuh44f86922017-01-03 23:59:50 -080032 }
33 int fd() { return fd_; }
34
Alex Perry5b1e8e32019-04-07 13:25:31 -070035 bool SetCameraControl(uint32_t id, const char *name, int value);
Tyler Chatowfdd7fbf2019-04-13 21:14:05 -070036 bool SetExposure(int abs_exp);
Alex Perry5b1e8e32019-04-07 13:25:31 -070037
Parker Schuh44f86922017-01-03 23:59:50 -080038 private:
39 void QueueBuffer(v4l2_buffer *buf);
40 void InitMMap();
Parker Schuh44f86922017-01-03 23:59:50 -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
Parker Schuh309dd722017-02-25 11:31:18 -080051 int tick_id_ = 0;
Parker Schuh44f86922017-01-03 23:59:50 -080052 // 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
Parker Schuh309dd722017-02-25 11:31:18 -080059 // 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;
Parker Schuh44f86922017-01-03 23:59:50 -080063
64 // set only at initialize
Parker Schuh24ee58d2017-03-11 16:13:23 -080065 aos::vision::CameraParams params_;
Parker Schuh44f86922017-01-03 23:59:50 -080066};
67
68} // namespace camera
69
70#endif // AOS_VISION_IMAGE_READER_H_