Improve multi (#7136)
[opentx.git] / radio / src / gvars.cpp
blob4a56d8b705dee202677259d338e9a8220d41458d
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "opentx.h"
23 uint8_t gvarDisplayTimer = 0;
24 uint8_t gvarLastChanged = 0;
26 uint8_t getGVarFlightMode(uint8_t fm, uint8_t gv) // TODO change params order to be consistent!
28 for (uint8_t i=0; i<MAX_FLIGHT_MODES; i++) {
29 if (fm == 0) return 0;
30 int16_t val = GVAR_VALUE(gv, fm);
31 if (val <= GVAR_MAX) return fm;
32 uint8_t result = val-GVAR_MAX-1;
33 if (result >= fm) result++;
34 fm = result;
36 return 0;
39 int16_t getGVarValue(int8_t gv, int8_t fm)
41 int8_t mul = 1;
42 if (gv < 0) {
43 gv = -1-gv;
44 mul = -1;
46 return GVAR_VALUE(gv, getGVarFlightMode(fm, gv)) * mul;
49 int32_t getGVarValuePrec1(int8_t gv, int8_t fm)
51 int8_t idx = (gv >= 0 ? gv : -gv - 1);
52 int8_t mul = (g_model.gvars[idx].prec == 0 ? 10 : 1); // explicit cast to `int` needed, othervise gv is promoted to double!
53 if (gv < 0) {
54 mul = -mul;
56 return GVAR_VALUE(idx, getGVarFlightMode(fm, idx)) * mul;
59 void setGVarValue(uint8_t gv, int16_t value, int8_t fm)
61 fm = getGVarFlightMode(fm, gv);
62 if (GVAR_VALUE(gv, fm) != value) {
63 SET_GVAR_VALUE(gv, fm, value);
67 int16_t getGVarFieldValue(int16_t val, int16_t min, int16_t max, int8_t fm)
69 if (GV_IS_GV_VALUE(val, min, max)) {
70 int8_t gv = GV_INDEX_CALCULATION(val, max);
71 val = getGVarValue(gv, fm);
73 return limit(min, val, max);
76 int32_t getGVarFieldValuePrec1(int16_t val, int16_t min, int16_t max, int8_t fm)
78 if (GV_IS_GV_VALUE(val, min, max)) {
79 int8_t gv = GV_INDEX_CALCULATION(val, max);
80 val = getGVarValuePrec1(gv, fm);
82 else {
83 val *= 10;
85 return limit<int>(min*10, val, max*10);