comparison test/testhaptic.c @ 2636:57ac1594164e gsoc2008_force_feedback

Added testhaptic to test haptic devices.
author Edgar Simo <bobbens@gmail.com>
date Wed, 06 Aug 2008 17:14:54 +0000
parents
children
comparison
equal deleted inserted replaced
2635:318e67011ad9 2636:57ac1594164e
1 /*
2 Copyright (c) 2008, Edgar Simo Serra
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9 * Neither the name of the Simple Directmedia Layer (SDL) nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 */
13
14 /*
15 * includes
16 */
17 #include "SDL.h"
18 #include "SDL_haptic.h"
19
20 #include <stdio.h> /* printf */
21 #include <string.h> /* strstr */
22
23
24
25 static SDL_Haptic *haptic;
26
27
28 /*
29 * prototypes
30 */
31 static void abort_execution (void);
32 static void HapticPrintSupported( SDL_Haptic * haptic );
33
34
35 /**
36 * @brief The entry point of this force feedback demo.
37 * @param[in] argc Number of arguments.
38 * @param[in] argv Array of argc arguments.
39 */
40 int main( int argc, char** argv )
41 {
42 int i;
43 char *name;
44 SDL_HapticEffect efx[5];
45 int id[5];
46 int nefx;
47 unsigned int supported;
48
49 name = NULL;
50 if (argc > 1) {
51 name = argv[1];
52 if ((strcmp(name,"--help")==0) || (strcmp(name,"-h")==0)) {
53 printf("USAGE: %s [device name]\n"
54 "If device name is specified, it will try to find a device whose name\n"
55 "contains device name for testing.\n", argv[0]);
56 return 0;
57 }
58 }
59
60 /* Initialize the force feedbackness */
61 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC);
62 printf("%d Haptic devices detected.\n", SDL_NumHaptics());
63 if (SDL_NumHaptics() > 0) {
64 /* We'll just use the first force feedback device found */
65 if (name == NULL) {
66 i = 0;
67 }
68 /* Try to find matching device */
69 else {
70 for (i=0; i<SDL_NumHaptics(); i++) {
71 if (strstr(SDL_HapticName(i), name)!=NULL)
72 break;
73 }
74
75 if (i >= SDL_NumHaptics()) {
76 printf("Unable to find device matching '%s', aborting.\n", name);
77 return 1;
78 }
79 }
80
81 haptic = SDL_HapticOpen(i);
82 if (haptic==NULL) {
83 perror("Unable to create the haptic device");
84 return 1;
85 }
86 printf("Device: %s\n",SDL_HapticName(i));
87 HapticPrintSupported(haptic);
88 }
89 else {
90 printf("No Haptic devices found!\n");
91 return 1;
92 }
93
94 /* We only want force feedback errors. */
95 SDL_ClearError();
96
97 /* Create effects. */
98 memset(&efx,0,sizeof(efx));
99 nefx = 0;
100 supported = SDL_HapticQuery(haptic);
101
102 printf("\nUploading effects\n");
103 /* First we'll try a SINE effect. */
104 if (supported & SDL_HAPTIC_SINE) {
105 printf(" effect %d: Sine Wave\n",nefx);
106 efx[nefx].type = SDL_HAPTIC_SINE;
107 efx[nefx].periodic.period = 1000;
108 efx[nefx].periodic.magnitude = 0x4000;
109 efx[nefx].periodic.length = 5000;
110 efx[nefx].periodic.attack_length = 1000;
111 efx[nefx].periodic.fade_length = 1000;
112 id[nefx] = SDL_HapticNewEffect(haptic,&efx[nefx]);
113 if (id[nefx] < 0) {
114 printf("UPLOADING EFFECT ERROR: %s\n",SDL_GetError());
115 abort_execution();
116 }
117 nefx++;
118 }
119 /* Now we'll try a SAWTOOTHUP */
120 if (supported & SDL_HAPTIC_SAWTOOTHUP) {
121 printf(" effect %d: Sawtooth Up\n",nefx);
122 efx[nefx].type = SDL_HAPTIC_SQUARE;
123 efx[nefx].periodic.period = 500;
124 efx[nefx].periodic.magnitude = 0x5000;
125 efx[nefx].periodic.length = 5000;
126 efx[nefx].periodic.attack_length = 1000;
127 efx[nefx].periodic.fade_length = 1000;
128 id[nefx] = SDL_HapticNewEffect(haptic,&efx[nefx]);
129 if (id[nefx] < 0) {
130 printf("UPLOADING EFFECT ERROR: %s\n",SDL_GetError());
131 abort_execution();
132 }
133 nefx++;
134 }
135 /* Now the classical constant effect. */
136 if (supported & SDL_HAPTIC_CONSTANT) {
137 printf(" effect %d: Constant Force\n",nefx);
138 efx[nefx].type = SDL_HAPTIC_CONSTANT;
139 efx[nefx].constant.direction.type = SDL_HAPTIC_POLAR;
140 efx[nefx].constant.direction.dir[0] = 20000; /* Force comes from the south-west. */
141 efx[nefx].constant.length = 5000;
142 efx[nefx].constant.level = 0x6000;
143 efx[nefx].constant.attack_length = 1000;
144 efx[nefx].constant.fade_length = 1000;
145 id[nefx] = SDL_HapticNewEffect(haptic,&efx[nefx]);
146 if (id[nefx] < 0) {
147 printf("UPLOADING EFFECT ERROR: %s\n",SDL_GetError());
148 abort_execution();
149 }
150 nefx++;
151 }
152 /* The cute spring effect. */
153 if (supported & SDL_HAPTIC_SPRING) {
154 printf(" effect %d: Condition Spring\n",nefx);
155 efx[nefx].type = SDL_HAPTIC_SPRING;
156 efx[nefx].condition.length = 5000;
157 for (i=0; i<SDL_HapticNumAxes(haptic); i++) {
158 efx[nefx].condition.right_sat[i] = 0x7FFF;
159 efx[nefx].condition.left_sat[i] = 0x7FFF;
160 efx[nefx].condition.right_coeff[i] = 0x2000;
161 efx[nefx].condition.left_coeff[i] = 0x2000;
162 efx[nefx].condition.center[i] = 0x1000; /* Displace the center for it to move. */
163 }
164 id[nefx] = SDL_HapticNewEffect(haptic,&efx[nefx]);
165 if (id[nefx] < 0) {
166 printf("UPLOADING EFFECT ERROR: %s\n",SDL_GetError());
167 abort_execution();
168 }
169 nefx++;
170 }
171 /* The pretty awesome inertia effect. */
172 if (supported & SDL_HAPTIC_INERTIA) {
173 printf(" effect %d: Condition Inertia\n",nefx);
174 efx[nefx].type = SDL_HAPTIC_SPRING;
175 efx[nefx].condition.length = 5000;
176 for (i=0; i<SDL_HapticNumAxes(haptic); i++) {
177 efx[nefx].condition.right_sat[i] = 0x7FFF;
178 efx[nefx].condition.left_sat[i] = 0x7FFF;
179 efx[nefx].condition.right_coeff[i] = 0x2000;
180 efx[nefx].condition.left_coeff[i] = 0x2000;
181 }
182 id[nefx] = SDL_HapticNewEffect(haptic,&efx[nefx]);
183 if (id[nefx] < 0) {
184 printf("UPLOADING EFFECT ERROR: %s\n",SDL_GetError());
185 abort_execution();
186 }
187 nefx++;
188 }
189
190 printf("\nNow playing effects for 5 seconds each with 1 second delay between\n");
191 for (i=0; i<nefx; i++) {
192 printf(" Playing effect %d\n",i);
193 SDL_HapticRunEffect(haptic, id[i], 1);
194 SDL_Delay(6000); /* Effects only have length 5000 */
195 }
196
197 /* Quit */
198 if (haptic != NULL)
199 SDL_HapticClose(haptic);
200 SDL_Quit();
201
202 return 0;
203 }
204
205
206 /*
207 * Cleans up a bit.
208 */
209 static void abort_execution (void)
210 {
211 printf("\nAborting program execution.\n");
212
213 SDL_HapticClose(haptic);
214 SDL_Quit();
215
216 exit(1);
217 }
218
219
220 /*
221 * Displays information about the haptic device.
222 */
223 static void HapticPrintSupported( SDL_Haptic * haptic )
224 {
225 unsigned int supported;
226
227 supported = SDL_HapticQuery(haptic);
228 printf(" Supported effects [%d effects, %d playing]:\n",
229 SDL_HapticNumEffects(haptic),
230 SDL_HapticNumEffectsPlaying(haptic));
231 if (supported & SDL_HAPTIC_CONSTANT) printf(" constant\n");
232 if (supported & SDL_HAPTIC_SINE) printf(" sine\n");
233 if (supported & SDL_HAPTIC_SQUARE) printf(" square\n");
234 if (supported & SDL_HAPTIC_TRIANGLE) printf(" triangle\n");
235 if (supported & SDL_HAPTIC_SAWTOOTHUP) printf(" sawtoothup\n");
236 if (supported & SDL_HAPTIC_SAWTOOTHDOWN) printf(" sawtoothdown\n");
237 if (supported & SDL_HAPTIC_RAMP) printf(" ramp\n");
238 if (supported & SDL_HAPTIC_FRICTION) printf(" friction\n");
239 if (supported & SDL_HAPTIC_SPRING) printf(" spring\n");
240 if (supported & SDL_HAPTIC_DAMPER) printf(" damper\n");
241 if (supported & SDL_HAPTIC_INERTIA) printf(" intertia\n");
242 if (supported & SDL_HAPTIC_CUSTOM) printf(" custom\n");
243 printf(" Supported capabilities:\n");
244 if (supported & SDL_HAPTIC_GAIN) printf(" gain\n");
245 if (supported & SDL_HAPTIC_AUTOCENTER) printf(" autocenter\n");
246 if (supported & SDL_HAPTIC_STATUS) printf(" status\n");
247 }