Skip to content

Commit 7d7a2cb

Browse files
committed
led
1 parent b6a0b39 commit 7d7a2cb

File tree

2 files changed

+51
-89
lines changed

2 files changed

+51
-89
lines changed

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

+33-67
Original file line numberDiff line numberDiff line change
@@ -24,85 +24,51 @@ std::shared_ptr<ControllerLEDMapping> LEDMappingFactory::CreateLEDMappingFromCon
2424
}
2525

2626
if (mappingClass == "SDLLEDMapping") {
27-
int32_t shipDeviceIndex =
28-
CVarGetInteger(StringHelper::Sprintf("%s.ShipDeviceIndex", mappingCvarKey.c_str()).c_str(), -1);
29-
30-
if (shipDeviceIndex < 0) {
31-
// something about this mapping is invalid
32-
CVarClear(mappingCvarKey.c_str());
33-
CVarSave();
34-
return nullptr;
35-
}
36-
3727
return std::make_shared<SDLLEDMapping>(portIndex, colorSource, savedColor);
3828
}
3929

4030
return nullptr;
4131
}
4232

4333
std::shared_ptr<ControllerLEDMapping> LEDMappingFactory::CreateLEDMappingFromSDLInput(uint8_t portIndex) {
44-
std::unordered_map<PhysicalDeviceType, SDL_GameController*> sdlControllersWithLEDs;
4534
std::shared_ptr<ControllerLEDMapping> mapping = nullptr;
4635

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

94-
// if (axisDirection == 0) {
95-
// continue;
96-
// }
43+
for (int32_t button = SDL_CONTROLLER_BUTTON_A; button < SDL_CONTROLLER_BUTTON_MAX; button++) {
44+
if (SDL_GameControllerGetButton(controller, static_cast<SDL_GameControllerButton>(button))) {
45+
mapping = std::make_shared<SDLLEDMapping>(portIndex, 0, Color_RGB8({ 0, 0, 0 }));
46+
break;
47+
}
48+
}
9749

98-
// mapping = std::make_shared<SDLLEDMapping>(portIndex, 0, Color_RGB8({ 0, 0, 0 }));
99-
// break;
100-
// }
101-
// }
50+
if (mapping != nullptr) {
51+
break;
52+
}
10253

103-
// for (auto [i, controller] : sdlControllersWithLEDs) {
104-
// SDL_GameControllerClose(controller);
105-
// }
54+
for (int32_t i = SDL_CONTROLLER_AXIS_LEFTX; i < SDL_CONTROLLER_AXIS_MAX; i++) {
55+
const auto axis = static_cast<SDL_GameControllerAxis>(i);
56+
const auto axisValue = SDL_GameControllerGetAxis(controller, axis) / 32767.0f;
57+
int32_t axisDirection = 0;
58+
if (axisValue < -0.7f) {
59+
axisDirection = NEGATIVE;
60+
} else if (axisValue > 0.7f) {
61+
axisDirection = POSITIVE;
62+
}
63+
64+
if (axisDirection == 0) {
65+
continue;
66+
}
67+
68+
mapping = std::make_shared<SDLLEDMapping>(portIndex, 0, Color_RGB8({ 0, 0, 0 }));
69+
break;
70+
}
71+
}
10672

10773
return mapping;
10874
}

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

+18-22
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,31 @@
22

33
#include "public/bridge/consolevariablebridge.h"
44
#include "utils/StringHelper.h"
5+
#include "Context.h"
56

67
namespace Ship {
78
SDLLEDMapping::SDLLEDMapping(uint8_t portIndex, uint8_t colorSource, Color_RGB8 savedColor)
89
: ControllerLEDMapping(PhysicalDeviceType::SDLGamepad, portIndex, colorSource, savedColor) {
910
}
1011

1112
void SDLLEDMapping::SetLEDColor(Color_RGB8 color) {
12-
// for (const auto& [instanceId, gamepad] :
13-
// Context::GetInstance()->GetControlDeck()->GetConnectedPhysicalDeviceManager()->GetConnectedSDLGamepadsForPort(mPortIndex))
14-
// { todo: LED
15-
// }
16-
17-
// if (!ControllerLoaded()) {
18-
// return;
19-
// }
20-
21-
// if (!SDL_GameControllerHasLED(mController)) {
22-
// return;
23-
// }
24-
25-
// if (mColorSource == LED_COLOR_SOURCE_OFF) {
26-
// color = { 0, 0, 0 };
27-
// }
28-
29-
// if (mColorSource == LED_COLOR_SOURCE_SET) {
30-
// color = mSavedColor;
31-
// }
32-
33-
// SDL_JoystickSetLED(SDL_GameControllerGetJoystick(mController), color.r, color.g, color.b);
13+
if (mColorSource == LED_COLOR_SOURCE_OFF) {
14+
color = { 0, 0, 0 };
15+
}
16+
17+
if (mColorSource == LED_COLOR_SOURCE_SET) {
18+
color = mSavedColor;
19+
}
20+
21+
for (const auto& [instanceId, gamepad] :
22+
Context::GetInstance()->GetControlDeck()->GetConnectedPhysicalDeviceManager()->GetConnectedSDLGamepadsForPort(mPortIndex))
23+
{
24+
if (!SDL_GameControllerHasLED(gamepad)) {
25+
continue;
26+
}
27+
28+
SDL_JoystickSetLED(SDL_GameControllerGetJoystick(gamepad), color.r, color.g, color.b);
29+
}
3430
}
3531

3632
std::string SDLLEDMapping::GetLEDMappingId() {

0 commit comments

Comments
 (0)