diff --git a/snes_controller/snes_controller.ino b/snes_controller/snes_controller.ino index 054cd9a..930016c 100644 --- a/snes_controller/snes_controller.ino +++ b/snes_controller/snes_controller.ino @@ -1,11 +1,31 @@ -static int latch = 12; // set the latch pin -static int clock = 13; // set the clock pin -static int datin = 11;// set the data in pin +/* +Based on https://github.com/njguibert/NesJoystickArduino/ +Adapted for SNES -boolean btn_up = false; -boolean btn_down = false; -boolean btn_left = false; -boolean btn_right = false; +Snes Controller - Arduino +White 5V - 5V +Brown GND - GND +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; @@ -56,98 +76,124 @@ void loop() { word controller_data = controllerRead(); // 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; - else + } + if (btn_b && bitRead(controller_data, 15) == 1) { joySt.buttons = joySt.buttons & 254; + btn_b = false; + } // 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; - else + } + if (btn_y && bitRead(controller_data, 14) == 1) { + btn_y = false; joySt.buttons = joySt.buttons & 253; + } // 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; - else + } + if (btn_select && bitRead(controller_data, 13) == 1) { + btn_select = false; joySt.buttons = joySt.buttons & 191; + } // 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; - else + } + if (btn_start && bitRead(controller_data, 12) == 1) { + btn_start = false; joySt.buttons = joySt.buttons & 127; + } // UP - BIT 12 - if (bitRead(controller_data, 11) == 0 && !btn_up) { + if (!btn_up && bitRead(controller_data, 11) == 0) { btn_up = true; joySt.yAxis = 0; } - - if (bitRead(controller_data, 11) == 1 && btn_up) { + if (btn_up && bitRead(controller_data, 11) == 1) { btn_up = false; joySt.yAxis = 127; } // DOWN - BIT 11 - if (bitRead(controller_data, 10) == 0 && !btn_down) { + if (!btn_down && bitRead(controller_data, 10) == 0) { btn_down = true; joySt.yAxis = 255; } - - if (bitRead(controller_data, 10) == 1 && btn_down) { + if (btn_down && bitRead(controller_data, 10) == 1) { btn_down = false; joySt.yAxis = 127; } // LEFT - BIT 10 - if (bitRead(controller_data, 9) == 0 && !btn_left) { + if (!btn_left && bitRead(controller_data, 9) == 0) { btn_left = true; joySt.xAxis = 0; } - - if (bitRead(controller_data, 9) == 1 && btn_left) { + if (btn_left && bitRead(controller_data, 9) == 1) { btn_left = false; joySt.xAxis = 127; } // RIGHT - BIT 9 - if (bitRead(controller_data, 8) == 0 && !btn_right) { + if (!btn_right && bitRead(controller_data, 8) == 0) { btn_right = true; joySt.xAxis = 255; } - - if (bitRead(controller_data, 8) == 1 && btn_right) { + if (btn_right && bitRead(controller_data, 8) == 1) { btn_right = false; joySt.xAxis = 127; } // 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; - else + } + if (btn_a && bitRead(controller_data, 7) == 1) { + btn_a = false; joySt.buttons = joySt.buttons & 251; + } // 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; - else + } + if (btn_x && bitRead(controller_data, 6) == 1) { + btn_x = true; joySt.buttons = joySt.buttons & 247; - + } // 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; - else + } + if (btn_l && bitRead(controller_data, 5) == 1) { + btn_l = false; joySt.buttons = joySt.buttons & 239; + } // 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; - else + } + if (btn_r && bitRead(controller_data, 4) == 1) { + btn_r = false; joySt.buttons = joySt.buttons & 223; + } Joystick.setState(&joySt); - }