javaInterface: Joystick.java

File Joystick.java, 2.5 kB (added by goskab, 6 months ago)

Joystick access class

Line 
1 import java.io.*;
2 import java.util.ArrayList;
3
4 /**
5  * Class to access a linux joystick
6  * @author goskab
7  *
8  */
9 public class Joystick {
10         private ArrayList<Integer> axes;
11         private ArrayList<Boolean> buttons;
12        
13         private FileInputStream fd;
14        
15         /**
16          * opens the proper file, initializes things
17          * note that we don't support reading the number of
18          * axes/buttons right now, just assume something
19          * @param file
20          */
21         Joystick(String file) {
22                 this.axes = new ArrayList<Integer>();
23                 this.buttons = new ArrayList<Boolean>();
24                 try {
25                         fd = new FileInputStream(file);
26                 }
27                 catch (FileNotFoundException e) {
28                         System.out.println("Joystick not present.");
29                         fd = null;
30                         OswaldInterfaceTest oit = new OswaldInterfaceTest(axes);
31                 }
32                 EventUpdater up = new EventUpdater();
33                 up.start();
34         }
35        
36         /**
37          * reads an event and updates the axes/button values
38          * @return true if event was read, false if not
39          */
40         public boolean readEvent() throws IOException {
41                 byte[] bytes = new byte[8];
42                 int value;
43                 int type;
44                 int number;
45                 if (fd.read(bytes, 0, 8) != 8) //something bad happened, just ignore it
46                         return false;
47                 //translate the value bytes... this is basically magic
48                 value = (((bytes[5] + 128) << 8) + bytes[4] + 128) - ((1 << 15) + 128);
49                 type = bytes[6]; //type of event
50                 number = bytes[7]; //number associated with the event
51                 switch (type) {
52                         case 0x01: //button
53                                 while (buttons.size() - 1 < number)
54                                         buttons.add(false);
55                                 buttons.set(number, (value == 1));
56                         break;
57                         case 0x02: //axis
58                                 while (axes.size() - 1 < number)
59                                         axes.add(0);
60                                 axes.set(number, value);
61                         break;
62                         default:
63                                 return false;
64                 }
65                 return true;
66         }
67        
68         /**
69          * gets the state of a button
70          * @param number button number to return
71          * @return the state of the button
72          */
73         public synchronized boolean getButton(int number) {
74                 if (number < buttons.size())
75                         return buttons.get(number);
76                 return false;
77         }
78        
79         /**
80          * gets the value of an axis
81          * @param number the axis number
82          * @return the value of the axis
83          */
84         public synchronized int getAxis(int number) {
85                 if (number < axes.size())
86                         return axes.get(number);
87                 return 0;
88         }
89        
90         /**
91          * runs the thread that updates the button values and calls the
92          * listeners (if any)
93          * @author goskab
94          */
95         private class EventUpdater extends Thread {
96                 /**
97                  * awesome run method
98                  */
99                 public void run() {
100                         if (fd == null)
101                                 return;
102                         while(true) {
103                                 try {
104                                         readEvent();
105                                 }
106                                 catch (Exception e) {
107                                         return;
108                                 }
109                                 this.yield();
110                         }
111                 }
112                
113         }
114 }