test/testhaptic.c
author Edgar Simo <bobbens@gmail.com>
Thu, 04 Sep 2008 13:43:39 +0000
changeset 2748 5668c3dfe7bc
parent 2713 0906692aa6a4
child 3338 9de326b3099c
permissions -rw-r--r--
Allow testing haptic devices by index.
     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 #include <ctype.h>              /* isdigit */
    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
    41 main(int argc, char **argv)
    42 {
    43     int i;
    44     char *name;
    45     int index;
    46     SDL_HapticEffect efx[5];
    47     int id[5];
    48     int nefx;
    49     unsigned int supported;
    50 
    51     name = NULL;
    52     index = -1;
    53     if (argc > 1) {
    54         name = argv[1];
    55         if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) {
    56             printf("USAGE: %s [device]\n"
    57                    "If device is a two-digit number it'll use it as an index, otherwise\n"
    58                    "it'll use it as if it were part of the device's name.\n",
    59                    argv[0]);
    60             return 0;
    61         }
    62 
    63         i = strlen(name);
    64         if ((i < 3) && isdigit(name[0]) && ((i == 1) || isdigit(name[1]))) {
    65             index = atoi(name);
    66             name = NULL;
    67         }
    68     }
    69 
    70     /* Initialize the force feedbackness */
    71     SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK |
    72              SDL_INIT_HAPTIC);
    73     printf("%d Haptic devices detected.\n", SDL_NumHaptics());
    74     if (SDL_NumHaptics() > 0) {
    75         /* We'll just use index or the first force feedback device found */
    76         if (name == NULL) {
    77             i = (index != -1) ? index : 0;
    78         }
    79         /* Try to find matching device */
    80         else {
    81             for (i = 0; i < SDL_NumHaptics(); i++) {
    82                 if (strstr(SDL_HapticName(i), name) != NULL)
    83                     break;
    84             }
    85 
    86             if (i >= SDL_NumHaptics()) {
    87                 printf("Unable to find device matching '%s', aborting.\n",
    88                        name);
    89                 return 1;
    90             }
    91         }
    92 
    93         haptic = SDL_HapticOpen(i);
    94         if (haptic == NULL) {
    95             printf("Unable to create the haptic device: %s\n",
    96                    SDL_GetError());
    97             return 1;
    98         }
    99         printf("Device: %s\n", SDL_HapticName(i));
   100         HapticPrintSupported(haptic);
   101     } else {
   102         printf("No Haptic devices found!\n");
   103         return 1;
   104     }
   105 
   106     /* We only want force feedback errors. */
   107     SDL_ClearError();
   108 
   109     /* Create effects. */
   110     memset(&efx, 0, sizeof(efx));
   111     nefx = 0;
   112     supported = SDL_HapticQuery(haptic);
   113 
   114     printf("\nUploading effects\n");
   115     /* First we'll try a SINE effect. */
   116     if (supported & SDL_HAPTIC_SINE) {
   117         printf("   effect %d: Sine Wave\n", nefx);
   118         efx[nefx].type = SDL_HAPTIC_SINE;
   119         efx[nefx].periodic.period = 1000;
   120         efx[nefx].periodic.magnitude = 0x4000;
   121         efx[nefx].periodic.length = 5000;
   122         efx[nefx].periodic.attack_length = 1000;
   123         efx[nefx].periodic.fade_length = 1000;
   124         id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
   125         if (id[nefx] < 0) {
   126             printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   127             abort_execution();
   128         }
   129         nefx++;
   130     }
   131     /* Now we'll try a SAWTOOTHUP */
   132     if (supported & SDL_HAPTIC_SAWTOOTHUP) {
   133         printf("   effect %d: Sawtooth Up\n", nefx);
   134         efx[nefx].type = SDL_HAPTIC_SQUARE;
   135         efx[nefx].periodic.period = 500;
   136         efx[nefx].periodic.magnitude = 0x5000;
   137         efx[nefx].periodic.length = 5000;
   138         efx[nefx].periodic.attack_length = 1000;
   139         efx[nefx].periodic.fade_length = 1000;
   140         id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
   141         if (id[nefx] < 0) {
   142             printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   143             abort_execution();
   144         }
   145         nefx++;
   146     }
   147     /* Now the classical constant effect. */
   148     if (supported & SDL_HAPTIC_CONSTANT) {
   149         printf("   effect %d: Constant Force\n", nefx);
   150         efx[nefx].type = SDL_HAPTIC_CONSTANT;
   151         efx[nefx].constant.direction.type = SDL_HAPTIC_POLAR;
   152         efx[nefx].constant.direction.dir[0] = 20000;    /* Force comes from the south-west. */
   153         efx[nefx].constant.length = 5000;
   154         efx[nefx].constant.level = 0x6000;
   155         efx[nefx].constant.attack_length = 1000;
   156         efx[nefx].constant.fade_length = 1000;
   157         id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
   158         if (id[nefx] < 0) {
   159             printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   160             abort_execution();
   161         }
   162         nefx++;
   163     }
   164     /* The cute spring effect. */
   165     if (supported & SDL_HAPTIC_SPRING) {
   166         printf("   effect %d: Condition Spring\n", nefx);
   167         efx[nefx].type = SDL_HAPTIC_SPRING;
   168         efx[nefx].condition.length = 5000;
   169         for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
   170             efx[nefx].condition.right_sat[i] = 0x7FFF;
   171             efx[nefx].condition.left_sat[i] = 0x7FFF;
   172             efx[nefx].condition.right_coeff[i] = 0x2000;
   173             efx[nefx].condition.left_coeff[i] = 0x2000;
   174             efx[nefx].condition.center[i] = 0x1000;     /* Displace the center for it to move. */
   175         }
   176         id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
   177         if (id[nefx] < 0) {
   178             printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   179             abort_execution();
   180         }
   181         nefx++;
   182     }
   183     /* The pretty awesome inertia effect. */
   184     if (supported & SDL_HAPTIC_INERTIA) {
   185         printf("   effect %d: Condition Inertia\n", nefx);
   186         efx[nefx].type = SDL_HAPTIC_SPRING;
   187         efx[nefx].condition.length = 5000;
   188         for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
   189             efx[nefx].condition.right_sat[i] = 0x7FFF;
   190             efx[nefx].condition.left_sat[i] = 0x7FFF;
   191             efx[nefx].condition.right_coeff[i] = 0x2000;
   192             efx[nefx].condition.left_coeff[i] = 0x2000;
   193         }
   194         id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
   195         if (id[nefx] < 0) {
   196             printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   197             abort_execution();
   198         }
   199         nefx++;
   200     }
   201 
   202     printf
   203         ("\nNow playing effects for 5 seconds each with 1 second delay between\n");
   204     for (i = 0; i < nefx; i++) {
   205         printf("   Playing effect %d\n", i);
   206         SDL_HapticRunEffect(haptic, id[i], 1);
   207         SDL_Delay(6000);        /* Effects only have length 5000 */
   208     }
   209 
   210     /* Quit */
   211     if (haptic != NULL)
   212         SDL_HapticClose(haptic);
   213     SDL_Quit();
   214 
   215     return 0;
   216 }
   217 
   218 
   219 /*
   220  * Cleans up a bit.
   221  */
   222 static void
   223 abort_execution(void)
   224 {
   225     printf("\nAborting program execution.\n");
   226 
   227     SDL_HapticClose(haptic);
   228     SDL_Quit();
   229 
   230     exit(1);
   231 }
   232 
   233 
   234 /*
   235  * Displays information about the haptic device.
   236  */
   237 static void
   238 HapticPrintSupported(SDL_Haptic * haptic)
   239 {
   240     unsigned int supported;
   241 
   242     supported = SDL_HapticQuery(haptic);
   243     printf("   Supported effects [%d effects, %d playing]:\n",
   244            SDL_HapticNumEffects(haptic), SDL_HapticNumEffectsPlaying(haptic));
   245     if (supported & SDL_HAPTIC_CONSTANT)
   246         printf("      constant\n");
   247     if (supported & SDL_HAPTIC_SINE)
   248         printf("      sine\n");
   249     if (supported & SDL_HAPTIC_SQUARE)
   250         printf("      square\n");
   251     if (supported & SDL_HAPTIC_TRIANGLE)
   252         printf("      triangle\n");
   253     if (supported & SDL_HAPTIC_SAWTOOTHUP)
   254         printf("      sawtoothup\n");
   255     if (supported & SDL_HAPTIC_SAWTOOTHDOWN)
   256         printf("      sawtoothdown\n");
   257     if (supported & SDL_HAPTIC_RAMP)
   258         printf("      ramp\n");
   259     if (supported & SDL_HAPTIC_FRICTION)
   260         printf("      friction\n");
   261     if (supported & SDL_HAPTIC_SPRING)
   262         printf("      spring\n");
   263     if (supported & SDL_HAPTIC_DAMPER)
   264         printf("      damper\n");
   265     if (supported & SDL_HAPTIC_INERTIA)
   266         printf("      intertia\n");
   267     if (supported & SDL_HAPTIC_CUSTOM)
   268         printf("      custom\n");
   269     printf("   Supported capabilities:\n");
   270     if (supported & SDL_HAPTIC_GAIN)
   271         printf("      gain\n");
   272     if (supported & SDL_HAPTIC_AUTOCENTER)
   273         printf("      autocenter\n");
   274     if (supported & SDL_HAPTIC_STATUS)
   275         printf("      status\n");
   276 }