aboutsummaryrefslogtreecommitdiff
path: root/ifeel.c
blob: 3c3ca9cc77f83f5296510e87a07e2ff5356f0f5f (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* 
 * ifeel mouse object for Miller Puckette's Pure Data
 * copyright 2003 Hans-Christoph Steiner <hans@eds.org>

 * This program is free software; you can redistribute it and/or               
 * modify it under the terms of the GNU General Public License                 
 * as published by the Free Software Foundation; either version 2              
 * of the License, or (at your option) any later version.                      
 * This program is distributed in the hope that it will be useful,             
 * but WITHOUT ANY WARRANTY; without even the implied warranty of              
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               
 * GNU General Public License for more details.                                
 * You should have received a copy of the GNU General Public License           
 * along with this program; if not, write to the Free Software                 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 *
 *
 * based on ifeel_send.c from the linux iFeel driver:
 * http://sourceforge.net/projects/tactile
 *
 * there is a difference in the naming schemes of the ifeel driver and this 
 * object.  The ifeel driver uses strength, delay, and count.  This object 
 * uses strength, interval, and count.
 *
 * strength       - the strength of the pulse (I am searching for a better word)
 * delay/interval - the interval in between each pulse
 * count          - the total number of pulses to do
 */

#include "m_pd.h"
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <time.h>

#ifndef NT
#include <sys/ioctl.h>
#endif

#include "ifeel.h"


#define DEBUG(x)
/*#define DEBUG(x) x */


#define IFEEL_DEVICE    "/dev/input/ifeel0"

static t_class *ifeel_class;

typedef struct _ifeel 
{
	t_object x_obj;
	int x_fd;
	struct ifeel_command x_ifeel_command;
} t_ifeel;



/******************************************************************************
support functions
******************************************************************************/

void ifeel_playcommand(t_ifeel *x) 
{
/*   const struct timespec *requested_time; */
/*   struct timespec *remaining; */
	
#ifdef __linux__
	if (ioctl(x->x_fd, USB_IFEEL_BUZZ_IOCTL, &x->x_ifeel_command) < 0) 
	{
		post("x->x_fd: %d",x->x_fd);
		post("strength: %d   interval: %d   count: %d",
			  x->x_ifeel_command.strength,x->x_ifeel_command.delay,x->x_ifeel_command.count);
		post("ERROR %s", strerror(errno));
		close(x->x_fd);  
	}
#endif  /* __linux__ */
	
	DEBUG(
		post("strength: %d   interval: %d   count: %d",
			  x->x_ifeel_command.strength,x->x_ifeel_command.delay,x->x_ifeel_command.count);
		post(""););
}


/******************************************************************************
input/control functions
******************************************************************************/
void ifeel_start(t_ifeel *x) {
  DEBUG(post("ifeel_start");)

  /* 
   * since ifeel_stop sets everything to zero, we need to
   * read the inlets again to get current values 
   */
  
  ifeel_playcommand(x);
}

void ifeel_stop(t_ifeel *x) 
{
	DEBUG(post("ifeel_stop"););
	struct ifeel_command temp_ifeel_command;

/* store previous command for restoring after stop  */
	temp_ifeel_command.strength = x->x_ifeel_command.strength;
	temp_ifeel_command.delay = x->x_ifeel_command.delay;
	temp_ifeel_command.count = x->x_ifeel_command.count;

	/* 
	 * there is no 'stop' ioctl, so set everything to zero 
	 * to achieve the same effect                          
	 */
	x->x_ifeel_command.strength = 0;
	x->x_ifeel_command.delay = 0;
	x->x_ifeel_command.count = 0;
	
	ifeel_playcommand(x);
	
	/* restore previous command so the start msg will work */
	x->x_ifeel_command.strength = temp_ifeel_command.strength;
	x->x_ifeel_command.delay = temp_ifeel_command.delay;
	x->x_ifeel_command.count = temp_ifeel_command.count;
}

void ifeel_strength(t_ifeel *x, t_floatarg strength) 
{
	DEBUG(post("ifeel_strength"););
	
/* 
 * make sure its in the proper range 
 * this object takes floats 0-1
 * the ifeel driver takes ints 0-255
 */
  strength = strength * 255;
  strength = (strength > 255 ? 255 : strength);
  strength = (strength < 0 ? 0 : strength);

  x->x_ifeel_command.strength  = (unsigned int)strength;
}

void ifeel_interval(t_ifeel *x, t_floatarg interval) 
{
	DEBUG(post("ifeel_interval"););
	
	interval = (interval < 0 ? 0 : interval);
	
	x->x_ifeel_command.delay  = (unsigned int)interval;
}

void ifeel_count(t_ifeel *x, t_floatarg count ) 
{
	DEBUG(post("ifeel_count"););
	
	count = (count < 0 ? 0 : count);  
	
	x->x_ifeel_command.count  = (unsigned int)count;
}

void ifeel_command(t_ifeel *x, t_floatarg interval, t_floatarg count, t_floatarg strength) 
{
	DEBUG(post("ifeel_command"););
	
	ifeel_strength(x,strength);
	ifeel_interval(x,interval);
	ifeel_count(x,count);
	
	ifeel_playcommand(x);
}

static int ifeel_open(t_ifeel *x) 
{
	return 1;
}

/******************************************************************************
  init/free functions
******************************************************************************/

void ifeel_free(t_ifeel *x)
{
	DEBUG(post("ifeel_free"););
	
   /* stop effect */
	ifeel_stop(x);
	
	/* close device */
	close(x->x_fd);
}

void *ifeel_new(t_symbol *device, t_floatarg strength, t_floatarg interval, t_floatarg count) {
	DEBUG(post("ifeel_new"););
	
	t_ifeel *x = (t_ifeel *)pd_new(ifeel_class);
  
	post("iFeel mouse, by Hans-Christoph Steiner <hans@eds.org>");
	post("");
	post ("WARNING * WARNING * WARNING * WARNING * WARNING * WARNING * WARNING");
	post ("This object is under development!  The interface could change at anytime!");
	post ("As I write cross-platform versions, the interface might have to change.");
	post ("WARNING * WARNING * WARNING * WARNING * WARNING * WARNING * WARNING");
	post("");
#ifndef __linux__
	post("    !! WARNING !! WARNING !! WARNING !! WARNING !! WARNING !! WARNING !!");
	post("     This is a dummy, since this object only works with a Linux kernel!");
	post("    !! WARNING !! WARNING !! WARNING !! WARNING !! WARNING !! WARNING !!");
#endif
  
  /* 
   * init to zero so I can use the ifeel_* methods to set the 
   * struct with the argument values
   */
	x->x_ifeel_command.strength = 0;
	x->x_ifeel_command.delay = 0;
	x->x_ifeel_command.count = 0;
	
	inlet_new(&x->x_obj,
				 &x->x_obj.ob_pd,
				 gensym("float"),
				 gensym("interval"));
	inlet_new(&x->x_obj,
				 &x->x_obj.ob_pd,
				 gensym("float"),
				 gensym("count"));
	inlet_new(&x->x_obj,
				 &x->x_obj.ob_pd,
				 gensym("float"),
				 gensym("strength"));
	
	if (device != &s_) 
	{
		post("Using %s",device->s_name);
		
		/* x->x_fd = open(IFEEL_DEVICE, O_RDWR); */
		if ((x->x_fd = open((char *) device->s_name, O_RDWR | O_NONBLOCK, 0)) <= 0) 
		{
			printf("ERROR %s\n", strerror(errno)); 
			return 0;
		}
		
/*     ifeel_strength(x,strength); */
/*     ifeel_interval(x,interval); */
/*     ifeel_count(x,count); */
	}
	
	else 
	{
		post("ifeel: You need to set an ifeel device (i.e /dev/input/ifeel0)");
	}
	
  return (void*)x;
}

void ifeel_setup(void)
{
	DEBUG(post("ifeel_setup"););
	
	ifeel_class = class_new(gensym("ifeel"),
									(t_newmethod)ifeel_new,
									(t_method)ifeel_free,
									sizeof(t_ifeel),
									CLASS_DEFAULT,
									A_DEFSYMBOL,
									A_DEFFLOAT,
									A_DEFFLOAT,
									A_DEFFLOAT,
									0);
	
	class_addbang(ifeel_class,ifeel_start);
	
	class_addmethod(ifeel_class, (t_method)ifeel_start,gensym("start"),0);
	class_addmethod(ifeel_class, (t_method)ifeel_stop,gensym("stop"),0);
	
	class_addmethod(ifeel_class, (t_method)ifeel_command,gensym("command"), 
						 A_DEFFLOAT,A_DEFFLOAT,A_DEFFLOAT,0);  
	
	class_addmethod(ifeel_class, (t_method)ifeel_strength,gensym("strength"),A_DEFFLOAT,0);  
	class_addmethod(ifeel_class, (t_method)ifeel_interval,gensym("interval"),A_DEFFLOAT,0);
	class_addmethod(ifeel_class, (t_method)ifeel_count,gensym("count"),A_DEFFLOAT,0);
}