blob: e18dd5944323246ac4cf5245bf7106fd134b4d99 (
plain)
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
|
/*
* WiiChuck
* Flamingo EDA http://www.flamingoeda.com
*
*/
#include "WiiChuck.h"
void WiiChuck::init()
{
Wire.begin();
Wire.beginTransmission(WII_I2C_ADDR);
Wire.send(0x40);
Wire.send(0x00);
Wire.endTransmission();
}
void WiiChuck::initWithPower()
{
initWithPowerPins(PORTC3, PORTC2);
}
void WiiChuck::initWithPowerPins(byte powerPin, byte gndPin)
{
DDRC |= _BV(powerPin) | _BV(gndPin);
PORTC &= ~_BV(gndPin);
PORTC |= _BV(powerPin);
delay(100);
init();
}
boolean WiiChuck::read()
{
int count = 0;
Wire.requestFrom(WII_I2C_ADDR, PACKAGE_LENGTH);
while(Wire.available()) {
buffer[count++] = decodeByte(Wire.receive());
}
// send request for next bytes
sendNullRequest();
if (count < 5) {
return false;
}
// update data
joyAxisX = buffer[0];
joyAxisY = buffer[1];
accelAxisX = buffer[2];
accelAxisY = buffer[3];
accelAxisZ = buffer[4];
buttonZ = 0;
buttonC = 0;
if ((buffer[5] >> 0) & 1) {
buttonZ = 1;
}
if ((buffer[5] >> 1) & 1) {
buttonC = 1;
}
if ((buffer[5] >> 2) & 1) {
accelAxisX += 2;
}
if ((buffer[5] >> 3) & 1) {
accelAxisX += 1;
}
if ((buffer[5] >> 4) & 1) {
accelAxisY += 2;
}
if ((buffer[5] >> 5) & 1) {
accelAxisY += 1;
}
if ((buffer[5] >> 2) & 1) {
accelAxisZ += 2;
}
if ((buffer[5] >> 3) & 1) {
accelAxisZ += 1;
}
return true;
}
int WiiChuck::getJoyAxisX()
{
return joyAxisX;
}
int WiiChuck::getJoyAxisY()
{
return joyAxisY;
}
int WiiChuck::getAccelAxisX()
{
return accelAxisX;
}
int WiiChuck::getAccelAxisY()
{
return accelAxisY;
}
int WiiChuck::getAccelAxisZ()
{
return accelAxisZ;
}
int WiiChuck::getButtonZ()
{
return buttonZ;
}
int WiiChuck::getButtonC()
{
return buttonC;
}
/*****************************************************************************
* PRIVATE METHODS
****************************************************************************/
byte WiiChuck::decodeByte(byte x)
{
x = (x ^ 0x17) + 0x17;
return x;
}
void WiiChuck::sendNullRequest()
{
Wire.beginTransmission(WII_I2C_ADDR);
Wire.send(0x00);
Wire.endTransmission();
}
|