Fix doc path
[opentx.git] / companion / src / firmwares / eeprominterface.h
blob9355ba467cc699d2b4e754bcce929e1673716c19
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 HasModuleR9MFlex,
111 HasModuleR9MMini,
112 PPMCenter,
113 PPMUnitMicroseconds,
114 SYMLimits,
115 HastxCurrentCalibration,
116 HasVolume,
117 HasBrightness,
118 PerModelTimers,
119 SlowScale,
120 SlowRange,
121 PermTimers,
122 HasSDLogs,
123 CSFunc,
124 LcdWidth,
125 LcdHeight,
126 LcdDepth,
127 GetThrSwitch,
128 HasDisplayText,
129 HasTopLcd,
130 GlobalFunctions,
131 VirtualInputs,
132 InputsLength,
133 TrainerInputs,
134 RtcTime,
135 SportTelemetry,
136 LuaScripts,
137 LuaInputsPerScript,
138 LuaOutputsPerScript,
139 LimitsPer1000,
140 EnhancedCurves,
141 HasFasOffset,
142 HasMahPersistent,
143 SimulatorVariant,
144 MavlinkTelemetry,
145 HasInputDiff,
146 HasMixerExpo,
147 HasBatMeterRange,
148 DangerousFunctions,
149 HasModelCategories
152 class EEPROMInterface
154 Q_DECLARE_TR_FUNCTIONS(EEPROMInterface)
156 public:
158 EEPROMInterface(Board::Type board):
159 board(board)
163 virtual ~EEPROMInterface() {}
165 inline Board::Type getBoard() { return board; }
167 virtual unsigned long load(RadioData &radioData, const uint8_t * eeprom, int size) = 0;
169 virtual unsigned long loadBackup(RadioData & radioData, const uint8_t * eeprom, int esize, int index) = 0;
171 virtual int save(uint8_t * eeprom, const RadioData & radioData, uint8_t version=0, uint32_t variant=0) = 0;
173 virtual int getSize(const ModelData &) = 0;
175 virtual int getSize(const GeneralSettings &) = 0;
177 //static void showEepromErrors(QWidget *parent, const QString &title, const QString &mainMessage, unsigned long errorsFound);
178 static void showEepromWarnings(QWidget *parent, const QString &title, unsigned long errorsFound);
180 protected:
182 Board::Type board;
184 private:
186 EEPROMInterface();
190 /* EEPROM string conversion function (used only by er9xeeprom and ersky9xeeprom) */
191 inline void getEEPROMString(char *dst, const char *src, int size)
193 memcpy(dst, src, size);
194 dst[size] = '\0';
195 for (int i=size-1; i>=0; i--) {
196 if (dst[i] == ' ')
197 dst[i] = '\0';
198 else
199 break;
203 // (used only by er9xeeprom and ersky9xeeprom)
204 inline int applyStickMode(int stick, unsigned int mode)
206 if (mode == 0 || mode > 4) {
207 std::cerr << "Incorrect stick mode" << mode;
208 return stick;
211 if (stick >= 1 && stick <= 4)
212 return modn12x3[mode-1][stick-1];
213 else
214 return stick;
217 float ValToTim(int value);
218 int TimToVal(float value);
220 void registerEEpromInterfaces();
221 void unregisterEEpromInterfaces();
222 void registerOpenTxFirmwares();
223 void unregisterOpenTxFirmwares();
225 enum EepromLoadErrors {
226 ALL_OK,
227 UNKNOWN_ERROR,
228 UNSUPPORTED_NEWER_VERSION,
229 WRONG_SIZE,
230 WRONG_FILE_SYSTEM,
231 NOT_OPENTX,
232 NOT_TH9X,
233 NOT_GRUVIN9X,
234 NOT_ERSKY9X,
235 NOT_ER9X,
236 UNKNOWN_BOARD,
237 WRONG_BOARD,
238 BACKUP_NOT_SUPPORTED,
240 HAS_WARNINGS,
241 OLD_VERSION,
242 WARNING_WRONG_FIRMWARE,
244 NUM_ERRORS
247 struct Option {
248 const char * name;
249 QString tooltip;
250 uint32_t variant;
253 class Firmware
255 Q_DECLARE_TR_FUNCTIONS(Firmware)
257 public:
258 Firmware(const QString & id, const QString & name, Board::Type board):
259 id(id),
260 name(name),
261 board(board),
262 variantBase(0),
263 base(NULL),
264 eepromInterface(NULL)
268 Firmware(Firmware * base, const QString & id, const QString & name, Board::Type board):
269 id(id),
270 name(name),
271 board(board),
272 variantBase(0),
273 base(base),
274 eepromInterface(NULL)
278 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, QString tooltip="", uint32_t variant=0);
297 virtual void addOptions(Option options[]);
299 virtual QString getStampUrl() = 0;
301 virtual QString getReleaseNotesUrl() = 0;
303 virtual QString getFirmwareUrl() = 0;
305 Board::Type getBoard() const
307 return board;
310 void setEEpromInterface(EEPROMInterface * eeprom)
312 eepromInterface = eeprom;
315 EEPROMInterface * getEEpromInterface()
317 return eepromInterface;
320 QString getName() const
322 return name;
325 QString getId() const
327 return id;
330 virtual int getCapability(Capability) = 0;
332 virtual QString getAnalogInputName(unsigned int index) = 0;
334 virtual QTime getMaxTimerStart() = 0;
336 virtual int isAvailable(PulsesProtocol proto, int port=0) = 0;
338 const int getFlashSize();
340 static Firmware * getFirmwareForId(const QString & id);
342 static QVector<Firmware *> getRegisteredFirmwares()
344 return registeredFirmwares;
347 static void addRegisteredFirmware(Firmware * fw)
349 registeredFirmwares.append(fw);
352 static Firmware * getDefaultVariant()
354 return defaultVariant;
356 static void setDefaultVariant(Firmware * value)
358 defaultVariant = value;
361 static Firmware * getCurrentVariant()
363 return currentVariant;
366 static void setCurrentVariant(Firmware * value)
368 currentVariant = value;
371 public:
372 QList<const char *> languages;
373 QList<const char *> ttslanguages;
374 QList< QList<Option> > opts;
377 protected:
378 QString id;
379 QString name;
380 Board::Type board;
381 unsigned int variantBase;
382 Firmware * base;
383 EEPROMInterface * eepromInterface;
385 static QVector<Firmware *> registeredFirmwares;
386 static Firmware * defaultVariant;
387 static Firmware * currentVariant;
389 private:
390 Firmware();
394 inline Firmware * getCurrentFirmware()
396 return Firmware::getCurrentVariant();
399 inline EEPROMInterface * getCurrentEEpromInterface()
401 return Firmware::getCurrentVariant()->getEEpromInterface();
404 inline Board::Type getCurrentBoard()
406 return Firmware::getCurrentVariant()->getBoard();
409 inline int divRoundClosest(const int n, const int d)
411 return ((n < 0) ^ (d < 0)) ? ((n - d/2)/d) : ((n + d/2)/d);
414 inline int calcRESXto100(int x)
416 return divRoundClosest(x*100, 1024);
419 #define CHECK_IN_ARRAY(T, index) ((unsigned int)index < DIM(T) ? T[(unsigned int)index] : CPN_STR_UNKNOWN_ITEM)
421 extern QList<EEPROMInterface *> eepromInterfaces;
423 bool loadFile(RadioData & radioData, const QString & filename);
426 #endif // _EEPROMINTERFACE_H_