Gamepad.h
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
27 #ifndef _GAMEPAD_H_
28 #define _GAMEPAD_H_
29 
35 #include <linux/joystick.h>
36 
37 #include <unistd.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <signal.h>
41 #include <string.h>
42 #include <fcntl.h>
43 #include <vector>
44 
45 #include <sigc++/functors/slot.h>
46 #include <sigc++/functors/ptr_fun.h>
47 #include <sigc++/functors/mem_fun.h>
48 #include <boost/shared_ptr.hpp>
49 
95 class Button
97 {
98  public:
100  {
101  value=0;
102  }
104  int value;
105 
107  sigc::slot<void, int> callback;
108 };
109 
110 typedef boost::shared_ptr<Button> ButtonPtr;
111 
117 class Gamepad
118 {
119  public:
121  typedef enum {AXIS,BUTTON} e_type;
122 
127  ret(0),
128  fd(0),
129  n_buttons(0),
130  n_axes(0)
131  {
132  }
133 
138  {
139  close(fd);
140  }
141 
142 
149  int startComm(const char*device)
150  {
151  fd = open(device, O_RDONLY | O_NONBLOCK);
152  if(fd<0)
153  {
154  sprintf(err,"Unable to open device: %s",device);
155  return -3;
156  }
157 
158  ret=ioctl(fd, JSIOCGAXES, &n_axes);
159  if(ret<0)
160  {
161  strcpy(err,"Unable to read number of axes");
162  return -3;
163  }
164 
165  ret=ioctl(fd, JSIOCGBUTTONS, &n_buttons);
166  if(ret<0)
167  {
168  strcpy(err,"Unable to read number of buttons");
169  return -3;
170  }
171 
172  ret=ioctl(fd, JSIOCGNAME(sizeof(name)), name);
173  if(ret<0)
174  strcpy(name,"Unknown");
175 
176  printf("\33[1m\33[36mGamepad\33[0m %s\n",name);
177 
178  for(int i=0;i<n_buttons;i++)
179  {
180  ButtonPtr button(new Button);
181  buttons_.push_back(button);
182  }
183 
184  for(int i=0;i<n_axes;i++)
185  {
186  ButtonPtr button(new Button);
187  axes_.push_back(button);
188  }
189 
190  return 0;
191  }
192 
198  int dispatch(bool debug=false)
199  {
200  struct js_event buffer[64];
201 
202  ret = read (fd, buffer, sizeof(struct js_event)*64);
203  if(ret<0)
204  return 0;
205 
206  for(int i=0; i<ret/(signed int)sizeof(struct js_event);i++)
207  {
208  if(buffer[i].type & JS_EVENT_BUTTON & ~JS_EVENT_INIT)
209  {
210  if(debug)printf("Button %d Value %d\n",buffer[i].number,buffer[i].value);
211  if(buttons_[buffer[i].number]->callback.empty())
212  continue;
213 
214  buttons_[buffer[i].number]->value = buffer[i].value;
215  buttons_[buffer[i].number]->callback(buttons_[buffer[i].number]->value);
216  }else if(buffer[i].type == JS_EVENT_AXIS)
217  {
218  if(debug)printf("Axis %d Value %d\n",buffer[i].number,buffer[i].value);
219  if(axes_[buffer[i].number]->callback.empty())
220  continue;
221 
222  axes_[buffer[i].number]->value = buffer[i].value;
223  axes_[buffer[i].number]->callback(axes_[buffer[i].number]->value);
224  }
225  }
226 
227  return 0;
228  }
229 
241  void plerr(int ret)
242  {
243  if(ret==-1)
244  {
245  printf("Error!! ");
246  printf("%s (raising SIGINT)\n",err);
247  raise(SIGINT);
248  }else if(ret==-2)
249  {
250  printf("Warning!! ");
251  printf("%s\n",err);
252  }else if(ret==-3)
253  {
254  printf("Error!! ");
255  printf("%s, ",err);fflush(stdout);
256  perror(NULL);
257  raise(SIGINT);
258  }
259  }
260 
261 
263  {
264  if(i>0 || i<buttons_.size())
265  return buttons_[i];
266  else
267  {
268  ButtonPtr empty;
269  return empty;
270  }
271  }
272 
273  ButtonPtr axes(uint i)
274  {
275  if(i>0 || i<axes_.size())
276  return axes_[i];
277  else
278  {
279  ButtonPtr empty;
280  return empty;
281  }
282  }
283 
284  private:
285 
287  std::vector<ButtonPtr> buttons_;
289  std::vector<ButtonPtr> axes_;
290 
292  int ret;
293 
295  int fd;
296 
299 
301  int n_axes;
302 
304  __u8 m_axes[ABS_CNT];
306  __u16 m_buttons[KEY_MAX - BTN_MISC + 1];
307 
309  char name[1024];
310 
312  char err[1024];
313 
315  char text[1024];
316 };
317 #endif
~Gamepad()
Class destructor.
Definition: Gamepad.h:137
__u8 m_axes[ABS_CNT]
Mapping variable.
Definition: Gamepad.h:304
int n_axes
Number of axes in the game pad.
Definition: Gamepad.h:301
ButtonPtr axes(uint i)
Definition: Gamepad.h:273
char name[1024]
Name of the game pad.
Definition: Gamepad.h:309
int dispatch(bool debug=false)
This function checks the gamepad status and calls the respective callback.
Definition: Gamepad.h:198
Button global structure.
Definition: Gamepad.h:96
int n_buttons
Number of buttons in the game pad.
Definition: Gamepad.h:298
boost::shared_ptr< Button > ButtonPtr
Definition: Gamepad.h:110
int startComm(const char *device)
Initialize comm with the gamepad.
Definition: Gamepad.h:149
int value
value of the button
Definition: Gamepad.h:104
void plerr(int ret)
Print local error function.
Definition: Gamepad.h:241
std::vector< ButtonPtr > axes_
Vector of axes, buttons and axes share the same data structure Button.
Definition: Gamepad.h:289
char text[1024]
Auxiliary string variable.
Definition: Gamepad.h:315
Gamepad()
Class constructor.
Definition: Gamepad.h:126
Button()
Definition: Gamepad.h:99
__u16 m_buttons[KEY_MAX-BTN_MISC+1]
Mapping variable.
Definition: Gamepad.h:306
Gamepad class.
Definition: Gamepad.h:117
int ret
Auxiliary error variable.
Definition: Gamepad.h:292
e_type
Type of input available on the game pad.
Definition: Gamepad.h:121
sigc::slot< void, int > callback
callback to be used when the button changes state
Definition: Gamepad.h:107
std::vector< ButtonPtr > buttons_
Vector of buttons.
Definition: Gamepad.h:287
char err[1024]
Error message.
Definition: Gamepad.h:312
ButtonPtr buttons(uint i)
Definition: Gamepad.h:262
int fd
File descriptor of the device.
Definition: Gamepad.h:295


atlascar_base
Author(s): Jorge Almeida, Sérgio Pinho, Miguel Oliveira, Pedro Salvado, Andre Oliveira and Pedro Pinheiro
autogenerated on Mon Mar 2 2015 01:31:23