-
Notifications
You must be signed in to change notification settings - Fork 29
/
TypeHelpers.hpp
144 lines (129 loc) · 4.7 KB
/
TypeHelpers.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) 2014-2017 Josh Blum
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <SoapySDR/Types.hpp>
#include <SoapySDR/Version.hpp> //feature constants
#include <uhd/types/device_addr.hpp>
#include <uhd/types/ranges.hpp>
#include <uhd/types/sensors.hpp>
#include <vector>
#define SOAPY_UHD_NO_DEEPER "soapy_uhd_no_deeper"
/***********************************************************************
* Helpful type conversions
**********************************************************************/
static inline SoapySDR::Kwargs dictToKwargs(const uhd::device_addr_t &addr)
{
SoapySDR::Kwargs kwargs;
const std::vector<std::string> keys = addr.keys();
for (size_t i = 0; i < keys.size(); i++)
{
kwargs[keys[i]] = addr[keys[i]];
}
return kwargs;
}
static inline uhd::device_addr_t kwargsToDict(const SoapySDR::Kwargs &kwargs)
{
uhd::device_addr_t addr;
for (SoapySDR::Kwargs::const_iterator it = kwargs.begin(); it != kwargs.end(); ++it)
{
addr[it->first] = it->second;
}
return addr;
}
static inline SoapySDR::RangeList metaRangeToRangeList(const uhd::meta_range_t &metaRange)
{
SoapySDR::RangeList out;
for (size_t i = 0; i < metaRange.size(); i++)
{
#ifdef SOAPY_SDR_API_HAS_RANGE_TYPE_STEP
out.push_back(SoapySDR::Range(metaRange[i].start(), metaRange[i].stop(), metaRange[i].step()));
#else
out.push_back(SoapySDR::Range(metaRange[i].start(), metaRange[i].stop()));
#endif
}
return out;
}
static inline uhd::meta_range_t rangeListToMetaRange(const SoapySDR::RangeList &ranges)
{
uhd::meta_range_t out;
for (size_t i = 0; i < ranges.size(); i++)
{
#ifdef SOAPY_SDR_API_HAS_RANGE_TYPE_STEP
out.push_back(uhd::range_t(ranges[i].minimum(), ranges[i].maximum(), ranges[i].step()));
#else
out.push_back(uhd::range_t(ranges[i].minimum(), ranges[i].maximum()));
#endif
}
if (out.empty()) out.push_back(uhd::range_t(0.0));
return out;
}
static inline SoapySDR::Range metaRangeToRange(const uhd::meta_range_t &metaRange)
{
#ifdef SOAPY_SDR_API_HAS_RANGE_TYPE_STEP
return SoapySDR::Range(metaRange.start(), metaRange.stop(), metaRange.step());
#else
return SoapySDR::Range(metaRange.start(), metaRange.stop());
#endif
}
static inline uhd::meta_range_t numberListToMetaRange(const std::vector<double> &nums)
{
uhd::meta_range_t out;
for (size_t i = 0; i < nums.size(); i++)
{
out.push_back(uhd::range_t(nums[i]));
}
if (out.empty()) out.push_back(uhd::range_t(0.0));
return out;
}
static inline std::vector<double> metaRangeToNumericList(const uhd::meta_range_t &metaRange)
{
std::vector<double> out;
//in this case, the bounds are in element 0
if (metaRange.size() == 1)
{
out.push_back(metaRange[0].start());
out.push_back(metaRange[0].stop());
return out;
}
for (size_t i = 0; i < metaRange.size(); i++)
{
//in these cases start == stop
out.push_back(metaRange[i].start());
}
return out;
}
static inline uhd::meta_range_t rangeToMetaRange(const SoapySDR::Range &range, double step = 0.0)
{
//when range step is supported, use it only if initialized to non-zero
#ifdef SOAPY_SDR_API_HAS_RANGE_TYPE_STEP
if (range.step() != 0.0) step = range.step();
#endif
return uhd::meta_range_t(range.minimum(), range.maximum(), step);
}
static inline SoapySDR::ArgInfo sensorToArgInfo(const uhd::sensor_value_t &sensor, const std::string &key)
{
SoapySDR::ArgInfo argInfo;
argInfo.key = key;
argInfo.value = sensor.value;
argInfo.name = sensor.name;
argInfo.units = sensor.unit;
switch (sensor.type)
{
case uhd::sensor_value_t::BOOLEAN: argInfo.type = SoapySDR::ArgInfo::BOOL; break;
case uhd::sensor_value_t::INTEGER: argInfo.type = SoapySDR::ArgInfo::INT; break;
case uhd::sensor_value_t::REALNUM: argInfo.type = SoapySDR::ArgInfo::FLOAT; break;
case uhd::sensor_value_t::STRING: argInfo.type = SoapySDR::ArgInfo::STRING; break;
}
return argInfo;
}
static inline uhd::sensor_value_t argInfoToSensor(const SoapySDR::ArgInfo &argInfo, const std::string &value)
{
switch (argInfo.type)
{
case SoapySDR::ArgInfo::BOOL: return uhd::sensor_value_t(argInfo.name, value == "true", argInfo.units, argInfo.units);
case SoapySDR::ArgInfo::INT: return uhd::sensor_value_t(argInfo.name, atoi(value.c_str()), argInfo.units);
case SoapySDR::ArgInfo::FLOAT: return uhd::sensor_value_t(argInfo.name, atof(value.c_str()), argInfo.units);
case SoapySDR::ArgInfo::STRING: return uhd::sensor_value_t(argInfo.name, value, argInfo.units);
}
return uhd::sensor_value_t(argInfo.name, value, argInfo.units);
}