Make TX volatge for simu more flexible (#7124)
[opentx.git] / companion / src / firmwares / eeprominterface.h
blob9632af1ac731e1b7de3169fb8738c4c1fd3d55ff
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 "boards.h"
25 #include "macros.h"
26 #include "radiodata.h"
27 #include "../../radio/src/definitions.h"
28 #include "simulatorinterface.h"
30 #include <QtCore>
31 #include <QStringList>
32 #include <QList>
33 #include <QDebug>
35 #include <iostream>
37 const uint8_t modn12x3[4][4]= {
38 {1, 2, 3, 4},
39 {1, 3, 2, 4},
40 {4, 2, 3, 1},
41 {4, 3, 2, 1} };
43 enum Capability {
44 Models,
45 ModelName,
46 FlightModes,
47 FlightModesName,
48 FlightModesHaveFades,
49 Imperial,
50 Mixes,
51 Timers,
52 TimersName,
53 VoicesAsNumbers,
54 VoicesMaxLength,
55 MultiLangVoice,
56 ModelImage,
57 CustomFunctions,
58 SafetyChannelCustomFunction,
59 LogicalSwitches,
60 CustomAndSwitches,
61 HasNegAndSwitches,
62 LogicalSwitchesExt,
63 RotaryEncoders,
64 Outputs,
65 ChannelsName,
66 ExtraInputs,
67 TrimsRange,
68 ExtendedTrimsRange,
69 NumCurves,
70 NumCurvePoints,
71 OffsetWeight,
72 Simulation,
73 SoundMod,
74 SoundPitch,
75 MaxVolume,
76 MaxContrast,
77 MinContrast,
78 Haptic,
79 HasBeeper,
80 ModelTrainerEnable,
81 HasExpoNames,
82 HasNoExpo,
83 HasMixerNames,
84 HasCvNames,
85 HasPxxCountry,
86 HasPPMStart,
87 HasGeneralUnits,
88 HasFAIMode,
89 OptrexDisplay,
90 PPMExtCtrl,
91 PPMFrameLength,
92 Telemetry,
93 TelemetryUnits,
94 TelemetryBars,
95 Heli,
96 Gvars,
97 GvarsInCS,
98 GvarsAreNamed,
99 GvarsFlightModes,
100 GvarsName,
101 NoTelemetryProtocol,
102 TelemetryCustomScreens,
103 TelemetryCustomScreensFieldsPerLine,
104 TelemetryMaxMultiplier,
105 HasVario,
106 HasVarioSink,
107 HasFailsafe,
108 HasSoundMixer,
109 NumModules,
110 NumFirstUsableModule,
111 HasModuleR9MFlex,
112 HasModuleR9MMini,
113 PPMCenter,
114 PPMUnitMicroseconds,
115 SYMLimits,
116 HastxCurrentCalibration,
117 HasVolume,
118 HasBrightness,
119 PerModelTimers,
120 SlowScale,
121 SlowRange,
122 PermTimers,
123 HasSDLogs,
124 CSFunc,
125 LcdWidth,
126 LcdHeight,
127 LcdDepth,
128 GetThrSwitch,
129 HasDisplayText,
130 HasTopLcd,
131 GlobalFunctions,
132 VirtualInputs,
133 InputsLength,
134 TrainerInputs,
135 RtcTime,
136 SportTelemetry,
137 LuaScripts,
138 LuaInputsPerScript,
139 LuaOutputsPerScript,
140 LimitsPer1000,
141 EnhancedCurves,
142 HasFasOffset,
143 HasMahPersistent,
144 SimulatorVariant,
145 MavlinkTelemetry,
146 HasInputDiff,
147 HasMixerExpo,
148 HasBatMeterRange,
149 DangerousFunctions,
150 HasModelCategories,
151 HasSwitchableJack,
152 PwrButtonPress
155 class EEPROMInterface
157 Q_DECLARE_TR_FUNCTIONS(EEPROMInterface)
159 public:
161 EEPROMInterface(Board::Type board):
162 board(board)
166 virtual ~EEPROMInterface() {}
168 inline Board::Type getBoard() { return board; }
170 virtual unsigned long load(RadioData &radioData, const uint8_t * eeprom, int size) = 0;
172 virtual unsigned long loadBackup(RadioData & radioData, const uint8_t * eeprom, int esize, int index) = 0;
174 virtual int save(uint8_t * eeprom, const RadioData & radioData, uint8_t version=0, uint32_t variant=0) = 0;
176 virtual int getSize(const ModelData &) = 0;
178 virtual int getSize(const GeneralSettings &) = 0;
180 //static void showEepromErrors(QWidget *parent, const QString &title, const QString &mainMessage, unsigned long errorsFound);
181 static QString getEepromWarnings(unsigned long errorsFound);
183 protected:
185 Board::Type board;
187 private:
189 EEPROMInterface();
193 /* EEPROM string conversion function (used only by er9xeeprom and ersky9xeeprom) */
194 inline void getEEPROMString(char *dst, const char *src, int size)
196 memcpy(dst, src, size);
197 dst[size] = '\0';
198 for (int i=size-1; i>=0; i--) {
199 if (dst[i] == ' ')
200 dst[i] = '\0';
201 else
202 break;
206 // (used only by er9xeeprom and ersky9xeeprom)
207 inline int applyStickMode(int stick, unsigned int mode)
209 if (mode == 0 || mode > 4) {
210 std::cerr << "Incorrect stick mode" << mode;
211 return stick;
214 if (stick >= 1 && stick <= 4)
215 return modn12x3[mode-1][stick-1];
216 else
217 return stick;
220 float ValToTim(int value);
221 int TimToVal(float value);
223 void registerEEpromInterfaces();
224 void unregisterEEpromInterfaces();
225 void registerOpenTxFirmwares();
226 void unregisterOpenTxFirmwares();
228 enum EepromLoadErrors {
229 ALL_OK,
230 UNKNOWN_ERROR,
231 UNSUPPORTED_NEWER_VERSION,
232 WRONG_SIZE,
233 WRONG_FILE_SYSTEM,
234 NOT_OPENTX,
235 NOT_TH9X,
236 NOT_GRUVIN9X,
237 NOT_ERSKY9X,
238 NOT_ER9X,
239 UNKNOWN_BOARD,
240 WRONG_BOARD,
241 BACKUP_NOT_SUPPORTED,
243 HAS_WARNINGS,
244 OLD_VERSION,
245 WARNING_WRONG_FIRMWARE,
247 NUM_ERRORS
250 class Firmware
252 Q_DECLARE_TR_FUNCTIONS(Firmware)
254 public:
255 struct Option {
256 const char * name = nullptr;
257 QString tooltip;
258 unsigned variant = 0;
260 explicit Option(const char * name, const QString & description, unsigned variant = 0) :
261 name(name), tooltip(description), variant(variant) { }
263 typedef QList<Option> OptionsGroup;
264 typedef QList<OptionsGroup> OptionsList;
267 explicit Firmware(const QString & id, const QString & name, Board::Type board) :
268 Firmware(nullptr, id, name, board)
271 explicit Firmware(Firmware * base, const QString & id, const QString & name, Board::Type board) :
272 id(id),
273 name(name),
274 board(board),
275 variantBase(0),
276 base(base),
277 eepromInterface(nullptr)
280 virtual ~Firmware() { }
282 inline const Firmware * getFirmwareBase() const
284 return base ? base : this;
287 virtual Firmware * getFirmwareVariant(const QString & id) { return NULL; }
289 unsigned int getVariantNumber();
291 virtual void addLanguage(const char * lang);
293 //virtual void addTTSLanguage(const char * lang);
295 virtual void addOption(const char * option, const QString & tooltip = QString(), unsigned variant = 0);
297 virtual void addOption(const Option & option);
299 virtual void addOptionsGroup(const OptionsGroup & options);
301 virtual QString getStampUrl() = 0;
303 virtual QString getReleaseNotesUrl() = 0;
305 virtual QString getFirmwareUrl() = 0;
307 Board::Type getBoard() const
309 return board;
312 void setEEpromInterface(EEPROMInterface * eeprom)
314 eepromInterface = eeprom;
317 EEPROMInterface * getEEpromInterface()
319 return eepromInterface;
322 QString getName() const
324 return name;
327 QString getId() const
329 return id;
332 QList<const char *> languageList() const
334 return languages;
337 OptionsList optionGroups() const
339 return opts;
342 virtual int getCapability(Capability) = 0;
344 virtual QString getAnalogInputName(unsigned int index) = 0;
346 virtual QTime getMaxTimerStart() = 0;
348 virtual bool isAvailable(PulsesProtocol proto, int port=0) = 0;
350 const int getFlashSize();
352 static Firmware * getFirmwareForId(const QString & id);
354 static QVector<Firmware *> getRegisteredFirmwares()
356 return registeredFirmwares;
359 static void addRegisteredFirmware(Firmware * fw)
361 registeredFirmwares.append(fw);
364 static Firmware * getDefaultVariant()
366 return defaultVariant;
368 static void setDefaultVariant(Firmware * value)
370 defaultVariant = value;
373 static Firmware * getCurrentVariant()
375 return currentVariant;
378 static void setCurrentVariant(Firmware * value)
380 currentVariant = value;
383 protected:
384 QString id;
385 QString name;
386 Board::Type board;
387 unsigned int variantBase;
388 Firmware * base;
389 EEPROMInterface * eepromInterface;
390 QList<const char *> languages;
391 //QList<const char *> ttslanguages;
392 OptionsList opts;
394 static QVector<Firmware *> registeredFirmwares;
395 static Firmware * defaultVariant;
396 static Firmware * currentVariant;
399 inline Firmware * getCurrentFirmware()
401 return Firmware::getCurrentVariant();
404 inline EEPROMInterface * getCurrentEEpromInterface()
406 return Firmware::getCurrentVariant()->getEEpromInterface();
409 inline Board::Type getCurrentBoard()
411 return Firmware::getCurrentVariant()->getBoard();
414 inline int divRoundClosest(const int n, const int d)
416 return ((n < 0) ^ (d < 0)) ? ((n - d/2)/d) : ((n + d/2)/d);
419 inline int calcRESXto100(int x)
421 return divRoundClosest(x*100, 1024);
424 extern QList<EEPROMInterface *> eepromInterfaces;
427 #endif // _EEPROMINTERFACE_H_