Validate RSSI instance to allow user defined RSSI source (issue #5842) (#5846)
[opentx.git] / companion / src / simulation / telemetrysimu.h
blob1ff3c78e43d0f0d92c213de2a80f7dd83cb7b42b
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 _TELEMETRYSIMU_H_
22 #define _TELEMETRYSIMU_H_
24 #include <QCloseEvent>
25 #include <QWidget>
26 #include <QTimer>
27 #include <QDateTime>
28 #include <QtCore/qmath.h>
29 #include <QFileDialog>
31 #include "simulatorinterface.h"
33 static double const SPEEDS[] = { 0.2, 0.4, 0.6, 0.8, 1, 2, 3, 4, 5 };
34 template<class t> t LIMIT(t mi, t x, t ma) { return std::min(std::max(mi, x), ma); }
36 namespace Ui {
37 class TelemetrySimulator;
40 class TelemetrySimulator : public QWidget
42 Q_OBJECT
44 public:
46 explicit TelemetrySimulator(QWidget * parent, SimulatorInterface * simulator);
47 virtual ~TelemetrySimulator();
49 signals:
51 void telemetryDataChanged(const QByteArray data);
53 protected slots:
55 virtual void hideEvent(QHideEvent *event);
56 virtual void showEvent(QShowEvent *event);
57 void onSimulatorStarted();
58 void onSimulatorStopped();
59 void setupDataFields();
60 void onSimulateToggled(bool isChecked);
61 void onLogTimerEvent();
62 void onLoadLogFile();
63 void onPlay();
64 void onRewind();
65 void onStepForward();
66 void onStepBack();
67 void onStop();
68 void onPositionIndicatorChanged(int value);
69 void onReplayRateChanged(int value);
70 void refreshSensorRatios();
71 void generateTelemetryFrame();
73 protected:
75 Ui::TelemetrySimulator * ui;
76 QTimer timer;
77 QTimer logTimer;
78 SimulatorInterface *simulator;
79 bool m_simuStarted;
80 bool m_telemEnable;
81 bool m_logReplayEnable;
83 // protected classes follow
85 class LogPlaybackController
87 public:
88 LogPlaybackController(Ui::TelemetrySimulator * ui);
89 bool isReady();
90 void loadLogFile();
91 void play();
92 void stop();
93 void rewind();
94 void stepForward(bool focusOnStop = false);
95 void stepBack();
96 void updatePositionLabel(int32_t percentage);
97 void setUiDataValues();
98 double logFrequency; // in seconds
99 bool logFileGpsCordsInDecimalFormat;
101 private:
102 enum CONVERT_TYPE {
103 RXBT_V,
104 RSSI,
105 RAS,
110 T1_DEGC,
111 T1_DEGF,
112 T2_DEGC,
113 T2_DEGF,
114 RPM,
115 FUEL,
116 VSPD_MS,
117 VSPD_FS,
118 ALT_FEET,
119 ALT_METERS,
120 FASV,
121 FASC,
122 CELS_GRE,
123 ASPD_KTS,
124 ASPD_KMH,
125 ASPD_MPH,
126 GALT_FEET,
127 GALT_METERS,
128 GSPD_KNTS,
129 GSPD_KMH,
130 GSPD_MPH,
131 GHDG_DEG,
132 GDATE,
133 G_LATLON,
134 ACCX,
135 ACCY,
136 ACCZ,
137 FUEL_QTY,
140 struct DATA_TO_FUNC_XREF {
141 CONVERT_TYPE functionIndex;
142 int32_t dataIndex;
145 double convertFeetToMeters(QString input);
146 double convertFahrenheitToCelsius(QString input);
147 QString convertGPSDate(QString input);
148 QString convertGPS(QString input);
149 void addColumnHash(QString key, CONVERT_TYPE functionIndex);
150 double convertDegMin(QString input);
151 QDateTime parseTransmittterTimestamp(QString row);
152 void calcLogFrequency();
153 void checkGpsFormat();
155 QMap<QString, CONVERT_TYPE> colToFuncMap; // contains all 'known' column headings and how they are to be processed
156 Ui::TelemetrySimulator * ui;
157 QStringList csvRecords; // contents of the log file (one string per line);
158 QStringList columnNames;
159 QList<DATA_TO_FUNC_XREF> supportedCols;
160 int32_t recordIndex;
161 bool stepping;
162 }; // LogPlaybackController
164 LogPlaybackController *logPlayback;
166 class FlvssEmulator
168 public:
169 uint32_t setAllCells_GetNextPair(double cellValues[6]);
170 static const uint32_t MAXCELLS = 6;
172 private:
173 void encodeAllCells();
174 void splitIntoCells(double totalVolts);
175 static uint32_t encodeCellPair(uint8_t cellNum, uint8_t firstCellNo, double cell1, double cell2);
176 double cellFloats[6];
177 uint32_t nextCellNum;
178 uint32_t numCells;
179 uint32_t cellData1;
180 uint32_t cellData2;
181 uint32_t cellData3;
182 }; // FlvssEmulator
184 class GPSEmulator
186 public:
187 GPSEmulator();
188 uint32_t getNextPacketData(uint32_t packetType);
189 void setGPSDateTime(QString dateTime);
190 void setGPSLatLon(QString latLon);
191 void setGPSCourse(double course);
192 void setGPSSpeedKMH(double speed);
193 void setGPSAltitude(double altitude);
195 private:
196 QDateTime dt;
197 bool sendLat;
198 bool sendDate;
199 double lat;
200 double lon;
201 double course;
202 double speedKNTS;
203 double altitude; // in meters
204 uint32_t encodeLatLon(double latLon, bool isLat);
205 uint32_t encodeDateTime(uint8_t yearOrHour, uint8_t monthOrMinute, uint8_t dayOrSecond, bool isDate);
206 }; // GPSEmulator
208 }; // TelemetrySimulator
210 #endif // _TELEMETRYSIMU_H_