src/haptic/nds/SDL_syshaptic.c
author Darren Alton <dalton@stevens.edu>
Sat, 06 Sep 2008 00:10:16 +0000
changeset 2749 1c5f440a60fe
child 2750 e3affc66d963
permissions -rw-r--r--
Initial work for NDS haptic support.
dalton@2749
     1
/*
dalton@2749
     2
    SDL - Simple DirectMedia Layer
dalton@2749
     3
    Copyright (C) 2008 Edgar Simo
dalton@2749
     4
dalton@2749
     5
    This library is free software; you can redistribute it and/or
dalton@2749
     6
    modify it under the terms of the GNU Lesser General Public
dalton@2749
     7
    License as published by the Free Software Foundation; either
dalton@2749
     8
    version 2.1 of the License, or (at your option) any later version.
dalton@2749
     9
dalton@2749
    10
    This library is distributed in the hope that it will be useful,
dalton@2749
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
dalton@2749
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
dalton@2749
    13
    Lesser General Public License for more details.
dalton@2749
    14
dalton@2749
    15
    You should have received a copy of the GNU Lesser General Public
dalton@2749
    16
    License along with this library; if not, write to the Free Software
dalton@2749
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
dalton@2749
    18
dalton@2749
    19
    Sam Lantinga
dalton@2749
    20
    slouken@libsdl.org
dalton@2749
    21
*/
dalton@2749
    22
#include "SDL_config.h"
dalton@2749
    23
dalton@2749
    24
#ifdef SDL_HAPTIC_NDS
dalton@2749
    25
dalton@2749
    26
#include "SDL_haptic.h"
dalton@2749
    27
#include "../SDL_syshaptic.h"
dalton@2749
    28
#include "SDL_joystick.h"
dalton@2749
    29
#include <nds/arm9/rumble.h>
dalton@2749
    30
#include <nds/memory.h>
dalton@2749
    31
dalton@2749
    32
#define MAX_HAPTICS  1
dalton@2749
    33
/* right now only the ezf3in1 (and maybe official rumble pak) are supported
dalton@2749
    34
   and there can only be one of those in at a time (in GBA slot.) */
dalton@2749
    35
dalton@2749
    36
SDL_Haptic *nds_haptic = NULL;
dalton@2749
    37
dalton@2749
    38
typedef struct
dalton@2749
    39
{
dalton@2749
    40
    enum
dalton@2749
    41
    { NONE, OFFICIAL, EZF3IN1 } type;
dalton@2749
    42
    int pos;
dalton@2749
    43
} NDS_HapticData;
dalton@2749
    44
dalton@2749
    45
dalton@2749
    46
dalton@2749
    47
void NDS_EZF_OpenNorWrite()
dalton@2749
    48
{
dalton@2749
    49
    GBA_BUS[0x0FF0000] = 0xD200;
dalton@2749
    50
    GBA_BUS[0x0000000] = 0x1500;
dalton@2749
    51
    GBA_BUS[0x0010000] = 0xD200;
dalton@2749
    52
    GBA_BUS[0x0020000] = 0x1500;
dalton@2749
    53
    GBA_BUS[0x0E20000] = 0x1500;
dalton@2749
    54
    GBA_BUS[0x0FE0000] = 0x1500;
dalton@2749
    55
}
dalton@2749
    56
dalton@2749
    57
dalton@2749
    58
void NDS_EZF_CloseNorWrite()
dalton@2749
    59
{
dalton@2749
    60
    GBA_BUS[0x0FF0000] = 0xD200;
dalton@2749
    61
    GBA_BUS[0x0000000] = 0x1500;
dalton@2749
    62
    GBA_BUS[0x0010000] = 0xD200;
dalton@2749
    63
    GBA_BUS[0x0020000] = 0x1500;
dalton@2749
    64
    GBA_BUS[0x0E20000] = 0xD200;
dalton@2749
    65
    GBA_BUS[0x0FE0000] = 0x1500;
dalton@2749
    66
}
dalton@2749
    67
dalton@2749
    68
void NDS_EZF_ChipReset()
dalton@2749
    69
{
dalton@2749
    70
    GBA_BUS[0x0000] = 0x00F0 ;
dalton@2749
    71
    GBA_BUS[0x1000] = 0x00F0 ;
dalton@2749
    72
}
dalton@2749
    73
uint32 NDS_EZF_IsPresent()
dalton@2749
    74
{
dalton@2749
    75
    vuint16 id1,id2;
dalton@2749
    76
dalton@2749
    77
    NDS_EZF_OpenNorWrite();
dalton@2749
    78
dalton@2749
    79
    GBA_BUS[0x0555] = 0x00AA;
dalton@2749
    80
    GBA_BUS[0x02AA] = 0x0055;
dalton@2749
    81
    GBA_BUS[0x0555] = 0x0090;
dalton@2749
    82
    GBA_BUS[0x1555] = 0x00AA;
dalton@2749
    83
    GBA_BUS[0x12AA] = 0x0055;
dalton@2749
    84
    GBA_BUS[0x1555] = 0x0090;
dalton@2749
    85
dalton@2749
    86
    id1 = GBA_BUS[0x0001];
dalton@2749
    87
    id2 = GBA_BUS[0x1001];
dalton@2749
    88
dalton@2749
    89
    if((id1!=0x227E)|| (id2!=0x227E)) {
dalton@2749
    90
        NDS_EZF_CloseNorWrite();
dalton@2749
    91
        return 0;
dalton@2749
    92
    }
dalton@2749
    93
dalton@2749
    94
    id1 = GBA_BUS[0x000E];
dalton@2749
    95
    id2 = GBA_BUS[0x100E];
dalton@2749
    96
dalton@2749
    97
    NDS_EZF_CloseNorWrite();
dalton@2749
    98
dalton@2749
    99
    if(id1==0x2218 && id2==0x2218) {
dalton@2749
   100
        return 1;
dalton@2749
   101
    }
dalton@2749
   102
dalton@2749
   103
    return 0;
dalton@2749
   104
}
dalton@2749
   105
dalton@2749
   106
void NDS_EZF_SetShake(u8 pos)
dalton@2749
   107
{
dalton@2749
   108
    u16 data = ((pos%3)|0x00F0);
dalton@2749
   109
dalton@2749
   110
    GBA_BUS[0x0FF0000] = 0xD200;
dalton@2749
   111
    GBA_BUS[0x0000000] = 0x1500;
dalton@2749
   112
    GBA_BUS[0x0010000] = 0xD200;
dalton@2749
   113
    GBA_BUS[0x0020000] = 0x1500;
dalton@2749
   114
    GBA_BUS[0x0F10000] = data;
dalton@2749
   115
    GBA_BUS[0x0FE0000] = 0x1500;
dalton@2749
   116
dalton@2749
   117
    GBA_BUS[0] = 0x0000; /* write any value for vibration. */
dalton@2749
   118
    GBA_BUS[0] = 0x0002;
dalton@2749
   119
}
dalton@2749
   120
dalton@2749
   121
static int
dalton@2749
   122
SDL_SYS_LogicError(void)
dalton@2749
   123
{
dalton@2749
   124
    SDL_SetError("Logic error: No haptic devices available.");
dalton@2749
   125
    return 0;
dalton@2749
   126
}
dalton@2749
   127
dalton@2749
   128
dalton@2749
   129
int
dalton@2749
   130
SDL_SYS_HapticInit(void)
dalton@2749
   131
{
dalton@2749
   132
    int ret = 0;
dalton@2749
   133
    if(isRumbleInserted()) {
dalton@2749
   134
        /* official rumble pak is present. */
dalton@2749
   135
        ret = 1;
dalton@2749
   136
        printf("debug: haptic present: nintendo\n");
dalton@2749
   137
    } else if(NDS_EZF_IsPresent()) {
dalton@2749
   138
        /* ezflash 3-in-1 pak is present. */
dalton@2749
   139
        ret = 1;
dalton@2749
   140
        printf("debug: haptic present: ezf3in1\n");
dalton@2749
   141
        NDS_EZF_ChipReset();
dalton@2749
   142
    } else {
dalton@2749
   143
        printf("debug: no haptic found\n");
dalton@2749
   144
    }
dalton@2749
   145
dalton@2749
   146
    return ret;
dalton@2749
   147
}
dalton@2749
   148
dalton@2749
   149
dalton@2749
   150
const char *
dalton@2749
   151
SDL_SYS_HapticName(int index)
dalton@2749
   152
{
dalton@2749
   153
    if(nds_haptic) {
dalton@2749
   154
        switch(nds_haptic->hwdata->type) {
dalton@2749
   155
            case OFFICIAL: return "Nintendo DS Rumble Pak";
dalton@2749
   156
            case EZF3IN1: return "EZFlash 3-in-1 Rumble";
dalton@2749
   157
            default: return NULL;
dalton@2749
   158
        }
dalton@2749
   159
    }
dalton@2749
   160
    return NULL;
dalton@2749
   161
}
dalton@2749
   162
dalton@2749
   163
dalton@2749
   164
int
dalton@2749
   165
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
dalton@2749
   166
{
dalton@2749
   167
    if(!haptic) {
dalton@2749
   168
        return -1;
dalton@2749
   169
    }
dalton@2749
   170
dalton@2749
   171
    haptic->hwdata = SDL_malloc(sizeof(NDS_HapticData));
dalton@2749
   172
    if(!haptic->hwdata) {
dalton@2749
   173
        SDL_OutOfMemory();
dalton@2749
   174
        return -1;
dalton@2749
   175
    }
dalton@2749
   176
    nds_haptic = haptic;
dalton@2749
   177
dalton@2749
   178
    haptic->supported = SDL_HAPTIC_CONSTANT;
dalton@2749
   179
dalton@2749
   180
    /* determine what is here, if anything */
dalton@2749
   181
    haptic->hwdata->type = NONE;
dalton@2749
   182
    if(isRumbleInserted()) {
dalton@2749
   183
        /* official rumble pak is present. */
dalton@2749
   184
        haptic->hwdata->type = OFFICIAL;
dalton@2749
   185
    } else if(NDS_EZF_IsPresent()) {
dalton@2749
   186
        /* ezflash 3-in-1 pak is present. */
dalton@2749
   187
        haptic->hwdata->type = EZF3IN1;
dalton@2749
   188
        NDS_EZF_ChipReset();
dalton@2749
   189
    } else {
dalton@2749
   190
        /* no haptic present */
dalton@2749
   191
        SDL_SYS_LogicError();
dalton@2749
   192
        return -1;
dalton@2749
   193
    }
dalton@2749
   194
dalton@2749
   195
    return 0;
dalton@2749
   196
}
dalton@2749
   197
dalton@2749
   198
dalton@2749
   199
int
dalton@2749
   200
SDL_SYS_HapticMouse(void)
dalton@2749
   201
{
dalton@2749
   202
    return -1;
dalton@2749
   203
}
dalton@2749
   204
dalton@2749
   205
dalton@2749
   206
int
dalton@2749
   207
SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
dalton@2749
   208
{
dalton@2749
   209
    return 0;
dalton@2749
   210
}
dalton@2749
   211
dalton@2749
   212
dalton@2749
   213
int
dalton@2749
   214
SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
dalton@2749
   215
{
dalton@2749
   216
    /*SDL_SYS_LogicError();*/
dalton@2749
   217
    return -1;
dalton@2749
   218
}
dalton@2749
   219
dalton@2749
   220
dalton@2749
   221
int
dalton@2749
   222
SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
dalton@2749
   223
{
dalton@2749
   224
    return 0;
dalton@2749
   225
}
dalton@2749
   226
dalton@2749
   227
dalton@2749
   228
void
dalton@2749
   229
SDL_SYS_HapticClose(SDL_Haptic * haptic)
dalton@2749
   230
{
dalton@2749
   231
    return;
dalton@2749
   232
}
dalton@2749
   233
dalton@2749
   234
dalton@2749
   235
void
dalton@2749
   236
SDL_SYS_HapticQuit(void)
dalton@2749
   237
{
dalton@2749
   238
    return;
dalton@2749
   239
}
dalton@2749
   240
dalton@2749
   241
dalton@2749
   242
int
dalton@2749
   243
SDL_SYS_HapticNewEffect(SDL_Haptic * haptic,
dalton@2749
   244
                        struct haptic_effect *effect, SDL_HapticEffect * base)
dalton@2749
   245
{
dalton@2749
   246
    SDL_SYS_LogicError();
dalton@2749
   247
    return -1;
dalton@2749
   248
}
dalton@2749
   249
dalton@2749
   250
dalton@2749
   251
int
dalton@2749
   252
SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
dalton@2749
   253
                           struct haptic_effect *effect,
dalton@2749
   254
                           SDL_HapticEffect * data)
dalton@2749
   255
{
dalton@2749
   256
    SDL_SYS_LogicError();
dalton@2749
   257
    return -1;
dalton@2749
   258
}
dalton@2749
   259
dalton@2749
   260
dalton@2749
   261
int
dalton@2749
   262
SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
dalton@2749
   263
                        Uint32 iterations)
dalton@2749
   264
{
dalton@2749
   265
    SDL_SYS_LogicError();
dalton@2749
   266
    return -1;
dalton@2749
   267
}
dalton@2749
   268
dalton@2749
   269
dalton@2749
   270
int
dalton@2749
   271
SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
dalton@2749
   272
{
dalton@2749
   273
    SDL_SYS_LogicError();
dalton@2749
   274
    return -1;
dalton@2749
   275
}
dalton@2749
   276
dalton@2749
   277
dalton@2749
   278
void
dalton@2749
   279
SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
dalton@2749
   280
{
dalton@2749
   281
    SDL_SYS_LogicError();
dalton@2749
   282
    return;
dalton@2749
   283
}
dalton@2749
   284
dalton@2749
   285
dalton@2749
   286
int
dalton@2749
   287
SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic,
dalton@2749
   288
                              struct haptic_effect *effect)
dalton@2749
   289
{
dalton@2749
   290
    SDL_SYS_LogicError();
dalton@2749
   291
    return -1;
dalton@2749
   292
}
dalton@2749
   293
dalton@2749
   294
dalton@2749
   295
int
dalton@2749
   296
SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
dalton@2749
   297
{
dalton@2749
   298
    SDL_SYS_LogicError();
dalton@2749
   299
    return -1;
dalton@2749
   300
}
dalton@2749
   301
dalton@2749
   302
dalton@2749
   303
int
dalton@2749
   304
SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
dalton@2749
   305
{
dalton@2749
   306
    SDL_SYS_LogicError();
dalton@2749
   307
    return -1;
dalton@2749
   308
}
dalton@2749
   309
dalton@2749
   310
int
dalton@2749
   311
SDL_SYS_HapticPause(SDL_Haptic * haptic)
dalton@2749
   312
{
dalton@2749
   313
    SDL_SYS_LogicError();
dalton@2749
   314
    return -1;
dalton@2749
   315
}
dalton@2749
   316
dalton@2749
   317
int
dalton@2749
   318
SDL_SYS_HapticUnpause(SDL_Haptic * haptic)
dalton@2749
   319
{
dalton@2749
   320
    SDL_SYS_LogicError();
dalton@2749
   321
    return -1;
dalton@2749
   322
}
dalton@2749
   323
dalton@2749
   324
int
dalton@2749
   325
SDL_SYS_HapticStopAll(SDL_Haptic * haptic)
dalton@2749
   326
{
dalton@2749
   327
    SDL_SYS_LogicError();
dalton@2749
   328
    return -1;
dalton@2749
   329
}
dalton@2749
   330
dalton@2749
   331
dalton@2749
   332
dalton@2749
   333
#endif /* SDL_HAPTIC_NDS */
dalton@2749
   334
/* vi: set ts=4 sw=4 expandtab: */