Bsongis/issue 4400 (#4425)
[opentx.git] / companion / src / simulation / trainersimu.cpp
blob50a50666954d3d72bd3bcaabc7f3d11a51f71124
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 TrainerSimulator::TrainerSimulator(QWidget * parent, SimulatorInterface * simulator):
28 QWidget(parent),
29 ui(new Ui::TrainerSimulator),
30 simulator(simulator)
32 ui->setupUi(this);
34 vJoyLeft = new VirtualJoystickWidget(this, 'L', false);
35 vJoyLeft->setStickColor(Qt::cyan);
36 ui->leftStickLayout->addWidget(vJoyLeft);
38 vJoyRight = new VirtualJoystickWidget(this, 'R', false);
39 vJoyRight->setStickColor(Qt::cyan);
40 ui->rightStickLayout->addWidget(vJoyRight);
42 timer = new QTimer(this);
43 timer->setInterval(10);
44 connect(timer, SIGNAL(timeout()), this, SLOT(onTimerEvent()));
47 TrainerSimulator::~TrainerSimulator()
49 if (timer) {
50 timer->stop();
51 delete timer;
53 if (vJoyLeft)
54 delete vJoyLeft;
55 if (vJoyRight)
56 delete vJoyRight;
58 delete ui;
61 void TrainerSimulator::showEvent(QShowEvent *event)
63 timer->start();
64 event->accept();
67 void TrainerSimulator::closeEvent(QCloseEvent *event)
69 timer->stop();
70 event->accept();
73 void TrainerSimulator::centerSticks()
75 if (vJoyLeft)
76 vJoyLeft->centerStick();
78 if (vJoyRight)
79 vJoyRight->centerStick();
82 void TrainerSimulator::onTimerEvent()
84 centerSticks();
85 setTrainerInputs();
88 void TrainerSimulator::setTrainerInputs()
90 if (!simulator)
91 return;
93 if (vJoyLeft) {
94 simulator->setTrainerInput(0, int( 512 * vJoyLeft->getStickX())); // LEFT HORZ
95 simulator->setTrainerInput(1, int(-512 * vJoyLeft->getStickY())); // LEFT VERT
97 if (vJoyRight) {
98 simulator->setTrainerInput(2, int(-512 * vJoyRight->getStickY())); // RGHT VERT
99 simulator->setTrainerInput(3, int( 512 * vJoyRight->getStickX())); // RGHT HORZ