summaryrefslogtreecommitdiff
path: root/src/ipa/raspberrypi/cam_helper_ov9281.cpp
blob: 9de868c31dc09e338ed45c116cfb1c6ba36c0eec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (C) 2021, Raspberry Pi (Trading) Limited
 *
 * cam_helper_ov9281.cpp - camera information for ov9281 sensor
 */

#include <assert.h>

#include "cam_helper.hpp"

using namespace RPiController;

class CamHelperOv9281 : public CamHelper
{
public:
	CamHelperOv9281();
	uint32_t GainCode(double gain) const override;
	double Gain(uint32_t gain_code) const override;
	void GetDelays(int &exposure_delay, int &gain_delay,
		       int &vblank_delay) const override;

private:
	/*
	 * Smallest difference between the frame length and integration time,
	 * in units of lines.
	 */
	static constexpr int frameIntegrationDiff = 4;
};

/*
 * OV9281 doesn't output metadata, so we have to use the "unicam parser" which
 * works by counting frames.
 */

CamHelperOv9281::CamHelperOv9281()
	: CamHelper({}, frameIntegrationDiff)
{
}

uint32_t CamHelperOv9281::GainCode(double gain) const
{
	return static_cast<uint32_t>(gain * 16.0);
}

double CamHelperOv9281::Gain(uint32_t gain_code) const
{
	return static_cast<double>(gain_code) / 16.0;
}

void CamHelperOv9281::GetDelays(int &exposure_delay, int &gain_delay,
				int &vblank_delay) const
{
	/* The driver appears to behave as follows: */
	exposure_delay = 2;
	gain_delay = 2;
	vblank_delay = 2;
}

static CamHelper *Create()
{
	return new CamHelperOv9281();
}

static RegisterCamHelper reg("ov9281", &Create);
kwd">count_(0) { timeout.connect(this, &ManagedTimer::timeoutHandler); } void start(std::chrono::milliseconds msec) { count_ = 0; start_ = std::chrono::steady_clock::now(); expiration_ = std::chrono::steady_clock::time_point(); Timer::start(msec); } void start(std::chrono::steady_clock::time_point deadline) { count_ = 0; start_ = std::chrono::steady_clock::now(); expiration_ = std::chrono::steady_clock::time_point(); Timer::start(deadline); } int jitter() { std::chrono::steady_clock::duration duration = expiration_ - deadline(); return abs(std::chrono::duration_cast<std::chrono::milliseconds>(duration).count()); } bool hasFailed() { return isRunning() || count_ != 1 || jitter() > 50; } private: void timeoutHandler() { expiration_ = std::chrono::steady_clock::now(); count_++; } unsigned int count_; std::chrono::steady_clock::time_point start_; std::chrono::steady_clock::time_point expiration_; }; class TimerTest : public Test { protected: int init() { return 0; } int run() { EventDispatcher *dispatcher = Thread::current()->eventDispatcher(); ManagedTimer timer; ManagedTimer timer2; /* Timer expiration. */ timer.start(1000ms); if (!timer.isRunning()) { cout << "Timer expiration test failed" << endl; return TestFail; } dispatcher->processEvents(); if (timer.hasFailed()) { cout << "Timer expiration test failed" << endl; return TestFail; } /* * 32 bit wrap test * Nanosecond resolution in a 32 bit value wraps at 4.294967 * seconds (0xFFFFFFFF / 1000000) */ timer.start(4295ms); dispatcher->processEvents(); if (timer.hasFailed()) { cout << "Timer expiration test failed" << endl; return TestFail; } /* Timer restart. */ timer.start(500ms); if (!timer.isRunning()) { cout << "Timer restart test failed" << endl; return TestFail; } dispatcher->processEvents(); if (timer.hasFailed()) { cout << "Timer restart test failed" << endl; return TestFail; } /* Timer restart before expiration. */ timer.start(50ms); timer.start(100ms); timer.start(150ms); dispatcher->processEvents(); if (timer.hasFailed()) { cout << "Timer restart before expiration test failed" << endl; return TestFail; } /* Timer with absolute deadline. */ timer.start(std::chrono::steady_clock::now() + std::chrono::milliseconds(200)); dispatcher->processEvents(); if (timer.hasFailed()) { cout << "Absolute deadline test failed" << endl; return TestFail; } /* Two timers. */ timer.start(1000ms); timer2.start(300ms); dispatcher->processEvents(); if (!timer.isRunning()) { cout << "Two timers test failed" << endl; return TestFail; } if (timer2.jitter() > 50) { cout << "Two timers test failed" << endl; return TestFail; } dispatcher->processEvents(); if (timer.jitter() > 50) { cout << "Two timers test failed" << endl; return TestFail; } /* Restart timer before expiration. */ timer.start(1000ms); timer2.start(300ms); dispatcher->processEvents(); if (timer2.jitter() > 50) { cout << "Two timers test failed" << endl; return TestFail; } timer.start(1000ms); dispatcher->processEvents(); if (timer.jitter() > 50) { cout << "Two timers test failed" << endl; return TestFail; } /* * Test that dynamically allocated timers are stopped when * deleted. This will result in a crash on failure. */ ManagedTimer *dyntimer = new ManagedTimer(); dyntimer->start(100ms); delete dyntimer; timer.start(200ms); dispatcher->processEvents(); return TestPass; } void cleanup() { } }; TEST_REGISTER(TimerTest)