Hotwheels-Cluster 1.2
Creation of Cluster APP for SEA:ME project.
 
Loading...
Searching...
No Matches
JoysticksController.cpp
Go to the documentation of this file.
1
17
19#include <QDebug>
20#include <QThread>
21
22
35 std::function<void(int)> steeringCallback,
36 std::function<void(int)> speedCallback, QObject *parent)
37 : QObject(parent), m_joystick(nullptr),
38 m_updateSteering(std::move(steeringCallback)),
39 m_updateSpeed(std::move(speedCallback)), m_running(false) {}
40
47 if (m_joystick) {
48 SDL_JoystickClose(m_joystick);
49 }
50 SDL_Quit();
51}
52
61 if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
62 qDebug() << "Failed to initialize SDL:" << SDL_GetError();
63 return false;
64 }
65
66 m_joystick = SDL_JoystickOpen(0);
67 if (!m_joystick) {
68 init();
69 }
70
71 return true;
72}
73
80
91 m_running = true;
92
93 if (!m_joystick) {
94 qDebug() << "Joystick not initialized.";
95 emit finished();
96 return;
97 }
98
99 while (m_running && !QThread::currentThread()->isInterruptionRequested()) {
100 SDL_Event e;
101 while (SDL_PollEvent(&e)) {
102 if (e.type == SDL_JOYAXISMOTION) {
103 if (e.jaxis.axis == 0) {
104 m_updateSteering(static_cast<int>(e.jaxis.value / 32767.0 * 180));
105 } else if (e.jaxis.axis == 3) {
106 m_updateSpeed(static_cast<int>(e.jaxis.value / 32767.0 * 100));
107 }
108 }
109 }
110 QThread::msleep(10);
111 }
112
113 // qDebug() << "Joystick controller loop finished.";
114 emit finished();
115}
File containing the JoysticksController class.
JoysticksController(std::function< void(int)> steeringCallback, std::function< void(int)> speedCallback, QObject *parent=nullptr)
Construct a JoysticksController object.
void requestStop()
Requests the joystick controller to stop.
std::function< void(int)> m_updateSpeed
bool init()
Initializes the joystick controller.
void processInput()
Runs the joystick controller loop.
~JoysticksController()
Destruct a JoysticksController object.
std::function< void(int)> m_updateSteering