Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Controller connection state not properly recognized  (Read 3370 times)
0 Members and 1 Guest are viewing this topic.
Offline GuiRitter

Junior Newbie





« Posted 2011-05-10 01:40:23 »

Hi all.

I'm developing a program to control a robot with a gamepad as a graduation task. It works well under a controlled enviroment, but I want to make the program be aware whether the controller is connected or not. The code is not running so well, and I'm not too good at debugging with NetBeans, so I want to know if you can help me with this. The only conclusion I could gather is that he keeps doing what he was doing before after I connected/disconnected the controller, but he will know if the controller is connected or not at the beginning of execution.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class Tela extends javax.swing.JFrame {
    public Tela() {
        // initialization
       addWindowListener( new WindowAdapter() {
            public void windowClosing(WindowEvent e){
                pollTimer.stop();
                System.exit(0);
            }
        });
        startPolling();
    }
    private void startPolling(){
        ActionListener pollPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent e){
                if(controle.checa() == 1){ // returns the number of Controller.Type.STICK or Controller.Type.GAMEPAD controllers connected
                   // interface update
               }else if(controle.checa() == 0){
                    // prints that there are no controllers
               }else{
                    // prints that there are too much controllers
               }
            }
        };
        pollTimer = new Timer(DELAY, pollPerformer);
        pollTimer.start();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> // NetBeans generated code for interface elements
   // </editor-fold>                        
   public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Tela().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify // NetBeans generated code for interface elements
   // End of variables declaration
   private Controle controle = new Controle(); // class wrapped around JInput
   private Timer pollTimer;
    private static final int DELAY = 20;
}

Offline Swot

Junior Newbie





« Reply #1 - Posted 2011-05-10 04:05:54 »

While I've only been here shortly, and not in depth, I think that JInput not detecting if a controller was unplugged or plugged was a recurring problem. So far as I can tell (someone can correct me if I'm wrong) this thread has a solution with the last post also being a solution which no one has expanded on though.
Offline endolf

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #2 - Posted 2011-05-10 08:07:36 »

Hi

Yup, it's a feature that people make a lot of noise about until I ask for a volunteer to implement it properly. As pointed out, there is a quick and dirty way to reset the controllers on windows.

Endolf

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GuiRitter

Junior Newbie





« Reply #3 - Posted 2011-05-15 21:08:33 »

While I've only been here shortly, and not in depth, I think that JInput not detecting if a controller was unplugged or plugged was a recurring problem. So far as I can tell (someone can correct me if I'm wrong) this thread has a solution with the last post also being a solution which no one has expanded on though.

Yes thanks a lot that works! I'm using it like that:
1  
2  
3  
private Controller cs[],ctrl;
cs = new DirectAndRawInputEnvironmentPlugin().getControllers();
// for() loop to assign the desired controller to ctrl


The problem is that it's too damn slow. I'll see what I can do to speed it up, either using another timer for the enviroment check or catching the "Failed to poll device" exception. By the way, is that an exception I can catch? Who throws it?
Offline macro86

Junior Newbie





« Reply #4 - Posted 2011-06-29 11:04:55 »

That doesn't work for me..
I'm on linux and if I use
1  
new DirectAndRawInputEnvironmentPlugin().getControllers();

I only get an empty Controller[]..sad
No hot plug for me Cheesy
Offline endolf

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #5 - Posted 2011-06-29 12:37:26 »

That doesn't work for me..
I'm on linux
You need
1  
new LinuxEnvironmentPlugin.getControllers()

Be warned though, you might end up with issues as the controller files are already open. You *could* loop through all your old controller objects, cast them to LinuxDevice and then call close on it so they can be reopened. At your own risk of course Smiley

Endolf

Offline macro86

Junior Newbie





« Reply #6 - Posted 2011-06-30 14:50:19 »

thanks a lot endolf  Wink
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (32 views)
2014-07-18 06:55:21

Zero Volt (28 views)
2014-07-17 23:47:54

danieldean (24 views)
2014-07-17 23:41:23

MustardPeter (25 views)
2014-07-16 23:30:00

Cero (40 views)
2014-07-16 00:42:17

Riven (42 views)
2014-07-14 18:02:53

OpenGLShaders (29 views)
2014-07-14 16:23:47

Riven (29 views)
2014-07-14 11:51:35

quew8 (27 views)
2014-07-13 13:57:52

SHC (63 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!