Validate RSSI instance to allow user defined RSSI source (issue #5842) (#5846)
[opentx.git] / companion / src / simulation / simulatorinterface.h
blob47c5c22896675912598878473c1cb346f3de8819
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 _SIMULATORINTERFACE_H_
22 #define _SIMULATORINTERFACE_H_
24 #include "firmwares/boards.h"
25 #include "constants.h"
27 #include <algorithm>
28 #include <inttypes.h>
30 #include <QObject>
31 #include <QString>
32 #include <QByteArray>
33 #include <QDir>
34 #include <QLibrary>
35 #include <QMap>
37 #define SIMULATOR_INTERFACE_HEARTBEAT_PERIOD 1000 // ms
39 class SimulatorInterface : public QObject
41 Q_OBJECT
43 public:
45 enum InputSourceType {
46 INPUT_SRC_NONE = 0,
47 INPUT_SRC_ANALOG, // any analog source, index into g_anas[]
48 INPUT_SRC_STICK, // Board::StickAxes, g_anas[index]
49 INPUT_SRC_KNOB, // g_anas[index + StickAxes]
50 INPUT_SRC_SLIDER, // g_anas[index + StickAxes + num_pots]
51 INPUT_SRC_TXVIN, // g_anas[Analogs::TX_VOLTAGE]
52 INPUT_SRC_SWITCH, // Named 2/3-pos switches
53 INPUT_SRC_TRIM_SW, // Board::TrimSwitches
54 INPUT_SRC_TRIM, // Board::TrimAxes
55 INPUT_SRC_KEY, // UI key/pushbutton
56 INPUT_SRC_ROTENC, // Rotary encoder (TODO)
57 INPUT_SRC_TRAINER, // Virtual trainer input
58 INPUT_SRC_ENUM_COUNT
61 enum OutputSourceType {
62 OUTPUT_SRC_NONE = 0,
63 OUTPUT_SRC_CHAN_OUT,
64 OUTPUT_SRC_CHAN_MIX,
65 OUTPUT_SRC_TRIM_VALUE,
66 OUTPUT_SRC_TRIM_RANGE,
67 OUTPUT_SRC_VIRTUAL_SW,
68 OUTPUT_SRC_PHASE,
69 OUTPUT_SRC_GVAR,
70 OUTPUT_SRC_ENUM_COUNT
73 // only for data not available from Boards or Firmware, eg. compile-time options
74 enum Capability {
75 CAP_LUA, // LUA
76 CAP_ROTARY_ENC, // ROTARY_ENCODERS
77 CAP_ROTARY_ENC_NAV, // ROTARY_ENCODER_NAVIGATION
78 CAP_TELEM_FRSKY_SPORT, // TELEMETRY_FRSKY_SPORT
79 CAP_ENUM_COUNT
82 // This allows automatic en/decoding of flight mode + gvar value to/from any int32
83 struct gVarMode_t {
84 int16_t value;
85 uint8_t mode;
86 uint8_t prec:2;
87 uint8_t unit:2;
89 gVarMode_t(int i = 0) {
90 set(i);
92 void set(int i) {
93 unit = (i >> 26) & 0x3;
94 prec = (i >> 24) & 0x3;
95 mode = (i >> 16) & 0xFF;
96 value = (i & 0xFFFF);
98 operator int() {
99 return (((unit & 0x3) << 26) | ((prec & 0x3) << 24) | ((mode & 0xFF) << 16) | (value & 0xFFFF));
101 gVarMode_t & operator =(const int i) {
102 set(i);
103 return *this;
107 struct TxOutputs {
108 TxOutputs() { clear(); }
109 void clear() { memset(this, 0, sizeof(TxOutputs)); }
111 int16_t chans[CPN_MAX_CHNOUT]; // final channel outputs
112 int16_t ex_chans[CPN_MAX_CHNOUT]; // raw mix outputs
113 qint32 gvars[CPN_MAX_FLIGHT_MODES][CPN_MAX_GVARS];
114 int trims[CPN_MAX_TRIMS]; // Board::TrimAxes enum
115 bool vsw[CPN_MAX_LOGICAL_SWITCHES]; // virtual/logic switches
116 int8_t phase;
117 qint16 trimRange; // TRIM_MAX or TRIM_EXTENDED_MAX
118 // bool beep;
121 virtual ~SimulatorInterface() {}
123 virtual QString name() = 0;
124 virtual bool isRunning() = 0;
125 virtual void readRadioData(QByteArray & dest) = 0;
126 virtual uint8_t * getLcd() = 0;
127 virtual uint8_t getSensorInstance(uint16_t id, uint8_t defaultValue = 0) = 0;
128 virtual uint16_t getSensorRatio(uint16_t id) = 0;
129 virtual const int getCapability(Capability cap) = 0;
131 public slots:
133 virtual void init() = 0;
134 virtual void start(const char * filename = NULL, bool tests = true) = 0;
135 virtual void stop() = 0;
136 virtual void setSdPath(const QString & sdPath = "", const QString & settingsPath = "") = 0;
137 virtual void setVolumeGain(const int value) = 0;
138 virtual void setRadioData(const QByteArray & data) = 0;
139 virtual void setAnalogValue(uint8_t index, int16_t value) = 0;
140 virtual void setKey(uint8_t key, bool state) = 0;
141 virtual void setSwitch(uint8_t swtch, int8_t state) = 0;
142 virtual void setTrim(unsigned int idx, int value) = 0;
143 virtual void setTrimSwitch(uint8_t trim, bool state) = 0;
144 virtual void setTrainerInput(unsigned int inputNumber, int16_t value) = 0;
145 virtual void setInputValue(int type, uint8_t index, int16_t value) = 0;
146 virtual void rotaryEncoderEvent(int steps) = 0;
147 virtual void setTrainerTimeout(uint16_t ms) = 0;
148 virtual void sendTelemetry(const QByteArray data) = 0;
149 virtual void setLuaStateReloadPermanentScripts() = 0;
150 virtual void addTracebackDevice(QIODevice * device) = 0;
151 virtual void removeTracebackDevice(QIODevice * device) = 0;
153 signals:
155 void started();
156 void stopped();
157 void heartbeat(qint32 loops, qint64 timestamp);
158 void runtimeError(const QString & error);
159 void lcdChange(bool backlightEnable);
160 void phaseChanged(qint8 phase, const QString & name);
161 void channelOutValueChange(quint8 index, qint32 value, qint32 limit);
162 void channelMixValueChange(quint8 index, qint32 value, qint32 limit);
163 void virtualSwValueChange(quint8 index, qint32 value);
164 void trimValueChange(quint8 index, qint32 value);
165 void trimRangeChange(quint8 index, qint32 min, qint16 max);
166 void gVarValueChange(quint8 index, qint32 value);
167 void outputValueChange(int type, quint8 index, qint32 value);
170 class SimulatorFactory {
172 public:
173 virtual ~SimulatorFactory() { }
174 virtual QString name() = 0;
175 virtual Board::Type type() = 0;
176 virtual SimulatorInterface *create() = 0;
179 class SimulatorLoader
181 public:
182 static void registerSimulators();
183 static void unregisterSimulators();
184 static QStringList getAvailableSimulators();
185 static QString findSimulatorByFirmwareName(const QString & name);
186 static SimulatorInterface * loadSimulator(const QString & name);
187 static bool unloadSimulator(const QString & name);
189 protected:
190 typedef SimulatorFactory * (*RegisterSimulator)();
192 static int registerSimulators(const QDir & dir);
193 static QMap<QString, QLibrary *> registeredSimulators;
196 #endif // _SIMULATORINTERFACE_H_