-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExperiment Code
78 lines (58 loc) · 1.61 KB
/
Experiment Code
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
// Libraries
#include <NintendoExtensionCtrl.h>
// Joystick
const int JoyCenter = 128; // 255 / 2
const int JoyDeadzone = 50; // +/- area around the center to ignore
//Map axes to axes
//Digital actions
KeyboardButton jump(' ');
KeyboardButton use('e');
MouseButton fire(MOUSE_LEFT);
// Nunchuk
Nunchuk Nchuk(Wire);
// Program Data
uint8_t xCenter = 0;
uint8_t yCenter = 0;
boolean weaponSwitched = false;
void setup() {
// I2C Init
Nchuk.begin();
//What clock rate is needed?
Nchuk.i2c().setClock(400000);
boolean Connected = false;
while (!(Connected)) {
Nchuk.connect();
}
xCenter = Nchuk.joyX();
yCenter = Nchuk.joyY();
}
void loop() {
boolean Ready = Nchuk.update();
if (Ready) {
// WASD
uint8_t x = Nchuk.joyX();
uint8_t y = Nchuk.joyY();
moveLeft.set(x < JoyCenter - JoyDeadzone);
moveRight.set(x > JoyCenter + JoyDeadzone);
moveForward.set(y > JoyCenter + JoyDeadzone);
moveBackward.set(y < JoyCenter - JoyDeadzone);
// Jump
jump.set(Nchuk.buttonZ());
// Use / Buy
use.set(Nchuk.buttonC());
// Aiming
int16_t x = ((int16_t) Nchuk.joyX() - (int16_t) xCenter) / 4;
int16_t y = ((int16_t) Nchuk.joyY() - (int16_t) yCenter) / 4;
Mouse.move(x, -y);
// Fire
fire.set(Nchuk.buttonZ());
// Next Weapon
if (Nchuk.buttonC()) {
if (!weaponSwitched) {
Mouse.move(0, 0, 1); // Scroll up
}
weaponSwitched = !weaponSwitched;
}
// Reload
}
}