libcamera: utils: Add helper class for std::chrono::duration

A new utils::Duration class is defined to represent a std::chrono::duration type
with double precision nanosecond timebase. Using a double minimises the loss of
precision when converting timebases. This helper class may be used by IPAs to
represent variables such as frame durations and exposure times.

An operator << overload is defined to help with displaying utils::Duration value
in stream objects. Currently, this will display the duration value in
microseconds.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
Reviewed-by: David Plowman <david.plowman@raspberrypi.com>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
This commit is contained in:
Naushir Patuck 2021-06-08 12:03:32 +01:00 committed by Laurent Pinchart
parent 75c1490827
commit 5055ca747c
3 changed files with 135 additions and 0 deletions

View file

@ -20,6 +20,7 @@
using namespace std;
using namespace libcamera;
using namespace std::literals::chrono_literals;
class UtilsTest : public Test
{
@ -128,6 +129,46 @@ protected:
return TestPass;
}
int testDuration()
{
std::ostringstream os;
utils::Duration exposure;
double ratio;
exposure = 25ms + 25ms;
if (exposure.get<std::micro>() != 50000.0) {
cerr << "utils::Duration failed to return microsecond count";
return TestFail;
}
exposure = 1.0s / 4;
if (exposure != 250ms) {
cerr << "utils::Duration failed scalar divide test";
return TestFail;
}
exposure = 5000.5us;
if (!exposure) {
cerr << "utils::Duration failed boolean test";
return TestFail;
}
os << exposure;
if (os.str() != "5000.50us") {
cerr << "utils::Duration operator << failed";
return TestFail;
}
exposure = 100ms;
ratio = exposure / 25ms;
if (ratio != 4.0) {
cerr << "utils::Duration failed ratio test";
return TestFail;
}
return TestPass;
}
int run()
{
/* utils::hex() test. */
@ -236,6 +277,10 @@ protected:
if (testEnumerate() != TestPass)
return TestFail;
/* utils::Duration test. */
if (testDuration() != TestPass)
return TestFail;
return TestPass;
}
};