-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSP-Nx-lib.nxc
executable file
·182 lines (156 loc) · 4.86 KB
/
PSP-Nx-lib.nxc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/************************************************************************/
/* */
/* Program Name: PSP-Nx-lib.nxc */
/* ============================= */
/* */
/* Copyright (c) 2008 by mindsensors.com */
/* Email: info (<at>) mindsensors (<dot>) com */
/* */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; version 3 of the License. */
/* Read the license at: http://www.gnu.org/licenses/gpl.txt */
/* */
/************************************************************************/
/*
* When Who Comments
* 10/10/08 Deepak Patil Initial authoring.
*/
/*--------------------------------------
Controller button layout:
----------------------------------------
L1 R1
L2 R2
d triang
a c square circle
b cross
l_j_b r_j_b
l_j_x r_j_x
l_j_y r_j_y
-------------------------------------- */
/*
bits as follows:
b1: a b c d x r_j_b l_j_b x
b2: square cross circle triang R1 L1 R2 L2
*/
struct psp
{
byte b1; //raw byte read from PSP-Nx
byte b2; //raw byte read from PSP-Nx
// computed button states
byte l1;
byte l2;
byte r1;
byte r2;
byte a;
byte b;
byte c;
byte d;
byte triang;
byte square;
byte circle;
byte cross;
byte l_j_b; // joystick button state
byte r_j_b; // joystick button state
int l_j_x; // analog value of joystick scaled from 0 to 100
int l_j_y; // analog value of joystick scaled from 0 to 100
int r_j_x; // analog value of joystick scaled from 0 to 100
int r_j_y; // analog value of joystick scaled from 0 to 100
};
void PSP_SendCommand(byte port, byte i2cAddr, byte cmd)
{
byte cmdBuf[];
ArrayBuild(cmdBuf, i2cAddr, 0x41, cmd);
I2CWrite(port, 0, cmdBuf);
int status = I2CCheckStatus(port);
while (status > NO_ERR)
status = I2CCheckStatus(port);
}
void PSP_PowerOFF(byte port, byte i2cAddr)
{
PSP_SendCommand(port, i2cAddr, 'D');
}
void PSP_PowerON(byte port, byte i2cAddr)
{
PSP_SendCommand(port, i2cAddr, 'E');
}
void PSP_ReadButtonState(byte port, byte i2cAddr, psp & currState)
{
byte message[];
byte buf[10];
int count, l;
byte b0;
byte b1;
byte nByteReady = 0;
SetSensorLowspeed(port);
PSP_PowerON(port, i2cAddr);
//PSP_PowerOFF(port, i2cAddr);
currState.b1 = 0;
currState.b2 = 0;
currState.l1 = 0;
currState.l2 = 0;
currState.r1 = 0;
currState.r2 = 0;
currState.a = 0;
currState.b = 0;
currState.c = 0;
currState.d = 0;
currState.triang = 0;
currState.square = 0;
currState.circle = 0;
currState.cross = 0;
currState.l_j_b = 0;
currState.r_j_b = 0;
currState.l_j_x = 0;
currState.l_j_y = 0;
currState.r_j_x = 0;
currState.r_j_y = 0;
// 0x42 - Button 1
// 0x43 - Button 2
// 0x44 - X left
// 0x45 - Y left
// 0x46 - X right
// 0x47 - Y right
ArrayBuild(message, i2cAddr, 0x42);
while (I2CStatus(port, nByteReady) == STAT_COMM_PENDING);
count = 6;
if(I2CBytes(port, message, count, buf)) {
b0 = buf[0];
b1 = buf[1];
currState.b1 = b0;
currState.b2 = b1;
currState.l_j_b = (b0 >> 1) & 0x01;
currState.r_j_b = (b0 >> 2) & 0x01;
currState.d = (b0 >> 4) & 0x01;
currState.c = (b0 >> 5) & 0x01;
currState.b = (b0 >> 6) & 0x01;
currState.a = (b0 >> 7) & 0x01;
currState.l2 = (b1 ) & 0x01;
currState.r2 = (b1 >> 1) & 0x01;
currState.l1 = (b1 >> 2) & 0x01;
currState.r1 = (b1 >> 3) & 0x01;
currState.triang = (b1 >> 4) & 0x01;
currState.circle = (b1 >> 5) & 0x01;
currState.cross = (b1 >> 6) & 0x01;
currState.square = (b1 >> 7) & 0x01;
currState.l_j_x = ((buf[2] - 128) * 100)/128;
currState.l_j_y = ((buf[3] - 128) * 100)/128;
currState.r_j_x = ((buf[4] - 128) * 100)/128;
currState.r_j_y = ((buf[5] - 128) * 100)/128;
}
}
string PSP_format_bin ( int i ) {
string s;
int j;
int b = 0x80;
s = "";
for ( j = 0; j < 8; j++) {
if ( i&b ) {
s += NumToStr(1);
} else {
s += NumToStr(0);
}
b = b>>1;
}
return (s);
}