Skip to content

Commit a1806e0

Browse files
committed
gyro
1 parent 4676b78 commit a1806e0

File tree

2 files changed

+73
-87
lines changed

2 files changed

+73
-87
lines changed

src/controller/controldevice/controller/mapping/factories/GyroMappingFactory.cpp

+35-59
Original file line numberDiff line numberDiff line change
@@ -43,70 +43,46 @@ std::shared_ptr<ControllerGyroMapping> GyroMappingFactory::CreateGyroMappingFrom
4343
}
4444

4545
std::shared_ptr<ControllerGyroMapping> GyroMappingFactory::CreateGyroMappingFromSDLInput(uint8_t portIndex) {
46-
std::unordered_map<PhysicalDeviceType, SDL_GameController*> sdlControllersWithGyro;
4746
std::shared_ptr<ControllerGyroMapping> mapping = nullptr;
4847

49-
// todo: gyro
50-
// for (auto [lusIndex, indexMapping] :
51-
// Context::GetInstance()->GetControlDeck()->GetDeviceIndexMappingManager()->GetAllDeviceIndexMappings()) {
52-
// auto sdlIndexMapping = std::dynamic_pointer_cast<ShipDeviceIndexToSDLDeviceIndexMapping>(indexMapping);
53-
54-
// if (sdlIndexMapping == nullptr) {
55-
// // this LUS index isn't mapped to an SDL index
56-
// continue;
57-
// }
58-
59-
// auto sdlIndex = sdlIndexMapping->GetSDLDeviceIndex();
60-
61-
// if (!SDL_IsGameController(sdlIndex)) {
62-
// // this SDL device isn't a game controller
63-
// continue;
64-
// }
65-
66-
// auto controller = SDL_GameControllerOpen(sdlIndex);
67-
// if (SDL_GameControllerHasSensor(controller, SDL_SENSOR_GYRO)) {
68-
// sdlControllersWithGyro[lusIndex] = SDL_GameControllerOpen(sdlIndex);
69-
// } else {
70-
// SDL_GameControllerClose(controller);
71-
// }
72-
// }
73-
74-
// for (auto [lusIndex, controller] : sdlControllersWithGyro) {
75-
// for (int32_t button = SDL_CONTROLLER_BUTTON_A; button < SDL_CONTROLLER_BUTTON_MAX; button++) {
76-
// if (SDL_GameControllerGetButton(controller, static_cast<SDL_GameControllerButton>(button))) {
77-
// mapping = std::make_shared<SDLGyroMapping>(portIndex, 1.0f, 0.0f, 0.0f, 0.0f);
78-
// mapping->Recalibrate();
79-
// break;
80-
// }
81-
// }
82-
83-
// if (mapping != nullptr) {
84-
// break;
85-
// }
86-
87-
// for (int32_t i = SDL_CONTROLLER_AXIS_LEFTX; i < SDL_CONTROLLER_AXIS_MAX; i++) {
88-
// const auto axis = static_cast<SDL_GameControllerAxis>(i);
89-
// const auto axisValue = SDL_GameControllerGetAxis(controller, axis) / 32767.0f;
90-
// int32_t axisDirection = 0;
91-
// if (axisValue < -0.7f) {
92-
// axisDirection = NEGATIVE;
93-
// } else if (axisValue > 0.7f) {
94-
// axisDirection = POSITIVE;
95-
// }
48+
for (auto [lusIndex, controller] :
49+
Context::GetInstance()->GetControlDeck()->GetConnectedPhysicalDeviceManager()->GetConnectedSDLGamepadsForPort(
50+
portIndex)) {
51+
if (!SDL_GameControllerHasSensor(controller, SDL_SENSOR_GYRO)) {
52+
continue;
53+
}
9654

97-
// if (axisDirection == 0) {
98-
// continue;
99-
// }
55+
for (int32_t button = SDL_CONTROLLER_BUTTON_A; button < SDL_CONTROLLER_BUTTON_MAX; button++) {
56+
if (SDL_GameControllerGetButton(controller, static_cast<SDL_GameControllerButton>(button))) {
57+
mapping = std::make_shared<SDLGyroMapping>(portIndex, 1.0f, 0.0f, 0.0f, 0.0f);
58+
mapping->Recalibrate();
59+
break;
60+
}
61+
}
10062

101-
// mapping = std::make_shared<SDLGyroMapping>(portIndex, 1.0f, 0.0f, 0.0f, 0.0f);
102-
// mapping->Recalibrate();
103-
// break;
104-
// }
105-
// }
63+
if (mapping != nullptr) {
64+
break;
65+
}
10666

107-
// for (auto [i, controller] : sdlControllersWithGyro) {
108-
// SDL_GameControllerClose(controller);
109-
// }
67+
for (int32_t i = SDL_CONTROLLER_AXIS_LEFTX; i < SDL_CONTROLLER_AXIS_MAX; i++) {
68+
const auto axis = static_cast<SDL_GameControllerAxis>(i);
69+
const auto axisValue = SDL_GameControllerGetAxis(controller, axis) / 32767.0f;
70+
int32_t axisDirection = 0;
71+
if (axisValue < -0.7f) {
72+
axisDirection = NEGATIVE;
73+
} else if (axisValue > 0.7f) {
74+
axisDirection = POSITIVE;
75+
}
76+
77+
if (axisDirection == 0) {
78+
continue;
79+
}
80+
81+
mapping = std::make_shared<SDLGyroMapping>(portIndex, 1.0f, 0.0f, 0.0f, 0.0f);
82+
mapping->Recalibrate();
83+
break;
84+
}
85+
}
11086

11187
return mapping;
11288
}

src/controller/controldevice/controller/mapping/sdl/SDLGyroMapping.cpp

+38-28
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,54 @@ void SDLGyroMapping::Recalibrate() {
1818
for (const auto& [instanceId, gamepad] :
1919
Context::GetInstance()->GetControlDeck()->GetConnectedPhysicalDeviceManager()->GetConnectedSDLGamepadsForPort(
2020
mPortIndex)) {
21-
// todo: gyro
21+
if (!SDL_GameControllerHasSensor(gamepad, SDL_SENSOR_GYRO)) {
22+
continue;
23+
}
24+
25+
// just use gyro on the first gyro supported device we find
26+
float gyroData[3];
27+
SDL_GameControllerSetSensorEnabled(gamepad, SDL_SENSOR_GYRO, SDL_TRUE);
28+
SDL_GameControllerGetSensorData(gamepad, SDL_SENSOR_GYRO, gyroData, 3);
29+
30+
mNeutralPitch = gyroData[0];
31+
mNeutralYaw = gyroData[1];
32+
mNeutralRoll = gyroData[2];
33+
return;
2234
}
2335

24-
// if (!ControllerLoaded()) {
25-
// mNeutralPitch = 0;
26-
// mNeutralYaw = 0;
27-
// mNeutralRoll = 0;
28-
// return;
29-
// }
30-
31-
// float gyroData[3];
32-
// SDL_GameControllerSetSensorEnabled(mController, SDL_SENSOR_GYRO, SDL_TRUE);
33-
// SDL_GameControllerGetSensorData(mController, SDL_SENSOR_GYRO, gyroData, 3);
34-
35-
// mNeutralPitch = gyroData[0];
36-
// mNeutralYaw = gyroData[1];
37-
// mNeutralRoll = gyroData[2];
36+
// if we didn't find a gyro device zero everything out
37+
mNeutralPitch = 0;
38+
mNeutralYaw = 0;
39+
mNeutralRoll = 0;
3840
}
3941

4042
void SDLGyroMapping::UpdatePad(float& x, float& y) {
43+
if (Context::GetInstance()->GetControlDeck()->GamepadGameInputBlocked()) {
44+
x = 0;
45+
y = 0;
46+
return;
47+
}
48+
4149
for (const auto& [instanceId, gamepad] :
4250
Context::GetInstance()->GetControlDeck()->GetConnectedPhysicalDeviceManager()->GetConnectedSDLGamepadsForPort(
4351
mPortIndex)) {
44-
// todo: gyro
52+
if (!SDL_GameControllerHasSensor(gamepad, SDL_SENSOR_GYRO)) {
53+
continue;
54+
}
55+
56+
// just use gyro on the first gyro supported device we find
57+
float gyroData[3];
58+
SDL_GameControllerSetSensorEnabled(gamepad, SDL_SENSOR_GYRO, SDL_TRUE);
59+
SDL_GameControllerGetSensorData(gamepad, SDL_SENSOR_GYRO, gyroData, 3);
60+
61+
x = (gyroData[0] - mNeutralPitch) * mSensitivity;
62+
y = (gyroData[1] - mNeutralYaw) * mSensitivity;
63+
return;
4564
}
4665

47-
// if (!ControllerLoaded() || Context::GetInstance()->GetControlDeck()->GamepadGameInputBlocked()) {
48-
// x = 0;
49-
// y = 0;
50-
// return;
51-
// }
52-
53-
// float gyroData[3];
54-
// SDL_GameControllerSetSensorEnabled(mController, SDL_SENSOR_GYRO, SDL_TRUE);
55-
// SDL_GameControllerGetSensorData(mController, SDL_SENSOR_GYRO, gyroData, 3);
56-
57-
// x = (gyroData[0] - mNeutralPitch) * mSensitivity;
58-
// y = (gyroData[1] - mNeutralYaw) * mSensitivity;
66+
// if we didn't find a gyro device zero everything out
67+
x = 0;
68+
y = 0;
5969
}
6070

6171
std::string SDLGyroMapping::GetGyroMappingId() {

0 commit comments

Comments
 (0)