-
Notifications
You must be signed in to change notification settings - Fork 5
/
Attenuator.cpp
379 lines (327 loc) · 9.27 KB
/
Attenuator.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/***************************************************************************
attenuator.cpp - description
----------------------------------------------------
begin : 2016
copyright : (C) 2016 by serge_m
email :
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "Attenuator.h"
#include <QtAlgorithms>
// Attenuator
void Attenuator::AddControlCode(int attenuation_db, int code)
{
_attenuations.append(attenuation_db);
_attenToControlCodeMap[attenuation_db] = code;
_controlCodeToAttenMap[code] = attenuation_db;
_attenToIndexMap[attenuation_db] = _attenuations.count() - 1;
}
void Attenuator::Clear()
{
_attenToControlCodeMap.clear();
_controlCodeToAttenMap.clear();
_attenToIndexMap.clear();
_attenuations.clear();
}
Attenuator::Attenuator()
{
AddControlCode(0, 0);
}
const QVector<int> Attenuator::GetAttenuations()
{
return _attenuations;
}
int Attenuator::GetControlCode(int attenuation_db)
{
if (_attenToControlCodeMap.contains(attenuation_db))
{
return _attenToControlCodeMap[attenuation_db];
}
return 0;
}
int Attenuator::GetControlCodeByIndex(int index)
{
if (index >= 0 && index < _attenuations.count())
{
return GetControlCode(_attenuations[index]);
}
return 0;
}
int Attenuator::GetIndex(int attenuation_db)
{
if (_attenToIndexMap.contains(attenuation_db))
{
return _attenToIndexMap[attenuation_db];
}
return 0;
}
int Attenuator::GetAttenuationByCode(int controlCode)
{
if (_controlCodeToAttenMap.contains(controlCode))
{
return _controlCodeToAttenMap[controlCode];
}
return 0;
}
bool Attenuator::CanAttenuateBy(int attenuation_db)
{
return _attenToControlCodeMap.contains(attenuation_db);
}
int Attenuator::GetMaxAttenuation()
{
return _attenuations.count() > 0 ? _attenuations.last() : 0;
}
// AttenuatorLoader
void AttenuatorLoader::Load(Attenuator &atten, AttenuatorType type, const QString &config)
{
switch (type)
{
case AttenuatorType_Fa:
LoadFa(atten);
break;
case AttenuatorType_Custom:
LoadCustom(atten, config);
break;
case AttenuatorType_Standard:
default:
// TODO: do something if arguments are invalid
LoadStandard(atten);
break;
}
}
void AttenuatorLoader::LoadStandard(Attenuator &atten)
{
atten.Clear();
atten.AddControlCode(0, 0x00);
atten.AddControlCode(10, 0x01);
atten.AddControlCode(20, 0x02);
atten.AddControlCode(30, 0x03);
atten.AddControlCode(40, 0x06);
atten.AddControlCode(50, 0x07);
}
void AttenuatorLoader::LoadFa(Attenuator &atten)
{
atten.Clear();
atten.AddControlCode(0, 0);
for (int i = 1; i < 33; ++i)
{
int attenuation = 0;
int controlCode = GetControlCodeByFaIndex(i, &attenuation);
atten.AddControlCode(attenuation, controlCode);
}
}
int AttenuatorLoader::GetControlCodeByFaIndex(int index, int *attenuation_db)
{
bool r1 = false, // -2dB
r2 = false, // -4dB
r3 = false, // -8db
r4 = false, // -4dB
r5 = false, // -16dB
r6 = false; // -32db
int db = (index+1) * 2;
*attenuation_db = db;
if(db == 32 or db >= 36)
{
r6 = true;
db = db - 32;
}
if(db == 16 or db >= 20)
{
r5 = true;
db = db - 16;
}
if(db != 0)
{
db = db - 4;
r4 = true;
if(db >= 8)
{
r3 = true;
db = db - 8;
}
if(db >= 4)
{
r2 = true;
db = db - 4;
}
if(db >= 2)
{
r1 = true;
db = db - 2;
}
}
int portb = 0;
if(r1)
{
portb += 1;
}
if(r2)
{
portb += 2;
}
if(r3)
{
portb += 4;
}
if(r4)
{
portb += 8;
}
if(r5)
{
portb += 16;
}
if(r6)
{
portb += 32;
}
return portb;
}
class CustomAttenCode
{
private:
int _code;
int _atten;
long _priority;
public:
CustomAttenCode()
: _code(0), _atten(0), _priority(0)
{
}
CustomAttenCode(const QVector<int> &stages, int code)
: _code(code), _atten(0), _priority(0)
{
// Go through the stages to calculate total attenuation
for (int stageNum = 0; stageNum < stages.count(); ++stageNum)
{
if ((code & (1 << stageNum)) != 0) // if stage is enabled by the attenuator control code
{
int stageAtten = stages[stageNum];
if (stageAtten <= 0)
{
// Stage is disabled, so the code is invalid
_atten = 0;
_priority = 0;
return;
}
_atten += stageAtten;
// Assume that stages with low attenuation have better linearity,
// so control codes which use low attenuation stages should have higher priority.
// Keep lower 8 bits for next prioritization parameter.
_priority -= ((stageAtten*stageAtten) << 8);
}
}
// Assume that stages with larger stageNum are physically located closer to the output,
// so they should have higher priority.
_priority += code;
}
int GetCode() const
{
return _code;
}
int GetAtten() const
{
return _atten;
}
long GetPriority() const
{
return _priority;
}
bool IsValid() const
{
return _atten > 0;
}
};
bool LessThanByAttenThenDescByPriority(const CustomAttenCode &c1, const CustomAttenCode &c2)
{
if (c1.GetAtten() != c2.GetAtten())
{
return c1.GetAtten() < c2.GetAtten();
}
return c1.GetPriority() > c2.GetPriority();
}
void AttenuatorLoader::LoadCustom(Attenuator &atten, const QString &config)
{
atten.Clear();
atten.AddControlCode(0, 0);
QVector<int> stages;
if (!TryParseConfig(config, stages))
{
// TODO: do something if arguments are invalid
return;
}
// Go through possible codes and selec those which are valid for the current configuration string
QVector<CustomAttenCode> codes;
int maxCode = (1 << stages.count()) - 1;
for (int code = 1; code <= maxCode; ++code)
{
CustomAttenCode codeInfo(stages, code);
if (codeInfo.IsValid())
{
codes.push_back(codeInfo);
}
}
// Order codes by attenuation and then by priority
qSort(codes.begin(), codes.end(), LessThanByAttenThenDescByPriority);
int previousAtten = 0;
for (int i = 0; i < codes.count(); ++i)
{
CustomAttenCode code = codes[i];
// Filter out redundant codes
if (code.GetAtten() != previousAtten)
{
atten.AddControlCode(code.GetAtten(), code.GetCode());
previousAtten = code.GetAtten();
}
}
}
bool AttenuatorLoader::IsCustomConfigValid(const QString &config)
{
QVector<int> dummy;
return TryParseConfig(config, dummy);
}
const int AttenuatorLoader::_maxCustomStages = 6;
const int AttenuatorLoader::_maxStageAtten = 500; // 500 db per stage should be more than enough for any hardware
const char AttenuatorLoader::_customStageConfigSeparator = ';';
bool AttenuatorLoader::TryParseConfig(const QString &config, QVector<int> &stages)
{
stages.clear();
QStringList splitted = config.split(QChar(_customStageConfigSeparator));
if (splitted.count() != _maxCustomStages)
{
return false;
}
QVector<int> tmpStages;
for (QStringList::const_iterator it = splitted.begin(); it != splitted.end(); ++it)
{
QString s =it->trimmed();
if (s.isEmpty())
{
// Attenuator stage is disabled
tmpStages.push_back(0);
}
else
{
bool isNumber = false;
int stageAtten = s.toInt(&isNumber);
if (!isNumber || stageAtten > _maxStageAtten)
{
return false;
}
tmpStages.push_back(stageAtten);
}
}
stages = tmpStages;
return true;
}
QString AttenuatorLoader::GetEmptyCustomConfig()
{
return QString(_maxCustomStages-1, _customStageConfigSeparator);
}