]> sigrok.org Git - pulseview.git/blame - pv/util.hpp
View: Change the type of tick_period to pv::util::Timestamp
[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
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
7a01bd36
JH
21#ifndef PULSEVIEW_UTIL_HPP
22#define PULSEVIEW_UTIL_HPP
f0c9f81c 23
d9e71737 24#include <cmath>
f0c9f81c 25
60d9b99a
JS
26#include <boost/multiprecision/cpp_dec_float.hpp>
27
806d3e1e 28#include <QMetaType>
f0c9f81c
JS
29#include <QString>
30
31namespace pv {
32namespace util {
33
d001f416 34enum class TimeUnit {
96b6316a
SA
35 Time = 1,
36 Samples = 2
37};
38
d001f416
JS
39enum class SIPrefix {
40 unspecified = -1,
41 yocto, zepto,
42 atto, femto, pico,
43 nano, micro, milli,
44 none,
45 kilo, mega, giga,
46 tera, peta, exa,
47 zetta, yotta
48};
49
50/// Returns the exponent that corresponds to a given prefix.
51int exponent(SIPrefix prefix);
52
60d9b99a
JS
53/// Timestamp type providing yoctosecond resolution.
54typedef boost::multiprecision::number<
55 boost::multiprecision::cpp_dec_float<24>,
56 boost::multiprecision::et_off> Timestamp;
57
f0c9f81c 58/**
4bc10a91
JH
59 * Formats a given value with the specified SI prefix.
60 * @param v The value to format.
61 * @param unit The unit of quantity.
d001f416 62 * @param prefix The prefix to use.
4bc10a91
JH
63 * @param precision The number of digits after the decimal separator.
64 * @param sign Whether or not to add a sign also for positive numbers.
65 *
66 * @return The formated value.
67 */
68QString format_si_value(
d001f416 69 const Timestamp& v, QString unit, SIPrefix prefix = SIPrefix::unspecified,
4bc10a91
JH
70 unsigned precision = 0, bool sign = true);
71
72/**
73 * Formats a given time with the specified SI prefix.
f0c9f81c 74 * @param t The time value in seconds to format.
d001f416 75 * @param prefix The prefix to use.
4bc10a91 76 * @param unit The unit of quantity.
ac981988 77 * @param precision The number of digits after the decimal separator or period (.).
f0c9f81c
JS
78 *
79 * @return The formated value.
80 */
81QString format_time(
d001f416
JS
82 const Timestamp& t, SIPrefix prefix = SIPrefix::unspecified,
83 TimeUnit unit = TimeUnit::Time, unsigned precision = 0);
f0c9f81c 84
62974f45
JS
85/**
86 * Formats a given time value with a SI prefix so that the
87 * value is between 1 and 999.
88 * @param second The time value in seconds to format.
89 *
90 * @return The formated value.
91 */
60d9b99a 92QString format_second(const Timestamp& second);
62974f45 93
f0c9f81c
JS
94} // namespace util
95} // namespace pv
96
806d3e1e
JS
97Q_DECLARE_METATYPE(pv::util::Timestamp)
98
7a01bd36 99#endif // PULSEVIEW_UTIL_HPP