-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWii.java
More file actions
259 lines (182 loc) · 5.18 KB
/
Copy pathWii.java
File metadata and controls
259 lines (182 loc) · 5.18 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import uj.lang.*;
public class Wii{
static byte[] gScreenData;
private static void printString(String s){
int i, L = s.Xlen_(); //we cheat by not claling string methods - they are slow
for(i = 0; i < L; i++) uj.lang.RT.consolePut((char)s.XbyteAt_(i));
}
private static byte[] i2cOp(byte[] in, int numOut){
byte[] out = null;
int i, L;
L = in.length;
UC.i2cStart();
UC.i2cSend((byte)0xA4);
for(i = 0; i < L; i++) UC.i2cSend(in[i]);
if(numOut != 0){
out = new byte[numOut];
UC.i2cStart();
UC.i2cSend((byte)0xA5);
for(i = 0; i < numOut; i++) out[i] = UC.i2cRecv(i != numOut - 1);
}
UC.i2cStop();
return out;
}
private static void puts(String s){
int i, L = s.Xlen_(); //we cheat by not claling string methods - they are slow
for(i = 0; i < L; i++) UC.lcdChar((char)s.XbyteAt_(i));
}
private static void setPix(int r, int c, boolean on){
//screen is as follows: 8 char rows, each col of which is 8 px high (one byte), 128 cols in screen
byte mask = (byte)(1 << (r & 7));
int rowOver8 = r >> 3;
int idx = c + (rowOver8 << 7);
if(on) mask = (byte)(gScreenData[idx] | mask);
else mask = (byte)(gScreenData[idx] &~ mask);
gScreenData[idx] = mask;
UC.glcdSetPixels(rowOver8, c, gScreenData[idx]);
}
private static boolean getPix(int r, int c){
byte mask = (byte)(1 << (r & 7));
int rowOver8 = r >> 3;
int idx = c + (rowOver8 << 7);
return (gScreenData[idx] & mask) != 0;
}
private static void erasePix(){
gScreenData = new byte [1024]; //cheap and easy - let garbage collector handle the rest :)
//now actually erase the screen
byte row [] = new byte[128];
for(int i = 0; i < 8; i++) UC.glcdSetManyPixels(i, 0, row, 0, 128);
}
private static void cube(int eyeX, int eyeY, int eyeZ, boolean on){
UC.lcdGotoXY(0, 0);
puts(toString(eyeX) + " _ " + toString(eyeY) + " _ " + toString(eyeZ) + " ");
}
private static void line(int x0, int y0, int x1, int y1, boolean on){
int dx = x1 - x0;
int dy = y1 - y0;
if(dx < 0) dx = -dx;
if(dy < 0) dy = -dy;
int sx = x0 < x1 ? 1 : -1;
int sy = y0 < y1 ? 1 : -1;
int err = dx - dy;
while(true){
setPix(y0, x0, on);
if(x0 == x1 && y0 == y1) return;
int e2 = err << 1;
if(e2 > -dy){
err -= dy;
x0 += sx;
}
if(e2 < dx){
err += dx;
y0 += sy;
}
}
}
public static int sqrt(int x){
int guess = 0x5A82; //approx sqrt(0x7FFFFFFF)/2
int step = (guess + 1) >> 1;
int approx;
while(step != 0){
approx = guess * guess;
if(approx == x) break;
else if(approx > x) guess -= step;
else guess += step;
step = step >> 1;
}
return guess;
}
public static void main(){
//draw mode vals
int posX = 64, posY = 32;
boolean posV = false;
boolean drawMode = true;
erasePix();
//init
i2cOp(new byte[]{(byte)0xF0, (byte)0x55}, 0);
i2cOp(new byte[]{(byte)0xFB, 0}, 0);
i2cOp(new byte[]{(byte)0xFA}, 6);
//send crypto key
i2cOp(new byte[]{(byte)0xF0, (byte)0xAA}, 0);
i2cOp(new byte[]{(byte)0x40, 0, 0, 0, 0, 0, 0}, 0);
i2cOp(new byte[]{(byte)0x40, 0, 0, 0, 0, 0, 0}, 0);
i2cOp(new byte[]{(byte)0x40, 0, 0, 0, 0}, 0);
//loop
while(true){
byte[] vals = i2cOp(new byte[]{0}, 6);
int joyX, joyY, accX, accY, accZ, t;
boolean Z, C;
joyX = ((int)vals[0]) & 0xFF;
joyY = ((int)vals[1]) & 0xFF;
accX = (((int)vals[2]) & 0xFF) << 2;
accY = (((int)vals[3]) & 0xFF) << 2;
accZ = (((int)vals[4]) & 0xFF) << 2;
t = ((int)vals[5]) & 0xFF;
Z = (t & 0x01) == 0;
C = (t & 0x02) == 0;
if((t & 0x04) != 0) accX += 2;
if((t & 0x08) != 0) accX += 1;
if((t & 0x10) != 0) accY += 2;
if((t & 0x20) != 0) accY += 1;
if((t & 0x40) != 0) accZ += 2;
if((t & 0x80) != 0) accZ += 1;
if(drawMode){
if(C && Z){ //switch mode
erasePix();
posV = false;
drawMode = false;
}
else{
if(C) posV = true; //C writes
else if(Z) posV = false; //Z erases
setPix(posY, posX, posV);
posX = joyX >> 1;
posY = (255 - joyY) >> 2;
posV = getPix(posY, posX);
setPix(posY, posX, true);
}
}
else{ //cube mode
if(C && Z){ //switch mode
erasePix();
drawMode = true;
}
else{
//if holding nunchuck as expected:
// X axis runs left to right (positives to the right)
// Y axis runs from user to joystick (positives at joystick)
// Z axis runs from top to bottom (positives on bottom of remote)
accX -= 512;
accY -= 512;
accZ -= 512;
UC.lcdGotoXY(0, 0);
puts(toString(accX) + " _ " + toString(accY) + " _ " + toString(accZ) + " ");
UC.lcdGotoXY(0, 1);
puts("MAG:" + toString(sqrt(accX * accX + accY * accY + accZ * accZ)) + " ");
}
}
}
}
private static String toString(int v){
int t = v;
int len = 0;
byte [] ret;
boolean neg = false;
if(v < 0){
neg = true;
len++;
v = -v;
}
do{
len++;
t /= 10;
}while(t != 0);
ret = new byte[len];
do{
ret[--len] = (byte)((v % 10) + (int)'0');
v /= 10;
}while(v != 0);
if(neg) ret[--len] = '-';
return new String(ret);
}
}