-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWiichuck.cpp
More file actions
76 lines (65 loc) · 1.61 KB
/
Wiichuck.cpp
File metadata and controls
76 lines (65 loc) · 1.61 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
#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include "Wiichuck.h"
#include <Wire.h>
void Wiichuck::init(int powerPin, int groundPin) {
// Set the output pins to VCC and GND
if(powerPin > 0) {
pinMode(powerPin, OUTPUT);
digitalWrite(powerPin, HIGH);
}
if(groundPin > 0) {
pinMode(groundPin, OUTPUT);
digitalWrite(groundPin, LOW);
}
delay(100);
Wire.begin();
Wire.beginTransmission(Address);
#if (ARDUINO >= 100)
Wire.write(0x40);
Wire.write(0x00);
#else
Wire.send(0x40);
Wire.send(0x00);
#endif
Wire.endTransmission();
// Set default calibration
calib.joyX = calib.joyY = 128;
calib.accelX = calib.accelY = calib.accelZ = 125; // accel and lsb together == 500.
calib.lsbX = calib.lsbY = calib.lsbZ = 0;
}
uint8_t Wiichuck::poll() {
Wire.requestFrom(Address, 6);// request data from nunchuck
int bytes = 0;
while(Wire.available() && bytes < 6) {
// receive uint8_t as an integer
#if (ARDUINO >= 100)
data.buffer[bytes++] = decode(Wire.read());
#else
data.buffer[bytes++] = decode(Wire.receive());
#endif
}
// send request for next data payload
Wire.beginTransmission(Address);
#if (ARDUINO >= 100)
Wire.write(0x00);
#else
Wire.send(0x00);
#endif
Wire.endTransmission();
delay(100);
return bytes >= 5;
}
void Wiichuck::calibrate() {
calib.joyX = data.parsed.joyX;
calib.joyY = data.parsed.joyY;
calib.accelX = data.parsed.accelX;
calib.accelY = data.parsed.accelY;
calib.accelZ = data.parsed.accelZ;
calib.lsbX = data.parsed.lsbX;
calib.lsbY = data.parsed.lsbY;
calib.lsbZ = data.parsed.lsbZ;
}