Skip to content

Commit

Permalink
led
Browse files Browse the repository at this point in the history
  • Loading branch information
briaguya-ai committed Jan 19, 2025
1 parent b6a0b39 commit 7d7a2cb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,85 +24,51 @@ std::shared_ptr<ControllerLEDMapping> LEDMappingFactory::CreateLEDMappingFromCon
}

if (mappingClass == "SDLLEDMapping") {
int32_t shipDeviceIndex =
CVarGetInteger(StringHelper::Sprintf("%s.ShipDeviceIndex", mappingCvarKey.c_str()).c_str(), -1);

if (shipDeviceIndex < 0) {
// something about this mapping is invalid
CVarClear(mappingCvarKey.c_str());
CVarSave();
return nullptr;
}

return std::make_shared<SDLLEDMapping>(portIndex, colorSource, savedColor);
}

return nullptr;
}

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

// todo: LED
// for (auto [lusIndex, indexMapping] :
// Context::GetInstance()->GetControlDeck()->GetDeviceIndexMappingManager()->GetAllDeviceIndexMappings()) {
// auto sdlIndexMapping = std::dynamic_pointer_cast<ShipDeviceIndexToSDLDeviceIndexMapping>(indexMapping);

// if (sdlIndexMapping == nullptr) {
// // this LUS index isn't mapped to an SDL index
// continue;
// }

// auto sdlIndex = sdlIndexMapping->GetSDLDeviceIndex();

// if (!SDL_IsGameController(sdlIndex)) {
// // this SDL device isn't a game controller
// continue;
// }

// auto controller = SDL_GameControllerOpen(sdlIndex);
// if (SDL_GameControllerHasLED(controller)) {
// sdlControllersWithLEDs[lusIndex] = SDL_GameControllerOpen(sdlIndex);
// } else {
// SDL_GameControllerClose(controller);
// }
// }

// for (auto [lusIndex, controller] : sdlControllersWithLEDs) {
// for (int32_t button = SDL_CONTROLLER_BUTTON_A; button < SDL_CONTROLLER_BUTTON_MAX; button++) {
// if (SDL_GameControllerGetButton(controller, static_cast<SDL_GameControllerButton>(button))) {
// mapping = std::make_shared<SDLLEDMapping>(portIndex, 0, Color_RGB8({ 0, 0, 0 }));
// break;
// }
// }

// if (mapping != nullptr) {
// break;
// }

// for (int32_t i = SDL_CONTROLLER_AXIS_LEFTX; i < SDL_CONTROLLER_AXIS_MAX; i++) {
// const auto axis = static_cast<SDL_GameControllerAxis>(i);
// const auto axisValue = SDL_GameControllerGetAxis(controller, axis) / 32767.0f;
// int32_t axisDirection = 0;
// if (axisValue < -0.7f) {
// axisDirection = NEGATIVE;
// } else if (axisValue > 0.7f) {
// axisDirection = POSITIVE;
// }
for (auto [lusIndex, controller] :
Context::GetInstance()->GetControlDeck()->GetConnectedPhysicalDeviceManager()->GetConnectedSDLGamepadsForPort(
portIndex)) {
if (!SDL_GameControllerHasLED(controller)) {
continue;
}

// if (axisDirection == 0) {
// continue;
// }
for (int32_t button = SDL_CONTROLLER_BUTTON_A; button < SDL_CONTROLLER_BUTTON_MAX; button++) {
if (SDL_GameControllerGetButton(controller, static_cast<SDL_GameControllerButton>(button))) {
mapping = std::make_shared<SDLLEDMapping>(portIndex, 0, Color_RGB8({ 0, 0, 0 }));
break;
}
}

// mapping = std::make_shared<SDLLEDMapping>(portIndex, 0, Color_RGB8({ 0, 0, 0 }));
// break;
// }
// }
if (mapping != nullptr) {
break;
}

// for (auto [i, controller] : sdlControllersWithLEDs) {
// SDL_GameControllerClose(controller);
// }
for (int32_t i = SDL_CONTROLLER_AXIS_LEFTX; i < SDL_CONTROLLER_AXIS_MAX; i++) {
const auto axis = static_cast<SDL_GameControllerAxis>(i);
const auto axisValue = SDL_GameControllerGetAxis(controller, axis) / 32767.0f;
int32_t axisDirection = 0;
if (axisValue < -0.7f) {
axisDirection = NEGATIVE;
} else if (axisValue > 0.7f) {
axisDirection = POSITIVE;
}

if (axisDirection == 0) {
continue;
}

mapping = std::make_shared<SDLLEDMapping>(portIndex, 0, Color_RGB8({ 0, 0, 0 }));
break;
}
}

return mapping;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,31 @@

#include "public/bridge/consolevariablebridge.h"
#include "utils/StringHelper.h"
#include "Context.h"

namespace Ship {
SDLLEDMapping::SDLLEDMapping(uint8_t portIndex, uint8_t colorSource, Color_RGB8 savedColor)
: ControllerLEDMapping(PhysicalDeviceType::SDLGamepad, portIndex, colorSource, savedColor) {
}

void SDLLEDMapping::SetLEDColor(Color_RGB8 color) {
// for (const auto& [instanceId, gamepad] :
// Context::GetInstance()->GetControlDeck()->GetConnectedPhysicalDeviceManager()->GetConnectedSDLGamepadsForPort(mPortIndex))
// { todo: LED
// }

// if (!ControllerLoaded()) {
// return;
// }

// if (!SDL_GameControllerHasLED(mController)) {
// return;
// }

// if (mColorSource == LED_COLOR_SOURCE_OFF) {
// color = { 0, 0, 0 };
// }

// if (mColorSource == LED_COLOR_SOURCE_SET) {
// color = mSavedColor;
// }

// SDL_JoystickSetLED(SDL_GameControllerGetJoystick(mController), color.r, color.g, color.b);
if (mColorSource == LED_COLOR_SOURCE_OFF) {
color = { 0, 0, 0 };
}

if (mColorSource == LED_COLOR_SOURCE_SET) {
color = mSavedColor;
}

for (const auto& [instanceId, gamepad] :
Context::GetInstance()->GetControlDeck()->GetConnectedPhysicalDeviceManager()->GetConnectedSDLGamepadsForPort(mPortIndex))
{
if (!SDL_GameControllerHasLED(gamepad)) {
continue;
}

SDL_JoystickSetLED(SDL_GameControllerGetJoystick(gamepad), color.r, color.g, color.b);
}
}

std::string SDLLEDMapping::GetLEDMappingId() {
Expand Down

0 comments on commit 7d7a2cb

Please sign in to comment.