-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.cpp
More file actions
248 lines (222 loc) · 6.9 KB
/
Controller.cpp
File metadata and controls
248 lines (222 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "Controller.hpp"
#include "SFML/Window/Joystick.hpp"
#include <iostream>
#include <string>
const std::string Controller::buttonName[] = {"A","B","X","Y","LB","RB","MENU","START","XBOX","LSB","RSB"};
Controller::Controller(unsigned int mode, unsigned int port){
this->setMode(mode);
this->setPort(port);
sf::Joystick::update();
if(sf::Joystick::isConnected(port)) std::cout << "Controller (Port #" << port << " Mode #" << mode << ") connected" <<std::endl;
else std::cout << "Connection failed you silly" << std::endl;
}
Controller::~Controller(){
std::cout << "Controller deconstructed" << std::endl;
}
void Controller::setMode(unsigned int mode){
if(mode>9) this->mode = 0; //assures all other channel arguments default to 0
else this->mode = mode;
}
void Controller::setPort(unsigned int port){
if(port>8) this->port = 0; //assures invalid port arguments default to 0
else this->port = port;
}
bool Controller::getButton(unsigned int buttonNumber){
sf::Joystick::update();
if(sf::Joystick::isButtonPressed(this->port,buttonNumber)) return true;
else return false;
}
bool Controller::getButton(std::string buttonString){
for(int x=0;x<11;x++){
if(buttonName[x].compare(std::string(buttonString)) == 0){
sf::Joystick::update();
if(sf::Joystick::isButtonPressed(this->port,x)) return true;
}
}
return false;
}
float Controller::getThrust(){
if(cruiseControl){
return this->cruiseControlThrust;
}
else{
float thrust = 0;
sf::Joystick::update();
switch(mode){
case 0:
case 1:
thrust = sf::Joystick::getAxisPosition(this->port, sf::Joystick::V);
thrust-=(2*thrust); //thrust values need to be reversed because of how SFML is designed
if(thrust<20) return 0; //cannot have negative thrust
else return thrust/100;
case 2:
case 3:
thrust = sf::Joystick::getAxisPosition(this->port, sf::Joystick::Y);
thrust-=(2*thrust); //thrust values need to be reversed because of how SFML is designed
if(thrust<20) return 0; //cannot have negative thrust
else return thrust/100;
case 4:
case 5:
case 9:
thrust = sf::Joystick::getAxisPosition(this->port, sf::Joystick::R);
return (thrust+=100)/200; //thrust values from the trigger range [-100,100] because of how SFML is designed... need to be from [0,200] then [0,100]
case 6:
case 7:
case 8:
thrust = sf::Joystick::getAxisPosition(this->port, sf::Joystick::Z);
return (thrust+=100)/200; //thrust values from the trigger range [-100,100] because of how SFML is designed... need to be from [0,200] then [0,100]
default:
std::cout << "Error: thrust" << std::endl;
return 0;
}
}
}
float Controller::getPitch(){
float pitch = 0;
sf::Joystick::update();
switch(mode){
case 0:
case 1:
case 4:
case 5:
case 8:
pitch = sf::Joystick::getAxisPosition(this->port, sf::Joystick::Y);
if(pitch<20 && pitch>-20) return 0;
else return pitch/100;
case 2:
case 3:
case 6:
case 7:
case 9:
pitch = sf::Joystick::getAxisPosition(this->port, sf::Joystick::V);
if(pitch<20 && pitch>-20) return 0;
else return pitch/100;
default:
std::cout << "Error: pitch" << std::endl;
return 0;
}
}
float Controller::getRoll(){
float roll = 0;
sf::Joystick::update();
switch(mode){
case 0:
case 1:
case 4:
case 5:
case 8:
roll = sf::Joystick::getAxisPosition(this->port, sf::Joystick::X);
if(roll<20 && roll>-20) return 0;
else return roll/100;
case 2:
case 3:
case 6:
case 7:
case 9:
roll = sf::Joystick::getAxisPosition(this->port, sf::Joystick::U);
if(roll<20 && roll>-20) return 0;
else return roll/100;
default:
std::cout << "Error: roll" << std::endl;
return 0;
}
}
float Controller::getYaw(){
float yaw = 0;
sf::Joystick::update();
switch(mode){
case 0:
case 3:
case 4:
case 7:
case 8:
yaw = sf::Joystick::getAxisPosition(this->port, sf::Joystick::X);
if(yaw<20 && yaw>-20) return 0;
else return yaw/100;
case 1:
case 2:
case 5:
case 6:
case 9:
yaw = sf::Joystick::getAxisPosition(this->port, sf::Joystick::U);
if(yaw<20 && yaw>-20) return 0;
else return yaw/100;
default:
std::cout << "Error: yaw" << std::endl;
return 0;
}
}
float Controller::getMotor(){
return this->getThrust();
}
float Controller::getElevator(){
return this->getPitch();
}
float Controller::getAileron(){
return this->getRoll();
}
float Controller::getRudder(){
return this->getYaw();
}
float Controller::toggleLeftHandMode(){
sf::Joystick::update();
if(sf::Joystick::getAxisPosition(this->port, sf::Joystick::PovX)==-100){
this->setMode(8);
}
}
float Controller::toggleRightHandMode(){
sf::Joystick::update();
if(sf::Joystick::getAxisPosition(this->port, sf::Joystick::PovX)==100){
this->setMode(9);
}
}
float Controller::toggleCruiseControll(){
float prevCruiseControlCounter = this->cruiseControlCounter;
sf::Joystick::update();
switch(mode){
case 0:
case 1:
case 4:
case 5:
case 8:
if(sf::Joystick::isButtonPressed(this->port,9)){
this->cruiseControlCounter++;
}
else{
this->cruiseControlCounter = 0;
}
break;
case 2:
case 3:
case 6:
case 7:
case 9:
if(sf::Joystick::isButtonPressed(this->port,10)){
this->cruiseControlCounter++;
}
else{
this->cruiseControlCounter = 0;
}
break;
}
if(cruiseControlCounter==0 && prevCruiseControlCounter>0){
std::cout << "cruise control toggled" << std::endl;
this->cruiseControlThrust = this->getThrust();
cruiseControl = !cruiseControl;
}
}
void Controller::listenForToggles(){
this->toggleLeftHandMode();
this->toggleRightHandMode();
this->toggleCruiseControll();
}
float Controller::listButtons(){
std::cout << "";
for(int x=0;x<32;x++){
sf::Joystick::update();
if(sf::Joystick::isButtonPressed(0,x)){
std::cout << buttonName[x] << " ";
}
}
std::cout << std::endl;
}