Skip to content

Gamepad reader for joysticks in windows and linux (if joystick driver is supported)

License

Notifications You must be signed in to change notification settings

deempalme/gamepad_reader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gamepad Reader

Gamepad reader for joysticks in windows and linux (if joystick driver is supported). This is a gamepad reader, it is configured to use Logitech Racing wheel G920 but you can modify it and use different joysticks.

1.1 Steps to setup the code in Windows:

  1. Download the development SDL library for Windows (it should be called SDL2-devel-2.*-mingw.tar.gz): http://www.libsdl.org/download-2.0.php

  2. Open compressed file and copy the content of the folder:

    /x86_64-w64-mingw32/lib
    

    into (depends in the compiler path shown in Qt->tools->Options->Build & Run->Compilers):

    C:/Qt/Qt5.5.1/Tools/mingw492_32/lib
    
  3. Copy the folder:

    /x86_64-w64-mingw32/include/SDL2
    

    into (depends in the compiler path shown in Qt->tools->Options->Build & Run->Compilers):

    C:/Qt/Qt5.5.1/Tools/mingw492_32/include
    
  4. If you are using x86_64-w64-mingw32 copy the dll file:

    /x86_64-w64-mingw32/bin/SDL2.dll
    

    into the folder where your executable program is located.

  5. Copy the content of the folder:

    /i686-w64-mingw32/lib
    

    into (depends in the compiler path shown in Qt->tools->Options->Build & Run->Compilers):

    C:/Qt/Qt5.5.1/Tools/mingw492_32/i686-w64-mingw32/lib
    
  6. Copy the folder:

    /i686-w64-mingw32/include/SDL2
    

    into (depends in the compiler path shown in Qt->tools->Options->Build & Run->Compilers):

    C:/Qt/Qt5.5.1/Tools/mingw492_32/i686-w64-mingw32/include
    
  7. If you are using i686-w64-mingw32 copy the dll file:

    /i686-w64-mingw32/bin/SDL2.dll
    

    into the folder where your executable program is located.

  8. Compile the program.

  9. Copy the folder called control_maps of this repository into the folder where your executable program is located.

  10. You can now run your executable program.

NOTE: If you copied the wrong .dll file in the step 4 or 7 the program may experience a unexpected exit, try copying the opposite .dll file into the folder where your executable is located.

1.2 Steps to setup the code in Ubuntu:

NOTE: Drivers for Logitech Racing wheel G920 are not yet available or don't work at ubuntu. If you want to know if the Gamepad has been detected you need to run:

```
sudo apt-get install joystick
sudo apt-get install jstest-gtk
```

Open **jstest-gtk** and you should be able to see if the Gamepad is detected, if the **jstest-gtk** screen is blank then, no Gamepad was detected.

  1. Install the following packages before installing SLD:

    sudo apt-get install build-essential xorg-dev libudev-dev libgl1-mesa-dev \
                  libglu1-mesa-dev libasound2-dev libpulse-dev libopenal-dev libogg-dev \
                  libvorbis-dev libaudiofile-dev libpng12-dev libfreetype6-dev libusb-dev \
                  libdbus-1-dev zlib1g-dev libdirectfb-dev
    
  2. Download the source code for SDL library (it should be called SDL2-2.*.tar.gz or SDL2-2.*.zip): http://www.libsdl.org/download-2.0.php

  3. Uncompress the file, open the folder and run the following commands with ubuntu terminal set in that folder:

    ./configure
    make
    sudo make install
    make clean
    
  4. You can now delete the uncompressed folder to free memory.

  5. Don't forget to run:

    sudo ldconfig
    

    to update the necessary links and cache to the libraries.

  6. Do the steps 8 to 10 at the section 1.1

2 How to use GamepadReader class

There are two main member functions to use in GamepadReader class:

  1. Update gamepad data:

    void GamepadReader::read_gamepad(const int index = -1);

    Which reads and updates the data in the detected gamepads, you should call this function at least once in each frame or cycle so, when you get the joystick values the data will be updated.

    If index >= 0 then, it will only update the std::vector<GamepadData>'s element width index = index, the index must be inside this vector range.

    It is a public slot therefore, it could be called using Signals and Slots from Qt.

  2. Get all gamepads data:

    const std::vector<GamepadData> *const get_gamepad_data();

    Which returns the address of all the values of the detected joysticks.

  3. Get a specific element inside the GamepadData vector:

    const GamepadData *const get_gamepad_data(const int index);

    Which returns the address of the element with index = index from the vector: std::vector<GamepadData> gamepads_;


 ##### _Example of use_:
 ```cpp
 // Creates the object
 GamepadReader gamepad_;
 
 // Updates the joysticks data
 gamepad_.read_gamepad();
 
 // Get the clutch value from the first found Joystick
 double clutch = gamepad_.get_gamepad_data()->at(0).clutch;
 ```

2.1 GamepadData structure:

GamepadData is a structfrom c++, the structure is the following:

struct GamepadData{
  //buttons:
  bool A     = false;
  bool B     = false;
  bool X     = false;
  bool Y     = false;
  bool RSB   = false;
  bool LSB   = false;
  bool RB    = false;
  bool LB    = false;
  bool back  = false;
  bool start = false;
  bool guide = false;
  //directional pad:
  bool up    = false;
  bool down  = false;
  bool left  = false;
  bool right = false;
  //steering: (angle in radians)
  double steering = 0;
  //pedals: (0 -> 1 : 0 = not pressed, 1 = maximum pressure)
  double clutch   = 0;
  double brake    = 0;
  double gas      = 0;
};

3 Using CarMaker

If you want to use GamepadReader in CarMaker use the files inside the folder called CARMAKER.

  1. Do the setup steps.

  2. You will need to modify CMakeLists.txt and add SOURCES or HPP_FILES:

    #sources
    set(SOURCES
    #    CM_Main.cpp
    #    CM_Vehicle.cpp
    #    User.cpp
    #    app_tmp.cpp
    #    ADT/ADTMain.cpp
    #    ADT/CMUserOutput.cpp
        GamepadReader.cpp
    )
    
    ...
    
    set(HPP_FILES
    #  ADT/ADTMain.h
      GamepadReader.h
      definitions.h
    )
    
  3. Don't forget to modify the path to CarMaker and Boost directories:

    #carmaker
    #remember to change the carmake's directory
    set(CARMAKER_DIR C:/IPG/hil/win32-5.0.3)
    
    ...
    
    #boost
    #remember to change boost's directory
    set(BOOST_INC_DIR C:/boost_1_60_0)
    

3.1 Errors using SDL2 in CarMaker at Windows

CarMaker is a wonderful b**ch, sometimes it will throw an error related to SDL2 undefined references. This is because CarMaker could use a previous version of SDL2, to fix this you need to search for the file "SDL2.dll" inside the main directory of CarMaker; modify the name of the found file for "SDL2_old.dll" and copy the new SDL2.dll file (from step 4 or 7 at section 1.1) into the same directory.

REMEMBER THIS STEP; you may need to change the name of "SDL2_old.dll" back to "SDL2.dll" in the future for other compiled programs.

About

Gamepad reader for joysticks in windows and linux (if joystick driver is supported)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published