Fix 2.2.2RC1 no gvar compile (#5961)
[opentx.git] / radio / src / gvars.cpp
blobee9c1a939a465d61f5cd478017bba217ff9200e4
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 #if defined(PCBSTD)
24 int16_t getGVarFieldValue(int16_t x, int16_t min, int16_t max)
26 if (GV_IS_GV_VALUE(x, min, max)) {
27 int8_t idx = GV_INDEX_CALCULATION(x, max);
28 int8_t mul = 1;
30 if (idx < 0) {
31 idx = -1-idx;
32 mul = -1;
35 x = GVAR_VALUE(idx, -1) * mul;
38 return limit(min, x, max);
41 void setGVarValue(uint8_t idx, int8_t value)
43 if (GVAR_VALUE(idx, -1) != value) {
44 SET_GVAR_VALUE(idx, -1, value);
47 #else
48 uint8_t gvarDisplayTimer = 0;
49 uint8_t gvarLastChanged = 0;
51 uint8_t getGVarFlightMode(uint8_t fm, uint8_t gv) // TODO change params order to be consistent!
53 for (uint8_t i=0; i<MAX_FLIGHT_MODES; i++) {
54 if (fm == 0) return 0;
55 int16_t val = GVAR_VALUE(gv, fm);
56 if (val <= GVAR_MAX) return fm;
57 uint8_t result = val-GVAR_MAX-1;
58 if (result >= fm) result++;
59 fm = result;
61 return 0;
64 int16_t getGVarValue(int8_t gv, int8_t fm)
66 int8_t mul = 1;
67 if (gv < 0) {
68 gv = -1-gv;
69 mul = -1;
71 return GVAR_VALUE(gv, getGVarFlightMode(fm, gv)) * mul;
74 int32_t getGVarValuePrec1(int8_t gv, int8_t fm)
76 int8_t mul;
77 uint8_t prec = g_model.gvars[abs((int)gv)].prec; // explicit cast to `int` needed, othervise gv is promoted to double!
78 if (prec == 0)
79 mul = 10;
80 else
81 mul = 1;
83 if (gv < 0) {
84 gv = -1-gv;
85 mul = -mul;
87 return GVAR_VALUE(gv, getGVarFlightMode(fm, gv)) * mul;
90 void setGVarValue(uint8_t gv, int16_t value, int8_t fm)
92 fm = getGVarFlightMode(fm, gv);
93 if (GVAR_VALUE(gv, fm) != value) {
94 SET_GVAR_VALUE(gv, fm, value);
98 int16_t getGVarFieldValue(int16_t val, int16_t min, int16_t max, int8_t fm)
100 if (GV_IS_GV_VALUE(val, min, max)) {
101 int8_t gv = GV_INDEX_CALCULATION(val, max);
102 val = getGVarValue(gv, fm);
104 return limit(min, val, max);
107 int32_t getGVarFieldValuePrec1(int16_t val, int16_t min, int16_t max, int8_t fm)
109 if (GV_IS_GV_VALUE(val, min, max)) {
110 int8_t gv = GV_INDEX_CALCULATION(val, max);
111 val = getGVarValuePrec1(gv, fm);
113 else {
114 val *= 10;
116 return limit<int>(min*10, val, max*10);
118 #endif