From 4196f9ba402083a293a09c797f291b3bb7f04f7a Mon Sep 17 00:00:00 2001 From: Pedro de Oliveira Date: Mon, 15 Sep 2014 00:19:13 +0100 Subject: [PATCH] =?UTF-8?q?Mil=20altera=C3=A7oes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- snes_controller/snes_controller.ino | 116 ++++++++++++++-------------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/snes_controller/snes_controller.ino b/snes_controller/snes_controller.ino index ef1e783..227746b 100644 --- a/snes_controller/snes_controller.ino +++ b/snes_controller/snes_controller.ino @@ -1,14 +1,14 @@ /* Based on https://github.com/njguibert/NesJoystickArduino/ -Adapted for SNES with info from http://www.gamefaqs.com/snes/916396-super-nintendo/faqs/5395 - -Snes Controller - Arduino -White 5V - 5V -Brown GND - GND -Yellow (Clock) - Pin 13 -Orange (Latch) - Pin 12 -Red (Data) - Pin 11 -*/ + Adapted for SNES with info from http://www.gamefaqs.com/snes/916396-super-nintendo/faqs/5395 + + 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 @@ -17,8 +17,10 @@ static int datin = 11; // set the data in pin JoyState_t joySt; void setup() { - //Serial.begin(9600); - + Serial.begin(9600); + // while the serial stream is not open, do nothing: + while (!Serial) ; + pinMode(latch,OUTPUT); pinMode(clock,OUTPUT); pinMode(datin,INPUT); @@ -29,80 +31,78 @@ void setup() { joySt.zAxis = 127; } -word controllerRead() { +word controllerRead() +{ word data = 0; - - digitalWrite(latch,LOW); - digitalWrite(clock,LOW); - + digitalWrite(latch,HIGH); delayMicroseconds(2); digitalWrite(latch,LOW); - - data = digitalRead(datin); - - for (int i = 1; i <= 15; i ++) { + delayMicroseconds(1); + + for (int i = 0; i < 16; i++) + { + data >>= 1; + data |= (digitalRead(datin) ? 0x8000 : 0); digitalWrite(clock,HIGH); - delayMicroseconds(2); - data = data << 1; - data = data | digitalRead(datin) ; - delayMicroseconds(2); + //delayMicroseconds(1); digitalWrite(clock,LOW); } - return data; } void loop() { /* - Clock Cycle Button Reported - =========== =============== - 1 B - 2 Y - 3 Select - 4 Start - 5 Up on joypad - 6 Down on joypad - 7 Left on joypad - 8 Right on joypad - 9 A - 10 X - 11 L - 12 R - 13 none (always high) - 14 none (always high) - 15 none (always high) - 16 none (always high) - */ + Clock Cycle Button Reported + =========== =============== + 1 B + 2 Y + 3 Select + 4 Start + 5 Up on joypad + 6 Down on joypad + 7 Left on joypad + 8 Right on joypad + 9 A + 10 X + 11 L + 12 R + 13 none (always high) + 14 none (always high) + 15 none (always high) + 16 none (always high) + */ word controller_data = controllerRead(); // Read 16 bit of data + Serial.println(controller_data, BIN); joySt.buttons = 0; // Reset buttons - joySt.buttons |= (!bitRead(controller_data, 15) << 0); // B - bit 16 - bit 1 on the Joystick - joySt.buttons |= (!bitRead(controller_data, 14) << 1); // Y - bit 15 - bit 2 on the Joystick - joySt.buttons |= (!bitRead(controller_data, 13) << 6); // Select - bit 14 - bit 7 on the Joystick - joySt.buttons |= (!bitRead(controller_data, 12) << 7); // Start - bit 13 - bit 8 on the Joystick - joySt.buttons |= (!bitRead(controller_data, 7) << 2); // A - bit 8 - bit 6 on the Joystick - joySt.buttons |= (!bitRead(controller_data, 6) << 3); // X - bit 7 - bit 5 on the Joystick - joySt.buttons |= (!bitRead(controller_data, 5) << 4); // L - bit 6 - bit 4 on the Joystick - joySt.buttons |= (!bitRead(controller_data, 4) << 5); // R - bit 5 - bit 3 on the Joystick + joySt.buttons |= (!bitRead(controller_data, 0) << 0); // B - bit 1 - bit 1 on the Joystick + joySt.buttons |= (!bitRead(controller_data, 1) << 1); // Y - bit 2 - bit 2 on the Joystick + joySt.buttons |= (!bitRead(controller_data, 2) << 6); // Select - bit 3 - bit 7 on the Joystick + joySt.buttons |= (!bitRead(controller_data, 3) << 7); // Start - bit 4 - bit 8 on the Joystick + joySt.buttons |= (!bitRead(controller_data, 8) << 2); // A - bit 9 - bit 6 on the Joystick + joySt.buttons |= (!bitRead(controller_data, 9) << 3); // X - bit 10 - bit 5 on the Joystick + joySt.buttons |= (!bitRead(controller_data, 10) << 4); // L - bit 11 - bit 4 on the Joystick + joySt.buttons |= (!bitRead(controller_data, 11) << 5); // R - bit 12 - bit 3 on the Joystick + - joySt.xAxis = 127; // Reset x axis joySt.yAxis = 127; // Rest y axis - - if (!bitRead(controller_data, 11)) // Up - bit 12 + + if (!bitRead(controller_data, 4)) // Up - bit 5 joySt.yAxis = 0; - if (!bitRead(controller_data, 10)) // Down - bit 11 + if (!bitRead(controller_data, 5)) // Down - bit 6 joySt.yAxis = 255; - if (!bitRead(controller_data, 9)) // Left - bit 10 + if (!bitRead(controller_data, 6)) // Left - bit 7 joySt.xAxis = 0; - if (!bitRead(controller_data, 8)) // Right - bit 9 + if (!bitRead(controller_data, 7)) // Right - bit 8 joySt.xAxis = 255; Joystick.setState(&joySt); // Update joystick state } +