test/testhaptic.c
branchgsoc2008_force_feedback
changeset 2636 57ac1594164e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testhaptic.c	Wed Aug 06 17:14:54 2008 +0000
     1.3 @@ -0,0 +1,247 @@
     1.4 +/*
     1.5 +Copyright (c) 2008, Edgar Simo Serra
     1.6 +All rights reserved.
     1.7 +
     1.8 +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     1.9 +
    1.10 +    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    1.11 +    * 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.
    1.12 +    * 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.
    1.13 +
    1.14 +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.
    1.15 +*/
    1.16 +
    1.17 +/*
    1.18 + * includes
    1.19 + */
    1.20 +#include "SDL.h"
    1.21 +#include "SDL_haptic.h"
    1.22 +
    1.23 +#include <stdio.h> /* printf */
    1.24 +#include <string.h> /* strstr */
    1.25 +
    1.26 +
    1.27 +
    1.28 +static SDL_Haptic *haptic;
    1.29 +
    1.30 +
    1.31 +/*
    1.32 + * prototypes
    1.33 + */
    1.34 +static void abort_execution (void);
    1.35 +static void HapticPrintSupported( SDL_Haptic * haptic );
    1.36 +
    1.37 +
    1.38 +/**
    1.39 + * @brief The entry point of this force feedback demo.
    1.40 + * @param[in] argc Number of arguments.
    1.41 + * @param[in] argv Array of argc arguments.
    1.42 + */
    1.43 +int main( int argc, char** argv )
    1.44 +{
    1.45 +   int i;
    1.46 +   char *name;
    1.47 +   SDL_HapticEffect efx[5];
    1.48 +   int id[5];
    1.49 +   int nefx;
    1.50 +   unsigned int supported;
    1.51 +
    1.52 +   name = NULL;
    1.53 +   if (argc > 1) {
    1.54 +      name = argv[1];
    1.55 +      if ((strcmp(name,"--help")==0) || (strcmp(name,"-h")==0)) {
    1.56 +         printf("USAGE: %s [device name]\n"
    1.57 +                "If device name is specified, it will try to find a device whose name\n"
    1.58 +                "contains device name for testing.\n", argv[0]);
    1.59 +         return 0;
    1.60 +      }
    1.61 +   }
    1.62 +
    1.63 +   /* Initialize the force feedbackness */
    1.64 +   SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC);
    1.65 +   printf("%d Haptic devices detected.\n", SDL_NumHaptics());
    1.66 +   if (SDL_NumHaptics() > 0) {
    1.67 +      /* We'll just use the first force feedback device found */
    1.68 +      if (name == NULL) {
    1.69 +         i = 0;
    1.70 +      }
    1.71 +      /* Try to find matching device */
    1.72 +      else {
    1.73 +         for (i=0; i<SDL_NumHaptics(); i++) {
    1.74 +            if (strstr(SDL_HapticName(i), name)!=NULL)
    1.75 +               break;
    1.76 +         }
    1.77 +
    1.78 +         if (i >= SDL_NumHaptics()) {
    1.79 +            printf("Unable to find device matching '%s', aborting.\n", name);
    1.80 +            return 1;
    1.81 +         }
    1.82 +      }
    1.83 +
    1.84 +      haptic = SDL_HapticOpen(i);
    1.85 +      if (haptic==NULL) {
    1.86 +         perror("Unable to create the haptic device");
    1.87 +         return 1;
    1.88 +      }
    1.89 +      printf("Device: %s\n",SDL_HapticName(i));
    1.90 +      HapticPrintSupported(haptic);
    1.91 +   }
    1.92 +   else {
    1.93 +      printf("No Haptic devices found!\n");
    1.94 +      return 1;
    1.95 +   }
    1.96 +
    1.97 +   /* We only want force feedback errors. */
    1.98 +   SDL_ClearError();
    1.99 +
   1.100 +   /* Create effects. */
   1.101 +   memset(&efx,0,sizeof(efx));
   1.102 +   nefx = 0;
   1.103 +   supported = SDL_HapticQuery(haptic);
   1.104 +
   1.105 +   printf("\nUploading effects\n");
   1.106 +   /* First we'll try a SINE effect. */
   1.107 +   if (supported & SDL_HAPTIC_SINE) {
   1.108 +      printf("   effect %d: Sine Wave\n",nefx);
   1.109 +      efx[nefx].type = SDL_HAPTIC_SINE;
   1.110 +      efx[nefx].periodic.period = 1000;
   1.111 +      efx[nefx].periodic.magnitude = 0x4000;
   1.112 +      efx[nefx].periodic.length = 5000;
   1.113 +      efx[nefx].periodic.attack_length = 1000;
   1.114 +      efx[nefx].periodic.fade_length = 1000;
   1.115 +      id[nefx] = SDL_HapticNewEffect(haptic,&efx[nefx]);
   1.116 +      if (id[nefx] < 0) {
   1.117 +         printf("UPLOADING EFFECT ERROR: %s\n",SDL_GetError());
   1.118 +         abort_execution();
   1.119 +      }
   1.120 +      nefx++;
   1.121 +   }
   1.122 +   /* Now we'll try a SAWTOOTHUP */
   1.123 +   if (supported & SDL_HAPTIC_SAWTOOTHUP) {
   1.124 +      printf("   effect %d: Sawtooth Up\n",nefx);
   1.125 +      efx[nefx].type = SDL_HAPTIC_SQUARE;
   1.126 +      efx[nefx].periodic.period = 500;
   1.127 +      efx[nefx].periodic.magnitude = 0x5000;
   1.128 +      efx[nefx].periodic.length = 5000;
   1.129 +      efx[nefx].periodic.attack_length = 1000;
   1.130 +      efx[nefx].periodic.fade_length = 1000;
   1.131 +      id[nefx] = SDL_HapticNewEffect(haptic,&efx[nefx]);
   1.132 +      if (id[nefx] < 0) {
   1.133 +         printf("UPLOADING EFFECT ERROR: %s\n",SDL_GetError());
   1.134 +         abort_execution();
   1.135 +      }
   1.136 +      nefx++;
   1.137 +   }
   1.138 +   /* Now the classical constant effect. */
   1.139 +   if (supported & SDL_HAPTIC_CONSTANT) {
   1.140 +      printf("   effect %d: Constant Force\n",nefx);
   1.141 +      efx[nefx].type = SDL_HAPTIC_CONSTANT;
   1.142 +      efx[nefx].constant.direction.type = SDL_HAPTIC_POLAR;
   1.143 +      efx[nefx].constant.direction.dir[0] = 20000; /* Force comes from the south-west. */
   1.144 +      efx[nefx].constant.length = 5000;
   1.145 +      efx[nefx].constant.level = 0x6000;
   1.146 +      efx[nefx].constant.attack_length = 1000;
   1.147 +      efx[nefx].constant.fade_length = 1000;
   1.148 +      id[nefx] = SDL_HapticNewEffect(haptic,&efx[nefx]);
   1.149 +      if (id[nefx] < 0) {
   1.150 +         printf("UPLOADING EFFECT ERROR: %s\n",SDL_GetError());
   1.151 +         abort_execution();
   1.152 +      }
   1.153 +      nefx++;
   1.154 +   }
   1.155 +   /* The cute spring effect. */
   1.156 +   if (supported & SDL_HAPTIC_SPRING) {
   1.157 +      printf("   effect %d: Condition Spring\n",nefx);
   1.158 +      efx[nefx].type = SDL_HAPTIC_SPRING;
   1.159 +      efx[nefx].condition.length = 5000;
   1.160 +      for (i=0; i<SDL_HapticNumAxes(haptic); i++) {
   1.161 +         efx[nefx].condition.right_sat[i] = 0x7FFF;
   1.162 +         efx[nefx].condition.left_sat[i] = 0x7FFF;
   1.163 +         efx[nefx].condition.right_coeff[i] = 0x2000;
   1.164 +         efx[nefx].condition.left_coeff[i] = 0x2000;
   1.165 +         efx[nefx].condition.center[i] = 0x1000; /* Displace the center for it to move. */
   1.166 +      }
   1.167 +      id[nefx] = SDL_HapticNewEffect(haptic,&efx[nefx]);
   1.168 +      if (id[nefx] < 0) {
   1.169 +         printf("UPLOADING EFFECT ERROR: %s\n",SDL_GetError());
   1.170 +         abort_execution();
   1.171 +      }
   1.172 +      nefx++;
   1.173 +   }
   1.174 +   /* The pretty awesome inertia effect. */
   1.175 +   if (supported & SDL_HAPTIC_INERTIA) {
   1.176 +      printf("   effect %d: Condition Inertia\n",nefx);
   1.177 +      efx[nefx].type = SDL_HAPTIC_SPRING;
   1.178 +      efx[nefx].condition.length = 5000;
   1.179 +      for (i=0; i<SDL_HapticNumAxes(haptic); i++) {
   1.180 +         efx[nefx].condition.right_sat[i] = 0x7FFF;
   1.181 +         efx[nefx].condition.left_sat[i] = 0x7FFF;
   1.182 +         efx[nefx].condition.right_coeff[i] = 0x2000;
   1.183 +         efx[nefx].condition.left_coeff[i] = 0x2000;
   1.184 +      }
   1.185 +      id[nefx] = SDL_HapticNewEffect(haptic,&efx[nefx]);
   1.186 +      if (id[nefx] < 0) {
   1.187 +         printf("UPLOADING EFFECT ERROR: %s\n",SDL_GetError());
   1.188 +         abort_execution();
   1.189 +      }
   1.190 +      nefx++;
   1.191 +   }
   1.192 +
   1.193 +   printf("\nNow playing effects for 5 seconds each with 1 second delay between\n");
   1.194 +   for (i=0; i<nefx; i++) {
   1.195 +      printf("   Playing effect %d\n",i);
   1.196 +      SDL_HapticRunEffect(haptic, id[i], 1);
   1.197 +      SDL_Delay(6000); /* Effects only have length 5000 */
   1.198 +   }
   1.199 +
   1.200 +   /* Quit */
   1.201 +   if (haptic != NULL) 
   1.202 +      SDL_HapticClose(haptic);
   1.203 +   SDL_Quit();
   1.204 +
   1.205 +   return 0;
   1.206 +}
   1.207 +
   1.208 +
   1.209 +/*
   1.210 + * Cleans up a bit.
   1.211 + */
   1.212 +static void abort_execution (void)
   1.213 +{
   1.214 +   printf("\nAborting program execution.\n");
   1.215 +
   1.216 +   SDL_HapticClose(haptic);
   1.217 +   SDL_Quit();
   1.218 +
   1.219 +   exit(1);
   1.220 +}
   1.221 +
   1.222 +
   1.223 +/*
   1.224 + * Displays information about the haptic device.
   1.225 + */
   1.226 +static void HapticPrintSupported( SDL_Haptic * haptic )
   1.227 +{
   1.228 +   unsigned int supported;
   1.229 +
   1.230 +   supported = SDL_HapticQuery(haptic);
   1.231 +   printf("   Supported effects [%d effects, %d playing]:\n",
   1.232 +         SDL_HapticNumEffects(haptic),
   1.233 +         SDL_HapticNumEffectsPlaying(haptic));
   1.234 +   if (supported & SDL_HAPTIC_CONSTANT) printf("      constant\n");
   1.235 +   if (supported & SDL_HAPTIC_SINE) printf("      sine\n");
   1.236 +   if (supported & SDL_HAPTIC_SQUARE) printf("      square\n");
   1.237 +   if (supported & SDL_HAPTIC_TRIANGLE) printf("      triangle\n");
   1.238 +   if (supported & SDL_HAPTIC_SAWTOOTHUP) printf("      sawtoothup\n");
   1.239 +   if (supported & SDL_HAPTIC_SAWTOOTHDOWN) printf("      sawtoothdown\n");
   1.240 +   if (supported & SDL_HAPTIC_RAMP) printf("      ramp\n");
   1.241 +   if (supported & SDL_HAPTIC_FRICTION) printf("      friction\n");
   1.242 +   if (supported & SDL_HAPTIC_SPRING) printf("      spring\n");
   1.243 +   if (supported & SDL_HAPTIC_DAMPER) printf("      damper\n");
   1.244 +   if (supported & SDL_HAPTIC_INERTIA) printf("      intertia\n");
   1.245 +   if (supported & SDL_HAPTIC_CUSTOM) printf("      custom\n");
   1.246 +   printf("   Supported capabilities:\n");
   1.247 +   if (supported & SDL_HAPTIC_GAIN) printf("      gain\n");
   1.248 +   if (supported & SDL_HAPTIC_AUTOCENTER) printf("      autocenter\n");
   1.249 +   if (supported & SDL_HAPTIC_STATUS) printf("      status\n");
   1.250 +}