-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameter.cpp
484 lines (425 loc) · 11.2 KB
/
parameter.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#include "parameter.h"
#define NEED_DESCRS
#include "parameters_list.h"
#include <algorithm>
#include <QLocale>
Parameter::Parameter(param_type_t parameter_type)
:ParameterBase()
{
param_type = parameter_type;
param_id = 0;
param_flags = PARAM_FLAG_NONE;
param_units = nullptr;
switch(parameter_type){
case PARAM_TYPE_INT:
param = new ParameterInt();
break;
case PARAM_TYPE_UINT:
param = new ParameterUInt();
break;
case PARAM_TYPE_FRACT_10:
param = new ParameterFract10();
break;
case PARAM_TYPE_FRACT_100:
param = new ParameterFract100();
break;
case PARAM_TYPE_FRACT_1000:
param = new ParameterFract1000();
break;
case PARAM_TYPE_FRACT_10000:
param = new ParameterFract10000();
break;
default:
param = nullptr;
}
}
Parameter::Parameter(param_type_t parameter_type, param_id_t parameter_id)
:Parameter(parameter_type)
{
param_id = parameter_id;
}
Parameter::Parameter(const param_descr_t *parameter_descr)
:Parameter(parameter_descr->type, parameter_descr->id)
{
setFlags(parameter_descr->flags);
setUnits(parameter_descr->units);
switch(parameter_descr->type){
case PARAM_TYPE_INT:
setRange(parameter_descr->min.int_value, parameter_descr->max.int_value);
setDefault(parameter_descr->def.int_value);
break;
case PARAM_TYPE_UINT:
setRange(parameter_descr->min.uint_value, parameter_descr->max.uint_value);
setDefault(parameter_descr->def.uint_value);
break;
case PARAM_TYPE_FRACT_10:
case PARAM_TYPE_FRACT_100:
case PARAM_TYPE_FRACT_1000:
case PARAM_TYPE_FRACT_10000:
setRange(fixed32_to_float(parameter_descr->min.fixed_value),
fixed32_to_float(parameter_descr->max.fixed_value));
setDefault(fixed32_to_float(parameter_descr->def.fixed_value));
break;
default:
break;
}
}
Parameter::~Parameter()
{
delete param;
}
param_type_t Parameter::type() const
{
return param_type;
}
param_id_t Parameter::id() const
{
return param_id;
}
void Parameter::setId(param_id_t parameter_id)
{
param_id = parameter_id;
}
param_flags_t Parameter::flags() const
{
return param_flags;
}
void Parameter::setFlags(param_flags_t parameter_flags)
{
param_flags = parameter_flags;
}
param_units_t Parameter::units() const
{
return param_units;
}
void Parameter::setUnits(param_units_t parameter_units)
{
param_units = parameter_units;
}
QVariant Parameter::toVariant() const
{
switch(param_type){
case PARAM_TYPE_INT:
return QVariant(toInt());
case PARAM_TYPE_UINT:
return QVariant(toUInt());
case PARAM_TYPE_FRACT_10:
case PARAM_TYPE_FRACT_100:
case PARAM_TYPE_FRACT_1000:
case PARAM_TYPE_FRACT_10000:
return QVariant(toFloat());
default:
break;
}
return QVariant();
}
QString Parameter::toString() const
{
switch(param_type){
case PARAM_TYPE_INT:
return QLocale().toString(toInt());
case PARAM_TYPE_UINT:
return QLocale().toString(toUInt());
case PARAM_TYPE_FRACT_10:
return QLocale().toString(toFloat(), 'f', 1);
case PARAM_TYPE_FRACT_100:
return QLocale().toString(toFloat(), 'f', 2);
case PARAM_TYPE_FRACT_1000:
return QLocale().toString(toFloat(), 'f', 3);
case PARAM_TYPE_FRACT_10000:
return QLocale().toString(toFloat(), 'f', 4);
default:
break;
}
return QString();
}
QString Parameter::minToString() const
{
switch(param_type){
case PARAM_TYPE_INT:
return QLocale().toString(minToInt());
case PARAM_TYPE_UINT:
return QLocale().toString(minToUInt());
case PARAM_TYPE_FRACT_10:
return QLocale().toString(minToFloat(), 'f', 1);
case PARAM_TYPE_FRACT_100:
return QLocale().toString(minToFloat(), 'f', 2);
case PARAM_TYPE_FRACT_1000:
return QLocale().toString(minToFloat(), 'f', 3);
case PARAM_TYPE_FRACT_10000:
return QLocale().toString(minToFloat(), 'f', 4);
default:
break;
}
return QString();
}
QString Parameter::maxToString() const
{
switch(param_type){
case PARAM_TYPE_INT:
return QLocale().toString(maxToInt());
case PARAM_TYPE_UINT:
return QLocale().toString(maxToUInt());
case PARAM_TYPE_FRACT_10:
return QLocale().toString(maxToFloat(), 'f', 1);
case PARAM_TYPE_FRACT_100:
return QLocale().toString(maxToFloat(), 'f', 2);
case PARAM_TYPE_FRACT_1000:
return QLocale().toString(maxToFloat(), 'f', 3);
case PARAM_TYPE_FRACT_10000:
return QLocale().toString(maxToFloat(), 'f', 4);
default:
break;
}
return QString();
}
QString Parameter::defToString() const
{
switch(param_type){
case PARAM_TYPE_INT:
return QLocale().toString(defToInt());
case PARAM_TYPE_UINT:
return QLocale().toString(defToUInt());
case PARAM_TYPE_FRACT_10:
return QLocale().toString(defToFloat(), 'f', 1);
case PARAM_TYPE_FRACT_100:
return QLocale().toString(defToFloat(), 'f', 2);
case PARAM_TYPE_FRACT_1000:
return QLocale().toString(defToFloat(), 'f', 3);
case PARAM_TYPE_FRACT_10000:
return QLocale().toString(defToFloat(), 'f', 4);
default:
break;
}
return QString();
}
int Parameter::toInt() const
{
if(param) return param->toInt();
return 0;
}
unsigned int Parameter::toUInt() const
{
if(param) return param->toUInt();
return 0;
}
float Parameter::toFloat() const
{
if(param) return param->toFloat();
return 0.0f;
}
uint16_t Parameter::toRaw() const
{
if(param) return param->toRaw();
return 0;
}
void Parameter::setVariant(const QVariant& val)
{
switch(param_type){
case PARAM_TYPE_INT:
setInt(val.toInt());
break;
case PARAM_TYPE_UINT:
setUInt(val.toUInt());
break;
case PARAM_TYPE_FRACT_10:
case PARAM_TYPE_FRACT_100:
case PARAM_TYPE_FRACT_1000:
case PARAM_TYPE_FRACT_10000:
setFloat(val.toFloat());
break;
default:
break;
}
}
void Parameter::setInt(int val)
{
if(param) param->setInt(val);
}
void Parameter::setUInt(unsigned int val)
{
if(param) param->setUInt(val);
}
void Parameter::setFloat(float val)
{
if(param) param->setFloat(val);
}
void Parameter::setRaw(uint16_t val)
{
if(param) param->setRaw(val);
}
void Parameter::setRange(int min_val, int max_val)
{
if(param) param->setRange(min_val, max_val);
}
void Parameter::setRange(unsigned int min_val, unsigned int max_val)
{
if(param) param->setRange(min_val, max_val);
}
void Parameter::setRange(float min_val, float max_val)
{
if(param) param->setRange(min_val, max_val);
}
void Parameter::setDefault(int def_val)
{
if(param) param->setDefault(def_val);
}
void Parameter::setDefault(unsigned int def_val)
{
if(param) param->setDefault(def_val);
}
void Parameter::setDefault(float def_val)
{
if(param) param->setDefault(def_val);
}
int Parameter::minToInt() const
{
if(param) return param->minToInt();
return 0;
}
unsigned int Parameter::minToUInt() const
{
if(param) return param->minToUInt();
return 0;
}
float Parameter::minToFloat() const
{
if(param) return param->minToFloat();
return 0;
}
int Parameter::maxToInt() const
{
if(param) return param->maxToInt();
return 0;
}
unsigned int Parameter::maxToUInt() const
{
if(param) return param->maxToUInt();
return 0;
}
float Parameter::maxToFloat() const
{
if(param) return param->maxToFloat();
return 0;
}
int Parameter::defToInt() const
{
if(param) return param->defToInt();
return 0;
}
unsigned int Parameter::defToUInt() const
{
if(param) return param->defToUInt();
return 0;
}
float Parameter::defToFloat() const
{
if(param) return param->defToFloat();
return 0;
}
bool Parameter::validate(int val) const
{
if(param) return param->validate(val);
return false;
}
bool Parameter::validate(unsigned int val) const
{
if(param) return param->validate(val);
return false;
}
bool Parameter::validate(float val) const
{
if(param) return param->validate(val);
return false;
}
void Parameter::applyDefault()
{
if(param) param->applyDefault();
}
const param_descr_t *Parameter::descrById(param_id_t parameter_id)
{
const param_descr_t* descrs_end = parameters_descrs + PARAMETERS_COUNT;
const param_descr_t* descr = std::lower_bound(parameters_descrs, descrs_end, parameter_id,
[](const param_descr_t& pd, param_id_t id) -> int{
if(pd.id < id) return 1;
//if(pd.id > id) return -1;
return 0;
});
if(descr == descrs_end) return nullptr;
if(descr->id != parameter_id) return nullptr;
return descr;
}
Parameter *Parameter::byId(param_id_t parameter_id)
{
const param_descr_t* descr = descrById(parameter_id);
if(descr == nullptr) return nullptr;
return new Parameter(descr);
}
QString Parameter::number(param_type_t param_type, int val)
{
switch(param_type){
case PARAM_TYPE_INT:
return QString::number(val);
case PARAM_TYPE_UINT:
return QString::number(static_cast<unsigned int>(val));
case PARAM_TYPE_FRACT_10:
return QString::number(static_cast<float>(val), 'f', 1);
case PARAM_TYPE_FRACT_100:
return QString::number(static_cast<float>(val), 'f', 2);
case PARAM_TYPE_FRACT_1000:
return QString::number(static_cast<float>(val), 'f', 3);
case PARAM_TYPE_FRACT_10000:
return QString::number(static_cast<float>(val), 'f', 4);
default:
break;
}
return QString();
}
QString Parameter::number(param_type_t param_type, unsigned int val)
{
switch(param_type){
case PARAM_TYPE_INT:
return QString::number(val);
case PARAM_TYPE_UINT:
return QString::number(val);
case PARAM_TYPE_FRACT_10:
return QString::number(static_cast<float>(val), 'f', 1);
case PARAM_TYPE_FRACT_100:
return QString::number(static_cast<float>(val), 'f', 2);
case PARAM_TYPE_FRACT_1000:
return QString::number(static_cast<float>(val), 'f', 3);
case PARAM_TYPE_FRACT_10000:
return QString::number(static_cast<float>(val), 'f', 4);
default:
break;
}
return QString();
}
QString Parameter::number(param_type_t param_type, float val)
{
switch(param_type){
case PARAM_TYPE_INT:
return QString::number(static_cast<int>(val));
case PARAM_TYPE_UINT:
return QString::number(static_cast<int>(val));
case PARAM_TYPE_FRACT_10:
return QString::number(val, 'f', 1);
case PARAM_TYPE_FRACT_100:
return QString::number(val, 'f', 2);
case PARAM_TYPE_FRACT_1000:
return QString::number(val, 'f', 3);
case PARAM_TYPE_FRACT_10000:
return QString::number(val, 'f', 4);
default:
break;
}
return QString();
}
Parameter::Parameter()
{
param = nullptr;
}
float Parameter::fixed32_to_float(fixed32_t f)
{
return static_cast<float>(f) / 0x10000;
}