]> sigrok.org Git - pulseview.git/blob - pv/util.hpp
Use a type with a greater resolution to represent time values
[pulseview.git] / pv / util.hpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #ifndef PULSEVIEW_UTIL_HPP
22 #define PULSEVIEW_UTIL_HPP
23
24 #include <cmath>
25
26 #include <boost/multiprecision/cpp_dec_float.hpp>
27
28 #include <QString>
29
30 namespace pv {
31 namespace util {
32
33 enum TimeUnit {
34         Time = 1,
35         Samples = 2
36 };
37
38 /// Timestamp type providing yoctosecond resolution.
39 typedef boost::multiprecision::number<
40         boost::multiprecision::cpp_dec_float<24>,
41         boost::multiprecision::et_off> Timestamp;
42
43 extern const int FirstSIPrefixPower;
44
45 /**
46  * Formats a given value with the specified SI prefix.
47  * @param v The value to format.
48  * @param unit The unit of quantity.
49  * @param prefix The number of the prefix, from 0 for 'yotta' up to
50  *   16 for 'yokto'. If prefix is set to -1, the prefix will be calculated.
51  * @param precision The number of digits after the decimal separator.
52  * @param sign Whether or not to add a sign also for positive numbers.
53  *
54  * @return The formated value.
55  */
56 QString format_si_value(
57         const Timestamp& v, QString unit, int prefix = -1,
58         unsigned precision = 0, bool sign = true);
59
60 /**
61  * Formats a given time with the specified SI prefix.
62  * @param t The time value in seconds to format.
63  * @param prefix The number of the prefix, from 0 for 'yotta' up to
64  *   16 for 'yokto'. If prefix is set to -1, the prefix will be calculated.
65  * @param unit The unit of quantity.
66  * @param precision The number of digits after the decimal separator or period (.).
67  *
68  * @return The formated value.
69  */
70 QString format_time(
71         const Timestamp& t, int prefix = -1,
72         TimeUnit unit = Time, unsigned precision = 0);
73
74 /**
75  * Formats a given time value with a SI prefix so that the
76  * value is between 1 and 999.
77  * @param second The time value in seconds to format.
78  *
79  * @return The formated value.
80  */
81 QString format_second(const Timestamp& second);
82
83 } // namespace util
84 } // namespace pv
85
86 #endif // PULSEVIEW_UTIL_HPP