Auto Exposure:

  - Filter results down to the three best.
  - Changing exposure based on distance to the target or at random.
  - Plumb exposure through the debug framework.
  - Refactor into target finder.

Change-Id: Ia083f56859938bf0825472fd15d248c545f6f2fc
diff --git a/aos/vision/debug/aveugle-source.cc b/aos/vision/debug/aveugle-source.cc
index cbe3d0a..d03be14 100644
--- a/aos/vision/debug/aveugle-source.cc
+++ b/aos/vision/debug/aveugle-source.cc
@@ -46,6 +46,9 @@
           ++i_;
         }
       });
+      interface_->InstallSetExposure([this](int abs_exp) {
+          this->SetExposure(abs_exp);
+      });
     }
     void ProcessImage(DataRef data, aos::monotonic_clock::time_point) override {
       prev_data_ = std::string(data);
diff --git a/aos/vision/debug/debug_framework.cc b/aos/vision/debug/debug_framework.cc
index 1d94217..7b3ad76 100644
--- a/aos/vision/debug/debug_framework.cc
+++ b/aos/vision/debug/debug_framework.cc
@@ -60,6 +60,11 @@
     if (GetScreenHeight() < 1024) {
       view_.SetScale(1.0);
     }
+
+    // Pass along the set exposure so that users can acceess it.
+    filter->InstallSetExposure([this](uint32_t abs_exp) {
+        this->SetExposure(abs_exp);
+    });
   }
 
   // This the first stage in the pipeline that takes
diff --git a/aos/vision/debug/debug_framework.h b/aos/vision/debug/debug_framework.h
index a46812f..d8ed4a1 100644
--- a/aos/vision/debug/debug_framework.h
+++ b/aos/vision/debug/debug_framework.h
@@ -39,6 +39,16 @@
   virtual std::function<void(uint32_t)> RegisterKeyPress() {
     return std::function<void(uint32_t)>();
   }
+
+  // The DebugFramework will tells us where to call to get the camera.
+  void InstallSetExposure(std::function<void(int)> set_exp) {
+    set_exposure_ = set_exp;
+  }
+  void SetExposure(int abs_exp) {
+    set_exposure_(abs_exp);
+  }
+ private:
+  std::function<void(int)> set_exposure_;
 };
 
 // For ImageSource implementations only. Allows registering key press events
@@ -51,6 +61,12 @@
     key_press_events_.emplace_back(std::move(key_press_event));
   }
 
+  // The camera will tell us where to call to set exposure.
+  void InstallSetExposure(std::function<void(int)> set_exp) {
+    set_exposure_ = std::move(set_exp);
+  }
+  void SetExposure(int abs_exp) { set_exposure_(abs_exp); }
+
   // The return value bool here for all of these is
   // if the frame is "interesting" ie has a target.
   virtual bool NewJpeg(DataRef data) = 0;
@@ -76,6 +92,8 @@
 
  private:
   std::vector<std::function<void(uint32_t)>> key_press_events_;
+
+  std::function<void(int)> set_exposure_;
 };
 
 // Implemented by each source type. Will stream frames to
diff --git a/aos/vision/image/image_stream.h b/aos/vision/image/image_stream.h
index 57e06cb..8aab97d 100644
--- a/aos/vision/image/image_stream.h
+++ b/aos/vision/image/image_stream.h
@@ -37,6 +37,11 @@
 
   void ReadEvent() override { reader_->HandleFrame(); }
 
+  bool SetExposure(int abs_exp) {
+    return reader_->SetCameraControl(V4L2_CID_EXPOSURE_ABSOLUTE,
+                                     "V4L2_CID_EXPOSURE_ABSOLUTE", abs_exp);
+  }
+
  private:
   void ProcessHelper(DataRef data, aos::monotonic_clock::time_point timestamp);
 
diff --git a/aos/vision/image/reader.cc b/aos/vision/image/reader.cc
index aaf9a10..23b58c2 100644
--- a/aos/vision/image/reader.cc
+++ b/aos/vision/image/reader.cc
@@ -85,17 +85,6 @@
   }
   --queued_;
 
-  if (tick_id_ % 10 == 0) {
-    if (!SetCameraControl(V4L2_CID_EXPOSURE_AUTO, "V4L2_CID_EXPOSURE_AUTO",
-                          V4L2_EXPOSURE_MANUAL)) {
-      LOG(FATAL, "Failed to set exposure\n");
-    }
-
-    if (!SetCameraControl(V4L2_CID_EXPOSURE_ABSOLUTE,
-                          "V4L2_CID_EXPOSURE_ABSOLUTE", params_.exposure())) {
-      LOG(FATAL, "Failed to set exposure\n");
-    }
-  }
   ++tick_id_;
   // Get a timestamp now as proxy for when the image was taken
   // TODO(ben): the image should come with a timestamp, parker
@@ -171,8 +160,6 @@
   struct v4l2_control setArg = {id, value};
   r = xioctl(fd_, VIDIOC_S_CTRL, &setArg);
   if (r == 0) {
-    LOG(DEBUG, "Set camera control %s from %d to %d\n", name, getArg.value,
-        value);
     return true;
   }
 
diff --git a/aos/vision/image/reader.h b/aos/vision/image/reader.h
index adb3a3c..25fc0bd 100644
--- a/aos/vision/image/reader.h
+++ b/aos/vision/image/reader.h
@@ -32,10 +32,11 @@
   }
   int fd() { return fd_; }
 
+  bool SetCameraControl(uint32_t id, const char *name, int value);
+
  private:
   void QueueBuffer(v4l2_buffer *buf);
   void InitMMap();
-  bool SetCameraControl(uint32_t id, const char *name, int value);
   void Init();
   void Start();
   void MMapBuffers();