Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add class 'GamepadDevice'. For normalized asignment of axis and button and Hot-Plug. #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ else()
# standalone project
project(jetson-utils)

# -std=gnu++11
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wno-write-strings")
# -std=gnu++14
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wno-write-strings")

# setup CUDA
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cuda")
Expand Down Expand Up @@ -71,7 +71,7 @@ file(GLOB jetsonUtilitySources *.cpp camera/*.cpp codec/*.cpp cuda/*.cu cuda/*.c
file(GLOB jetsonUtilityIncludes *.h *.hpp camera/*.h codec/*.h cuda/*.h cuda/*.cuh display/*.h image/*.h image/*.inl input/*.h network/*.h threads/*.h threads/*.inl video/*.h)

cuda_add_library(jetson-utils SHARED ${jetsonUtilitySources})
target_link_libraries(jetson-utils GL GLU GLEW gstreamer-1.0 gstapp-1.0 gstpbutils-1.0 ${CUDA_nppicc_LIBRARY})
target_link_libraries(jetson-utils GL GLU GLEW gstreamer-1.0 gstapp-1.0 gstpbutils-1.0 SDL2 ${CUDA_nppicc_LIBRARY})


# transfer all headers to the include directory
Expand Down
124 changes: 124 additions & 0 deletions input/devGamepad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright (c) 2021, edgecraft. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

#include "devGamepad.h"
// #include "devInput.h"

#include "logging.h"


// Open 1st device.
void GamepadDevice::Open1stDevice()
{
auto num = SDL_NumJoysticks();
LogInfo("Num of Joystick: %d\n", num);
Gamepad = NULL;
event = {};
axis_motion = false;
button_down = false;
button_up = false;

for (int i = 0; i < num; i++) {
if (SDL_IsGameController(i)) {
Gamepad = SDL_GameControllerOpen(i);
if (Gamepad == NULL) {
LogError("Could not open gamecontroller %i: %s\n", i, SDL_GetError());
}

SDL_GameControllerEventState(SDL_ENABLE);
break; // first gamepad.
}
}
}

// constructor
GamepadDevice::GamepadDevice()
{
Open1stDevice();
}


// destructor
GamepadDevice::~GamepadDevice()
{
SDL_GameControllerClose(Gamepad);
}


// Create
std::unique_ptr<GamepadDevice> GamepadDevice::Create( const char* name )
{
auto gpad = std::make_unique<GamepadDevice>();
printf("======== Gamepad(%s) ========\n",
SDL_GameControllerGetAttached(gpad->Gamepad) ? "Attached" : "***** Removed *****");

auto name_str = SDL_GameControllerName(gpad->Gamepad);
auto map_str = SDL_GameControllerMapping(gpad->Gamepad);

if (name_str == NULL || map_str == NULL) {
LogError("gamepad -- can't opened device\n");
// return std::move(nullptr);
} else {
LogSuccess("gamepad -- opened device %s:%s\n", name_str, map_str);

// SDL_free(const_cast<char *>(name_str));
// SDL_free(const_cast<char *>(map_str));
}

return std::move(gpad);
}


// Poll
bool GamepadDevice::Poll( uint32_t timeout )
{
axis_motion = false;
button_down = false;
button_up = false;

while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_CONTROLLERAXISMOTION:
axis_motion = true;
break;
case SDL_CONTROLLERBUTTONDOWN:
button_down = true;
break;
case SDL_CONTROLLERBUTTONUP:
button_up = true;
break;
case SDL_CONTROLLERDEVICEADDED:
LogInfo("*** Gamepad Attached ***\n");
Open1stDevice();
break;
case SDL_CONTROLLERDEVICEREMOVED:
LogInfo("*** Gamepad Removed ***\n");
// SDL_GameControllerClose(Gamepad);
break;
default:
;
}
// LogInfo("[GamePad]: Event: %d\n", event.type);
}

return true;
}
112 changes: 112 additions & 0 deletions input/devGamepad.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright (c) 2021, edgecraft. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

#ifndef __DEV_GAMEPAD_H__
#define __DEV_GAMEPAD_H__

#include <SDL2/SDL.h>

#include <iostream>
#include <memory>
#include <vector>
#include <string>

/**
* Gamepad device
* @ingroup input
*/
class GamepadDevice
{
public:
/**
* Create device
*/
static std::unique_ptr<GamepadDevice> Create( const char* device="hogehoge USB Gamepad" );

// constructor
GamepadDevice();

/**
* Destructor
*/
virtual ~GamepadDevice();

/**
* Poll the device for updates
*/
bool Poll( uint32_t timeout=0 );

// Open 1st device.
void Open1stDevice();

// Is Gamepad Attached.
bool IsAttached() const { return SDL_GameControllerGetAttached(Gamepad); }

// Get Axis.
int16_t GetAxis(SDL_GameControllerAxis axis) const {
return SDL_GameControllerGetAxis(Gamepad, axis);
}
int16_t GetAxis_Left_X() const { return GetAxis(SDL_CONTROLLER_AXIS_LEFTX); }
int16_t GetAxis_Left_Y() const { return GetAxis(SDL_CONTROLLER_AXIS_LEFTY); }
int16_t GetAxis_Right_X() const { return GetAxis(SDL_CONTROLLER_AXIS_RIGHTX); }
int16_t GetAxis_Right_Y() const { return GetAxis(SDL_CONTROLLER_AXIS_RIGHTY); }
int16_t GetAxis_Trigger_L() const { return GetAxis(SDL_CONTROLLER_AXIS_TRIGGERLEFT); }
int16_t GetAxis_Trigger_R() const { return GetAxis(SDL_CONTROLLER_AXIS_TRIGGERRIGHT); }

// Is Axis Motion.
bool IsAxisMotion() const { return axis_motion; }

// Get Button.
uint8_t GetButton(SDL_GameControllerButton button) const {
return SDL_GameControllerGetButton(Gamepad, button);
}
uint8_t GetButton_A() const { return GetButton(SDL_CONTROLLER_BUTTON_A); }
uint8_t GetButton_B() const { return GetButton(SDL_CONTROLLER_BUTTON_B); }
uint8_t GetButton_X() const { return GetButton(SDL_CONTROLLER_BUTTON_X); }
uint8_t GetButton_Y() const { return GetButton(SDL_CONTROLLER_BUTTON_Y); }
uint8_t GetButton_Back() const { return GetButton(SDL_CONTROLLER_BUTTON_BACK); }
uint8_t GetButton_Guide() const { return GetButton(SDL_CONTROLLER_BUTTON_GUIDE); }
uint8_t GetButton_Start() const { return GetButton(SDL_CONTROLLER_BUTTON_START); }
uint8_t GetButton_Stick_L() const { return GetButton(SDL_CONTROLLER_BUTTON_LEFTSTICK); }
uint8_t GetButton_Stick_R() const { return GetButton(SDL_CONTROLLER_BUTTON_RIGHTSTICK); }
uint8_t GetButton_Shoulder_L() const { return GetButton(SDL_CONTROLLER_BUTTON_LEFTSHOULDER); }
uint8_t GetButton_Shoulder_R() const { return GetButton(SDL_CONTROLLER_BUTTON_RIGHTSHOULDER); }
uint8_t GetButton_Dpad_U() const { return GetButton(SDL_CONTROLLER_BUTTON_DPAD_UP); }
uint8_t GetButton_Dpad_D() const { return GetButton(SDL_CONTROLLER_BUTTON_DPAD_DOWN); }
uint8_t GetButton_Dpad_L() const { return GetButton(SDL_CONTROLLER_BUTTON_DPAD_LEFT); }
uint8_t GetButton_Dpad_R() const { return GetButton(SDL_CONTROLLER_BUTTON_DPAD_RIGHT); }

// Is Button Down/Up.
bool IsButtonDown() const { return button_down; }
bool IsButtonUp() const { return button_up; }


protected:
SDL_GameController *Gamepad;

SDL_Event event;
bool axis_motion;
bool button_down;
bool button_up;
};

#endif