Validate RSSI instance to allow user defined RSSI source (issue #5842) (#5846)
[opentx.git] / companion / src / simulation / trainersimu.cpp
blobf26fa05af08f89db3f2e47d63dab64611103751e
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 #include <stdint.h>
22 #include "trainersimu.h"
23 #include "ui_trainersimu.h"
24 #include "helpers.h"
25 #include "virtualjoystickwidget.h"
27 #define TRAINERSIMU_HEARTBEAT_PERIOD 500 // [ms]
29 TrainerSimulator::TrainerSimulator(QWidget * parent, SimulatorInterface * simulator):
30 QWidget(parent),
31 ui(new Ui::TrainerSimulator),
32 simulator(simulator),
33 m_simuStarted(false)
35 ui->setupUi(this);
37 vJoyLeft = new VirtualJoystickWidget(this, 'L', false);
38 vJoyLeft->setStickColor(Qt::cyan);
39 vJoyLeft->setStickScale(512);
40 ui->leftStickLayout->addWidget(vJoyLeft);
42 vJoyRight = new VirtualJoystickWidget(this, 'R', false);
43 vJoyRight->setStickColor(Qt::cyan);
44 vJoyRight->setStickScale(512);
45 ui->rightStickLayout->addWidget(vJoyRight);
47 connect(vJoyLeft, &VirtualJoystickWidget::valueChange, this, &TrainerSimulator::onRadioWidgetValueChange);
48 connect(vJoyRight, &VirtualJoystickWidget::valueChange, this, &TrainerSimulator::onRadioWidgetValueChange);
50 connect(this, &TrainerSimulator::trainerHeartbeat, simulator, &SimulatorInterface::setTrainerTimeout);
51 connect(this, &TrainerSimulator::trainerChannelChange, simulator, &SimulatorInterface::setTrainerInput);
52 connect(simulator, &SimulatorInterface::started, this, &TrainerSimulator::onSimulatorStarted);
53 connect(simulator, &SimulatorInterface::stopped, this, &TrainerSimulator::onSimulatorStopped);
55 timer.setInterval(TRAINERSIMU_HEARTBEAT_PERIOD - 10);
56 connect(&timer, &QTimer::timeout, this, &TrainerSimulator::emitHeartbeat);
59 TrainerSimulator::~TrainerSimulator()
61 if (vJoyLeft)
62 delete vJoyLeft;
63 if (vJoyRight)
64 delete vJoyRight;
66 delete ui;
69 void TrainerSimulator::showEvent(QShowEvent *event)
71 start();
74 void TrainerSimulator::hideEvent(QHideEvent *event)
76 stop();
79 void TrainerSimulator::start()
81 timer.start();
84 void TrainerSimulator::stop()
86 timer.stop();
89 void TrainerSimulator::onSimulatorStarted()
91 m_simuStarted = true;
94 void TrainerSimulator::onSimulatorStopped()
96 m_simuStarted = false;
97 stop();
100 void TrainerSimulator::emitHeartbeat()
102 emit trainerHeartbeat(TRAINERSIMU_HEARTBEAT_PERIOD);
105 void TrainerSimulator::onRadioWidgetValueChange(RadioWidget::RadioWidgetType type, int index, int value)
107 if (type == RadioWidget::RADIO_WIDGET_STICK && m_simuStarted && timer.isActive()) {
108 emit trainerChannelChange(index, value);
109 emitHeartbeat();