]> sigrok.org Git - pulseview.git/blob - pv/util.hpp
Trace View: Move ruler time conversion from View to Ruler
[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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef PULSEVIEW_UTIL_HPP
21 #define PULSEVIEW_UTIL_HPP
22
23 #include <cmath>
24 #include <string>
25 #include <vector>
26
27 #ifndef Q_MOC_RUN
28 #include <boost/multiprecision/cpp_dec_float.hpp>
29 #endif
30
31 #include <QMetaType>
32 #include <QString>
33
34 using std::string;
35 using std::vector;
36
37 namespace pv {
38 namespace util {
39
40 enum class TimeUnit {
41         None = 0,
42         Time = 1,
43         Samples = 2
44 };
45
46 enum class SIPrefix {
47         unspecified = -1,
48         yocto, zepto,
49         atto, femto, pico,
50         nano, micro, milli,
51         none,
52         kilo, mega, giga,
53         tera, peta, exa,
54         zetta, yotta
55 };
56
57 /// Returns the exponent that corresponds to a given prefix.
58 int exponent(SIPrefix prefix);
59
60 /// Timestamp type providing yoctosecond resolution.
61 typedef boost::multiprecision::number<
62         boost::multiprecision::cpp_dec_float<24>,
63         boost::multiprecision::et_off> Timestamp;
64
65 /**
66  * Formats a given timestamp with the specified SI prefix.
67  *
68  * If 'prefix' is left 'unspecified', the function chooses a prefix so that
69  * the value in front of the decimal point is between 1 and 999.
70  *
71  * The default value "s" for the unit argument makes the most sense when
72  * formatting time values, but a different value can be given if the function
73  * is reused to format a value of another quantity.
74  *
75  * @param t The value to format.
76  * @param prefix The SI prefix to use.
77  * @param precision The number of digits after the decimal separator.
78  * @param unit The unit of quantity.
79  * @param sign Whether or not to add a sign also for positive numbers.
80  *
81  * @return The formatted value.
82  */
83 QString format_time_si(const Timestamp& v,
84         SIPrefix prefix = SIPrefix::unspecified, unsigned precision = 0,
85         QString unit = "s", bool sign = true);
86
87 /**
88  * Formats a given value into a representation using SI units.
89  *
90  * If 'prefix' is left 'unspecified', the function chooses a prefix so that
91  * the value in front of the decimal point is between 1 and 999.
92  *
93  * @param value The value to format.
94  * @param prefix The SI prefix to use.
95  * @param precision The number of digits after the decimal separator.
96  * @param unit The unit of quantity.
97  * @param sign Whether or not to add a sign also for positive numbers.
98  *
99  * @return The formatted value.
100  */
101 QString format_value_si(double v,
102         SIPrefix prefix = SIPrefix::unspecified, unsigned precision = 0,
103         QString unit = "", bool sign = true);
104
105 /**
106  * Wrapper around 'format_time_si()' that interprets the given 'precision'
107  * value as the number of decimal places if the timestamp would be formatted
108  * without a SI prefix (using 'SIPrefix::none') and adjusts the precision to
109  * match the given 'prefix'
110  *
111  * @param t The value to format.
112  * @param prefix The SI prefix to use.
113  * @param precision The number of digits after the decimal separator if the
114  *        'prefix' would be 'SIPrefix::none', see above for more information.
115  * @param unit The unit of quantity.
116  * @param sign Whether or not to add a sign also for positive numbers.
117  *
118  * @return The formatted value.
119  */
120 QString format_time_si_adjusted(const Timestamp& t, SIPrefix prefix,
121         unsigned precision = 0, QString unit = "s", bool sign = true);
122
123 /**
124  * Formats the given timestamp using "[+-]DD:HH:MM:SS.mmm uuu nnn ppp..." format.
125  *
126  * "DD" and "HH" are left out if they would be "00" (but if "DD" is generated,
127  * "HH" is also always generated. The "MM:SS" part is always produced, the
128  * number of subsecond digits can be influenced using the 'precision' parameter.
129  *
130  * @param t The value to format.
131  * @param precision The number of digits after the decimal separator.
132  * @param sign Whether or not to add a sign also for positive numbers.
133  *
134  * @return The formatted value.
135  */
136 QString format_time_minutes(const Timestamp& t, signed precision = 0,
137         bool sign = true);
138
139 vector<string> split_string(string text, string separator);
140
141 } // namespace util
142 } // namespace pv
143
144 Q_DECLARE_METATYPE(pv::util::Timestamp)
145
146 #endif // PULSEVIEW_UTIL_HPP