src/haptic/nds/SDL_syshaptic.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 22 Aug 2010 12:34:31 -0700
changeset 4761 bb2e32f5a556
parent 3013 8cc00819c8d6
child 4868 d6adaafcfb10
permissions -rw-r--r--
Removed spurious carriage returns
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
slouken@4761
    46
void
slouken@4761
    47
NDS_EZF_OpenNorWrite() 
dalton@2750
    48
{
slouken@4761
    49
    GBA_BUS[0x0FF0000] = 0xD200;
slouken@4761
    50
    GBA_BUS[0x0000000] = 0x1500;
slouken@4761
    51
    GBA_BUS[0x0010000] = 0xD200;
slouken@4761
    52
    GBA_BUS[0x0020000] = 0x1500;
slouken@4761
    53
    GBA_BUS[0x0E20000] = 0x1500;
slouken@4761
    54
    GBA_BUS[0x0FE0000] = 0x1500;
slouken@4761
    55
} 
dalton@2749
    56
slouken@4761
    57
void
dalton@2750
    58
slouken@4761
    59
NDS_EZF_CloseNorWrite() 
slouken@4761
    60
{
slouken@4761
    61
    GBA_BUS[0x0FF0000] = 0xD200;
slouken@4761
    62
    GBA_BUS[0x0000000] = 0x1500;
slouken@4761
    63
    GBA_BUS[0x0010000] = 0xD200;
slouken@4761
    64
    GBA_BUS[0x0020000] = 0x1500;
slouken@4761
    65
    GBA_BUS[0x0E20000] = 0xD200;
dalton@2750
    66
    GBA_BUS[0x0FE0000] = 0x1500;
dalton@2750
    67
}
dalton@2750
    68
dalton@2750
    69
void
slouken@4761
    70
NDS_EZF_ChipReset()
slouken@4761
    71
{
slouken@4761
    72
    GBA_BUS[0x0000] = 0x00F0;
dalton@2750
    73
    GBA_BUS[0x1000] = 0x00F0;
slouken@4761
    74
} uint32 NDS_EZF_IsPresent() 
dalton@2749
    75
{
dalton@2749
    76
    vuint16 id1, id2;
dalton@2749
    77
dalton@2749
    78
    NDS_EZF_OpenNorWrite();
dalton@2749
    79
dalton@2749
    80
    GBA_BUS[0x0555] = 0x00AA;
dalton@2749
    81
    GBA_BUS[0x02AA] = 0x0055;
dalton@2749
    82
    GBA_BUS[0x0555] = 0x0090;
dalton@2749
    83
    GBA_BUS[0x1555] = 0x00AA;
slouken@4761
    84
    GBA_BUS[0x12AA] = 0x0055;
slouken@4761
    85
    GBA_BUS[0x1555] = 0x0090;
slouken@4761
    86
    id1 = GBA_BUS[0x0001];
dalton@2750
    87
    id2 = GBA_BUS[0x1001];
slouken@4761
    88
    if ((id1 != 0x227E) || (id2 != 0x227E)) {
dalton@2750
    89
        NDS_EZF_CloseNorWrite();
slouken@4761
    90
        return 0;
slouken@4761
    91
    }
dalton@2749
    92
    id1 = GBA_BUS[0x000E];
dalton@2749
    93
    id2 = GBA_BUS[0x100E];
slouken@4761
    94
slouken@4761
    95
    NDS_EZF_CloseNorWrite();
dalton@2749
    96
    if (id1 == 0x2218 && id2 == 0x2218) {
slouken@4761
    97
        return 1;
slouken@4761
    98
    }
slouken@4761
    99
    return 0;
slouken@4761
   100
}
dalton@2749
   101
void
dalton@2750
   102
NDS_EZF_SetShake(u8 pos) 
slouken@4761
   103
{
slouken@4761
   104
    u16 data = ((pos % 3) | 0x00F0);
slouken@4761
   105
    GBA_BUS[0x0FF0000] = 0xD200;
slouken@4761
   106
    GBA_BUS[0x0000000] = 0x1500;
slouken@4761
   107
    GBA_BUS[0x0010000] = 0xD200;
slouken@4761
   108
    GBA_BUS[0x0020000] = 0x1500;
dalton@2749
   109
    GBA_BUS[0x0F10000] = data;
dalton@2750
   110
    GBA_BUS[0x0FE0000] = 0x1500;
dalton@2750
   111
slouken@4761
   112
    GBA_BUS[0] = 0x0000;        /* write any value for vibration. */
dalton@2749
   113
    GBA_BUS[0] = 0x0002;
dalton@2749
   114
}
dalton@2749
   115
dalton@2749
   116
static int
dalton@2749
   117
SDL_SYS_LogicError(void)
dalton@2749
   118
{
dalton@2749
   119
    SDL_SetError("Logic error: No haptic devices available.");
dalton@2749
   120
    return 0;
dalton@2749
   121
}
dalton@2749
   122
dalton@2749
   123
dalton@2749
   124
int
dalton@2749
   125
SDL_SYS_HapticInit(void)
dalton@2750
   126
{
dalton@2749
   127
    int ret = 0;
dalton@2749
   128
    if (isRumbleInserted()) {
dalton@2749
   129
        /* official rumble pak is present. */
dalton@2750
   130
        ret = 1;
dalton@2749
   131
        printf("debug: haptic present: nintendo\n");
dalton@2749
   132
    } else if (NDS_EZF_IsPresent()) {
dalton@2749
   133
        /* ezflash 3-in-1 pak is present. */
dalton@2749
   134
        ret = 1;
dalton@2749
   135
        printf("debug: haptic present: ezf3in1\n");
dalton@2749
   136
        NDS_EZF_ChipReset();
dalton@2749
   137
    } else {
dalton@2749
   138
        printf("debug: no haptic found\n");
dalton@2749
   139
    }
dalton@2749
   140
dalton@2749
   141
    return ret;
dalton@2749
   142
}
dalton@2749
   143
dalton@2749
   144
dalton@2749
   145
const char *
dalton@2750
   146
SDL_SYS_HapticName(int index)
dalton@2750
   147
{
dalton@2750
   148
    if (nds_haptic) {
dalton@2750
   149
        switch (nds_haptic->hwdata->type) {
dalton@2750
   150
        case OFFICIAL:
dalton@2750
   151
            return "Nintendo DS Rumble Pak";
dalton@2750
   152
        case EZF3IN1:
dalton@2750
   153
            return "EZFlash 3-in-1 Rumble";
dalton@2749
   154
        default:
dalton@2749
   155
            return NULL;
dalton@2749
   156
        }
dalton@2749
   157
    }
dalton@2749
   158
    return NULL;
dalton@2749
   159
}
dalton@2749
   160
dalton@2749
   161
dalton@2749
   162
int
dalton@2750
   163
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
dalton@2749
   164
{
dalton@2749
   165
    if (!haptic) {
dalton@2749
   166
        return -1;
dalton@2749
   167
    }
dalton@2750
   168
dalton@2749
   169
    haptic->hwdata = SDL_malloc(sizeof(NDS_HapticData));
dalton@2749
   170
    if (!haptic->hwdata) {
dalton@2749
   171
        SDL_OutOfMemory();
dalton@2749
   172
        return -1;
dalton@2749
   173
    }
dalton@2749
   174
    nds_haptic = haptic;
dalton@2749
   175
dalton@2749
   176
    haptic->supported = SDL_HAPTIC_CONSTANT;
dalton@2749
   177
dalton@2750
   178
    /* determine what is here, if anything */
dalton@2749
   179
    haptic->hwdata->type = NONE;
dalton@2749
   180
    if (isRumbleInserted()) {
dalton@2750
   181
        /* official rumble pak is present. */
dalton@2749
   182
        haptic->hwdata->type = OFFICIAL;
dalton@2749
   183
    } else if (NDS_EZF_IsPresent()) {
dalton@2749
   184
        /* ezflash 3-in-1 pak is present. */
dalton@2749
   185
        haptic->hwdata->type = EZF3IN1;
dalton@2749
   186
        NDS_EZF_ChipReset();
dalton@2749
   187
    } else {
dalton@2749
   188
        /* no haptic present */
dalton@2749
   189
        SDL_SYS_LogicError();
dalton@2749
   190
        return -1;
dalton@2749
   191
    }
dalton@2749
   192
dalton@2749
   193
    return 0;
dalton@2749
   194
}
dalton@2749
   195
dalton@2749
   196
dalton@2749
   197
int
dalton@2749
   198
SDL_SYS_HapticMouse(void)
dalton@2749
   199
{
dalton@2749
   200
    return -1;
dalton@2749
   201
}
dalton@2749
   202
dalton@2749
   203
dalton@2749
   204
int
dalton@2749
   205
SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
dalton@2749
   206
{
dalton@2749
   207
    return 0;
dalton@2749
   208
}
dalton@2749
   209
dalton@2749
   210
dalton@2749
   211
int
dalton@2750
   212
SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
dalton@2749
   213
{
dalton@2749
   214
    /*SDL_SYS_LogicError(); */
dalton@2749
   215
    return -1;
dalton@2749
   216
}
dalton@2749
   217
dalton@2749
   218
dalton@2749
   219
int
dalton@2749
   220
SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
dalton@2749
   221
{
dalton@2749
   222
    return 0;
dalton@2749
   223
}
dalton@2749
   224
dalton@2749
   225
dalton@2749
   226
void
dalton@2749
   227
SDL_SYS_HapticClose(SDL_Haptic * haptic)
dalton@2749
   228
{
dalton@2749
   229
    return;
dalton@2749
   230
}
dalton@2749
   231
dalton@2749
   232
dalton@2749
   233
void
dalton@2749
   234
SDL_SYS_HapticQuit(void)
dalton@2749
   235
{
dalton@2749
   236
    return;
dalton@2749
   237
}
dalton@2749
   238
dalton@2749
   239
dalton@2749
   240
int
dalton@2749
   241
SDL_SYS_HapticNewEffect(SDL_Haptic * haptic,
dalton@2749
   242
                        struct haptic_effect *effect, SDL_HapticEffect * base)
dalton@2749
   243
{
dalton@2749
   244
    SDL_SYS_LogicError();
dalton@2749
   245
    return -1;
dalton@2749
   246
}
dalton@2749
   247
dalton@2749
   248
dalton@2749
   249
int
dalton@2749
   250
SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
dalton@2749
   251
                           struct haptic_effect *effect,
dalton@2749
   252
                           SDL_HapticEffect * data)
dalton@2749
   253
{
dalton@2749
   254
    SDL_SYS_LogicError();
dalton@2749
   255
    return -1;
dalton@2749
   256
}
dalton@2749
   257
dalton@2749
   258
dalton@2749
   259
int
dalton@2749
   260
SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
dalton@2749
   261
                        Uint32 iterations)
dalton@2749
   262
{
dalton@2749
   263
    SDL_SYS_LogicError();
dalton@2749
   264
    return -1;
dalton@2749
   265
}
dalton@2749
   266
dalton@2749
   267
dalton@2749
   268
int
dalton@2749
   269
SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
dalton@2749
   270
{
dalton@2749
   271
    SDL_SYS_LogicError();
dalton@2749
   272
    return -1;
dalton@2749
   273
}
dalton@2749
   274
dalton@2749
   275
dalton@2749
   276
void
dalton@2749
   277
SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
dalton@2749
   278
{
dalton@2749
   279
    SDL_SYS_LogicError();
dalton@2749
   280
    return;
dalton@2749
   281
}
dalton@2749
   282
dalton@2749
   283
dalton@2749
   284
int
dalton@2749
   285
SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic,
dalton@2749
   286
                              struct haptic_effect *effect)
dalton@2749
   287
{
dalton@2749
   288
    SDL_SYS_LogicError();
dalton@2749
   289
    return -1;
dalton@2749
   290
}
dalton@2749
   291
dalton@2749
   292
dalton@2749
   293
int
dalton@2749
   294
SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
dalton@2749
   295
{
dalton@2749
   296
    SDL_SYS_LogicError();
dalton@2749
   297
    return -1;
dalton@2749
   298
}
dalton@2749
   299
dalton@2749
   300
dalton@2749
   301
int
dalton@2749
   302
SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
dalton@2749
   303
{
dalton@2749
   304
    SDL_SYS_LogicError();
dalton@2749
   305
    return -1;
dalton@2749
   306
}
dalton@2749
   307
dalton@2749
   308
int
dalton@2749
   309
SDL_SYS_HapticPause(SDL_Haptic * haptic)
dalton@2749
   310
{
dalton@2749
   311
    SDL_SYS_LogicError();
dalton@2749
   312
    return -1;
dalton@2749
   313
}
dalton@2749
   314
dalton@2749
   315
int
dalton@2749
   316
SDL_SYS_HapticUnpause(SDL_Haptic * haptic)
dalton@2749
   317
{
dalton@2749
   318
    SDL_SYS_LogicError();
dalton@2749
   319
    return -1;
dalton@2749
   320
}
dalton@2749
   321
dalton@2749
   322
int
dalton@2749
   323
SDL_SYS_HapticStopAll(SDL_Haptic * haptic)
dalton@2749
   324
{
dalton@2749
   325
    SDL_SYS_LogicError();
dalton@2749
   326
    return -1;
dalton@2749
   327
}
dalton@2749
   328
dalton@2749
   329
dalton@2749
   330
slouken@4761
   331
#endif /* SDL_HAPTIC_NDS */
slouken@4761
   332
/* vi: set ts=4 sw=4 expandtab: */