class_gamepad.cpp
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 ***************************************************************************************************/
33 
35 {
36  ret=0;
37  fd=0;
38  n_buttons=0;
39  n_axes=0;
40 }
41 
43 {
44  close(fd);
45 }
46 
47 int class_gamepad::StartComm(const char*device)
48 {
49  fd = open(device, O_RDONLY | O_NONBLOCK);
50  if(fd<0)
51  {
52  sprintf(err,"Unable to open device: %s",device);
53  return -3;
54  }
55 
56  ret=ioctl(fd, JSIOCGAXES, &n_axes);
57  if(ret<0)
58  {
59  strcpy(err,"Unable to read number of axes");
60  return -3;
61  }
62 
63  ret=ioctl(fd, JSIOCGBUTTONS, &n_buttons);
64  if(ret<0)
65  {
66  strcpy(err,"Unable to read number of buttons");
67  return -3;
68  }
69 
70  ret=ioctl(fd, JSIOCGNAME(sizeof(name)), name);
71  if(ret<0)
72  strcpy(name,"Unknown");
73 
74  printf("\33[1m\33[36mGamepad\33[0m %s\n",name);
75 
76  buttons=(t_button*)malloc(n_buttons*sizeof(t_button));
77 
78  axes=(t_button*)malloc(n_axes*sizeof(t_button));
79 
80  for(int i=0;i<n_buttons;i++)
81  buttons[i].callback=NULL;
82 
83  for(int i=0;i<n_axes;i++)
84  axes[i].callback=NULL;
85 
86  return 0;
87 }
88 
90 {
91  ret=ioctl(fd,JSIOCGAXMAP,&m_axes);
92  if(ret<0)
93  {
94  strcpy(err,"Unable to read axes mapping");
95  return -3;
96  }
97 
98  ret=ioctl(fd,JSIOCGBTNMAP,&m_buttons);
99  if(ret<0)
100  {
101  strcpy(err,"Unable to read buttons mapping");
102  return -3;
103  }
104 
105  return 0;
106 }
107 
109 {
110  ret=ioctl(fd,JSIOCSAXMAP,&m_axes);
111  if(ret<0)
112  {
113  strcpy(err,"Unable to set axes mapping");
114  return -3;
115  }
116 
117  ret=ioctl(fd,JSIOCSBTNMAP,&m_buttons);
118  if(ret<0)
119  {
120  strcpy(err,"Unable to set buttons mapping");
121  return -3;
122  }
123 
124  return 0;
125 }
126 
127 int class_gamepad::RegisterCallback(e_type type,int id,void (*callback)(int value,void*data),void*data)
128 {
129  if(type==AXIS)
130  {
131  if(id>n_axes)
132  {
133  sprintf(err,"This device does not have this axis: Axis %d",id);
134  return -2;
135  }
136 
137  axes[id].callback=callback;
138  axes[id].userdata=data;
139 
140  }else if(type==BUTTON)
141  {
142  if(id>n_buttons)
143  {
144  sprintf(err,"This device does not have this Button: Button %d",id);
145  return -2;
146  }
147 
148  buttons[id].callback=callback;
149  buttons[id].userdata=data;
150  }
151 
152 
153  return 0;
154 }
155 
156 int class_gamepad::Dispatch(bool debug)
157 {
158  struct js_event buffer[64];
159 
160  ret = read (fd, buffer, sizeof(struct js_event)*64);
161  if(ret<0)
162  return 0;
163 
164  for(int i=0; i<ret/(signed int)sizeof(struct js_event);i++)
165  {
166  if(buffer[i].type & JS_EVENT_BUTTON & ~JS_EVENT_INIT)
167  {
168  if(debug)printf("Button %d Value %d\n",buffer[i].number,buffer[i].value);
169  if(buttons[buffer[i].number].callback==NULL)
170  continue;
171 
172  buttons[buffer[i].number].value = buffer[i].value;
173  buttons[buffer[i].number].callback(buttons[buffer[i].number].value,buttons[buffer[i].number].userdata);
174  }else if(buffer[i].type == JS_EVENT_AXIS)
175  {
176  if(debug)printf("Axis %d Value %d\n",buffer[i].number,buffer[i].value);
177  if(axes[buffer[i].number].callback==NULL)
178  continue;
179 
180  axes[buffer[i].number].value = buffer[i].value;
181  axes[buffer[i].number].callback(axes[buffer[i].number].value,axes[buffer[i].number].userdata);
182  }
183  }
184 
185  return 0;
186 }
187 
188 void class_gamepad::plerr(int ret)
189 {
190  if(ret==-1)
191  {
192  printf("Error!! ");
193  printf("%s (raising SIGINT)\n",err);
194  raise(SIGINT);
195  }else if(ret==-2)
196  {
197  printf("Warning!! ");
198  printf("%s\n",err);
199  }else if(ret==-3)
200  {
201  printf("Error!! ");
202  printf("%s, ",err);fflush(stdout);
203  perror(NULL);
204  raise(SIGINT);
205  }
206 }
Gamepad communication class declaration generic code.
t_button * axes
void plerr(int ret)
Print local error function This function prints the error present in the err variable (this is a priv...
int Dispatch(bool debug=false)
This function checks the gamepad status and calls the respective callback.
~class_gamepad()
Class destructor.
t_button * buttons
class_gamepad()
Class constructor.
int GetButtonMapping(void)
Get the button mapping from the device.
void(* callback)(int value, void *data)
Definition: class_gamepad.h:97
int SetButtonMapping(void)
Set the button mapping to the device.
char err[1024]
int RegisterCallback(e_type type, int id, void(*callback)(int value, void *data), void *data)
Register a callback for a specific button or axis.
__u8 m_axes[ABS_CNT]
char name[1024]
int StartComm(const char *device)
Initialize comm with the gamepad.
__u16 m_buttons[KEY_MAX-BTN_MISC+1]


atlasmv_base
Author(s): David Gameiro, Jorge Almeida
autogenerated on Mon Mar 2 2015 01:31:28