-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathm_wii.c
192 lines (161 loc) · 4.41 KB
/
m_wii.c
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
// -----------------------------------------------------------------------------
// M2 Wiimote Pixart Sensor interface
// version: 1.0
// date: Oct 13, 2011
// author: J. Fiene
// -----------------------------------------------------------------------------
#include "m_general.h" // just for debugging
#include "m_wii.h"
#include "m_usb.h"
#define MWIITWIADDR 0x58
#define TWI_MAX_WAIT 1000
#define INTERPACKET 10
// private function prototypes
unsigned char start_write(unsigned char address);
unsigned char start_read(unsigned char address);
unsigned char send_byte(unsigned char byte);
unsigned char twi_wait(void);
void end(void);
char m_wii_open()
{
m_bus_init();
// enter configuration mode
if(!start_write(MWIITWIADDR)){return 0;}
if(!send_byte(0x30)){return 0;}
if(!send_byte(0x01)){return 0;}
end();
m_wait(INTERPACKET);
// sensitivity setting, part 1
if(!start_write(MWIITWIADDR)){return 0;}
if(!send_byte(0x00)){return 0;}
if(!send_byte(0x00)){return 0;}
if(!send_byte(0x00)){return 0;}
if(!send_byte(0x00)){return 0;}
if(!send_byte(0x00)){return 0;}
if(!send_byte(0x00)){return 0;}
if(!send_byte(0x00)){return 0;}
if(!send_byte(0x90)){return 0;} // p0
end();
m_wait(INTERPACKET);
// sensitivity setting, part 2
if(!start_write(MWIITWIADDR)){return 0;}
if(!send_byte(0x07)){return 0;}
if(!send_byte(0x00)){return 0;}
if(!send_byte(0x41)){return 0;} // p1
end();
m_wait(INTERPACKET);
// sensitivity setting, part 3
if(!start_write(MWIITWIADDR)){return 0;}
if(!send_byte(0x1A)){return 0;}
if(!send_byte(0x40)){return 0;} // p2
if(!send_byte(0x00)){return 0;} // p3
end();
m_wait(INTERPACKET);
// not quite sure
if(!start_write(MWIITWIADDR)){return 0;}
if(!send_byte(0x33)){return 0;}
if(!send_byte(0x03)){return 0;}
end();
m_wait(INTERPACKET);
// not quite sure
if(!start_write(MWIITWIADDR)){return 0;}
if(!send_byte(0x30)){return 0;}
if(!send_byte(0x08)){return 0;}
end();
m_wait(INTERPACKET);
return 1;
}
char m_wii_read(unsigned int* blob_data)
{
int i;
unsigned char temp[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
// prepare for reading
if(!start_write(MWIITWIADDR)){return 0;}
if(!send_byte(0x36)){return 0;}
end();
_delay_us(25);
if(!start_read(MWIITWIADDR)){return 0;}
for(i=0;i<15;i++)
{
TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN); // clear the flag, enable ACKs, and wait for another byte
while(!(TWCR & (1<<TWINT))){}; // wait for an interrupt to signal that a new byte is available
temp[i] = TWDR;
}
TWCR = (1<<TWINT) | (1<<TWEN); // clear the flag, no ACK, and wait for another byte
while(!(TWCR & (1<<TWINT))){}; // wait for an interrupt to signal that a new byte is available
temp[15] = TWDR;
end();
for(i=0;i<10;i+=3) // for each blob:
{ // i = 0, 3, 6, 9
blob_data[i] = (unsigned int)temp[i+1] + ((int)(temp[i+3] & 0b00110000) <<4); // X
blob_data[i+1] = (unsigned int)temp[i+2] + ((int)(temp[i+3] & 0b11000000) <<2); // Y
blob_data[i+2] = temp[i+3] & 0b00001111; // size
}
return 1;
}
//
//
// PRIVATE FUNCTIONS BELOW
//
//
unsigned char start_write(unsigned char address)
{
unsigned char status;
// START
TWCR = (1<<TWEN)|(1<<TWSTA)|(1<<TWINT);
if(!twi_wait())
{
return 0;
}
// ADDRESS
status = send_byte(address<<1);
if(status== 0x20){ // ACK was not received - may not be connected/listening
TWCR = (1<<TWINT)|(1<<TWEN)| (1<<TWSTO); // let go of the line (STOP)
return 0; // failure
}
return 1; // success
}
unsigned char start_read(unsigned char address)
{
unsigned char status;
// START
TWCR = (1<<TWEN)|(1<<TWSTA)|(1<<TWINT);
if(!twi_wait())
{
return 0;
}
// ADDRESS
status = send_byte(((address<<1) + 1));
if(status== 0x48){ // ACK was not received - may not be connected/listening
TWCR = (1<<TWINT)|(1<<TWEN)| (1<<TWSTO); // let go of the line (STOP)
return 0; // failure
}
return 1; // success
}
unsigned char send_byte(unsigned char byte)
{
TWDR = byte; // load the byte
TWCR = (1<<TWINT) | (1<<TWEN); // send the byte
if(twi_wait()) // timed out
{
return (TWSR & 0xF8); // return the status with prescaler bits masked out
} else {
return 0; // comm failure
}
}
unsigned char twi_wait(void)
{
unsigned int wait=0;
while((!(TWCR & (1<<TWINT))) && (wait++<TWI_MAX_WAIT)){}; // wait for acknowledgement that they byte was sent
if(wait==TWI_MAX_WAIT)
{
return 0; // fail
} else {
return 1; // success
}
}
void end(void)
{
// STOP
TWCR = (1<<TWINT)|(1<<TWEN)| (1<<TWSTO);
}