williamahartman / Jamepad

A better way to use gamepads in Java
Other
111 stars 33 forks source link

No way to get power status from ControllerIndex #31

Open JS-E opened 4 years ago

JS-E commented 4 years ago

Hey,

Saw the update which looks awesome with the new power and the sdl wrapper, unlocking a bunch of new things.

I can't seem to get the power status from a ControllerIndex? Am i doing something wrong?

electronstudio commented 4 years ago

ControllerIndex is a Jamepad API. Jamepad doesn't expose power level, the new features are in sdl2gdx.

There are 3 sdl2gdx methods that could be used:

  1. Easiest high level API: https://electronstudio.github.io/sdl2gdx/uk/co/electronstudio/sdl2gdx/SDL2Controller.html#getPowerLevel--

  2. https://electronstudio.github.io/sdl2gdx/org/libsdl/SDL_Joystick.html#currentPowerLevel--

  3. Lowest level: https://electronstudio.github.io/sdl2gdx/org/libsdl/SDL.html#SDL_JoystickCurrentPowerLevel-long-

electronstudio commented 4 years ago

Here's a program that uses method 1:

import com.badlogic.gdx.controllers.Controller;
import uk.co.electronstudio.sdl2gdx.SDL2Controller;
import uk.co.electronstudio.sdl2gdx.SDL2ControllerManager;

public class SDL2Test {
    public static void main(String[] arg){
        SDL2ControllerManager controllerManager = new SDL2ControllerManager();
        for (Controller controller : controllerManager.getControllers()) {
            SDL2Controller c = (SDL2Controller) controller;
            System.out.println(c.getName() + "POWERLEVEL: " +c.getPowerLevel());
        }
    }
}
electronstudio commented 4 years ago

Using method 2:

import org.libsdl.SDL;
import org.libsdl.SDL_Joystick;

public class SDL2Test {
    public static void main(String[] arg){
        SDL.SDL_Init( SDL.SDL_INIT_JOYSTICK );
        for(int i=0; i< SDL.SDL_NumJoysticks(); i++) {
            SDL_Joystick j = SDL_Joystick.JoystickOpen(i);
            System.out.println(j.name() + " POWERLEVEL: "+j.currentPowerLevel());
        }
    }
}
JS-E commented 4 years ago

Appreciate the reply, i'll get that stuffed in :D Awesome.

On another note, is there anyway to get the battery voltage? I know this hasn't been exposed at all, but does the sdl2gdx library have that potential ability? The reason i ask is because i'd like to build a little test that tracks voltage over time to see potential battery problems.

electronstudio commented 4 years ago

SDL doesn't get battery voltage. I would be very surprised if XInput or DirectInput did either. You'd probably be looking at writing a device-specific USB driver.

electronstudio commented 4 years ago

xinput has https://docs.microsoft.com/en-us/windows/win32/api/xinput/ns-xinput-xinput_battery_information but that's the same data that sdl2gdx exposes.