00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __PSPV4_H__
00013 #define __PSPV4_H__
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #pragma systemFile
00038
00039 #ifndef __COMMON_H__
00040 #include "common.h"
00041 #endif
00042
00043 #define PSPV4_I2C_ADDR 0x02
00044
00045 #define PSPV4_REG_CMD 0x41
00046
00047 #define PSPV4_CMD_INITIALISE 0x49
00048
00049 #define PSPV4_REG_BTN_SET_1 0x42
00050 #define PSPV4_REG_BTN_SET_2 0x43
00051 #define PSPV4_REG_JLEFT 0x44
00052 #define PSPV4_REG_JRIGHT 0x46
00053
00054 #define PSPV4_REG_PRES_JPAD_UP 0x4A
00055 #define PSPV4_REG_PRES_JPAD_RIGHT 0x4B
00056 #define PSPV4_REG_PRES_JPAD_DOWN 0x4C
00057 #define PSPV4_REG_PRES_JPAD_LEFT 0x4D
00058 #define PSPV4_REG_PRES_BTN_L2 0x4E
00059 #define PSPV4_REG_PRES_BTN_R2 0x4F
00060 #define PSPV4_REG_PRES_BTN_L1 0x50
00061 #define PSPV4_REG_PRES_BTN_R1 0x51
00062 #define PSPV4_REG_PRES_BTN_TRIANGLE 0x52
00063 #define PSPV4_REG_PRES_BTN_SQUARE 0x53
00064 #define PSPV4_REG_PRES_BTN_CIRCLE 0x54
00065 #define PSPV4_REG_PRES_BTN_CROSS 0x55
00066
00067 #define PSPV4_REG_REF_SIGNAL_CODE 0x56
00068 #define PSPV4_REG_REF_TX_TYPE 0x57
00069 #define PSPV4_REG_REF_SIGNAL_RAW 0x58
00070 #define PSPV4_REG_REF_SIGNAL_COUNT 0x5C
00071
00072 #define PSPV4_SIGNAL_FAST_REWIND 0x32
00073 #define PSPV4_SIGNAL_FAST_FORWARD 0x34
00074 #define PSPV4_SIGNAL_PLAY 0x35
00075 #define PSPV4_SIGNAL_STOP 0x36
00076
00077 tByteArray PSPV4_I2CRequest;
00078 tByteArray PSPV4_I2CReply;
00079
00080 bool PSPV4initalised[4] = {false, false, false, false};
00081
00082 typedef struct
00083 {
00084
00085 ubyte left1Btn;
00086 ubyte left2Btn;
00087 ubyte right1Btn;
00088 ubyte rigth2Btn;
00089 ubyte joypadLeft;
00090 ubyte joypadDown;
00091 ubyte joypadRight;
00092 ubyte joypadUp;
00093 ubyte triangleBtn;
00094 ubyte squareBtn;
00095 ubyte circleBtn;
00096 ubyte crossBtn;
00097 ubyte joystickLeftBtn;
00098 ubyte joystickRightBtn;
00099 ubyte selectBtn;
00100 ubyte startBtn;
00101 int joystickLeft_x;
00102 int joystickLeft_y;
00103 int joystickRight_x;
00104 int joystickRight_y;
00105 } tPSP;
00106
00107 tPSP pspController;
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 bool _PSPV4init(tSensors link, ubyte address = PSPV4_I2C_ADDR)
00119 {
00120 PSPV4_I2CRequest[0] = 3;
00121 PSPV4_I2CRequest[1] = address ;
00122 PSPV4_I2CRequest[2] = 0x41;
00123 PSPV4_I2CRequest[3] = PSPV4_CMD_INITIALISE;
00124
00125 if (!writeI2C(link, PSPV4_I2CRequest))
00126 {
00127 return false;
00128 }
00129
00130 wait1Msec(100);
00131 PSPV4initalised[link] = true;
00132
00133 return true;
00134 }
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 ubyte _PSPV4readByte (tSensors link, ubyte reg, ubyte address = PSPV4_I2C_ADDR)
00147 {
00148
00149 if (!PSPV4initalised[link])
00150 {
00151 _PSPV4init(link, address);
00152 }
00153
00154 memset(PSPV4_I2CRequest, 0, sizeof(PSPV4_I2CRequest));
00155
00156 PSPV4_I2CRequest[0] = 2;
00157 PSPV4_I2CRequest[1] = address;
00158 PSPV4_I2CRequest[2] = reg;
00159
00160 if (!writeI2C(link, PSPV4_I2CRequest, PSPV4_I2CReply, 1))
00161 return 0;
00162
00163 return PSPV4_I2CReply[0];
00164 }
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 bool PSPV4readButtons(tSensors link, tPSP &controllerState, ubyte address = PSPV4_I2C_ADDR)
00176 {
00177 ubyte b0;
00178 ubyte b1;
00179
00180
00181 if (!PSPV4initalised[link])
00182 {
00183 _PSPV4init(link, address);
00184 }
00185
00186 memset(PSPV4_I2CRequest, 0, sizeof(PSPV4_I2CRequest));
00187 memset(controllerState, 0, sizeof(controllerState));
00188
00189 PSPV4_I2CRequest[0] = 2;
00190 PSPV4_I2CRequest[1] = address;
00191 PSPV4_I2CRequest[2] = PSPV4_REG_BTN_SET_1;
00192
00193 if (!writeI2C(link, PSPV4_I2CRequest, PSPV4_I2CReply, 6))
00194 return false;
00195
00196 b0 = ~PSPV4_I2CReply[0];
00197 b1 = ~PSPV4_I2CReply[1];
00198
00199 controllerState.selectBtn = (b0 >> 0) & 0x01;
00200 controllerState.joystickLeftBtn = (b0 >> 1) & 0x01;
00201 controllerState.joystickRightBtn = (b0 >> 2) & 0x01;
00202 controllerState.startBtn = (b0 >> 3) & 0x01;
00203 controllerState.joypadUp = (b0 >> 4) & 0x01;
00204 controllerState.joypadRight = (b0 >> 5) & 0x01;
00205 controllerState.joypadDown = (b0 >> 6) & 0x01;
00206 controllerState.joypadLeft = (b0 >> 7) & 0x01;
00207
00208 controllerState.left2Btn = (b1 >> 0) & 0x01;
00209 controllerState.rigth2Btn = (b1 >> 1) & 0x01;
00210 controllerState.left1Btn = (b1 >> 2) & 0x01;
00211 controllerState.right1Btn = (b1 >> 3) & 0x01;
00212 controllerState.triangleBtn = (b1 >> 4) & 0x01;
00213 controllerState.circleBtn = (b1 >> 5) & 0x01;
00214 controllerState.crossBtn = (b1 >> 6) & 0x01;
00215 controllerState.squareBtn = (b1 >> 7) & 0x01;
00216
00217 controllerState.joystickLeft_x = (((long)PSPV4_I2CReply[2] - 128) * 100)/128;
00218 controllerState.joystickLeft_y = (((long)PSPV4_I2CReply[3] - 128) * 100)/128;
00219 controllerState.joystickRight_x = (((long)PSPV4_I2CReply[4] - 128) * 100)/128;
00220 controllerState.joystickRight_y = (((long)PSPV4_I2CReply[5] - 128) * 100)/128;
00221
00222 return true;
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 bool PSPV4readButtonPressure(tSensors link, tPSP &controllerState, ubyte address = PSPV4_I2C_ADDR)
00235 {
00236
00237 if (!PSPV4initalised[link])
00238 {
00239 _PSPV4init(link, address);
00240 }
00241
00242 if (!PSPV4readButtons(link, controllerState, address))
00243 {
00244 return false;
00245 }
00246
00247 memset(PSPV4_I2CRequest, 0, sizeof(PSPV4_I2CRequest));
00248 PSPV4_I2CRequest[0] = 2;
00249 PSPV4_I2CRequest[1] = address;
00250 PSPV4_I2CRequest[2] = PSPV4_REG_PRES_JPAD_UP;
00251
00252 if (!writeI2C(link, PSPV4_I2CRequest, PSPV4_I2CReply, 12))
00253 return false;
00254
00255 controllerState.joypadUp = (PSPV4_I2CReply[0] * 100) / 255;
00256 controllerState.joypadRight = (PSPV4_I2CReply[1] * 100) / 255;
00257 controllerState.joypadDown = (PSPV4_I2CReply[2] * 100) / 255;
00258 controllerState.joypadLeft = (PSPV4_I2CReply[3] * 100) / 255;
00259
00260 controllerState.left2Btn = (PSPV4_I2CReply[4] * 100) / 255;
00261 controllerState.rigth2Btn = (PSPV4_I2CReply[5] * 100) / 255;
00262 controllerState.left1Btn = (PSPV4_I2CReply[6] * 100) / 255;
00263 controllerState.right1Btn = (PSPV4_I2CReply[7] * 100) / 255;
00264 controllerState.triangleBtn = (PSPV4_I2CReply[8] * 100) / 255;
00265 controllerState.circleBtn = (PSPV4_I2CReply[9] * 100) / 255;
00266 controllerState.crossBtn = (PSPV4_I2CReply[10] * 100) / 255;
00267 controllerState.squareBtn = (PSPV4_I2CReply[11] * 100) / 255;
00268
00269 return true;
00270 }
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283 byte PSPV4readRefSignal(tSensors link, ubyte address = PSPV4_I2C_ADDR)
00284 {
00285 return _PSPV4readByte(link, PSPV4_REG_REF_SIGNAL_CODE, address);
00286 }
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 byte PSPV4readRefSignalCount(tSensors link, ubyte address = PSPV4_I2C_ADDR)
00297 {
00298 return _PSPV4readByte(link, PSPV4_REG_REF_SIGNAL_COUNT, address);
00299 }
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309 byte PSPV4readRefTXType(tSensors link, ubyte address = PSPV4_I2C_ADDR)
00310 {
00311 return _PSPV4readByte(link, PSPV4_REG_REF_TX_TYPE, address);
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 long PSPV4readRawRefTXValue(tSensors link, ubyte address = PSPV4_I2C_ADDR)
00323 {
00324
00325 if (!PSPV4initalised[link])
00326 {
00327 _PSPV4init(link, address);
00328 }
00329
00330 memset(PSPV4_I2CRequest, 0, sizeof(PSPV4_I2CRequest));
00331
00332 PSPV4_I2CRequest[0] = 2;
00333 PSPV4_I2CRequest[1] = address;
00334 PSPV4_I2CRequest[2] = PSPV4_REG_REF_SIGNAL_RAW;
00335
00336 if (!writeI2C(link, PSPV4_I2CRequest, PSPV4_I2CReply, 4))
00337 return 0;
00338
00339 return PSPV4_I2CReply[0] + (PSPV4_I2CReply[1]<<8) + (PSPV4_I2CReply[2]<<16) + (PSPV4_I2CReply[3]<<24);
00340 }
00341
00342
00343
00344 #endif // __PSPV4_H__
00345
00346
00347
00348
00349
00350