More comments. gsoc2008_force_feedback
authorEdgar Simo <bobbens@gmail.com>
Tue, 01 Jul 2008 10:44:42 +0000
branchgsoc2008_force_feedback
changeset 24815d0ea4576f20
parent 2480 b883974445fc
child 2482 b51ad78812d5
More comments.
Clamped values in struct ff_effect.
Added waveforms.
include/SDL_haptic.h
src/haptic/linux/SDL_syshaptic.c
     1.1 --- a/include/SDL_haptic.h	Tue Jul 01 09:22:22 2008 +0000
     1.2 +++ b/include/SDL_haptic.h	Tue Jul 01 10:44:42 2008 +0000
     1.3 @@ -57,9 +57,38 @@
     1.4  #define SDL_HAPTIC_GAIN       (1<<8)
     1.5  #define SDL_HAPTIC_AUTOCENTER (1<<9)
     1.6  
     1.7 +typedef enum SDL_waveform {
     1.8 +   SDL_WAVEFORM_SINE,
     1.9 +   SDL_WAVEFORM_SQUARE,
    1.10 +   SDL_WAVEFORM_TRIANGLE,
    1.11 +   SDL_WAVEFORM_SAWTOOTHUP,
    1.12 +   SDL_WAVEFORM_SAWTOOTHDOWN
    1.13 +} SDL_waveform;
    1.14 +
    1.15 +
    1.16 +/*
    1.17 + * All values max at 32767 (0x7fff).  Signed values also can be negative.
    1.18 + * Time values unless specified otherwise are in milliseconds.
    1.19 + *
    1.20 + * Common parts:
    1.21 + * 
    1.22 + * Replay:
    1.23 + *    Uint16 length;    Duration of effect.
    1.24 + *    Uint16 delay;     Delay before starting effect.
    1.25 + *
    1.26 + * Trigger:
    1.27 + *    Uint16 button;    Button that triggers effect.
    1.28 + *    Uint16 interval;  How soon before effect can be triggered again.
    1.29 + *
    1.30 + * Envelope:
    1.31 + *    Uint16 attack_length;   Duration of the attack.
    1.32 + *    Uint16 attack_level;    Level at the start of the attack.
    1.33 + *    Uint16 fade_length;     Duration of the fade out.
    1.34 + *    Uint16 fade_level;      Level at the end of the fade.
    1.35 + */
    1.36  typedef struct SDL_HapticConstant {
    1.37     /* Header */
    1.38 -   Uint16 type;
    1.39 +   Uint16 type; /* SDL_HAPTIC_CONSTANT */
    1.40     Uint16 direction;
    1.41  
    1.42     /* Replay */
    1.43 @@ -71,7 +100,7 @@
    1.44     Uint16 interval;
    1.45  
    1.46     /* Constant */
    1.47 -   Sint16 level;
    1.48 +   Sint16 level; /* Strength of the constant effect. */
    1.49  
    1.50     /* Envelope */
    1.51     Uint16 attack_length;
    1.52 @@ -81,7 +110,7 @@
    1.53  } SDL_HapticConstant;
    1.54  typedef struct SDL_HapticPeriodic {
    1.55     /* Header */
    1.56 -   Uint16 type;
    1.57 +   Uint16 type; /* SDL_HAPTIC_PERIODIC */
    1.58     Uint16 direction;
    1.59  
    1.60     /* Replay */
    1.61 @@ -93,11 +122,11 @@
    1.62     Uint16 interval;
    1.63  
    1.64     /* Periodic */
    1.65 -   Uint16 waveform;
    1.66 -   Uint16 period;
    1.67 -   Sint16 magnitude;
    1.68 -   Sint16 offset;
    1.69 -   Uint16 phase;
    1.70 +   SDL_waveform waveform; /* Type of effect */
    1.71 +   Uint16 period; /* Period of the wave */
    1.72 +   Sint16 magnitude; /* Peak value */
    1.73 +   Sint16 offset; /* Mean value of the wave */
    1.74 +   Uint16 phase; /* Horizontal shift */
    1.75  
    1.76     /* Envelope */
    1.77     Uint16 attack_length;
     2.1 --- a/src/haptic/linux/SDL_syshaptic.c	Tue Jul 01 09:22:22 2008 +0000
     2.2 +++ b/src/haptic/linux/SDL_syshaptic.c	Tue Jul 01 10:44:42 2008 +0000
     2.3 @@ -271,8 +271,10 @@
     2.4     SDL_hapticlist[0].fname = NULL;
     2.5  }
     2.6  
     2.7 +#define  CLAMP(x)    (((x) > 32767) ? 32767 : x)
     2.8  /*
     2.9   * Initializes the linux effect struct from a haptic_effect.
    2.10 + * Values above 32767 (for unsigned) are unspecified so we must clamp.
    2.11   */
    2.12  static int
    2.13  SDL_SYS_ToFFEffect( struct ff_effect * dest, SDL_HapticEffect * src )
    2.14 @@ -289,24 +291,24 @@
    2.15  
    2.16           /* Header */
    2.17           dest->type = FF_CONSTANT;
    2.18 -         dest->direction = constant->direction;
    2.19 +         dest->direction = CLAMP(constant->direction);
    2.20  
    2.21           /* Replay */
    2.22 -         dest->replay.length = constant->length;
    2.23 -         dest->replay.delay = constant->delay;
    2.24 +         dest->replay.length = CLAMP(constant->length);
    2.25 +         dest->replay.delay = CLAMP(constant->delay);
    2.26  
    2.27           /* Trigger */
    2.28 -         dest->trigger.button = constant->button;
    2.29 -         dest->trigger.interval = constant->interval;
    2.30 +         dest->trigger.button = CLAMP(constant->button);
    2.31 +         dest->trigger.interval = CLAMP(constant->interval);
    2.32  
    2.33           /* Constant */
    2.34           dest->u.constant.level = constant->level;
    2.35  
    2.36           /* Envelope */
    2.37 -         dest->u.constant.envelope.attack_length = constant->attack_length;
    2.38 -         dest->u.constant.envelope.attack_level = constant->attack_level;
    2.39 -         dest->u.constant.envelope.fade_length = constant->fade_length;
    2.40 -         dest->u.constant.envelope.fade_level = constant->fade_level;
    2.41 +         dest->u.constant.envelope.attack_length = CLAMP(constant->attack_length);
    2.42 +         dest->u.constant.envelope.attack_level = CLAMP(constant->attack_level);
    2.43 +         dest->u.constant.envelope.fade_length = CLAMP(constant->fade_length);
    2.44 +         dest->u.constant.envelope.fade_level = CLAMP(constant->fade_level);
    2.45  
    2.46           break;
    2.47  
    2.48 @@ -315,28 +317,48 @@
    2.49  
    2.50           /* Header */
    2.51           dest->type = FF_PERIODIC;
    2.52 -         dest->direction = periodic->direction;
    2.53 +         dest->direction = CLAMP(periodic->direction);
    2.54           
    2.55           /* Replay */
    2.56 -         dest->replay.length = periodic->length;
    2.57 -         dest->replay.delay = periodic->delay;
    2.58 +         dest->replay.length = CLAMP(periodic->length);
    2.59 +         dest->replay.delay = CLAMP(periodic->delay);
    2.60           
    2.61           /* Trigger */
    2.62 -         dest->trigger.button = periodic->button;
    2.63 -         dest->trigger.interval = periodic->interval;
    2.64 +         dest->trigger.button = CLAMP(periodic->button);
    2.65 +         dest->trigger.interval = CLAMP(periodic->interval);
    2.66           
    2.67           /* Constant */
    2.68 -         dest->u.periodic.waveform = periodic->waveform;
    2.69 -         dest->u.periodic.period = periodic->period;
    2.70 +         switch (periodic->waveform) {
    2.71 +            case SDL_WAVEFORM_SINE:
    2.72 +               dest->u.periodic.waveform = FF_SINE;
    2.73 +               break;
    2.74 +            case SDL_WAVEFORM_SQUARE:
    2.75 +               dest->u.periodic.waveform = FF_SQUARE;
    2.76 +               break;
    2.77 +            case SDL_WAVEFORM_TRIANGLE:
    2.78 +               dest->u.periodic.waveform = FF_TRIANGLE;
    2.79 +               break;
    2.80 +            case SDL_WAVEFORM_SAWTOOTHUP:
    2.81 +               dest->u.periodic.waveform = FF_SAW_UP;
    2.82 +               break;
    2.83 +            case SDL_WAVEFORM_SAWTOOTHDOWN:
    2.84 +               dest->u.periodic.waveform = FF_SAW_DOWN;
    2.85 +               break;
    2.86 +
    2.87 +            default:
    2.88 +               SDL_SetError("Unknown waveform.");
    2.89 +               return -1;
    2.90 +         }
    2.91 +         dest->u.periodic.period = CLAMP(periodic->period);
    2.92           dest->u.periodic.magnitude = periodic->magnitude;
    2.93           dest->u.periodic.offset = periodic->offset;
    2.94 -         dest->u.periodic.phase = periodic->phase;
    2.95 +         dest->u.periodic.phase = CLAMP(periodic->phase);
    2.96           
    2.97           /* Envelope */
    2.98 -         dest->u.periodic.envelope.attack_length = periodic->attack_length;
    2.99 -         dest->u.periodic.envelope.attack_level = periodic->attack_level;
   2.100 -         dest->u.periodic.envelope.fade_length = periodic->fade_length;
   2.101 -         dest->u.periodic.envelope.fade_level = periodic->fade_level;
   2.102 +         dest->u.periodic.envelope.attack_length = CLAMP(periodic->attack_length);
   2.103 +         dest->u.periodic.envelope.attack_level = CLAMP(periodic->attack_level);
   2.104 +         dest->u.periodic.envelope.fade_length = CLAMP(periodic->fade_length);
   2.105 +         dest->u.periodic.envelope.fade_level = CLAMP(periodic->fade_level);
   2.106  
   2.107           break;
   2.108