This commit is contained in:
Pedro de Oliveira 2014-09-14 21:47:58 +01:00
parent cbfaef67b2
commit 9bdd4bce6a
1 changed files with 83 additions and 37 deletions

View File

@ -1,11 +1,31 @@
static int latch = 12; // set the latch pin /*
static int clock = 13; // set the clock pin Based on https://github.com/njguibert/NesJoystickArduino/
static int datin = 11;// set the data in pin Adapted for SNES
boolean btn_up = false; Snes Controller - Arduino
boolean btn_down = false; White 5V - 5V
boolean btn_left = false; Brown GND - GND
boolean btn_right = false; Yellow (Clock) - Pin 13
Orange (Latch) - Pin 12
Red (Data) - Pin 11
*/
static int clock = 13; // set the clock pin
static int latch = 12; // set the latch pin
static int datin = 11; // set the data in pin
boolean btn_up = false;
boolean btn_down = false;
boolean btn_left = false;
boolean btn_right = false;
boolean btn_a = false;
boolean btn_b = false;
boolean btn_x = false;
boolean btn_y = false;
boolean btn_l = false;
boolean btn_r = false;
boolean btn_select = false;
boolean btn_start = false;
JoyState_t joySt; JoyState_t joySt;
@ -56,98 +76,124 @@ void loop() {
word controller_data = controllerRead(); word controller_data = controllerRead();
// B - BIT 16 // B - BIT 16
if (bitRead(controller_data, 15) == 0) if (!btn_b && bitRead(controller_data, 15) == 0) {
btn_b = true;
joySt.buttons = joySt.buttons | 1; joySt.buttons = joySt.buttons | 1;
else }
if (btn_b && bitRead(controller_data, 15) == 1) {
joySt.buttons = joySt.buttons & 254; joySt.buttons = joySt.buttons & 254;
btn_b = false;
}
// Y - BIT 15 // Y - BIT 15
if (bitRead(controller_data, 14) == 0) if (!btn_y && bitRead(controller_data, 14) == 0) {
btn_y = true;
joySt.buttons = joySt.buttons | 2; joySt.buttons = joySt.buttons | 2;
else }
if (btn_y && bitRead(controller_data, 14) == 1) {
btn_y = false;
joySt.buttons = joySt.buttons & 253; joySt.buttons = joySt.buttons & 253;
}
// SELECT - BIT 14 // SELECT - BIT 14
if (bitRead(controller_data, 13) == 0) if (!btn_select && bitRead(controller_data, 13) == 0) {
btn_select = true;
joySt.buttons = joySt.buttons | 64; joySt.buttons = joySt.buttons | 64;
else }
if (btn_select && bitRead(controller_data, 13) == 1) {
btn_select = false;
joySt.buttons = joySt.buttons & 191; joySt.buttons = joySt.buttons & 191;
}
// START - BIT 13 // START - BIT 13
if (bitRead(controller_data, 12) == 0) if (!btn_start && bitRead(controller_data, 12) == 0) {
btn_start = true;
joySt.buttons = joySt.buttons | 128; joySt.buttons = joySt.buttons | 128;
else }
if (btn_start && bitRead(controller_data, 12) == 1) {
btn_start = false;
joySt.buttons = joySt.buttons & 127; joySt.buttons = joySt.buttons & 127;
}
// UP - BIT 12 // UP - BIT 12
if (bitRead(controller_data, 11) == 0 && !btn_up) { if (!btn_up && bitRead(controller_data, 11) == 0) {
btn_up = true; btn_up = true;
joySt.yAxis = 0; joySt.yAxis = 0;
} }
if (btn_up && bitRead(controller_data, 11) == 1) {
if (bitRead(controller_data, 11) == 1 && btn_up) {
btn_up = false; btn_up = false;
joySt.yAxis = 127; joySt.yAxis = 127;
} }
// DOWN - BIT 11 // DOWN - BIT 11
if (bitRead(controller_data, 10) == 0 && !btn_down) { if (!btn_down && bitRead(controller_data, 10) == 0) {
btn_down = true; btn_down = true;
joySt.yAxis = 255; joySt.yAxis = 255;
} }
if (btn_down && bitRead(controller_data, 10) == 1) {
if (bitRead(controller_data, 10) == 1 && btn_down) {
btn_down = false; btn_down = false;
joySt.yAxis = 127; joySt.yAxis = 127;
} }
// LEFT - BIT 10 // LEFT - BIT 10
if (bitRead(controller_data, 9) == 0 && !btn_left) { if (!btn_left && bitRead(controller_data, 9) == 0) {
btn_left = true; btn_left = true;
joySt.xAxis = 0; joySt.xAxis = 0;
} }
if (btn_left && bitRead(controller_data, 9) == 1) {
if (bitRead(controller_data, 9) == 1 && btn_left) {
btn_left = false; btn_left = false;
joySt.xAxis = 127; joySt.xAxis = 127;
} }
// RIGHT - BIT 9 // RIGHT - BIT 9
if (bitRead(controller_data, 8) == 0 && !btn_right) { if (!btn_right && bitRead(controller_data, 8) == 0) {
btn_right = true; btn_right = true;
joySt.xAxis = 255; joySt.xAxis = 255;
} }
if (btn_right && bitRead(controller_data, 8) == 1) {
if (bitRead(controller_data, 8) == 1 && btn_right) {
btn_right = false; btn_right = false;
joySt.xAxis = 127; joySt.xAxis = 127;
} }
// A - BIT 8 // A - BIT 8
if (bitRead(controller_data, 7) == 0) if (!btn_a && bitRead(controller_data, 7) == 0) {
btn_a = true;
joySt.buttons = joySt.buttons | 4; joySt.buttons = joySt.buttons | 4;
else }
if (btn_a && bitRead(controller_data, 7) == 1) {
btn_a = false;
joySt.buttons = joySt.buttons & 251; joySt.buttons = joySt.buttons & 251;
}
// X - BIT 7 // X - BIT 7
if (bitRead(controller_data, 6) == 0) if (!btn_x && bitRead(controller_data, 6) == 0) {
btn_x = true;
joySt.buttons = joySt.buttons | 8; joySt.buttons = joySt.buttons | 8;
else }
if (btn_x && bitRead(controller_data, 6) == 1) {
btn_x = true;
joySt.buttons = joySt.buttons & 247; joySt.buttons = joySt.buttons & 247;
}
// L - BIT 6 // L - BIT 6
if (bitRead(controller_data, 5) == 0) if (!btn_l && bitRead(controller_data, 5) == 0) {
btn_l = true;
joySt.buttons = joySt.buttons | 16; joySt.buttons = joySt.buttons | 16;
else }
if (btn_l && bitRead(controller_data, 5) == 1) {
btn_l = false;
joySt.buttons = joySt.buttons & 239; joySt.buttons = joySt.buttons & 239;
}
// R - BIT 5 // R - BIT 5
if (bitRead(controller_data, 4) == 0) if (!btn_r && bitRead(controller_data, 4) == 0) {
btn_r = true;
joySt.buttons = joySt.buttons | 32; joySt.buttons = joySt.buttons | 32;
else }
if (btn_r && bitRead(controller_data, 4) == 1) {
btn_r = false;
joySt.buttons = joySt.buttons & 223; joySt.buttons = joySt.buttons & 223;
}
Joystick.setState(&joySt); Joystick.setState(&joySt);
} }