]> sigrok.org Git - pulseview.git/blame - pv/util.hpp
Fix #1549 by notifying user of wrong input parameter names
[pulseview.git] / pv / util.hpp
CommitLineData
f0c9f81c
JS
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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
f0c9f81c
JS
18 */
19
8845be3c
UH
20#ifndef PULSEVIEW_PV_UTIL_HPP
21#define PULSEVIEW_PV_UTIL_HPP
f0c9f81c 22
d9e71737 23#include <cmath>
34f4a40b
GS
24#include <string>
25#include <vector>
f0c9f81c 26
2b68d17c 27#ifndef Q_MOC_RUN
60d9b99a 28#include <boost/multiprecision/cpp_dec_float.hpp>
2b68d17c 29#endif
60d9b99a 30
806d3e1e 31#include <QMetaType>
f0c9f81c
JS
32#include <QString>
33
34f4a40b
GS
34using std::string;
35using std::vector;
36
f0c9f81c
JS
37namespace pv {
38namespace util {
39
d001f416 40enum class TimeUnit {
66279897 41 None = 0,
96b6316a
SA
42 Time = 1,
43 Samples = 2
44};
45
d001f416
JS
46enum 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.
58int exponent(SIPrefix prefix);
59
60d9b99a
JS
60/// Timestamp type providing yoctosecond resolution.
61typedef boost::multiprecision::number<
62 boost::multiprecision::cpp_dec_float<24>,
63 boost::multiprecision::et_off> Timestamp;
64
0efa7f0c
SA
65/**
66 * Chooses a prefix so that the value in front of the decimal point is between 1 and 999.
67 */
68SIPrefix determine_value_prefix(double v);
69
f0c9f81c 70/**
3ccf0f7f
JS
71 * Formats a given timestamp with the specified SI prefix.
72 *
73 * If 'prefix' is left 'unspecified', the function chooses a prefix so that
74 * the value in front of the decimal point is between 1 and 999.
75 *
76 * The default value "s" for the unit argument makes the most sense when
77 * formatting time values, but a different value can be given if the function
78 * is reused to format a value of another quantity.
79 *
80 * @param t The value to format.
81 * @param prefix The SI prefix to use.
4bc10a91 82 * @param precision The number of digits after the decimal separator.
3ccf0f7f 83 * @param unit The unit of quantity.
4bc10a91
JH
84 * @param sign Whether or not to add a sign also for positive numbers.
85 *
39ccf9c3 86 * @return The formatted value.
4bc10a91 87 */
c063290a
UH
88QString format_time_si(const Timestamp& v,
89 SIPrefix prefix = SIPrefix::unspecified, unsigned precision = 0,
90 QString unit = "s", bool sign = true);
4bc10a91 91
0cbadf1c
SA
92/**
93 * Formats a given value into a representation using SI units.
94 *
95 * If 'prefix' is left 'unspecified', the function chooses a prefix so that
96 * the value in front of the decimal point is between 1 and 999.
97 *
98 * @param value The value to format.
99 * @param prefix The SI prefix to use.
100 * @param precision The number of digits after the decimal separator.
101 * @param unit The unit of quantity.
102 * @param sign Whether or not to add a sign also for positive numbers.
103 *
104 * @return The formatted value.
105 */
106QString format_value_si(double v,
107 SIPrefix prefix = SIPrefix::unspecified, unsigned precision = 0,
108 QString unit = "", bool sign = true);
109
4bc10a91 110/**
39ccf9c3 111 * Wrapper around 'format_time_si()' that interprets the given 'precision'
3ccf0f7f 112 * value as the number of decimal places if the timestamp would be formatted
39ccf9c3 113 * without a SI prefix (using 'SIPrefix::none') and adjusts the precision to
3ccf0f7f
JS
114 * match the given 'prefix'
115 *
116 * @param t The value to format.
117 * @param prefix The SI prefix to use.
118 * @param precision The number of digits after the decimal separator if the
119 * 'prefix' would be 'SIPrefix::none', see above for more information.
4bc10a91 120 * @param unit The unit of quantity.
3ccf0f7f 121 * @param sign Whether or not to add a sign also for positive numbers.
f0c9f81c 122 *
39ccf9c3 123 * @return The formatted value.
f0c9f81c 124 */
c063290a
UH
125QString format_time_si_adjusted(const Timestamp& t, SIPrefix prefix,
126 unsigned precision = 0, QString unit = "s", bool sign = true);
f0c9f81c 127
62974f45 128/**
3ccf0f7f
JS
129 * Formats the given timestamp using "[+-]DD:HH:MM:SS.mmm uuu nnn ppp..." format.
130 *
131 * "DD" and "HH" are left out if they would be "00" (but if "DD" is generated,
132 * "HH" is also always generated. The "MM:SS" part is always produced, the
133 * number of subsecond digits can be influenced using the 'precision' parameter.
134 *
135 * @param t The value to format.
136 * @param precision The number of digits after the decimal separator.
137 * @param sign Whether or not to add a sign also for positive numbers.
62974f45 138 *
39ccf9c3 139 * @return The formatted value.
62974f45 140 */
c063290a 141QString format_time_minutes(const Timestamp& t, signed precision = 0,
3ccf0f7f 142 bool sign = true);
62974f45 143
34f4a40b
GS
144vector<string> split_string(string text, string separator);
145
f0c9f81c
JS
146} // namespace util
147} // namespace pv
148
806d3e1e
JS
149Q_DECLARE_METATYPE(pv::util::Timestamp)
150
8845be3c 151#endif // PULSEVIEW_PV_UTIL_HPP