[companion] Adjust GVAR not possible in global functions (fix #5425)
[opentx.git] / companion / src / eeprominterface.h
blob0674f29dc6f1ad94171cb2605d364b6c87a77691
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 #ifndef _EEPROMINTERFACE_H_
22 #define _EEPROMINTERFACE_H_
24 #include "macros.h"
25 #include "radiodata.h"
26 #include "../../radio/src/definitions.h"
27 #include "simulatorinterface.h"
28 #include <QStringList>
29 #include <QList>
30 #include <iostream>
31 #include <QDebug>
33 const uint8_t modn12x3[4][4]= {
34 {1, 2, 3, 4},
35 {1, 3, 2, 4},
36 {4, 2, 3, 1},
37 {4, 3, 2, 1} };
39 enum Capability {
40 Models,
41 ModelName,
42 FlightModes,
43 FlightModesName,
44 FlightModesHaveFades,
45 Imperial,
46 Mixes,
47 Timers,
48 TimersName,
49 VoicesAsNumbers,
50 VoicesMaxLength,
51 MultiLangVoice,
52 ModelImage,
53 CustomFunctions,
54 SafetyChannelCustomFunction,
55 LogicalSwitches,
56 CustomAndSwitches,
57 HasNegAndSwitches,
58 LogicalSwitchesExt,
59 RotaryEncoders,
60 Outputs,
61 ChannelsName,
62 ExtraInputs,
63 TrimsRange,
64 ExtendedTrimsRange,
65 NumCurves,
66 NumCurvePoints,
67 OffsetWeight,
68 Simulation,
69 SoundMod,
70 SoundPitch,
71 MaxVolume,
72 Haptic,
73 HasBeeper,
74 ModelTrainerEnable,
75 HasExpoNames,
76 HasNoExpo,
77 HasMixerNames,
78 HasCvNames,
79 HasPxxCountry,
80 HasPPMStart,
81 HasGeneralUnits,
82 HasFAIMode,
83 OptrexDisplay,
84 PPMExtCtrl,
85 PPMFrameLength,
86 Telemetry,
87 TelemetryUnits,
88 TelemetryBars,
89 Heli,
90 Gvars,
91 GvarsInCS,
92 GvarsAreNamed,
93 GvarsFlightModes,
94 GvarsName,
95 NoTelemetryProtocol,
96 TelemetryCustomScreens,
97 TelemetryCustomScreensFieldsPerLine,
98 TelemetryMaxMultiplier,
99 HasVario,
100 HasVarioSink,
101 HasFailsafe,
102 HasSoundMixer,
103 NumModules,
104 PPMCenter,
105 PPMUnitMicroseconds,
106 SYMLimits,
107 HastxCurrentCalibration,
108 HasVolume,
109 HasBrightness,
110 PerModelTimers,
111 SlowScale,
112 SlowRange,
113 PermTimers,
114 HasSDLogs,
115 CSFunc,
116 LcdWidth,
117 LcdHeight,
118 LcdDepth,
119 GetThrSwitch,
120 HasDisplayText,
121 HasTopLcd,
122 GlobalFunctions,
123 VirtualInputs,
124 InputsLength,
125 TrainerInputs,
126 RtcTime,
127 SportTelemetry,
128 LuaScripts,
129 LuaInputsPerScript,
130 LuaOutputsPerScript,
131 LimitsPer1000,
132 EnhancedCurves,
133 HasFasOffset,
134 HasMahPersistent,
135 MultiposPots,
136 MultiposPotsPositions,
137 SimulatorVariant,
138 MavlinkTelemetry,
139 HasInputDiff,
140 HasMixerExpo,
141 HasBatMeterRange,
142 DangerousFunctions,
143 HasModelCategories
146 class SimulatorInterface;
147 class EEPROMInterface
149 public:
151 EEPROMInterface(Board::Type board):
152 board(board)
156 virtual ~EEPROMInterface() {}
158 inline Board::Type getBoard() { return board; }
160 virtual unsigned long load(RadioData &radioData, const uint8_t * eeprom, int size) = 0;
162 virtual unsigned long loadBackup(RadioData & radioData, const uint8_t * eeprom, int esize, int index) = 0;
164 virtual int save(uint8_t * eeprom, const RadioData & radioData, uint8_t version=0, uint32_t variant=0) = 0;
166 virtual int getSize(const ModelData &) = 0;
168 virtual int getSize(const GeneralSettings &) = 0;
170 protected:
172 Board::Type board;
174 private:
176 EEPROMInterface();
180 /* EEPROM string conversion functions */
181 void setEEPROMString(char *dst, const char *src, int size);
182 void getEEPROMString(char *dst, const char *src, int size);
184 float ValToTim(int value);
185 int TimToVal(float value);
187 inline int applyStickMode(int stick, unsigned int mode)
189 if (mode == 0 || mode > 4) {
190 std::cerr << "Incorrect stick mode" << mode;
191 return stick;
194 if (stick >= 1 && stick <= 4)
195 return modn12x3[mode-1][stick-1];
196 else
197 return stick;
200 void registerEEpromInterfaces();
201 void unregisterEEpromInterfaces();
202 void registerOpenTxFirmwares();
203 void unregisterOpenTxFirmwares();
205 enum EepromLoadErrors {
206 ALL_OK,
207 UNKNOWN_ERROR,
208 UNSUPPORTED_NEWER_VERSION,
209 WRONG_SIZE,
210 WRONG_FILE_SYSTEM,
211 NOT_OPENTX,
212 NOT_TH9X,
213 NOT_GRUVIN9X,
214 NOT_ERSKY9X,
215 NOT_ER9X,
216 UNKNOWN_BOARD,
217 WRONG_BOARD,
218 BACKUP_NOT_SUPPORTED,
220 HAS_WARNINGS,
221 OLD_VERSION,
222 WARNING_WRONG_FIRMWARE,
224 NUM_ERRORS
227 void ShowEepromErrors(QWidget *parent, const QString &title, const QString &mainMessage, unsigned long errorsFound);
228 void ShowEepromWarnings(QWidget *parent, const QString &title, unsigned long errorsFound);
231 struct Option {
232 const char * name;
233 QString tooltip;
234 uint32_t variant;
237 class Firmware {
239 public:
240 Firmware(const QString & id, const QString & name, Board::Type board):
241 id(id),
242 name(name),
243 board(board),
244 variantBase(0),
245 base(NULL),
246 eepromInterface(NULL)
250 Firmware(Firmware * base, const QString & id, const QString & name, Board::Type board):
251 id(id),
252 name(name),
253 board(board),
254 variantBase(0),
255 base(base),
256 eepromInterface(NULL)
260 virtual ~Firmware()
264 inline const Firmware * getFirmwareBase() const
266 return base ? base : this;
269 virtual Firmware * getFirmwareVariant(const QString & id) { return NULL; }
271 unsigned int getVariantNumber();
273 virtual void addLanguage(const char *lang);
275 virtual void addTTSLanguage(const char *lang);
277 virtual void addOption(const char *option, QString tooltip="", uint32_t variant=0);
279 virtual void addOptions(Option options[]);
281 virtual QString getStampUrl() = 0;
283 virtual QString getReleaseNotesUrl() = 0;
285 virtual QString getFirmwareUrl() = 0;
287 Board::Type getBoard() const
289 return board;
292 void setEEpromInterface(EEPROMInterface * eeprom)
294 eepromInterface = eeprom;
297 EEPROMInterface * getEEpromInterface()
299 return eepromInterface;
302 QString getName() const
304 return name;
307 QString getId() const
309 return id;
312 virtual int getCapability(Capability) = 0;
314 virtual QString getAnalogInputName(unsigned int index) = 0;
316 virtual QTime getMaxTimerStart() = 0;
318 virtual int isAvailable(PulsesProtocol proto, int port=0) = 0;
320 const int getFlashSize();
322 static Firmware * getFirmwareForId(const QString & id);
324 static QVector<Firmware *> getRegisteredFirmwares()
326 return registeredFirmwares;
328 static void addRegisteredFirmware(Firmware * fw)
330 registeredFirmwares.append(fw);
333 static Firmware * getDefaultVariant()
335 return defaultVariant;
337 static void setDefaultVariant(Firmware * value)
339 defaultVariant = value;
342 static Firmware * getCurrentVariant()
344 return currentVariant;
346 static void setCurrentVariant(Firmware * value)
348 currentVariant = value;
351 public:
352 QList<const char *> languages;
353 QList<const char *> ttslanguages;
354 QList< QList<Option> > opts;
357 protected:
358 QString id;
359 QString name;
360 Board::Type board;
361 unsigned int variantBase;
362 Firmware * base;
363 EEPROMInterface * eepromInterface;
365 static QVector<Firmware *> registeredFirmwares;
366 static Firmware * defaultVariant;
367 static Firmware * currentVariant;
369 private:
370 Firmware();
374 inline Firmware * getCurrentFirmware()
376 return Firmware::getCurrentVariant();
379 inline EEPROMInterface * getCurrentEEpromInterface()
381 return Firmware::getCurrentVariant()->getEEpromInterface();
384 inline Board::Type getCurrentBoard()
386 return Firmware::getCurrentVariant()->getBoard();
389 inline int divRoundClosest(const int n, const int d)
391 return ((n < 0) ^ (d < 0)) ? ((n - d/2)/d) : ((n + d/2)/d);
394 inline int calcRESXto100(int x)
396 return divRoundClosest(x*100, 1024);
399 #define CHECK_IN_ARRAY(T, index) ((unsigned int)index < DIM(T) ? T[(unsigned int)index] : "???")
401 extern QList<EEPROMInterface *> eepromInterfaces;
403 bool loadFile(RadioData & radioData, const QString & filename);
406 #endif // _EEPROMINTERFACE_H_