blob: e913ad1aa2af3abc7f41942cba7014fc7e0b6d52 [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
8#include "aos/common/time.h"
9#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
35 private:
36 void QueueBuffer(v4l2_buffer *buf);
37 void InitMMap();
38 bool SetCameraControl(uint32_t id, const char *name, int value);
39 void Init();
40 void Start();
41 void MMapBuffers();
42 // File descriptor of the camera
43 int fd_;
44 // Name of the camera device.
45 std::string dev_name_;
46
47 ProcessCb process_;
48
Parker Schuh309dd722017-02-25 11:31:18 -080049 int tick_id_ = 0;
Parker Schuh44f86922017-01-03 23:59:50 -080050 // The number of buffers currently queued in v4l2.
51 uint32_t queued_;
52 struct Buffer;
53 // TODO(parker): This should be a smart pointer, but it cannot
54 // because the buffers are not ummapped.
55 Buffer *buffers_;
56
Parker Schuh309dd722017-02-25 11:31:18 -080057 // TODO(parker): The timestamps should be queue insertion timestamps
58 // which will remove the impact of kNumBuffers.
59 // TODO(parker): Flush the queue (or tweak the FPS) if we fall behind.
60 static const unsigned int kNumBuffers = 5;
Parker Schuh44f86922017-01-03 23:59:50 -080061
62 // set only at initialize
Parker Schuh24ee58d2017-03-11 16:13:23 -080063 aos::vision::CameraParams params_;
Parker Schuh44f86922017-01-03 23:59:50 -080064};
65
66} // namespace camera
67
68#endif // AOS_VISION_IMAGE_READER_H_