[libcamera-devel,09/10] ipa: raspberrypi: Set a constrained range
diff mbox series

Message ID 20220613142853.98484-10-jeanmichel.hautbois@ideasonboard.com
State New
Headers show
Series
  • ipa: raspberrypi: Introduce an autofocus algorithm
Related show

Commit Message

Jean-Michel Hautbois June 13, 2022, 2:28 p.m. UTC
Give the algorithm a way to have a constrained range for the lens.
Add a default SetRange() call and implement it to set the low bound
and high bound for the search.

There is no functionnal change expected but gives the ability for the
pipeline handler to set the range at runtime.

Signed-off-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>
---
 .../raspberrypi/controller/af_algorithm.hpp   |  2 ++
 src/ipa/raspberrypi/controller/iob/af.cpp     | 24 ++++++++++++++-----
 src/ipa/raspberrypi/controller/iob/af.h       |  4 ++++
 3 files changed, 24 insertions(+), 6 deletions(-)

Patch
diff mbox series

diff --git a/src/ipa/raspberrypi/controller/af_algorithm.hpp b/src/ipa/raspberrypi/controller/af_algorithm.hpp
index 925b2d03..0008113b 100644
--- a/src/ipa/raspberrypi/controller/af_algorithm.hpp
+++ b/src/ipa/raspberrypi/controller/af_algorithm.hpp
@@ -28,6 +28,8 @@  public:
 	virtual void SetWindows(const libcamera::Rectangle &afWindows) = 0;
 	// set AF range
 	virtual void SetRange(const uint32_t &range) = 0;
+	// set Lens Range
+	virtual void SetLensRange(const uint32_t &low, const uint32_t &high) = 0;
 	// set AF speed
 	virtual void setSpeed(const uint32_t &speed) = 0;
 };
diff --git a/src/ipa/raspberrypi/controller/iob/af.cpp b/src/ipa/raspberrypi/controller/iob/af.cpp
index 1d9bc2bc..a10e1d0a 100644
--- a/src/ipa/raspberrypi/controller/iob/af.cpp
+++ b/src/ipa/raspberrypi/controller/iob/af.cpp
@@ -40,7 +40,7 @@  Af::Af(Controller *controller)
 	: AfAlgorithm(controller), focus_(0), bestFocus_(0),
 	  currentContrast_(0.0), previousContrast_(0.0), maxContrast_(0.0),
 	  maxStep_(0), coarseCompleted_(false), fineCompleted_(false),
-	  mode_(libcamera::controls::AfModeManual)
+	  mode_(libcamera::controls::AfModeManual), lowStep_(0), highStep_(kMaxFocusSteps)
 {
 }
 
@@ -90,6 +90,18 @@  void Af::SetRange([[maybe_unused]] const uint32_t &range)
 {
 }
 
+void Af::SetLensRange(const uint32_t &low, const uint32_t &high)
+{
+	lowStep_ = low;
+	highStep_ = high;
+
+	LOG(IoBAf, Debug) << "Lens range set between " << lowStep_
+			  << " and " << highStep_;
+
+	focus_ = lowStep_;
+	maxStep_ = highStep_;
+}
+
 void Af::setSpeed([[maybe_unused]] const uint32_t &speed)
 {
 }
@@ -118,7 +130,7 @@  void Af::afCoarseScan()
 		status_.lensPosition = focus_;
 		previousContrast_ = 0;
 		maxStep_ = std::clamp(focus_ + static_cast<uint32_t>((focus_ * kFineRange)),
-				      0U, kMaxFocusSteps);
+				      0U, highStep_);
 	}
 }
 
@@ -136,7 +148,7 @@  void Af::afFineScan()
 
 bool Af::afScan(uint32_t minSteps)
 {
-	if (focus_ > maxStep_) {
+	if (focus_ + minSteps > maxStep_) {
 		/* If the max step is reached, move lens to the position. */
 		status_.lensPosition = bestFocus_;
 		return true;
@@ -180,13 +192,13 @@  bool Af::afScan(uint32_t minSteps)
 void Af::afReset()
 {
 	LOG(IoBAf, Debug) << "Reset AF parameters";
-	status_.lensPosition = 0;
-	focus_ = 0;
+	status_.lensPosition = lowStep_;
+	focus_ = lowStep_;
+	maxStep_ = highStep_;
 	status_.state = libcamera::controls::AfStateScanning;
 	previousContrast_ = 0.0;
 	coarseCompleted_ = false;
 	fineCompleted_ = false;
-	maxStep_ = kMaxFocusSteps;
 	maxContrast_ = 0.0;
 }
 
diff --git a/src/ipa/raspberrypi/controller/iob/af.h b/src/ipa/raspberrypi/controller/iob/af.h
index 12d7bbbd..52b9c37d 100644
--- a/src/ipa/raspberrypi/controller/iob/af.h
+++ b/src/ipa/raspberrypi/controller/iob/af.h
@@ -29,6 +29,7 @@  public:
 	void Pause(const uint32_t &pause) override;
 	void SetWindows(const libcamera::Rectangle &afWindows) override;
 	void SetRange(const uint32_t &range) override;
+	void SetLensRange(const uint32_t &low, const uint32_t &high) override;
 	void setSpeed(const uint32_t &speed) override;
 private:
 	bool afNeedIgnoreFrame();
@@ -58,6 +59,9 @@  private:
 	bool fineCompleted_;
 
 	uint32_t mode_;
+
+	uint32_t lowStep_;
+	uint32_t highStep_;
 };
 
 } /* namespace RPiController */